@@ -188,29 +188,9 @@ var _ = Describe("ImageUpdateAutomation", func() {
188188 BeforeEach (func () {
189189 // Insert a setter reference into the deployment file,
190190 // before creating the automation object itself.
191- tmp , err := ioutil .TempDir ("" , "gotest-imageauto-setters" )
192- Expect (err ).ToNot (HaveOccurred ())
193- defer os .RemoveAll (tmp )
194- repo , err := git .PlainClone (tmp , false , & git.CloneOptions {
195- URL : repoURL ,
196- ReferenceName : plumbing .NewBranchReferenceName (defaultBranch ),
197- })
198- Expect (err ).ToNot (HaveOccurred ())
199-
200- replaceMarker (tmp , policyKey )
201- worktree , err := repo .Worktree ()
202- Expect (err ).ToNot (HaveOccurred ())
203- _ , err = worktree .Add ("deploy.yaml" )
204- Expect (err ).ToNot (HaveOccurred ())
205- _ , err = worktree .Commit ("Install setter marker" , & git.CommitOptions {
206- Author : & object.Signature {
207- Name : "Testbot" ,
208- 209- When : time .Now (),
210- },
191+ commitInRepo (repoURL , "Install setter marker" , func (tmp string ) {
192+ replaceMarker (tmp , policyKey )
211193 })
212- Expect (err ).ToNot (HaveOccurred ())
213- Expect (repo .Push (& git.PushOptions {RemoteName : "origin" })).To (Succeed ())
214194
215195 // pull the head commit we just pushed, so it's not
216196 // considered a new commit when checking for a commit
@@ -229,6 +209,7 @@ var _ = Describe("ImageUpdateAutomation", func() {
229209 Namespace : updateKey .Namespace ,
230210 },
231211 Spec : imagev1.ImageUpdateAutomationSpec {
212+ RunInterval : & metav1.Duration {Duration : 2 * time .Hour }, // this is to ensure any subsequent run should be outside the scope of the testing
232213 Checkout : imagev1.GitCheckoutSpec {
233214 GitRepositoryRef : corev1.LocalObjectReference {
234215 Name : gitRepoKey .Name ,
@@ -259,22 +240,9 @@ var _ = Describe("ImageUpdateAutomation", func() {
259240 Expect (err ).ToNot (HaveOccurred ())
260241 Expect (commit .Message ).To (Equal (commitMessage ))
261242
262- tmp , err := ioutil .TempDir ("" , "gotest-imageauto" )
263- Expect (err ).ToNot (HaveOccurred ())
264- defer os .RemoveAll (tmp )
265-
266- expected , err := ioutil .TempDir ("" , "gotest-imageauto-expected" )
267- Expect (err ).ToNot (HaveOccurred ())
268- defer os .RemoveAll (expected )
269- copy .Copy ("testdata/appconfig-setters-expected" , expected )
270- replaceMarker (expected , policyKey )
271-
272- _ , err = git .PlainClone (tmp , false , & git.CloneOptions {
273- URL : repoURL ,
274- ReferenceName : plumbing .NewBranchReferenceName (defaultBranch ),
243+ compareRepoWithExpected (repoURL , "testdata/appconfig-setters-expected" , func (tmp string ) {
244+ replaceMarker (tmp , policyKey )
275245 })
276- Expect (err ).ToNot (HaveOccurred ())
277- test .ExpectMatchingDirectories (tmp , expected )
278246 })
279247
280248 It ("stops updating when suspended" , func () {
@@ -292,6 +260,52 @@ var _ = Describe("ImageUpdateAutomation", func() {
292260 return ready != nil && ready .Status == metav1 .ConditionFalse && ready .Reason == meta .SuspendedReason
293261 }, timeout , time .Second ).Should (BeTrue ())
294262 })
263+
264+ It ("runs when the reconcile request annotation is added" , func () {
265+ // the automation has run, and is not expected to run
266+ // again for 2 hours. Make a commit to the git repo
267+ // which needs to be undone by automation, then add
268+ // the annotation and make sure it runs again.
269+ Expect (k8sClient .Get (context .Background (), updateKey , updateBySetters )).To (Succeed ())
270+ lastRun := updateBySetters .Status .LastAutomationRunTime
271+ Expect (lastRun ).ToNot (BeNil ())
272+
273+ commitInRepo (repoURL , "Revert image update" , func (tmp string ) {
274+ // revert the change made by copying the old version
275+ // of the file back over then restoring the setter
276+ // marker
277+ copy .Copy ("testdata/appconfig/deploy.yaml" , filepath .Join (tmp , "deploy.yaml" ))
278+ replaceMarker (tmp , policyKey )
279+ })
280+ // check that it was reverted
281+ compareRepoWithExpected (repoURL , "testdata/appconfig" , func (tmp string ) {
282+ replaceMarker (tmp , policyKey )
283+ })
284+
285+ ts := time .Now ().String ()
286+ var updatePatch imagev1.ImageUpdateAutomation
287+ updatePatch .Name = updateKey .Name
288+ updatePatch .Namespace = updateKey .Namespace
289+ updatePatch .ObjectMeta .Annotations = map [string ]string {
290+ meta .ReconcileRequestAnnotation : ts ,
291+ }
292+ Expect (k8sClient .Patch (context .Background (), & updatePatch , client .Merge )).To (Succeed ())
293+
294+ Eventually (func () bool {
295+ if err := k8sClient .Get (context .Background (), updateKey , updateBySetters ); err != nil {
296+ return false
297+ }
298+ newLastRun := updateBySetters .Status .LastAutomationRunTime
299+ return newLastRun != nil && newLastRun .Time .After (lastRun .Time )
300+ }, timeout , time .Second ).Should (BeTrue ())
301+ // check that the annotation was recorded as seen
302+ Expect (updateBySetters .Status .LastHandledReconcileAt ).To (Equal (ts ))
303+
304+ // check that a new commit was made
305+ compareRepoWithExpected (repoURL , "testdata/appconfig-setters-expected" , func (tmp string ) {
306+ replaceMarker (tmp , policyKey )
307+ })
308+ })
295309 })
296310 })
297311})
@@ -326,6 +340,51 @@ func waitForNewHead(repo *git.Repository) {
326340 }, timeout , time .Second ).Should (BeTrue ())
327341}
328342
343+ func compareRepoWithExpected (repoURL , fixture string , changeFixture func (tmp string )) {
344+ expected , err := ioutil .TempDir ("" , "gotest-imageauto-expected" )
345+ Expect (err ).ToNot (HaveOccurred ())
346+ defer os .RemoveAll (expected )
347+ copy .Copy (fixture , expected )
348+ changeFixture (expected )
349+
350+ tmp , err := ioutil .TempDir ("" , "gotest-imageauto" )
351+ Expect (err ).ToNot (HaveOccurred ())
352+ defer os .RemoveAll (tmp )
353+ _ , err = git .PlainClone (tmp , false , & git.CloneOptions {
354+ URL : repoURL ,
355+ ReferenceName : plumbing .NewBranchReferenceName (defaultBranch ),
356+ })
357+ Expect (err ).ToNot (HaveOccurred ())
358+ test .ExpectMatchingDirectories (tmp , expected )
359+ }
360+
361+ func commitInRepo (repoURL , msg string , changeFiles func (path string )) {
362+ tmp , err := ioutil .TempDir ("" , "gotest-imageauto" )
363+ Expect (err ).ToNot (HaveOccurred ())
364+ defer os .RemoveAll (tmp )
365+ repo , err := git .PlainClone (tmp , false , & git.CloneOptions {
366+ URL : repoURL ,
367+ ReferenceName : plumbing .NewBranchReferenceName (defaultBranch ),
368+ })
369+ Expect (err ).ToNot (HaveOccurred ())
370+
371+ changeFiles (tmp )
372+
373+ worktree , err := repo .Worktree ()
374+ Expect (err ).ToNot (HaveOccurred ())
375+ _ , err = worktree .Add ("." )
376+ Expect (err ).ToNot (HaveOccurred ())
377+ _ , err = worktree .Commit (msg , & git.CommitOptions {
378+ Author : & object.Signature {
379+ Name : "Testbot" ,
380+ 381+ When : time .Now (),
382+ },
383+ })
384+ Expect (err ).ToNot (HaveOccurred ())
385+ Expect (repo .Push (& git.PushOptions {RemoteName : "origin" })).To (Succeed ())
386+ }
387+
329388// Initialise a git server with a repo including the files in dir.
330389func initGitRepo (gitServer * gittestserver.GitServer , fixture , repositoryPath string ) error {
331390 fs := memfs .New ()
0 commit comments