Skip to content

Commit cc23343

Browse files
committed
fix: clean up retained TrueNAS resources in E2E tests
The reclaim policy retain tests and adoption tests were leaving TrueNAS backend resources (datasets, NFS shares, NVMe-oF subsystems, iSCSI targets/extents) after each run because: - Reclaim retain tests only deleted the K8s PV but not the TrueNAS resources (Retain policy skips CSI DeleteVolume) - Adoption tests cleaned up the adopted volume but not the original retained ZVOL that was orphaned during the test Add explicit TrueNAS cleanup for all three protocols in the retain tests and delete the original retained ZVOL in both adoption tests.
1 parent 90935ce commit cc23343

File tree

3 files changed

+59
-6
lines changed

3 files changed

+59
-6
lines changed

tests/e2e/iscsi/adoption_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,13 @@ var _ = Describe("iSCSI Volume Adoption", func() {
214214
err = f.K8s.WaitForPodDeleted(ctx, adoptedPodName, deleteTimeout)
215215
Expect(err).NotTo(HaveOccurred())
216216

217-
// TrueNAS resources (iSCSI target, extent, ZVOL) are cleaned up
218-
// by the framework's PVC cleanup, which triggers CSI DeleteVolume.
219-
// The adopting StorageClass has no deleteStrategy=retain, so CSI performs full cleanup.
217+
// The adopted PVC cleanup (via RegisterPVCCleanup) triggers CSI DeleteVolume
218+
// which cleans up the NEW iSCSI target/extent and ZVOL. However, the ORIGINAL
219+
// retained ZVOL (zvolPath) is left behind because adoption creates a new
220+
// dataset path. Clean up the original retained ZVOL explicitly.
221+
By("Cleaning up original retained ZVOL from TrueNAS")
222+
err = f.TrueNAS.DeleteDataset(ctx, zvolPath)
223+
Expect(err).NotTo(HaveOccurred(), "Failed to delete original retained ZVOL")
220224
})
221225

222226
It("should mark a volume as adoptable when markAdoptable=true", func() {

tests/e2e/nvmeof/adoption_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,13 @@ var _ = Describe("NVMe-oF Volume Adoption", func() {
215215
err = f.K8s.WaitForPodDeleted(ctx, adoptedPodName, deleteTimeout)
216216
Expect(err).NotTo(HaveOccurred())
217217

218-
// TrueNAS resources (NVMe-oF subsystem, port bindings, ZVOL) are cleaned up
219-
// by the framework's PVC cleanup, which triggers CSI DeleteVolume.
220-
// The adopting StorageClass has no deleteStrategy=retain, so CSI performs full cleanup.
218+
// The adopted PVC cleanup (via RegisterPVCCleanup) triggers CSI DeleteVolume
219+
// which cleans up the NEW subsystem and namespace. However, the ORIGINAL
220+
// retained ZVOL (zvolPath) is left behind because adoption creates a new
221+
// dataset path. Clean up the original retained ZVOL explicitly.
222+
By("Cleaning up original retained ZVOL from TrueNAS")
223+
err = f.TrueNAS.DeleteDataset(ctx, zvolPath)
224+
Expect(err).NotTo(HaveOccurred(), "Failed to delete original retained ZVOL")
221225
})
222226

223227
It("should mark a volume as adoptable when markAdoptable=true", func() {

tests/e2e/reclaim_policy_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ package e2e
33

44
import (
55
"context"
6+
"path"
67
"time"
78

89
. "github.com/onsi/ginkgo/v2"
910
. "github.com/onsi/gomega"
1011
corev1 "k8s.io/api/core/v1"
12+
"k8s.io/klog/v2"
1113

1214
"github.com/fenio/tns-csi/tests/e2e/framework"
1315
)
@@ -222,9 +224,52 @@ var _ = Describe("Reclaim Policy", func() {
222224
GinkgoWriter.Printf("[%s] Retain reclaim policy verified - PV retained in %s state\n", proto.name, pv.Status.Phase)
223225
}
224226

227+
By("Getting volume handle before cleaning up retained PV")
228+
volumeHandle, handleErr := f.K8s.GetVolumeHandle(ctx, pvName)
229+
Expect(handleErr).NotTo(HaveOccurred(), "Failed to get volume handle")
230+
225231
By("Cleaning up retained PV")
226232
err = f.K8s.DeletePV(ctx, pvName)
227233
Expect(err).NotTo(HaveOccurred(), "Failed to delete retained PV")
234+
235+
By("Cleaning up retained TrueNAS resources")
236+
if f.TrueNAS != nil && volumeHandle != "" {
237+
cleanupRetainedTrueNASResources(ctx, f.TrueNAS, proto.id, volumeHandle)
238+
}
228239
})
229240
}
230241
})
242+
243+
// cleanupRetainedTrueNASResources removes TrueNAS backend resources that are left behind
244+
// when a volume uses Retain reclaim policy (K8s PV deletion does not trigger CSI DeleteVolume).
245+
func cleanupRetainedTrueNASResources(ctx context.Context, truenas *framework.TrueNASVerifier, protocol, volumeHandle string) {
246+
switch protocol {
247+
case "nfs":
248+
sharePath := "/mnt/" + volumeHandle
249+
if err := truenas.DeleteNFSShare(ctx, sharePath); err != nil {
250+
klog.Warningf("Failed to cleanup retained NFS share %s: %v", sharePath, err)
251+
}
252+
if err := truenas.DeleteDataset(ctx, volumeHandle); err != nil {
253+
klog.Warningf("Failed to cleanup retained NFS dataset %s: %v", volumeHandle, err)
254+
}
255+
case "nvmeof":
256+
subsystemNQN := "nqn.2137.csi.tns:" + path.Base(volumeHandle)
257+
if err := truenas.DeleteNVMeOFSubsystem(ctx, subsystemNQN); err != nil {
258+
klog.Warningf("Failed to cleanup retained NVMe-oF subsystem %s: %v", subsystemNQN, err)
259+
}
260+
if err := truenas.DeleteDataset(ctx, volumeHandle); err != nil {
261+
klog.Warningf("Failed to cleanup retained ZVOL %s: %v", volumeHandle, err)
262+
}
263+
case "iscsi":
264+
targetName := path.Base(volumeHandle)
265+
if err := truenas.DeleteISCSITarget(ctx, targetName); err != nil {
266+
klog.Warningf("Failed to cleanup retained iSCSI target %s: %v", targetName, err)
267+
}
268+
if err := truenas.DeleteISCSIExtent(ctx, targetName); err != nil {
269+
klog.Warningf("Failed to cleanup retained iSCSI extent %s: %v", targetName, err)
270+
}
271+
if err := truenas.DeleteDataset(ctx, volumeHandle); err != nil {
272+
klog.Warningf("Failed to cleanup retained ZVOL %s: %v", volumeHandle, err)
273+
}
274+
}
275+
}

0 commit comments

Comments
 (0)