|
1 | 1 | package e2e |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "strings" |
5 | 6 | "testing" |
| 7 | + "time" |
6 | 8 |
|
7 | 9 | v1 "k8s.io/api/core/v1" |
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + "k8s.io/apimachinery/pkg/types" |
| 12 | + "k8s.io/apimachinery/pkg/util/wait" |
8 | 13 |
|
9 | | - "github.com/argoproj/argo-cd/v2/test/e2e/fixture" |
| 14 | + "github.com/stretchr/testify/require" |
10 | 15 |
|
11 | 16 | . "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" |
| 17 | + "github.com/argoproj/argo-cd/v2/test/e2e/fixture" |
12 | 18 | . "github.com/argoproj/argo-cd/v2/test/e2e/fixture/app" |
| 19 | + "github.com/argoproj/argo-cd/v2/util/errors" |
13 | 20 | ) |
14 | 21 |
|
15 | 22 | func TestGitSemverResolutionNotUsingConstraint(t *testing.T) { |
@@ -51,3 +58,145 @@ func TestGitSemverResolutionUsingConstraint(t *testing.T) { |
51 | 58 | Expect(SyncStatusIs(SyncStatusCodeSynced)). |
52 | 59 | Expect(Pod(func(p v1.Pod) bool { return strings.HasPrefix(p.Name, "new-app") })) |
53 | 60 | } |
| 61 | + |
| 62 | +func TestAnnotatedTagInStatusSyncRevision(t *testing.T) { |
| 63 | + Given(t). |
| 64 | + Path(guestbookPath). |
| 65 | + When(). |
| 66 | + // Create annotated tag name 'annotated-tag' |
| 67 | + AddAnnotatedTag("annotated-tag", "my-generic-tag-message"). |
| 68 | + // Create Application targeting annotated-tag, with automatedSync: true |
| 69 | + CreateFromFile(func(app *Application) { |
| 70 | + app.Spec.Source.TargetRevision = "annotated-tag" |
| 71 | + app.Spec.SyncPolicy = &SyncPolicy{Automated: &SyncPolicyAutomated{Prune: true, SelfHeal: false}} |
| 72 | + }). |
| 73 | + Then(). |
| 74 | + Expect(SyncStatusIs(SyncStatusCodeSynced)). |
| 75 | + And(func(app *Application) { |
| 76 | + annotatedTagIDOutput, err := fixture.Run(fixture.TmpDir+"/testdata.git", "git", "show-ref", "annotated-tag") |
| 77 | + require.NoError(t, err) |
| 78 | + require.NotEmpty(t, annotatedTagIDOutput) |
| 79 | + // example command output: |
| 80 | + // "569798c430515ffe170bdb23e3aafaf8ae24b9ff refs/tags/annotated-tag" |
| 81 | + annotatedTagIDFields := strings.Fields(string(annotatedTagIDOutput)) |
| 82 | + require.Len(t, annotatedTagIDFields, 2) |
| 83 | + |
| 84 | + targetCommitID, err := fixture.Run(fixture.TmpDir+"/testdata.git", "git", "rev-parse", "--verify", "annotated-tag^{commit}") |
| 85 | + // example command output: |
| 86 | + // "bcd35965e494273355265b9f0bf85075b6bc5163" |
| 87 | + require.NoError(t, err) |
| 88 | + require.NotEmpty(t, targetCommitID) |
| 89 | + |
| 90 | + require.NotEmpty(t, app.Status.Sync.Revision, "revision in sync status should be set by sync operation") |
| 91 | + |
| 92 | + require.NotEqual(t, app.Status.Sync.Revision, annotatedTagIDFields[0], "revision should not match the annotated tag id") |
| 93 | + require.Equal(t, app.Status.Sync.Revision, strings.TrimSpace(string(targetCommitID)), "revision SHOULD match the target commit SHA") |
| 94 | + }) |
| 95 | +} |
| 96 | + |
| 97 | +// Test updates to K8s resources should not trigger a self-heal when self-heal is false. |
| 98 | +func TestAutomatedSelfHealingAgainstAnnotatedTag(t *testing.T) { |
| 99 | + Given(t). |
| 100 | + Path(guestbookPath). |
| 101 | + When(). |
| 102 | + AddAnnotatedTag("annotated-tag", "my-generic-tag-message"). |
| 103 | + // App should be auto-synced once created |
| 104 | + CreateFromFile(func(app *Application) { |
| 105 | + app.Spec.Source.TargetRevision = "annotated-tag" |
| 106 | + app.Spec.SyncPolicy = &SyncPolicy{Automated: &SyncPolicyAutomated{Prune: true, SelfHeal: false}} |
| 107 | + }). |
| 108 | + Then(). |
| 109 | + Expect(SyncStatusIs(SyncStatusCodeSynced)). |
| 110 | + ExpectConsistently(SyncStatusIs(SyncStatusCodeSynced), WaitDuration, time.Second*10). |
| 111 | + When(). |
| 112 | + // Update the annotated tag to a new git commit, that has a new revisionHistoryLimit. |
| 113 | + PatchFile("guestbook-ui-deployment.yaml", `[{"op": "replace", "path": "/spec/revisionHistoryLimit", "value": 10}]`). |
| 114 | + AddAnnotatedTag("annotated-tag", "my-generic-tag-message"). |
| 115 | + Refresh(RefreshTypeHard). |
| 116 | + // The Application should update to the new annotated tag value within 10 seconds. |
| 117 | + And(func() { |
| 118 | + // Deployment revisionHistoryLimit should switch to 10 |
| 119 | + timeoutErr := wait.PollUntilContextTimeout(t.Context(), 1*time.Second, 10*time.Second, true, func(context.Context) (done bool, err error) { |
| 120 | + deployment, err := fixture.KubeClientset.AppsV1().Deployments(fixture.DeploymentNamespace()).Get(t.Context(), "guestbook-ui", metav1.GetOptions{}) |
| 121 | + if err != nil { |
| 122 | + return false, nil |
| 123 | + } |
| 124 | + |
| 125 | + revisionHistoryLimit := deployment.Spec.RevisionHistoryLimit |
| 126 | + return revisionHistoryLimit != nil && *revisionHistoryLimit == 10, nil |
| 127 | + }) |
| 128 | + require.NoError(t, timeoutErr) |
| 129 | + }). |
| 130 | + // Update the Deployment to a different revisionHistoryLimit |
| 131 | + And(func() { |
| 132 | + errors.NewHandler(t).FailOnErr(fixture.KubeClientset.AppsV1().Deployments(fixture.DeploymentNamespace()).Patch(t.Context(), |
| 133 | + "guestbook-ui", types.MergePatchType, []byte(`{"spec": {"revisionHistoryLimit": 9}}`), metav1.PatchOptions{})) |
| 134 | + }). |
| 135 | + // The revisionHistoryLimit should NOT be self-healed, because selfHealing: false. It should remain at 9. |
| 136 | + And(func() { |
| 137 | + // Wait up to 10 seconds to ensure that deployment revisionHistoryLimit does NOT should switch to 10, it should remain at 9. |
| 138 | + waitErr := wait.PollUntilContextTimeout(t.Context(), 1*time.Second, 10*time.Second, true, func(context.Context) (done bool, err error) { |
| 139 | + deployment, err := fixture.KubeClientset.AppsV1().Deployments(fixture.DeploymentNamespace()).Get(t.Context(), "guestbook-ui", metav1.GetOptions{}) |
| 140 | + if err != nil { |
| 141 | + return false, nil |
| 142 | + } |
| 143 | + |
| 144 | + revisionHistoryLimit := deployment.Spec.RevisionHistoryLimit |
| 145 | + return revisionHistoryLimit != nil && *revisionHistoryLimit != 9, nil |
| 146 | + }) |
| 147 | + require.Error(t, waitErr, "A timeout error should occur, indicating that revisionHistoryLimit never changed from 9") |
| 148 | + }) |
| 149 | +} |
| 150 | + |
| 151 | +func TestAutomatedSelfHealingAgainstLightweightTag(t *testing.T) { |
| 152 | + Given(t). |
| 153 | + Path(guestbookPath). |
| 154 | + When(). |
| 155 | + AddTag("annotated-tag"). |
| 156 | + // App should be auto-synced once created |
| 157 | + CreateFromFile(func(app *Application) { |
| 158 | + app.Spec.Source.TargetRevision = "annotated-tag" |
| 159 | + app.Spec.SyncPolicy = &SyncPolicy{Automated: &SyncPolicyAutomated{Prune: true, SelfHeal: false}} |
| 160 | + }). |
| 161 | + Then(). |
| 162 | + Expect(SyncStatusIs(SyncStatusCodeSynced)). |
| 163 | + ExpectConsistently(SyncStatusIs(SyncStatusCodeSynced), WaitDuration, time.Second*10). |
| 164 | + When(). |
| 165 | + // Update the annotated tag to a new git commit, that has a new revisionHistoryLimit. |
| 166 | + PatchFile("guestbook-ui-deployment.yaml", `[{"op": "replace", "path": "/spec/revisionHistoryLimit", "value": 10}]`). |
| 167 | + AddTagWithForce("annotated-tag"). |
| 168 | + Refresh(RefreshTypeHard). |
| 169 | + // The Application should update to the new annotated tag value within 10 seconds. |
| 170 | + And(func() { |
| 171 | + // Deployment revisionHistoryLimit should switch to 10 |
| 172 | + timeoutErr := wait.PollUntilContextTimeout(t.Context(), 1*time.Second, 10*time.Second, true, func(context.Context) (done bool, err error) { |
| 173 | + deployment, err := fixture.KubeClientset.AppsV1().Deployments(fixture.DeploymentNamespace()).Get(t.Context(), "guestbook-ui", metav1.GetOptions{}) |
| 174 | + if err != nil { |
| 175 | + return false, nil |
| 176 | + } |
| 177 | + |
| 178 | + revisionHistoryLimit := deployment.Spec.RevisionHistoryLimit |
| 179 | + return revisionHistoryLimit != nil && *revisionHistoryLimit == 10, nil |
| 180 | + }) |
| 181 | + require.NoError(t, timeoutErr) |
| 182 | + }). |
| 183 | + // Update the Deployment to a different revisionHistoryLimit |
| 184 | + And(func() { |
| 185 | + errors.NewHandler(t).FailOnErr(fixture.KubeClientset.AppsV1().Deployments(fixture.DeploymentNamespace()).Patch(t.Context(), |
| 186 | + "guestbook-ui", types.MergePatchType, []byte(`{"spec": {"revisionHistoryLimit": 9}}`), metav1.PatchOptions{})) |
| 187 | + }). |
| 188 | + // The revisionHistoryLimit should NOT be self-healed, because selfHealing: false |
| 189 | + And(func() { |
| 190 | + // Wait up to 10 seconds to ensure that deployment revisionHistoryLimit does NOT should switch to 10, it should remain at 9. |
| 191 | + waitErr := wait.PollUntilContextTimeout(t.Context(), 1*time.Second, 10*time.Second, true, func(context.Context) (done bool, err error) { |
| 192 | + deployment, err := fixture.KubeClientset.AppsV1().Deployments(fixture.DeploymentNamespace()).Get(t.Context(), "guestbook-ui", metav1.GetOptions{}) |
| 193 | + if err != nil { |
| 194 | + return false, nil |
| 195 | + } |
| 196 | + |
| 197 | + revisionHistoryLimit := deployment.Spec.RevisionHistoryLimit |
| 198 | + return revisionHistoryLimit != nil && *revisionHistoryLimit != 9, nil |
| 199 | + }) |
| 200 | + require.Error(t, waitErr, "A timeout error should occur, indicating that revisionHistoryLimit never changed from 9") |
| 201 | + }) |
| 202 | +} |
0 commit comments