|
38 | 38 | server: https://cluster-api.example.com |
39 | 39 | ` |
40 | 40 |
|
| 41 | +const multisourceApp = ` |
| 42 | +apiVersion: argoproj.io/v1alpha1 |
| 43 | +kind: Application |
| 44 | +metadata: |
| 45 | + annotations: |
| 46 | + argocd.argoproj.io/manifest-generate-paths: . |
| 47 | + name: guestbook |
| 48 | + namespace: codefresh |
| 49 | +spec: |
| 50 | + sources: |
| 51 | + - path: some/path |
| 52 | + repoURL: https://github.com/argoproj/argocd-example-apps.git |
| 53 | + targetRevision: HEAD |
| 54 | + ksonnet: |
| 55 | + environment: default |
| 56 | + destination: |
| 57 | + namespace: ` + test.FakeDestNamespace + ` |
| 58 | + server: https://cluster-api.example.com |
| 59 | +` |
| 60 | + |
41 | 61 | const fakeAppWithOperation = ` |
42 | 62 | apiVersion: argoproj.io/v1alpha1 |
43 | 63 | kind: Application |
@@ -266,53 +286,85 @@ func Test_getRevisions(r *testing.T) { |
266 | 286 | } |
267 | 287 |
|
268 | 288 | func Test_ChangeRevision(r *testing.T) { |
269 | | - r.Run("Change revision", func(t *testing.T) { |
270 | | - client := &mocks.ApplicationClient{} |
271 | | - client.On("GetChangeRevision", mock.Anything, mock.Anything).Return(&appclient.ChangeRevisionResponse{ |
272 | | - Revision: ptr.To("new-revision"), |
273 | | - }, nil) |
274 | | - acrService := newTestACRService(client) |
275 | | - app := createTestApp(syncedAppWithHistory) |
276 | | - |
277 | | - err := acrService.ChangeRevision(r.Context(), app, false) |
278 | | - require.NoError(t, err) |
279 | | - |
280 | | - app, err = acrService.applicationClientset.ArgoprojV1alpha1().Applications(app.Namespace).Get(r.Context(), app.Name, metav1.GetOptions{}) |
281 | | - require.NoError(t, err) |
282 | | - |
283 | | - assert.Equal(t, "new-revision", app.Status.OperationState.Operation.Sync.ChangeRevision) |
284 | | - }) |
| 289 | + newChangeRevision := "new-revision" |
| 290 | + gitRevision := "c732f4d2ef24c7eeb900e9211ff98f90bb646505" |
| 291 | + useAnnotations := true |
| 292 | + appSrc := syncedAppWithHistory |
285 | 293 |
|
286 | | - r.Run("Change revision already exists", func(t *testing.T) { |
| 294 | + testNewChangeRevision := func(t *testing.T) (*acrService, *test2.Hook, *appsv1.Application) { |
| 295 | + t.Helper() |
287 | 296 | client := &mocks.ApplicationClient{} |
288 | 297 | client.On("GetChangeRevision", mock.Anything, mock.Anything).Return(&appclient.ChangeRevisionResponse{ |
289 | | - Revision: ptr.To("new-revision"), |
| 298 | + Revision: ptr.To(newChangeRevision), |
290 | 299 | }, nil) |
291 | | - |
292 | 300 | logger, logHook := test2.NewNullLogger() |
293 | | - |
294 | 301 | acrService := newTestACRService(client) |
295 | 302 | acrService.logger = logger |
| 303 | + app := createTestApp(appSrc) |
| 304 | + orgManifestPath := app.GetAnnotation(appsv1.AnnotationKeyManifestGeneratePaths) |
| 305 | + err := acrService.ChangeRevision(r.Context(), app, useAnnotations) |
| 306 | + require.NoError(t, err) |
| 307 | + assert.Equal(t, orgManifestPath, app.GetAnnotation(appsv1.AnnotationKeyManifestGeneratePaths)) |
| 308 | + return acrService, logHook, app |
| 309 | + } |
296 | 310 |
|
297 | | - app := createTestApp(syncedAppWithHistory) |
| 311 | + testLastLogEntry := func(t *testing.T, hook *test2.Hook, expectedMsg string) { |
| 312 | + t.Helper() |
| 313 | + lastLogEntry := hook.LastEntry() |
| 314 | + if lastLogEntry == nil { |
| 315 | + t.Fatal("No log entry") |
| 316 | + } |
| 317 | + require.Equal(t, expectedMsg, lastLogEntry.Message) |
| 318 | + } |
298 | 319 |
|
299 | | - err := acrService.ChangeRevision(r.Context(), app, false) |
| 320 | + r.Run("New change revision with annotations", func(t *testing.T) { |
| 321 | + acrService, _, app := testNewChangeRevision(t) |
| 322 | + app, err := acrService.applicationClientset.ArgoprojV1alpha1().Applications(app.Namespace).Get(r.Context(), app.Name, metav1.GetOptions{}) |
300 | 323 | require.NoError(t, err) |
| 324 | + assert.Len(t, app.GetAnnotations(), 5) |
| 325 | + assert.Equal(t, newChangeRevision, app.Status.OperationState.Operation.Sync.ChangeRevision) |
| 326 | + assert.Equal(t, newChangeRevision, app.GetAnnotation(CHANGE_REVISION_ANN)) |
| 327 | + assert.Equal(t, "[\""+"new-revision"+"\"]", app.GetAnnotation(CHANGE_REVISIONS_ANN)) |
| 328 | + assert.Equal(t, gitRevision, app.GetAnnotation(GIT_REVISION_ANN)) |
| 329 | + assert.Equal(t, "[\""+gitRevision+"\"]", app.GetAnnotation(GIT_REVISIONS_ANN)) |
| 330 | + }) |
301 | 331 |
|
302 | | - app, err = acrService.applicationClientset.ArgoprojV1alpha1().Applications(app.Namespace).Get(r.Context(), app.Name, metav1.GetOptions{}) |
| 332 | + r.Run("Change revision already exists with annotations", func(t *testing.T) { |
| 333 | + acrService, logHook, app := testNewChangeRevision(t) |
| 334 | + app, err := acrService.applicationClientset.ArgoprojV1alpha1().Applications(app.Namespace).Get(r.Context(), app.Name, metav1.GetOptions{}) |
| 335 | + require.NoError(t, err) |
| 336 | + err = acrService.ChangeRevision(r.Context(), app, useAnnotations) |
303 | 337 | require.NoError(t, err) |
| 338 | + testLastLogEntry(t, logHook, "Change revision already calculated for application guestbook") |
| 339 | + }) |
304 | 340 |
|
305 | | - assert.Equal(t, "new-revision", app.Status.OperationState.Operation.Sync.ChangeRevision) |
| 341 | + useAnnotations = false |
306 | 342 |
|
307 | | - err = acrService.ChangeRevision(r.Context(), app, false) |
| 343 | + r.Run("New change revision without annotations", func(t *testing.T) { |
| 344 | + acrService, _, app := testNewChangeRevision(t) |
| 345 | + app, err := acrService.applicationClientset.ArgoprojV1alpha1().Applications(app.Namespace).Get(r.Context(), app.Name, metav1.GetOptions{}) |
| 346 | + require.NoError(t, err) |
| 347 | + assert.Len(t, app.GetAnnotations(), 1) |
| 348 | + assert.Equal(t, newChangeRevision, app.Status.OperationState.Operation.Sync.ChangeRevision) |
| 349 | + }) |
308 | 350 |
|
| 351 | + appSrc = syncedAppWithSingleHistory |
| 352 | + newChangeRevision = "" |
| 353 | + r.Run("No previous revision", func(t *testing.T) { |
| 354 | + acrService, logHook, app := testNewChangeRevision(t) |
| 355 | + app, err := acrService.applicationClientset.ArgoprojV1alpha1().Applications(app.Namespace).Get(r.Context(), app.Name, metav1.GetOptions{}) |
309 | 356 | require.NoError(t, err) |
| 357 | + assert.Len(t, app.GetAnnotations(), 1) |
| 358 | + testLastLogEntry(t, logHook, "No patch needed") |
| 359 | + }) |
310 | 360 |
|
311 | | - lastLogEntry := logHook.LastEntry() |
312 | | - if lastLogEntry == nil { |
313 | | - t.Fatal("No log entry") |
314 | | - } |
| 361 | + appSrc = multisourceApp |
315 | 362 |
|
316 | | - require.Equal(t, "Change revision already calculated for application guestbook", lastLogEntry.Message) |
| 363 | + r.Run("No current revision", func(t *testing.T) { |
| 364 | + acrService, logHook, app := testNewChangeRevision(t) |
| 365 | + app, err := acrService.applicationClientset.ArgoprojV1alpha1().Applications(app.Namespace).Get(r.Context(), app.Name, metav1.GetOptions{}) |
| 366 | + require.NoError(t, err) |
| 367 | + assert.Len(t, app.GetAnnotations(), 1) |
| 368 | + testLastLogEntry(t, logHook, "Got empty current revision for application guestbook, is it an unsupported multisource or helm repo based application?") |
317 | 369 | }) |
318 | 370 | } |
0 commit comments