Skip to content

Commit

Permalink
Add tests for uninstall error
Browse files Browse the repository at this point in the history
Signed-off-by: Sunny <[email protected]>
  • Loading branch information
darkowlzz committed Sep 18, 2024
1 parent f65b95b commit ad0d38c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
64 changes: 64 additions & 0 deletions internal/controller/helmrelease_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2628,6 +2628,70 @@ func TestHelmReleaseReconciler_reconcileUninstall(t *testing.T) {
g.Expect(conditions.GetMessage(obj, meta.ReadyCondition)).To(ContainSubstring("no namespace provided"))
g.Expect(obj.GetConditions()).To(HaveLen(1))
})

t.Run("error due to failing delete hook", func(t *testing.T) {
g := NewWithT(t)

ns, err := testEnv.CreateNamespace(context.TODO(), "reconcile-uninstall")
g.Expect(err).ToNot(HaveOccurred())
t.Cleanup(func() {
_ = testEnv.Delete(context.TODO(), ns)
})

rls := testutil.BuildRelease(&helmrelease.MockReleaseOptions{
Name: "reconcile-uninstall",
Namespace: ns.Name,
Version: 1,
Chart: testutil.BuildChart(testutil.ChartWithFailingHook()),
Status: helmrelease.StatusDeployed,
}, testutil.ReleaseWithFailingHook())

obj := &v2.HelmRelease{
ObjectMeta: metav1.ObjectMeta{
Name: "reconcile-uninstall",
Namespace: ns.Name,
DeletionTimestamp: &metav1.Time{Time: time.Now()},
},
Spec: v2.HelmReleaseSpec{
Uninstall: &v2.Uninstall{
KeepHistory: true,
Timeout: &metav1.Duration{Duration: time.Millisecond},
},
},
Status: v2.HelmReleaseStatus{
StorageNamespace: ns.Name,
History: v2.Snapshots{
release.ObservedToSnapshot(release.ObserveRelease(rls)),
},
},
}

r := &HelmReleaseReconciler{
Client: testEnv.Client,
GetClusterConfig: GetTestClusterConfig,
EventRecorder: record.NewFakeRecorder(32),
}

// Store the Helm release mock in the test namespace.
getter, err := r.buildRESTClientGetter(context.TODO(), obj)
g.Expect(err).ToNot(HaveOccurred())

cfg, err := action.NewConfigFactory(getter, action.WithStorage(helmdriver.SecretsDriverName, obj.Status.StorageNamespace))
g.Expect(err).ToNot(HaveOccurred())

store := helmstorage.Init(cfg.Driver)
g.Expect(store.Create(rls)).To(Succeed())

err = r.reconcileUninstall(context.TODO(), getter, obj)
g.Expect(err).To(HaveOccurred())

// Verify status of Helm release has not been updated.
g.Expect(obj.Status.StorageNamespace).ToNot(BeEmpty())

// Verify Helm release has not been uninstalled.
_, err = store.History(rls.Name)
g.Expect(err).ToNot(HaveOccurred())
})
}

func TestHelmReleaseReconciler_checkDependencies(t *testing.T) {
Expand Down
8 changes: 8 additions & 0 deletions internal/reconcile/uninstall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ func TestUninstall_Reconcile(t *testing.T) {
status func(releases []*helmrelease.Release) v2.HelmReleaseStatus
// wantErr is the error that is expected to be returned.
wantErr error
// wantErrString is the error string that is expected to be in the
// returned error. This is used for scenarios that return
// untyped/unwrapped error that can't be asserted for their value.
wantErrString string
// expectedConditions are the conditions that are expected to be set on
// the HelmRelease after running rollback.
expectConditions []metav1.Condition
Expand Down Expand Up @@ -150,6 +154,7 @@ func TestUninstall_Reconcile(t *testing.T) {
}
},
expectFailures: 1,
wantErrString: "timed out waiting",
},
{
name: "uninstall failure without storage update",
Expand Down Expand Up @@ -244,6 +249,7 @@ func TestUninstall_Reconcile(t *testing.T) {
}
},
expectFailures: 1,
wantErrString: "Failed to purge the release",
},
{
name: "uninstall without current",
Expand Down Expand Up @@ -482,6 +488,8 @@ func TestUninstall_Reconcile(t *testing.T) {
})
if tt.wantErr != nil {
g.Expect(errors.Is(got, tt.wantErr)).To(BeTrue())
} else if tt.wantErrString != "" {
g.Expect(got.Error()).To(ContainSubstring(tt.wantErrString))
} else {
g.Expect(got).ToNot(HaveOccurred())
}
Expand Down

0 comments on commit ad0d38c

Please sign in to comment.