Skip to content

Commit 6070280

Browse files
committed
Unit tests
1 parent 1fe9593 commit 6070280

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

pkg/common/unittestcommon/types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
cnstypes "github.com/vmware/govmomi/cns/types"
2525
"github.com/vmware/govmomi/object"
2626
"github.com/vmware/govmomi/vim25/types"
27+
v1 "k8s.io/api/core/v1"
2728
"sigs.k8s.io/vsphere-csi-driver/v3/pkg/apis/migration"
2829
cnsvolume "sigs.k8s.io/vsphere-csi-driver/v3/pkg/common/cns-lib/volume"
2930
"sigs.k8s.io/vsphere-csi-driver/v3/pkg/common/cns-lib/vsphere"
@@ -47,6 +48,10 @@ type FakeK8SOrchestrator struct {
4748
featureStates map[string]string
4849
// CSINodeTopology instances for topology testing
4950
csiNodeTopologyInstances []interface{}
51+
// PVCs for testing
52+
pvcs []*v1.PersistentVolumeClaim
53+
// RWMutex to synchronize access to 'pvcs' field from multiple callers
54+
pvcsLock *sync.RWMutex
5055
}
5156

5257
// volumeMigration holds mocked migrated volume information

pkg/common/unittestcommon/utils.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,8 +544,35 @@ func (c *FakeK8SOrchestrator) SetCSINodeTopologyInstances(instances []interface{
544544
}
545545

546546
func (c *FakeK8SOrchestrator) ListPVCs(ctx context.Context, namespace string) []*v1.PersistentVolumeClaim {
547-
//TODO implement me
548-
panic("implement me")
547+
if c.pvcsLock == nil {
548+
c.pvcsLock = &sync.RWMutex{}
549+
}
550+
c.pvcsLock.RLock()
551+
defer c.pvcsLock.RUnlock()
552+
553+
if namespace == "" {
554+
// Return all PVCs
555+
return c.pvcs
556+
}
557+
558+
// Filter by namespace
559+
var filteredPVCs []*v1.PersistentVolumeClaim
560+
for _, pvc := range c.pvcs {
561+
if pvc.Namespace == namespace {
562+
filteredPVCs = append(filteredPVCs, pvc)
563+
}
564+
}
565+
return filteredPVCs
566+
}
567+
568+
// SetPVCs sets the PVCs for testing
569+
func (c *FakeK8SOrchestrator) SetPVCs(pvcs []*v1.PersistentVolumeClaim) {
570+
if c.pvcsLock == nil {
571+
c.pvcsLock = &sync.RWMutex{}
572+
}
573+
c.pvcsLock.Lock()
574+
defer c.pvcsLock.Unlock()
575+
c.pvcs = pvcs
549576
}
550577

551578
// configFromVCSim starts a vcsim instance and returns config for use against the

pkg/syncer/cnsoperator/manager/cleanupcustomresources.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ func cleanUpCnsUnregisterVolumeInstances(ctx context.Context, restClientConfig *
115115
}
116116
}
117117

118+
var (
119+
newClientForGroup = k8s.NewClientForGroup
120+
newForConfig = func(config *rest.Config) (kubernetes.Interface, error) {
121+
return kubernetes.NewForConfig(config)
122+
}
123+
)
124+
118125
// cleanupPVCs removes the `CNSPvcFinalizer` from the PVCs in cases
119126
// where the CnsNodeVmBatchAttachment CR gets deleted before removing the finalizer.
120127
// This is EXTREMELY UNLIKELY to happen but still a possibility that has to be addressed.
@@ -142,7 +149,7 @@ func cleanupPVCs(ctx context.Context, config rest.Config) {
142149
pvcMap[pvc.Namespace][pvc.Name] = struct{}{}
143150
}
144151

145-
cnsClient, err := k8s.NewClientForGroup(ctx, &config, cnsoperatorv1alpha1.GroupName)
152+
cnsClient, err := newClientForGroup(ctx, &config, cnsoperatorv1alpha1.GroupName)
146153
if err != nil {
147154
log.Error("failed to create cns operator client")
148155
return
@@ -162,7 +169,7 @@ func cleanupPVCs(ctx context.Context, config rest.Config) {
162169
}
163170
}
164171

165-
c, err := kubernetes.NewForConfig(&config)
172+
c, err := newForConfig(&config)
166173
if err != nil {
167174
log.Error("failed to create core API client")
168175
return

0 commit comments

Comments
 (0)