From 610b4a9604311f864132f79cb55abc52c21e808b Mon Sep 17 00:00:00 2001 From: May Zhang Date: Mon, 12 Apr 2021 09:15:43 -0700 Subject: [PATCH] fix: adding tests for helm OCI registry (#5978) * initial draft of adding tests for OCI Signed-off-by: May Zhang * fix: initial draft of adding tests for OCI Signed-off-by: May Zhang * bring up registry for tests Signed-off-by: May Zhang * bring up registry for tests Signed-off-by: May Zhang * bring up registry for tests Signed-off-by: May Zhang * fix: just to test if PR is working Signed-off-by: May Zhang * adding additional helm oci dependencies tests Signed-off-by: May Zhang --- Makefile | 1 + Procfile | 1 + test/container/Dockerfile | 6 +++ test/container/Procfile | 1 + test/e2e/fixture/app/context.go | 10 ++++ test/e2e/fixture/fixture.go | 4 ++ test/e2e/fixture/repos/repos.go | 25 +++++++++ test/e2e/helm_test.go | 53 +++++++++++++++++++ .../helm-oci-with-dependencies/Chart.yaml | 7 +++ test/fixture/testrepos/start-helm-registry.sh | 3 ++ util/helm/helm.go | 14 ++--- 11 files changed, 119 insertions(+), 6 deletions(-) create mode 100644 test/e2e/testdata/helm-oci-with-dependencies/Chart.yaml create mode 100755 test/fixture/testrepos/start-helm-registry.sh diff --git a/Makefile b/Makefile index b66b973055d07..39259447b0fe4 100644 --- a/Makefile +++ b/Makefile @@ -88,6 +88,7 @@ define run-in-test-server -w ${DOCKER_WORKDIR} \ -p ${ARGOCD_E2E_APISERVER_PORT}:8080 \ -p 4000:4000 \ + -p 5000:5000 \ $(TEST_TOOLS_PREFIX)$(TEST_TOOLS_IMAGE):$(TEST_TOOLS_TAG) \ bash -c "$(1)" endef diff --git a/Procfile b/Procfile index f4a5867c1a823..02714e7f3b4ec 100644 --- a/Procfile +++ b/Procfile @@ -5,4 +5,5 @@ redis: bash -c "if [ $ARGOCD_REDIS_LOCAL == 'true' ]; then redis-server --save ' repo-server: sh -c "FORCE_LOG_COLORS=1 ARGOCD_FAKE_IN_CLUSTER=true ARGOCD_GNUPGHOME=${ARGOCD_GNUPGHOME:-/tmp/argocd-local/gpg/keys} ARGOCD_GPG_DATA_PATH=${ARGOCD_GPG_DATA_PATH:-/tmp/argocd-local/gpg/source} ARGOCD_TLS_DATA_PATH=${ARGOCD_TLS_DATA_PATH:-/tmp/argocd-local/tls} ARGOCD_SSH_DATA_PATH=${ARGOCD_SSH_DATA_PATH:-/tmp/argocd-local/ssh} ARGOCD_BINARY_NAME=argocd-repo-server go run ./cmd/main.go --loglevel debug --port ${ARGOCD_E2E_REPOSERVER_PORT:-8081} --redis localhost:${ARGOCD_E2E_REDIS_PORT:-6379}" ui: sh -c 'cd ui && ${ARGOCD_E2E_YARN_CMD:-yarn} start' git-server: test/fixture/testrepos/start-git.sh +helm-registry: test/fixture/testrepos/start-helm-registry.sh dev-mounter: [[ "$ARGOCD_E2E_TEST" != "true" ]] && go run hack/dev-mounter/main.go --configmap argocd-ssh-known-hosts-cm=${ARGOCD_SSH_DATA_PATH:-/tmp/argocd-local/ssh} --configmap argocd-tls-certs-cm=${ARGOCD_TLS_DATA_PATH:-/tmp/argocd-local/tls} --configmap argocd-gpg-keys-cm=${ARGOCD_GPG_DATA_PATH:-/tmp/argocd-local/gpg/source} diff --git a/test/container/Dockerfile b/test/container/Dockerfile index 3253808a4e7d6..c1ad80e7c8515 100644 --- a/test/container/Dockerfile +++ b/test/container/Dockerfile @@ -4,6 +4,8 @@ FROM node:12.18.4 as node FROM golang:1.16.0 as golang +FROM registry:2.7.1 as registry + FROM ubuntu:20.10 ENV DEBIAN_FRONTEND=noninteractive @@ -63,6 +65,10 @@ COPY ./test/fixture/testrepos/ssh_host_*_key* /etc/ssh/ # Copy redis binaries to the image COPY --from=redis /usr/local/bin/* /usr/local/bin/ +# Copy registry binaries to the image +COPY --from=registry /bin/registry /usr/local/bin/ +COPY --from=registry /etc/docker/registry/config.yml /etc/docker/registry/config.yml + # Copy node binaries COPY --from=node /usr/local/lib/node_modules /usr/local/lib/node_modules COPY --from=node /usr/local/bin/node /usr/local/bin diff --git a/test/container/Procfile b/test/container/Procfile index 414e12196814d..ca35e147961fc 100644 --- a/test/container/Procfile +++ b/test/container/Procfile @@ -7,4 +7,5 @@ ui: sh -c "test $ARGOCD_IN_CI = true && exit 0; cd ui && ARGOCD_E2E_YARN_HOST=0. sshd: sudo sh -c "test $ARGOCD_E2E_TEST = true && /usr/sbin/sshd -p 2222 -D -e" fcgiwrap: sudo sh -c "test $ARGOCD_E2E_TEST = true && (fcgiwrap -s unix:/var/run/fcgiwrap.socket & sleep 1 && chmod 777 /var/run/fcgiwrap.socket && wait)" nginx: sudo sh -c "test $ARGOCD_E2E_TEST = true && nginx -g 'daemon off;' -c $(pwd)/test/fixture/testrepos/nginx.conf" +helm-registry: sudo sh -c "registry serve /etc/docker/registry/config.yml" dev-mounter: test "$ARGOCD_E2E_TEST" != "true" && go run hack/dev-mounter/main.go --configmap argocd-ssh-known-hosts-cm=${ARGOCD_SSH_DATA_PATH:-/tmp/argocd-local/ssh} --configmap argocd-tls-certs-cm=${ARGOCD_TLS_DATA_PATH:-/tmp/argocd-local/tls} --configmap argocd-gpg-keys-cm=${ARGOCD_GPG_DATA_PATH:-/tmp/argocd-local/gpg/source} diff --git a/test/e2e/fixture/app/context.go b/test/e2e/fixture/app/context.go index 2f005689234f3..f1771e514b63f 100644 --- a/test/e2e/fixture/app/context.go +++ b/test/e2e/fixture/app/context.go @@ -114,6 +114,16 @@ func (c *Context) HelmRepoAdded(name string) *Context { return c } +func (c *Context) HelmOCIRepoAdded(name string) *Context { + repos.AddHelmOCIRepo(name) + return c +} + +func (c *Context) PushChartToOCIRegistry(chartPathName, chartName, chartVersion string) *Context { + repos.PushChartToOCIRegistry(chartPathName, chartName, chartVersion) + return c +} + func (c *Context) HTTPSCredentialsUserPassAdded() *Context { repos.AddHTTPSCredentialsUserPass() return c diff --git a/test/e2e/fixture/fixture.go b/test/e2e/fixture/fixture.go index 242461371b284..863a33f132fdd 100644 --- a/test/e2e/fixture/fixture.go +++ b/test/e2e/fixture/fixture.go @@ -85,9 +85,11 @@ const ( RepoURLTypeSSHSubmodule = "ssh-sub" RepoURLTypeSSHSubmoduleParent = "ssh-par" RepoURLTypeHelm = "helm" + RepoURLTypeHelmOCI = "helm-oci" GitUsername = "admin" GitPassword = "password" GpgGoodKeyID = "D56C4FCA57A46444" + HelmOCIRegistryURL = "localhost:5000/myrepo" ) // TestNamespace returns the namespace where Argo CD E2E test instance will be @@ -243,6 +245,8 @@ func RepoURL(urlType RepoURLType) string { // Default - file based Git repository case RepoURLTypeHelm: return GetEnvWithDefault(EnvRepoURLTypeHelm, "https://localhost:9444/argo-e2e/testdata.git/helm-repo/local") + case RepoURLTypeHelmOCI: + return HelmOCIRegistryURL default: return GetEnvWithDefault(EnvRepoURLDefault, fmt.Sprintf("file://%s", repoDirectory())) } diff --git a/test/e2e/fixture/repos/repos.go b/test/e2e/fixture/repos/repos.go index 5fa169e7d8d5c..0520595db4414 100644 --- a/test/e2e/fixture/repos/repos.go +++ b/test/e2e/fixture/repos/repos.go @@ -1,6 +1,8 @@ package repos import ( + "fmt" + "os" "path/filepath" "github.com/argoproj/argo-cd/v2/test/e2e/fixture" @@ -77,6 +79,18 @@ func AddHelmRepo(name string) { errors.FailOnErr(fixture.RunCli(args...)) } +func AddHelmOCIRepo(name string) { + args := []string{ + "repo", + "add", + fixture.HelmOCIRegistryURL, + "--type", "helm", + "--name", name, + "--enable-oci", + } + errors.FailOnErr(fixture.RunCli(args...)) +} + // AddHTTPSRepoCredentialsUserPass adds E2E username/password credentials for HTTPS repos to context func AddHTTPSCredentialsUserPass() { var repoURLType fixture.RepoURLType = fixture.RepoURLTypeHTTPS @@ -110,3 +124,14 @@ func AddSSHCredentials() { args := []string{"repocreds", "add", fixture.RepoBaseURL(repoURLType), "--ssh-private-key-path", keyPath} errors.FailOnErr(fixture.RunCli(args...)) } + +// PushChartToOCIRegistry adds a helm chart to helm OCI registry +func PushChartToOCIRegistry(chartPathName, chartName, chartVersion string) { + chartAbsPath, err := filepath.Abs(fmt.Sprintf("./testdata/%s", chartPathName)) + errors.CheckError(err) + + _ = os.Setenv("HELM_EXPERIMENTAL_OCI", "1") + errors.FailOnErr(fixture.Run("", "helm", "chart", "save", chartAbsPath, fmt.Sprintf("%s/%s:%s", fixture.HelmOCIRegistryURL, chartName, chartVersion))) + errors.FailOnErr(fixture.Run("", "helm", "chart", "push", fmt.Sprintf("%s/%s:%s", fixture.HelmOCIRegistryURL, chartName, chartVersion))) + +} diff --git a/test/e2e/helm_test.go b/test/e2e/helm_test.go index fafad78cd4365..18a51108b3560 100644 --- a/test/e2e/helm_test.go +++ b/test/e2e/helm_test.go @@ -446,3 +446,56 @@ func TestHelmRepoDiffLocal(t *testing.T) { assert.Empty(t, diffOutput) }) } + +func TestHelmOCIRegistry(t *testing.T) { + Given(t). + PushChartToOCIRegistry("helm-values", "helm-values", "1.0.0"). + HelmOCIRepoAdded("myrepo"). + RepoURLType(RepoURLTypeHelmOCI). + Chart("helm-values"). + Revision("1.0.0"). + When(). + Create(). + Then(). + When(). + Sync(). + Then(). + Expect(OperationPhaseIs(OperationSucceeded)). + Expect(HealthIs(health.HealthStatusHealthy)). + Expect(SyncStatusIs(SyncStatusCodeSynced)) +} + +func TestGitWithHelmOCIRegistryDependencies(t *testing.T) { + Given(t). + PushChartToOCIRegistry("helm-values", "helm-values", "1.0.0"). + HelmOCIRepoAdded("myrepo"). + Path("helm-oci-with-dependencies"). + When(). + Create(). + Then(). + When(). + Sync(). + Then(). + Expect(OperationPhaseIs(OperationSucceeded)). + Expect(HealthIs(health.HealthStatusHealthy)). + Expect(SyncStatusIs(SyncStatusCodeSynced)) +} + +func TestHelmOCIRegistryWithDependencies(t *testing.T) { + Given(t). + PushChartToOCIRegistry("helm-values", "helm-values", "1.0.0"). + PushChartToOCIRegistry("helm-oci-with-dependencies", "helm-oci-with-dependencies", "1.0.0"). + HelmOCIRepoAdded("myrepo"). + RepoURLType(RepoURLTypeHelmOCI). + Chart("helm-oci-with-dependencies"). + Revision("1.0.0"). + When(). + Create(). + Then(). + When(). + Sync(). + Then(). + Expect(OperationPhaseIs(OperationSucceeded)). + Expect(HealthIs(health.HealthStatusHealthy)). + Expect(SyncStatusIs(SyncStatusCodeSynced)) +} diff --git a/test/e2e/testdata/helm-oci-with-dependencies/Chart.yaml b/test/e2e/testdata/helm-oci-with-dependencies/Chart.yaml new file mode 100644 index 0000000000000..761c722de2d14 --- /dev/null +++ b/test/e2e/testdata/helm-oci-with-dependencies/Chart.yaml @@ -0,0 +1,7 @@ +apiVersion: v2 +name: helm-oci-with-dependencies +version: 1.0.0 +dependencies: + - name: helm-values + repository: "oci://localhost:5000/myrepo" + version: 1.0.0 diff --git a/test/fixture/testrepos/start-helm-registry.sh b/test/fixture/testrepos/start-helm-registry.sh new file mode 100755 index 0000000000000..d803b1349481e --- /dev/null +++ b/test/fixture/testrepos/start-helm-registry.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash +export HELM_EXPERIMENTAL_OCI=1 +docker run -p 5000:5000 --restart=always --name registry registry diff --git a/util/helm/helm.go b/util/helm/helm.go index 9ba25575b316a..8bd9c0584862d 100644 --- a/util/helm/helm.go +++ b/util/helm/helm.go @@ -76,14 +76,16 @@ func (h *helm) DependencyBuild() error { repo := h.repos[i] if repo.EnableOci { h.cmd.IsHelmOci = true - _, err := h.cmd.Login(repo.Repo, repo.Creds) + if repo.Creds.Username != "" && repo.Creds.Password != "" { + _, err := h.cmd.Login(repo.Repo, repo.Creds) - defer func() { - _, _ = h.cmd.Logout(repo.Repo, repo.Creds) - }() + defer func() { + _, _ = h.cmd.Logout(repo.Repo, repo.Creds) + }() - if err != nil { - return err + if err != nil { + return err + } } } else { _, err := h.cmd.RepoAdd(repo.Name, repo.Repo, repo.Creds)