Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test reconcile with request annotation #266

Open
wants to merge 1 commit into
base: reconcilers-dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 57 additions & 23 deletions controllers/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,16 +348,15 @@ func TestImageUpdateAutomation_e2e(t *testing.T) {
g.Expect(err).ToNot(HaveOccurred())
}

// Create an image policy.
policyKey := types.NamespacedName{
Name: imagePolicyName,
Namespace: namespace,
}

// Create ImagePolicy and ImageUpdateAutomation resource for each of the
// test cases and cleanup at the end.

t.Run("PushSpec", func(t *testing.T) {
// Create an image policy.
policyKey := types.NamespacedName{
Name: imagePolicyName,
Namespace: namespace,
}
// NB not testing the image reflector controller; this
// will make a "fully formed" ImagePolicy object.
err = createImagePolicyWithLatestImage(imagePolicyName, namespace, "not-expected-to-exist", "1.x", latestImage)
Expand Down Expand Up @@ -440,6 +439,11 @@ func TestImageUpdateAutomation_e2e(t *testing.T) {
})

t.Run("with update strategy setters", func(t *testing.T) {
// Create an image policy.
policyKey := types.NamespacedName{
Name: imagePolicyName,
Namespace: namespace,
}
err = createImagePolicyWithLatestImage(imagePolicyName, namespace, "not-expected-to-exist", "1.x", latestImage)
g.Expect(err).ToNot(HaveOccurred(), "failed to create ImagePolicy resource")

Expand Down Expand Up @@ -486,16 +490,56 @@ func TestImageUpdateAutomation_e2e(t *testing.T) {
compareRepoWithExpected(g, cloneLocalRepoURL, branch, "testdata/appconfig-setters-expected", func(tmp string) {
g.Expect(replaceMarker(tmp, policyKey)).To(Succeed())
})
})

t.Run("no reconciliation when object is suspended", func(t *testing.T) {
err = createImagePolicyWithLatestImage(imagePolicyName, namespace, "not-expected-to-exist", "1.x", latestImage)
g.Expect(err).ToNot(HaveOccurred(), "failed to create ImagePolicy resource")
t.Run("reconciles with reconcile request annotation", func(t *testing.T) {
// The automation has run, and is not expected to run
// again for 2 hours. Make a commit to the git repo
// which needs to be undone by automation, then add
// the annotation and make sure it runs again.

// Get the last run value.
var updateObj imagev1.ImageUpdateAutomation
g.Expect(testEnv.Get(context.Background(), updateKey, &updateObj)).To(Succeed())
lastRun := updateObj.Status.LastAutomationRunTime
g.Expect(lastRun).ToNot(BeNil())

// Reverse image update.
commitInRepo(g, cloneLocalRepoURL, branch, "Revert image update", func(tmp string) {
g.Expect(copy.Copy("testdata/appconfig/deploy.yaml", filepath.Join(tmp, "deploy.yaml")))
g.Expect(replaceMarker(tmp, policyKey))
})

defer func() {
g.Expect(deleteImagePolicy(imagePolicyName, namespace)).ToNot(HaveOccurred())
}()
// Check that it was reverted.
compareRepoWithExpected(g, cloneLocalRepoURL, branch, "testdata/appconfig", func(tmp string) {
g.Expect(replaceMarker(tmp, policyKey)).To(Succeed())
})

ts := time.Now().String()

var updatePatch imagev1.ImageUpdateAutomation
g.Expect(testEnv.Get(context.Background(), updateKey, &updatePatch)).To(Succeed())
updatePatch.ObjectMeta.Annotations = map[string]string{
meta.ReconcileRequestAnnotation: ts,
}
g.Expect(testEnv.Patch(context.Background(), &updatePatch, client.Merge)).To(Succeed())

g.Eventually(func() bool {
if err := testEnv.Get(context.Background(), updateKey, &updateObj); err != nil {
return false
}
newLastRun := updateObj.Status.LastAutomationRunTime
return newLastRun != nil && newLastRun.Time.After(lastRun.Time)
}, timeout, time.Second).Should(BeTrue())

g.Expect(updateObj.Status.LastHandledReconcileAt).To(Equal(ts))

compareRepoWithExpected(g, cloneLocalRepoURL, branch, "testdata/appconfig-setters-expected", func(tmp string) {
g.Expect(replaceMarker(tmp, policyKey))
})
})
})

t.Run("no reconciliation when object is suspended", func(t *testing.T) {
// Create the automation object.
updateKey := types.NamespacedName{
Namespace: namespace,
Expand Down Expand Up @@ -552,16 +596,6 @@ func TestImageUpdateAutomation_e2e(t *testing.T) {
g.Expect(testEnv.Get(context.Background(), updateKey, &checkUpdate)).To(Succeed())
g.Expect(checkUpdate.Status.ObservedGeneration).NotTo(Equal(checkUpdate.ObjectMeta.Generation))
})

t.Run("reconciles with reconcile request annotation", func(t *testing.T) {
// The automation has run, and is not expected to run
// again for 2 hours. Make a commit to the git repo
// which needs to be undone by automation, then add
// the annotation and make sure it runs again.

// TODO: Implement adding request annotation.
// Refer: https://github.com/fluxcd/image-automation-controller/pull/82/commits/4fde199362b42fa37068f2e6c6885cfea474a3d1#diff-1168fadffa18bd096582ae7f8b6db744fd896bd5600ee1d1ac6ac4474af251b9L292-L334
})
}

// Run the protocol based e2e tests against the git implementations.
Expand Down