Skip to content

Commit

Permalink
tests: Add unit tests for pkg/argocd (argoproj-labs#818)
Browse files Browse the repository at this point in the history
Signed-off-by: isequeir <[email protected]>
  • Loading branch information
ishitasequeira authored and sribiere-jellysmack committed Aug 13, 2024
1 parent 388f586 commit 4d21d86
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
51 changes: 51 additions & 0 deletions pkg/argocd/gitcreds_test.go
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")
}
21 changes: 21 additions & 0 deletions pkg/argocd/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3185,3 +3185,24 @@ func mockGit(t *testing.T) (gitMock *gitmock.Client, dir string, cleanup func())
_ = os.RemoveAll(dir)
}
}

func Test_GetRepositoryLock(t *testing.T) {
state := NewSyncIterationState()

// Test case 1: Get lock for a repository that doesn't exist in the state
repo1 := "repo1"
lock1 := state.GetRepositoryLock(repo1)
require.NotNil(t, lock1)
require.Equal(t, lock1, state.repositoryLocks[repo1])

// Test case 2: Get lock for the same repository again, should return the same lock
lock2 := state.GetRepositoryLock(repo1)
require.Equal(t, lock1, lock2)

// Test case 3: Get lock for a different repository, should return a different lock
repo2 := "repo2"
lock3 := state.GetRepositoryLock(repo2)
require.NotNil(t, lock3)
require.NotNil(t, state.repositoryLocks[repo2])
require.Equal(t, lock3, state.repositoryLocks[repo2])
}

0 comments on commit 4d21d86

Please sign in to comment.