forked from argoproj-labs/argocd-image-updater
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: Add unit tests for pkg/argocd (argoproj-labs#818)
Signed-off-by: isequeir <[email protected]>
- Loading branch information
1 parent
388f586
commit 4d21d86
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package argocd | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/argoproj-labs/argocd-image-updater/ext/git" | ||
"github.com/argoproj-labs/argocd-image-updater/pkg/kube" | ||
"github.com/argoproj-labs/argocd-image-updater/test/fake" | ||
"github.com/argoproj-labs/argocd-image-updater/test/fixture" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetCredsFromSecret(t *testing.T) { | ||
wbc := &WriteBackConfig{ | ||
GitRepo: "https://github.com/example/repo.git", | ||
GitCreds: git.NoopCredsStore{}, | ||
} | ||
|
||
secret1 := fixture.NewSecret("foo", "bar", map[string][]byte{"username": []byte("myuser"), "password": []byte("mypass")}) | ||
secret2 := fixture.NewSecret("foo1", "bar1", map[string][]byte{"username": []byte("myuser")}) | ||
kubeClient := kube.KubernetesClient{ | ||
Clientset: fake.NewFakeClientsetWithResources(secret1, secret2), | ||
} | ||
|
||
// Test case 1: Valid secret reference | ||
credentialsSecret := "foo/bar" | ||
expectedCreds := git.NewHTTPSCreds("myuser", "mypass", "", "", true, "", wbc.GitCreds, false) | ||
creds, err := getCredsFromSecret(wbc, credentialsSecret, &kubeClient) | ||
assert.NoError(t, err) | ||
assert.Equal(t, expectedCreds, creds) | ||
|
||
// Test case 2: Invalid secret reference | ||
credentialsSecret = "invalid" | ||
_, err = getCredsFromSecret(wbc, credentialsSecret, &kubeClient) | ||
assert.Error(t, err) | ||
assert.EqualError(t, err, "secret ref must be in format 'namespace/name', but is 'invalid'") | ||
|
||
// Test case 3: Missing field in secret | ||
credentialsSecret = "foo1/bar1" | ||
_, err = getCredsFromSecret(wbc, credentialsSecret, &kubeClient) | ||
assert.Error(t, err) | ||
assert.EqualError(t, err, "invalid secret foo1/bar1: does not contain field password") | ||
|
||
// Test case 4: Unknown repository type | ||
credentialsSecret = "foo/bar" | ||
wbc.GitRepo = "unknown://example.com/repo.git" | ||
_, err = getCredsFromSecret(wbc, credentialsSecret, &kubeClient) | ||
assert.Error(t, err) | ||
assert.EqualError(t, err, "unknown repository type") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters