diff --git a/pkg/helm/chartmanager.go b/pkg/helm/chartmanager.go index a6d9a87926..ad7ec626d8 100644 --- a/pkg/helm/chartmanager.go +++ b/pkg/helm/chartmanager.go @@ -47,7 +47,6 @@ func NewChartManager(cfg *rest.Config, driver string) *ChartManager { // newActionConfig Create a new Helm action config from in-cluster service account func (h *ChartManager) newActionConfig(ctx context.Context, namespace string) (*action.Configuration, error) { - actionConfig := new(action.Configuration) logAdapter := func(format string, v ...interface{}) { log := logf.FromContext(ctx) logv2 := log.V(2) @@ -55,10 +54,10 @@ func (h *ChartManager) newActionConfig(ctx context.Context, namespace string) (* logv2.Info(fmt.Sprintf(format, v...)) } } - if err := actionConfig.Init(h.restClientGetter, namespace, h.driver, logAdapter); err != nil { - return nil, err - } - return actionConfig, nil + + actionConfig := new(action.Configuration) + err := actionConfig.Init(h.restClientGetter, namespace, h.driver, logAdapter) + return actionConfig, err } // UpgradeOrInstallChart upgrades a chart in cluster or installs it new if it does not already exist diff --git a/pkg/helm/chartmanager_test.go b/pkg/helm/chartmanager_test.go index 9bbcc3a31d..ff6ef6fb66 100644 --- a/pkg/helm/chartmanager_test.go +++ b/pkg/helm/chartmanager_test.go @@ -21,6 +21,7 @@ import ( "testing" "github.com/istio-ecosystem/sail-operator/pkg/test" + . "github.com/istio-ecosystem/sail-operator/pkg/test/util/ginkgo" . "github.com/onsi/gomega" "helm.sh/helm/v3/pkg/release" corev1 "k8s.io/api/core/v1" @@ -34,13 +35,11 @@ import ( var ctx = context.TODO() -func TestUpgradeOrInstallChart(t *testing.T) { - _, cl, cfg := test.SetupEnv(os.Stdout, false) +var ( + relName = "my-release" + chartDir = filepath.Join("testdata", "chart") - relName := "my-release" - chartDir := filepath.Join("testdata", "chart") - - owner := metav1.OwnerReference{ + owner = metav1.OwnerReference{ APIVersion: "v1", Kind: "Istio", Name: "my-istio", @@ -48,10 +47,11 @@ func TestUpgradeOrInstallChart(t *testing.T) { Controller: ptr.Of(true), } - tests := []struct { - name string - setup func(*WithT, client.Client, *ChartManager, string) - wantErr bool + tests = []struct { + name string + setup func(*WithT, client.Client, *ChartManager, string) + wantErrOnInstall bool + skipUninstallTest bool }{ { name: "release does not exist", @@ -98,7 +98,8 @@ func TestUpgradeOrInstallChart(t *testing.T) { install(g, helm, chartDir, ns, relName, owner) setReleaseStatus(g, helm, ns, relName, release.StatusUninstalled) }, - wantErr: true, + wantErrOnInstall: true, + skipUninstallTest: true, // If the release is marked as Uninstalled, helm doesn't uninstall it }, { name: "release in uninstalling state", @@ -106,7 +107,7 @@ func TestUpgradeOrInstallChart(t *testing.T) { install(g, helm, chartDir, ns, relName, owner) setReleaseStatus(g, helm, ns, relName, release.StatusUninstalling) }, - wantErr: true, + wantErrOnInstall: true, }, { name: "release in unknown state", @@ -114,7 +115,7 @@ func TestUpgradeOrInstallChart(t *testing.T) { install(g, helm, chartDir, ns, relName, owner) setReleaseStatus(g, helm, ns, relName, release.StatusUnknown) }, - wantErr: true, + wantErrOnInstall: true, }, { name: "release in superseded state", @@ -122,7 +123,7 @@ func TestUpgradeOrInstallChart(t *testing.T) { install(g, helm, chartDir, ns, relName, owner) setReleaseStatus(g, helm, ns, relName, release.StatusSuperseded) }, - wantErr: true, + wantErrOnInstall: true, }, { name: "release in pending-rollback state", @@ -130,24 +131,29 @@ func TestUpgradeOrInstallChart(t *testing.T) { install(g, helm, chartDir, ns, relName, owner) setReleaseStatus(g, helm, ns, relName, release.StatusPendingRollback) }, - wantErr: true, + wantErrOnInstall: true, }, } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { +) + +func TestUpgradeOrInstallChart(t *testing.T) { + _, cl, cfg := test.SetupEnv(os.Stdout, false) + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { g := NewWithT(t) helm := NewChartManager(cfg, "") ns := "test-" + rand.String(8) g.Expect(createNamespace(cl, ns)).To(Succeed()) - if tt.setup != nil { - tt.setup(g, cl, helm, ns) + if tc.setup != nil { + tc.setup(g, cl, helm, ns) } rel, err := helm.UpgradeOrInstallChart(ctx, chartDir, Values{"value": "my-value"}, ns, relName, owner) - if tt.wantErr { + if tc.wantErrOnInstall { g.Expect(err).To(HaveOccurred()) } else { g.Expect(err).ToNot(HaveOccurred()) @@ -163,6 +169,36 @@ func TestUpgradeOrInstallChart(t *testing.T) { } } +func TestUninstallChart(t *testing.T) { + _, cl, cfg := test.SetupEnv(os.Stdout, false) + + for _, tc := range tests { + if tc.skipUninstallTest { + continue + } + + t.Run(tc.name, func(t *testing.T) { + g := NewWithT(t) + helm := NewChartManager(cfg, "") + ns := "test-" + rand.String(8) + + g.Expect(createNamespace(cl, ns)).To(Succeed()) + + if tc.setup != nil { + tc.setup(g, cl, helm, ns) + } + + response, err := helm.UninstallChart(ctx, relName, ns) + + g.Expect(err).ToNot(HaveOccurred()) + g.Expect(response).ToNot(BeNil()) + + configMap := &corev1.ConfigMap{} + g.Expect(cl.Get(ctx, types.NamespacedName{Name: "test", Namespace: ns}, configMap)).To(ReturnNotFoundError()) + }) + } +} + func createNamespace(cl client.Client, ns string) error { return cl.Create(ctx, &corev1.Namespace{ ObjectMeta: metav1.ObjectMeta{