Skip to content

Commit 65b0e7e

Browse files
committed
test: improved acr_service tests
Improved to check annotations, annotations feature flag and handle more error/no-op conditions Signed-off-by: Eugene Doudine <eugene.doudine@octopus.com>
1 parent 7150d49 commit 65b0e7e

File tree

2 files changed

+85
-33
lines changed

2 files changed

+85
-33
lines changed

acr_controller/service/acr_service.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func (c *acrService) ChangeRevision(ctx context.Context, a *application.Applicat
8383

8484
currentRevision, previousRevision := c.getRevisions(ctx, a)
8585
if currentRevision == "" {
86-
c.logger.Infof("Got empty revision for application %s, is it an unsupported multisource or helm repo based application?", app.Name)
86+
c.logger.Infof("Got empty current revision for application %s, is it an unsupported multisource or helm repo based application?", app.Name)
8787
return nil
8888
}
8989
revision, err := c.calculateRevision(ctx, app, currentRevision, previousRevision)
@@ -122,15 +122,15 @@ func (c *acrService) ChangeRevision(ctx context.Context, a *application.Applicat
122122
}
123123
}
124124
if len(patchMap) > 0 {
125-
c.logger.Infof("patching resource: %v", patchMap)
125+
c.logger.Infof("Patching resource: %v", patchMap)
126126
patch, err := json.Marshal(patchMap)
127127
if err != nil {
128128
return err
129129
}
130130
_, err = c.applicationClientset.ArgoprojV1alpha1().Applications(a.Namespace).Patch(ctx, a.Name, types.MergePatchType, patch, metav1.PatchOptions{})
131131
return err
132132
}
133-
c.logger.Infof("no patch needed")
133+
c.logger.Infof("No patch needed")
134134
return nil
135135
}
136136

acr_controller/service/acr_service_test.go

Lines changed: 82 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,26 @@ spec:
3838
server: https://cluster-api.example.com
3939
`
4040

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+
4161
const fakeAppWithOperation = `
4262
apiVersion: argoproj.io/v1alpha1
4363
kind: Application
@@ -266,53 +286,85 @@ func Test_getRevisions(r *testing.T) {
266286
}
267287

268288
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
285293

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()
287296
client := &mocks.ApplicationClient{}
288297
client.On("GetChangeRevision", mock.Anything, mock.Anything).Return(&appclient.ChangeRevisionResponse{
289-
Revision: ptr.To("new-revision"),
298+
Revision: ptr.To(newChangeRevision),
290299
}, nil)
291-
292300
logger, logHook := test2.NewNullLogger()
293-
294301
acrService := newTestACRService(client)
295302
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+
}
296310

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+
}
298319

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{})
300323
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+
})
301331

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)
303337
require.NoError(t, err)
338+
testLastLogEntry(t, logHook, "Change revision already calculated for application guestbook")
339+
})
304340

305-
assert.Equal(t, "new-revision", app.Status.OperationState.Operation.Sync.ChangeRevision)
341+
useAnnotations = false
306342

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+
})
308350

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{})
309356
require.NoError(t, err)
357+
assert.Len(t, app.GetAnnotations(), 1)
358+
testLastLogEntry(t, logHook, "No patch needed")
359+
})
310360

311-
lastLogEntry := logHook.LastEntry()
312-
if lastLogEntry == nil {
313-
t.Fatal("No log entry")
314-
}
361+
appSrc = multisourceApp
315362

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?")
317369
})
318370
}

0 commit comments

Comments
 (0)