From 07c3cc46c20410bce00e0c2a854fd1fd28b14f43 Mon Sep 17 00:00:00 2001 From: Soule BA Date: Tue, 12 Mar 2024 15:50:35 +0100 Subject: [PATCH] adding a test case for invamid chart and chartRef definition Signed-off-by: Soule BA --- .../helm.toolkit.fluxcd.io_helmreleases.yaml | 5 +- .../install-from-ocirepo-source/test.yaml | 13 +---- docs/api/v2beta2/helm.md | 2 +- .../controller/helmrelease_controller_test.go | 47 +++++++++++++++++++ 4 files changed, 53 insertions(+), 14 deletions(-) diff --git a/config/crd/bases/helm.toolkit.fluxcd.io_helmreleases.yaml b/config/crd/bases/helm.toolkit.fluxcd.io_helmreleases.yaml index afc1af172..60887bd37 100644 --- a/config/crd/bases/helm.toolkit.fluxcd.io_helmreleases.yaml +++ b/config/crd/bases/helm.toolkit.fluxcd.io_helmreleases.yaml @@ -1420,8 +1420,9 @@ spec: - spec type: object chartRef: - description: ChartRef holds a reference to a source controller resource - containing the Helm chart artifact. + description: |- + ChartRef holds a reference to a source controller resource containing the + Helm chart artifact. properties: apiVersion: description: APIVersion of the referent. diff --git a/config/testdata/install-from-ocirepo-source/test.yaml b/config/testdata/install-from-ocirepo-source/test.yaml index b2ada044e..f7f75dd80 100644 --- a/config/testdata/install-from-ocirepo-source/test.yaml +++ b/config/testdata/install-from-ocirepo-source/test.yaml @@ -4,7 +4,7 @@ kind: OCIRepository metadata: name: podinfo-ocirepo spec: - interval: 10m + interval: 30s url: oci://ghcr.io/stefanprodan/charts/podinfo ref: tag: 6.6.0 @@ -14,19 +14,10 @@ kind: HelmRelease metadata: name: podinfo-from-ocirepo spec: - releaseName: podinfo chartRef: kind: OCIRepository name: podinfo-ocirepo - interval: 50m - driftDetection: - mode: enabled - install: - timeout: 10m - remediation: - retries: 3 - upgrade: - timeout: 10m + interval: 30s values: resources: requests: diff --git a/docs/api/v2beta2/helm.md b/docs/api/v2beta2/helm.md index e59442f9a..ba76811c6 100644 --- a/docs/api/v2beta2/helm.md +++ b/docs/api/v2beta2/helm.md @@ -2383,7 +2383,7 @@ run by the controller.

Snapshots -([]*github.com/fluxcd/helm-controller/api/v2beta2.Snapshot alias)

+([]*./api/v2beta2.Snapshot alias)

(Appears on: HelmReleaseStatus) diff --git a/internal/controller/helmrelease_controller_test.go b/internal/controller/helmrelease_controller_test.go index 4f729599d..ea7d82269 100644 --- a/internal/controller/helmrelease_controller_test.go +++ b/internal/controller/helmrelease_controller_test.go @@ -19,6 +19,7 @@ package controller import ( "context" "errors" + "fmt" "strings" "testing" "time" @@ -32,6 +33,7 @@ import ( apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/types" "k8s.io/cli-runtime/pkg/genericclioptions" "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" @@ -889,6 +891,51 @@ func TestHelmReleaseReconciler_reconcileRelease(t *testing.T) { } func TestHelmReleaseReconciler_reconcileReleaseFromOCIRepositorySource(t *testing.T) { + + t.Run("handles chartRef and Chart definition failure", func(t *testing.T) { + g := NewWithT(t) + + obj := &v2.HelmRelease{ + ObjectMeta: metav1.ObjectMeta{ + Name: "release", + Namespace: "mock", + }, + Spec: v2.HelmReleaseSpec{ + ChartRef: &v2.CrossNamespaceSourceReference{ + Kind: "OCIRepository", + Name: "ocirepo", + Namespace: "mock", + }, + Chart: v2.HelmChartTemplate{ + Spec: v2.HelmChartTemplateSpec{ + Chart: "mychart", + SourceRef: v2.CrossNamespaceObjectReference{ + Name: "something", + }, + }, + }, + }, + } + + r := &HelmReleaseReconciler{ + Client: fake.NewClientBuilder(). + WithScheme(NewTestScheme()). + WithStatusSubresource(&v2.HelmRelease{}). + WithObjects(obj). + Build(), + } + + res, err := r.Reconcile(context.TODO(), reconcile.Request{ + NamespacedName: types.NamespacedName{ + Namespace: obj.GetNamespace(), + Name: obj.GetName(), + }, + }) + + // only chartRef or Chart must be set + g.Expect(errors.Is(err, reconcile.TerminalError(fmt.Errorf("invalid Chart reference")))).To(BeTrue()) + g.Expect(res.IsZero()).To(BeTrue()) + }) t.Run("handles ChartRef get failure", func(t *testing.T) { g := NewWithT(t)