Skip to content

Commit

Permalink
OSSM-6127 Add tests for chartmanager.UninstallChart() (#1754)
Browse files Browse the repository at this point in the history
  • Loading branch information
luksa committed Mar 29, 2024
1 parent 3c00767 commit e60dc48
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 25 deletions.
9 changes: 4 additions & 5 deletions pkg/helm/chartmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,17 @@ 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)
if logv2.Enabled() {
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
Expand Down
76 changes: 56 additions & 20 deletions pkg/helm/chartmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -34,24 +35,23 @@ 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",
UID: "1234",
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",
Expand Down Expand Up @@ -98,56 +98,62 @@ 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",
setup: func(g *WithT, cl client.Client, helm *ChartManager, ns string) {
install(g, helm, chartDir, ns, relName, owner)
setReleaseStatus(g, helm, ns, relName, release.StatusUninstalling)
},
wantErr: true,
wantErrOnInstall: true,
},
{
name: "release in unknown state",
setup: func(g *WithT, cl client.Client, helm *ChartManager, ns string) {
install(g, helm, chartDir, ns, relName, owner)
setReleaseStatus(g, helm, ns, relName, release.StatusUnknown)
},
wantErr: true,
wantErrOnInstall: true,
},
{
name: "release in superseded state",
setup: func(g *WithT, cl client.Client, helm *ChartManager, ns string) {
install(g, helm, chartDir, ns, relName, owner)
setReleaseStatus(g, helm, ns, relName, release.StatusSuperseded)
},
wantErr: true,
wantErrOnInstall: true,
},
{
name: "release in pending-rollback state",
setup: func(g *WithT, cl client.Client, helm *ChartManager, ns string) {
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())
Expand All @@ -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{
Expand Down

0 comments on commit e60dc48

Please sign in to comment.