Skip to content

Commit a3f5b6c

Browse files
ishitasequeiraTchoupinax
authored andcommitted
Add unit tests for pkg/argocd/gitcreds.go (argoproj-labs#820)
Signed-off-by: isequeir <[email protected]> Signed-off-by: Tchoupinax <[email protected]>
1 parent 05561b9 commit a3f5b6c

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

pkg/argocd/gitcreds_test.go

+109
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"github.com/argoproj-labs/argocd-image-updater/test/fake"
99
"github.com/argoproj-labs/argocd-image-updater/test/fixture"
1010

11+
"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
12+
1113
"github.com/stretchr/testify/assert"
1214
)
1315

@@ -49,3 +51,110 @@ func TestGetCredsFromSecret(t *testing.T) {
4951
assert.Error(t, err)
5052
assert.EqualError(t, err, "unknown repository type")
5153
}
54+
55+
func TestGetGitCredsSource(t *testing.T) {
56+
kubeClient := &kube.KubernetesClient{}
57+
wbc := &WriteBackConfig{
58+
GitRepo: "https://github.com/example/repo.git",
59+
GitCreds: git.NoopCredsStore{},
60+
}
61+
62+
// Test case 1: 'repocreds' credentials
63+
creds1, err := getGitCredsSource("repocreds", kubeClient, wbc)
64+
assert.NoError(t, err)
65+
assert.NotNil(t, creds1)
66+
67+
// Test case 2: 'secret:<namespace>/<secret>' credentials
68+
creds2, err := getGitCredsSource("secret:foo/bar", kubeClient, wbc)
69+
assert.NoError(t, err)
70+
assert.NotNil(t, creds2)
71+
72+
// Test case 3: Unexpected credentials format
73+
_, err = getGitCredsSource("invalid", kubeClient, wbc)
74+
assert.Error(t, err)
75+
assert.EqualError(t, err, "unexpected credentials format. Expected 'repocreds' or 'secret:<namespace>/<secret>' but got 'invalid'")
76+
}
77+
78+
func TestGetCAPath(t *testing.T) {
79+
// Test case 1: HTTPS URL
80+
repoURL := "https://github.com/example/repo.git"
81+
expectedCAPath := ""
82+
caPath := getCAPath(repoURL)
83+
assert.Equal(t, expectedCAPath, caPath)
84+
85+
// Test case 2: OCI URL
86+
repoURL = "oci://example.com/repo"
87+
expectedCAPath = ""
88+
caPath = getCAPath(repoURL)
89+
assert.Equal(t, expectedCAPath, caPath)
90+
91+
// Test case 3: SSH URL
92+
repoURL = "[email protected]:example/repo.git"
93+
expectedCAPath = ""
94+
caPath = getCAPath(repoURL)
95+
assert.Equal(t, expectedCAPath, caPath)
96+
97+
// Test case 4: Invalid URL
98+
repoURL = "invalid-url"
99+
expectedCAPath = ""
100+
caPath = getCAPath(repoURL)
101+
assert.Equal(t, expectedCAPath, caPath)
102+
}
103+
104+
func TestGetGitCreds(t *testing.T) {
105+
106+
store := git.NoopCredsStore{}
107+
108+
// Test case 1: HTTP credentials
109+
repo := &v1alpha1.Repository{
110+
Username: "myuser",
111+
Password: "mypass",
112+
Repo: "https://github.com/example/repo.git",
113+
}
114+
expectedHTTPSCreds := git.NewHTTPSCreds("myuser", "mypass", "", "", false, "", store, false)
115+
httpCreds := GetGitCreds(repo, store)
116+
assert.Equal(t, expectedHTTPSCreds, httpCreds)
117+
118+
// Test case 2: SSH credentials
119+
repo = &v1alpha1.Repository{
120+
Username: "myuser",
121+
SSHPrivateKey: "privatekey",
122+
Repo: "https://github.com/example/repo.git",
123+
}
124+
expectedSSHCreds := git.NewSSHCreds("privatekey", "", false, store, "")
125+
sshCreds := GetGitCreds(repo, store)
126+
assert.Equal(t, expectedSSHCreds, sshCreds)
127+
128+
// Test case 3: GitHub App credentials
129+
repo = &v1alpha1.Repository{
130+
Username: "myuser",
131+
GithubAppPrivateKey: "appprivatekey",
132+
GithubAppId: 123,
133+
GithubAppInstallationId: 456,
134+
GitHubAppEnterpriseBaseURL: "enterpriseurl",
135+
Repo: "https://github.com/example/repo.git",
136+
TLSClientCertData: "certdata",
137+
TLSClientCertKey: "certkey",
138+
Insecure: true,
139+
Proxy: "proxy",
140+
}
141+
expectedGitHubAppCreds := git.NewGitHubAppCreds(123, 456, "appprivatekey", "enterpriseurl", "https://github.com/example/repo.git", "certdata", "certkey", true, "proxy", store)
142+
githubAppCreds := GetGitCreds(repo, store)
143+
assert.Equal(t, expectedGitHubAppCreds, githubAppCreds)
144+
145+
// Test case 4: Google Cloud credentials
146+
repo = &v1alpha1.Repository{
147+
Username: "myuser",
148+
GCPServiceAccountKey: "serviceaccountkey",
149+
}
150+
expectedGoogleCloudCreds := git.NewGoogleCloudCreds("serviceaccountkey", store)
151+
googleCloudCreds := GetGitCreds(repo, store)
152+
repo.Password = ""
153+
repo.SSHPrivateKey = ""
154+
assert.Equal(t, expectedGoogleCloudCreds, googleCloudCreds)
155+
156+
// Test case 5: No credentials
157+
expectedNopCreds := git.NopCreds{}
158+
nopCreds := GetGitCreds(nil, store)
159+
assert.Equal(t, expectedNopCreds, nopCreds)
160+
}

0 commit comments

Comments
 (0)