mirror of
https://github.com/hanzoai/deploy.git
synced 2026-10-07 08:44:50 +00:00
go.mod still declared `module github.com/hanzoai/deploy` after the repo was renamed. A Go module path is not cosmetic — it is the resolvable location. So `go get github.com/hanzoai/cd` could not work, and anything importing this module had to spell a name that no longer exists at that address. The build log of v3.6.0 is where it surfaced: the compiler was still emitting github.com/hanzoai/deploy/cmd/cd/commands. 3541 import paths across 690 .go, 19 .proto, 4 .yaml, 3 .sh, 2 .mod, Procfile and Makefile. Plus the 62 ghcr.io/hanzoai/deploy image references, moved to ghcr.io/hanzoai/cd to match hanzo.yml's build target — a distinct string from the module path, replaced separately so neither could clobber the other. go mod tidy clean, CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build ./... clean. Co-authored-by: Hanzo Dev <[email protected]>
424 lines
13 KiB
Go
424 lines
13 KiB
Go
package db
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
corev1 "k8s.io/api/core/v1"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"github.com/hanzoai/cd/common"
|
|
appsv1 "github.com/hanzoai/cd/pkg/apis/application/v1alpha1"
|
|
"github.com/hanzoai/cd/util/settings"
|
|
)
|
|
|
|
var repoArgoCD = &corev1.Secret{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Namespace: testNamespace,
|
|
Name: "some-repo-secret",
|
|
Annotations: map[string]string{
|
|
common.AnnotationKeyManagedBy: common.AnnotationValueManagedByArgoCD,
|
|
},
|
|
Labels: map[string]string{
|
|
common.LabelKeySecretType: common.LabelValueSecretTypeRepository,
|
|
},
|
|
},
|
|
Data: map[string][]byte{
|
|
"name": []byte("SomeRepo"),
|
|
"url": []byte("[email protected]:argoproj/argo-cd.git"),
|
|
"username": []byte("someUsername"),
|
|
"password": []byte("somePassword"),
|
|
"type": []byte("git"),
|
|
},
|
|
}
|
|
|
|
var repoArgoProj = &corev1.Secret{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Namespace: testNamespace,
|
|
Name: "some-other-repo-secret",
|
|
Annotations: map[string]string{
|
|
common.AnnotationKeyManagedBy: common.AnnotationValueManagedByArgoCD,
|
|
},
|
|
Labels: map[string]string{
|
|
common.LabelKeySecretType: common.LabelValueSecretTypeRepository,
|
|
},
|
|
},
|
|
Data: map[string][]byte{
|
|
"name": []byte("OtherRepo"),
|
|
"url": []byte("[email protected]:argoproj/argoproj.git"),
|
|
"username": []byte("someUsername"),
|
|
"password": []byte("somePassword"),
|
|
"type": []byte("git"),
|
|
},
|
|
}
|
|
|
|
var repoArgoCDWrite = &corev1.Secret{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Namespace: testNamespace,
|
|
Name: "some-repo-secret",
|
|
Annotations: map[string]string{
|
|
common.AnnotationKeyManagedBy: common.AnnotationValueManagedByArgoCD,
|
|
},
|
|
Labels: map[string]string{
|
|
common.LabelKeySecretType: common.LabelValueSecretTypeRepositoryWrite,
|
|
},
|
|
},
|
|
Data: map[string][]byte{
|
|
"name": []byte("SomeRepo"),
|
|
"url": []byte("[email protected]:argoproj/argo-cd.git"),
|
|
"username": []byte("someUsername"),
|
|
"password": []byte("somePassword"),
|
|
"type": []byte("git"),
|
|
},
|
|
}
|
|
|
|
var repoArgoProjWrite = &corev1.Secret{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Namespace: testNamespace,
|
|
Name: "some-other-repo-secret",
|
|
Annotations: map[string]string{
|
|
common.AnnotationKeyManagedBy: common.AnnotationValueManagedByArgoCD,
|
|
},
|
|
Labels: map[string]string{
|
|
common.LabelKeySecretType: common.LabelValueSecretTypeRepositoryWrite,
|
|
},
|
|
},
|
|
Data: map[string][]byte{
|
|
"name": []byte("OtherRepo"),
|
|
"url": []byte("[email protected]:argoproj/argoproj.git"),
|
|
"username": []byte("someUsername"),
|
|
"password": []byte("somePassword"),
|
|
"type": []byte("git"),
|
|
},
|
|
}
|
|
|
|
func TestDb_CreateRepository(t *testing.T) {
|
|
t.Parallel()
|
|
clientset := getClientset()
|
|
settingsManager := settings.NewSettingsManager(t.Context(), clientset, testNamespace)
|
|
testee := &db{
|
|
ns: testNamespace,
|
|
kubeclientset: clientset,
|
|
settingsMgr: settingsManager,
|
|
}
|
|
|
|
input := &appsv1.Repository{
|
|
Name: "TestRepo",
|
|
Repo: "[email protected]:argoproj/argo-cd.git",
|
|
Username: "someUsername",
|
|
Password: "somePassword",
|
|
}
|
|
|
|
// The repository was indeed created successfully
|
|
output, err := testee.CreateRepository(t.Context(), input)
|
|
require.NoError(t, err)
|
|
assert.Same(t, input, output)
|
|
|
|
secret, err := clientset.CoreV1().Secrets(testNamespace).Get(
|
|
t.Context(),
|
|
RepoURLToSecretName(repoSecretPrefix, input.Repo, ""),
|
|
metav1.GetOptions{},
|
|
)
|
|
assert.NotNil(t, secret)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
func TestDb_GetRepository(t *testing.T) {
|
|
t.Parallel()
|
|
clientset := getClientset(repoArgoCD, repoArgoProj)
|
|
settingsManager := settings.NewSettingsManager(t.Context(), clientset, testNamespace)
|
|
testee := &db{
|
|
ns: testNamespace,
|
|
kubeclientset: clientset,
|
|
settingsMgr: settingsManager,
|
|
}
|
|
|
|
repository, err := testee.GetRepository(t.Context(), "[email protected]:argoproj/argoproj.git", "")
|
|
require.NoError(t, err)
|
|
require.NotNil(t, repository)
|
|
assert.Equal(t, "OtherRepo", repository.Name)
|
|
|
|
repository, err = testee.GetRepository(t.Context(), "[email protected]:argoproj/argo-cd.git", "")
|
|
require.NoError(t, err)
|
|
require.NotNil(t, repository)
|
|
assert.Equal(t, "SomeRepo", repository.Name)
|
|
|
|
repository, err = testee.GetRepository(t.Context(), "[email protected]:argoproj/not-existing.git", "")
|
|
require.NoError(t, err)
|
|
assert.NotNil(t, repository)
|
|
assert.Equal(t, "[email protected]:argoproj/not-existing.git", repository.Repo)
|
|
}
|
|
|
|
func TestDb_GetWriteRepository(t *testing.T) {
|
|
t.Parallel()
|
|
clientset := getClientset(repoArgoCDWrite, repoArgoProjWrite)
|
|
settingsManager := settings.NewSettingsManager(t.Context(), clientset, testNamespace)
|
|
testee := &db{
|
|
ns: testNamespace,
|
|
kubeclientset: clientset,
|
|
settingsMgr: settingsManager,
|
|
}
|
|
|
|
repository, err := testee.GetWriteRepository(t.Context(), "[email protected]:argoproj/argoproj.git", "")
|
|
require.NoError(t, err)
|
|
require.NotNil(t, repository)
|
|
assert.Equal(t, "OtherRepo", repository.Name)
|
|
|
|
repository, err = testee.GetWriteRepository(t.Context(), "[email protected]:argoproj/argo-cd.git", "")
|
|
require.NoError(t, err)
|
|
require.NotNil(t, repository)
|
|
assert.Equal(t, "SomeRepo", repository.Name)
|
|
}
|
|
|
|
func TestDb_GetWriteRepository_SecretNotFound_DefaultRepo(t *testing.T) {
|
|
t.Parallel()
|
|
clientset := getClientset(repoArgoCD)
|
|
settingsManager := settings.NewSettingsManager(t.Context(), clientset, testNamespace)
|
|
testee := &db{
|
|
ns: testNamespace,
|
|
kubeclientset: clientset,
|
|
settingsMgr: settingsManager,
|
|
}
|
|
|
|
repository, err := testee.GetWriteRepository(t.Context(), "[email protected]:argoproj/argo-cd.git", "")
|
|
require.NoError(t, err)
|
|
require.NotNil(t, repository)
|
|
assert.Empty(t, repository.Name)
|
|
}
|
|
|
|
func TestDb_ListRepositories(t *testing.T) {
|
|
t.Parallel()
|
|
clientset := getClientset(repoArgoCD, repoArgoProj)
|
|
settingsManager := settings.NewSettingsManager(t.Context(), clientset, testNamespace)
|
|
testee := &db{
|
|
ns: testNamespace,
|
|
kubeclientset: clientset,
|
|
settingsMgr: settingsManager,
|
|
}
|
|
|
|
repositories, err := testee.ListRepositories(t.Context())
|
|
require.NoError(t, err)
|
|
assert.Len(t, repositories, 2)
|
|
}
|
|
|
|
func TestDb_UpdateRepository(t *testing.T) {
|
|
t.Parallel()
|
|
secretRepository := &appsv1.Repository{
|
|
Name: "SomeRepo",
|
|
Repo: "[email protected]:argoproj/argo-cd.git",
|
|
Username: "someUsername",
|
|
Password: "somePassword",
|
|
Type: "git",
|
|
}
|
|
|
|
clientset := getClientset(repoArgoCD)
|
|
settingsManager := settings.NewSettingsManager(t.Context(), clientset, testNamespace)
|
|
testee := &db{
|
|
ns: testNamespace,
|
|
kubeclientset: clientset,
|
|
settingsMgr: settingsManager,
|
|
}
|
|
|
|
secretRepository.Username = "UpdatedUsername"
|
|
repository, err := testee.UpdateRepository(t.Context(), secretRepository)
|
|
require.NoError(t, err)
|
|
assert.NotNil(t, repository)
|
|
assert.Same(t, secretRepository, repository)
|
|
|
|
secret, err := clientset.CoreV1().Secrets(testNamespace).Get(
|
|
t.Context(),
|
|
"some-repo-secret",
|
|
metav1.GetOptions{},
|
|
)
|
|
require.NoError(t, err)
|
|
assert.NotNil(t, secret)
|
|
assert.Equal(t, "UpdatedUsername", string(secret.Data["username"]))
|
|
}
|
|
|
|
func TestDb_DeleteRepository(t *testing.T) {
|
|
t.Parallel()
|
|
clientset := getClientset(repoArgoCD, repoArgoProj)
|
|
settingsManager := settings.NewSettingsManager(t.Context(), clientset, testNamespace)
|
|
testee := &db{
|
|
ns: testNamespace,
|
|
kubeclientset: clientset,
|
|
settingsMgr: settingsManager,
|
|
}
|
|
|
|
err := testee.DeleteRepository(t.Context(), "[email protected]:argoproj/argoproj.git", "")
|
|
require.NoError(t, err)
|
|
|
|
err = testee.DeleteRepository(t.Context(), "[email protected]:argoproj/argo-cd.git", "")
|
|
require.NoError(t, err)
|
|
|
|
_, err = clientset.CoreV1().Secrets(testNamespace).Get(t.Context(), "some-repo-secret", metav1.GetOptions{})
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestDb_GetRepositoryCredentials(t *testing.T) {
|
|
t.Parallel()
|
|
gitHubRepoCredsSecret := &corev1.Secret{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Namespace: testNamespace,
|
|
Name: "some-repocreds-secret",
|
|
Labels: map[string]string{
|
|
common.LabelKeySecretType: common.LabelValueSecretTypeRepoCreds,
|
|
},
|
|
},
|
|
Data: map[string][]byte{
|
|
"type": []byte("git"),
|
|
"url": []byte("[email protected]:argoproj"),
|
|
"username": []byte("someUsername"),
|
|
"password": []byte("somePassword"),
|
|
},
|
|
}
|
|
gitLabRepoCredsSecret := &corev1.Secret{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Namespace: testNamespace,
|
|
Name: "some-other-repocreds-secret",
|
|
Labels: map[string]string{
|
|
common.LabelKeySecretType: common.LabelValueSecretTypeRepoCreds,
|
|
},
|
|
},
|
|
Data: map[string][]byte{
|
|
"type": []byte("git"),
|
|
"url": []byte("[email protected]"),
|
|
"username": []byte("someUsername"),
|
|
"password": []byte("somePassword"),
|
|
},
|
|
}
|
|
|
|
clientset := getClientset(gitHubRepoCredsSecret, gitLabRepoCredsSecret)
|
|
testee := NewDB(testNamespace, settings.NewSettingsManager(t.Context(), clientset, testNamespace), clientset)
|
|
|
|
repoCreds, err := testee.GetRepositoryCredentials(t.Context(), "[email protected]:argoproj/argoproj.git")
|
|
require.NoError(t, err)
|
|
require.NotNil(t, repoCreds)
|
|
assert.Equal(t, "[email protected]:argoproj", repoCreds.URL)
|
|
|
|
repoCreds, err = testee.GetRepositoryCredentials(t.Context(), "[email protected]:someorg/foobar.git")
|
|
require.NoError(t, err)
|
|
require.NotNil(t, repoCreds)
|
|
assert.Equal(t, "[email protected]", repoCreds.URL)
|
|
|
|
repoCreds, err = testee.GetRepositoryCredentials(t.Context(), "[email protected]:example/not-existing.git")
|
|
require.NoError(t, err)
|
|
assert.Nil(t, repoCreds)
|
|
}
|
|
|
|
func TestRepoURLToSecretName(t *testing.T) {
|
|
t.Parallel()
|
|
tables := []struct {
|
|
repoURL string
|
|
secretName string
|
|
project string
|
|
}{{
|
|
repoURL: "git://[email protected]:argoproj/ARGO-cd.git",
|
|
secretName: "repo-83273445",
|
|
project: "",
|
|
}, {
|
|
repoURL: "git://[email protected]:argoproj/ARGO-cd.git",
|
|
secretName: "repo-2733415816",
|
|
project: "foobar",
|
|
}, {
|
|
repoURL: "https://github.com/argoproj/ARGO-cd",
|
|
secretName: "repo-1890113693",
|
|
project: "",
|
|
}, {
|
|
repoURL: "https://github.com/argoproj/ARGO-cd",
|
|
secretName: "repo-4161185408",
|
|
project: "foobar",
|
|
}, {
|
|
repoURL: "https://github.com/hanzoai/cd",
|
|
secretName: "repo-42374749",
|
|
project: "",
|
|
}, {
|
|
repoURL: "https://github.com/hanzoai/cd",
|
|
secretName: "repo-1894545728",
|
|
project: "foobar",
|
|
}, {
|
|
repoURL: "https://github.com/hanzoai/cd.git",
|
|
secretName: "repo-821842295",
|
|
project: "",
|
|
}, {
|
|
repoURL: "https://github.com/hanzoai/cd.git",
|
|
secretName: "repo-1474166686",
|
|
project: "foobar",
|
|
}, {
|
|
repoURL: "https://github.com/argoproj/argo_cd.git",
|
|
secretName: "repo-1049844989",
|
|
project: "",
|
|
}, {
|
|
repoURL: "https://github.com/argoproj/argo_cd.git",
|
|
secretName: "repo-3916272608",
|
|
project: "foobar",
|
|
}, {
|
|
repoURL: "ssh://[email protected]/hanzoai/cd.git",
|
|
secretName: "repo-3569564120",
|
|
project: "",
|
|
}, {
|
|
repoURL: "ssh://[email protected]/hanzoai/cd.git",
|
|
secretName: "repo-754834421",
|
|
project: "foobar",
|
|
}}
|
|
|
|
for _, v := range tables {
|
|
sn := RepoURLToSecretName(repoSecretPrefix, v.repoURL, v.project)
|
|
assert.Equal(t, sn, v.secretName, "Expected secret name %q for repo %q; instead, got %q", v.secretName, v.repoURL, sn)
|
|
}
|
|
}
|
|
|
|
func Test_CredsURLToSecretName(t *testing.T) {
|
|
tables := map[string]string{
|
|
"git://[email protected]:argoproj": "creds-2483499391",
|
|
"git://[email protected]:argoproj/": "creds-1465032944",
|
|
"[email protected]:argoproj": "creds-2666065091",
|
|
"[email protected]:argoproj/": "creds-346879876",
|
|
}
|
|
|
|
for k, v := range tables {
|
|
sn := RepoURLToSecretName(credSecretPrefix, k, "")
|
|
assert.Equal(t, sn, v, "Expected secret name %q for repo %q; instead, got %q", v, k, sn)
|
|
}
|
|
}
|
|
|
|
func Test_GetProjectRepositories(t *testing.T) {
|
|
repoSecretWithProject := &corev1.Secret{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Namespace: testNamespace,
|
|
Name: "some-repo-secret",
|
|
Labels: map[string]string{
|
|
common.LabelKeySecretType: common.LabelValueSecretTypeRepository,
|
|
},
|
|
},
|
|
Data: map[string][]byte{
|
|
"type": []byte("git"),
|
|
"url": []byte("[email protected]:argoproj/argo-cd"),
|
|
"project": []byte("some-project"),
|
|
},
|
|
}
|
|
|
|
repoSecretWithoutProject := &corev1.Secret{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Namespace: testNamespace,
|
|
Name: "some-other-repo-secret",
|
|
Labels: map[string]string{
|
|
common.LabelKeySecretType: common.LabelValueSecretTypeRepository,
|
|
},
|
|
},
|
|
Data: map[string][]byte{
|
|
"type": []byte("git"),
|
|
"url": []byte("[email protected]:argoproj/argo-cd"),
|
|
},
|
|
}
|
|
|
|
clientset := getClientset(repoSecretWithProject, repoSecretWithoutProject)
|
|
argoDB := NewDB(testNamespace, settings.NewSettingsManager(t.Context(), clientset, testNamespace), clientset)
|
|
|
|
repos, err := argoDB.GetProjectRepositories("some-project")
|
|
require.NoError(t, err)
|
|
assert.Len(t, repos, 1)
|
|
assert.Equal(t, "[email protected]:argoproj/argo-cd", repos[0].Repo)
|
|
}
|