Skip to content

Commit 60b89f2

Browse files
authored
tests: add tests for github app credentials (#862)
Signed-off-by: Cheng Fang <cfang@redhat.com>
1 parent c8bcd8f commit 60b89f2

File tree

2 files changed

+102
-1
lines changed

2 files changed

+102
-1
lines changed

pkg/argocd/gitcreds.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func getCredsFromSecret(wbc *WriteBackConfig, credentialsSecret string, kubeClie
145145
if err != nil {
146146
return nil, fmt.Errorf("invalid value in field githubAppID: %w", err)
147147
}
148-
intGithubAppInstallationID, _ := strconv.ParseInt(string(githubAppInstallationID), 10, 64)
148+
intGithubAppInstallationID, err := strconv.ParseInt(string(githubAppInstallationID), 10, 64)
149149
if err != nil {
150150
return nil, fmt.Errorf("invalid value in field githubAppInstallationID: %w", err)
151151
}

pkg/argocd/update_test.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,75 @@ func Test_UpdateApplication(t *testing.T) {
9595
assert.Equal(t, 2, res.NumImagesUpdated)
9696
})
9797

98+
t.Run("Update app w/ GitHub App creds", func(t *testing.T) {
99+
mockClientFn := func(endpoint *registry.RegistryEndpoint, username, password string) (registry.RegistryClient, error) {
100+
regMock := regmock.RegistryClient{}
101+
regMock.On("NewRepository", mock.Anything).Return(nil)
102+
regMock.On("Tags", mock.Anything).Return([]string{"1.0.2", "1.0.3"}, nil)
103+
return &regMock, nil
104+
}
105+
106+
argoClient := argomock.ArgoCD{}
107+
argoClient.On("UpdateSpec", mock.Anything, mock.Anything).Return(nil, nil)
108+
109+
secret := fixture.NewSecret("argocd-image-updater", "git-creds", map[string][]byte{
110+
"githubAppID": []byte("12345678"),
111+
"githubAppInstallationID": []byte("87654321"),
112+
"githubAppPrivateKey": []byte("foo"),
113+
})
114+
kubeClient := kube.KubernetesClient{
115+
Clientset: fake.NewFakeClientsetWithResources(secret),
116+
}
117+
118+
annotations := map[string]string{
119+
common.ImageUpdaterAnnotation: "foo=gcr.io/jannfis/foobar:>=1.0.1",
120+
common.WriteBackMethodAnnotation: "git:secret:argocd-image-updater/git-creds",
121+
}
122+
appImages := &ApplicationImages{
123+
Application: v1alpha1.Application{
124+
ObjectMeta: v1.ObjectMeta{
125+
Name: "guestbook",
126+
Namespace: "guestbook",
127+
Annotations: annotations,
128+
},
129+
Spec: v1alpha1.ApplicationSpec{
130+
Source: &v1alpha1.ApplicationSource{
131+
RepoURL: "https://example.com/example",
132+
TargetRevision: "main",
133+
Kustomize: &v1alpha1.ApplicationSourceKustomize{
134+
Images: v1alpha1.KustomizeImages{
135+
"jannfis/foobar:1.0.1",
136+
},
137+
},
138+
},
139+
},
140+
Status: v1alpha1.ApplicationStatus{
141+
SourceType: v1alpha1.ApplicationSourceTypeKustomize,
142+
Summary: v1alpha1.ApplicationSummary{
143+
Images: []string{
144+
"gcr.io/jannfis/foobar:1.0.1",
145+
},
146+
},
147+
},
148+
},
149+
Images: *parseImageList(annotations),
150+
}
151+
res := UpdateApplication(&UpdateConfiguration{
152+
NewRegFN: mockClientFn,
153+
ArgoClient: &argoClient,
154+
KubeClient: &kubeClient,
155+
UpdateApp: appImages,
156+
DryRun: false,
157+
}, NewSyncIterationState())
158+
assert.Equal(t, v1alpha1.KustomizeImage("gcr.io/jannfis/foobar:1.0.3"), appImages.Application.Spec.Source.Kustomize.Images[0])
159+
assert.Equal(t, 0, res.NumSkipped)
160+
assert.Equal(t, 1, res.NumApplicationsProcessed)
161+
assert.Equal(t, 1, res.NumImagesConsidered)
162+
// configured githubApp creds will take effect and git client will catch the invalid GithubAppPrivateKey "foo":
163+
// "Could not update application spec: could not parse private key: invalid key: Key must be a PEM encoded PKCS1 or PKCS8 key"
164+
assert.Equal(t, 1, res.NumErrors)
165+
})
166+
98167
t.Run("Test successful update", func(t *testing.T) {
99168
mockClientFn := func(endpoint *registry.RegistryEndpoint, username, password string) (registry.RegistryClient, error) {
100169
regMock := regmock.RegistryClient{}
@@ -2623,6 +2692,38 @@ func Test_GetGitCreds(t *testing.T) {
26232692
// Must have HTTPS GitHub App creds
26242693
_, ok := creds.(git.GitHubAppCreds)
26252694
require.True(t, ok)
2695+
2696+
// invalid secrete data in GitHub App creds
2697+
invalidSecretEntries := []map[string][]byte{
2698+
{ // missing githubAppPrivateKey
2699+
"githubAppID": []byte("12345678"),
2700+
"githubAppInstallationID": []byte("87654321"),
2701+
}, { // missing githubAppInstallationID
2702+
"githubAppID": []byte("12345678"),
2703+
"githubAppPrivateKey": []byte("foo"),
2704+
}, { // missing githubAppID
2705+
"githubAppInstallationID": []byte("87654321"),
2706+
"githubAppPrivateKey": []byte("foo"),
2707+
}, { // ID should be a number
2708+
"githubAppID": []byte("NaN"),
2709+
"githubAppInstallationID": []byte("87654321"),
2710+
"githubAppPrivateKey": []byte("foo"),
2711+
}, {
2712+
"githubAppID": []byte("12345678"),
2713+
"githubAppInstallationID": []byte("NaN"),
2714+
"githubAppPrivateKey": []byte("foo"),
2715+
},
2716+
}
2717+
for _, secretEntry := range invalidSecretEntries {
2718+
secret = fixture.NewSecret("argocd-image-updater", "git-creds", secretEntry)
2719+
kubeClient = kube.KubernetesClient{
2720+
Clientset: fake.NewFakeClientsetWithResources(secret),
2721+
}
2722+
wbc, err = getWriteBackConfig(&app, &kubeClient, &argoClient)
2723+
require.NoError(t, err)
2724+
_, err = wbc.GetCreds(&app)
2725+
require.Error(t, err)
2726+
}
26262727
})
26272728

26282729
t.Run("SSH creds from a secret", func(t *testing.T) {

0 commit comments

Comments
 (0)