Skip to content

Commit

Permalink
adding a test case for invamid chart and chartRef definition
Browse files Browse the repository at this point in the history
Signed-off-by: Soule BA <[email protected]>
  • Loading branch information
souleb committed Mar 12, 2024
1 parent 0606cd2 commit 07c3cc4
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 14 deletions.
5 changes: 3 additions & 2 deletions config/crd/bases/helm.toolkit.fluxcd.io_helmreleases.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 2 additions & 11 deletions config/testdata/install-from-ocirepo-source/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion docs/api/v2beta2/helm.md
Original file line number Diff line number Diff line change
Expand Up @@ -2383,7 +2383,7 @@ run by the controller.</p>
</div>
</div>
<h3 id="helm.toolkit.fluxcd.io/v2beta2.Snapshots">Snapshots
(<code>[]*github.com/fluxcd/helm-controller/api/v2beta2.Snapshot</code> alias)</h3>
(<code>[]*./api/v2beta2.Snapshot</code> alias)</h3>
<p>
(<em>Appears on:</em>
<a href="#helm.toolkit.fluxcd.io/v2beta2.HelmReleaseStatus">HelmReleaseStatus</a>)
Expand Down
47 changes: 47 additions & 0 deletions internal/controller/helmrelease_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package controller
import (
"context"
"errors"
"fmt"
"strings"
"testing"
"time"
Expand All @@ -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"
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 07c3cc4

Please sign in to comment.