Skip to content

Commit

Permalink
PWX-38345: Put empty string check in volumeName while collecting the …
Browse files Browse the repository at this point in the history
…volumes for enumerate call in GetPodVolumes. (#1818)
  • Loading branch information
diptiranjanpx authored Jul 30, 2024
1 parent c81f52d commit db52d37
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 1 deletion.
2 changes: 1 addition & 1 deletion drivers/volume/portworx/portworx.go
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,7 @@ func (p *portworx) GetPodVolumes(podSpec *v1.PodSpec, namespace string, includeP
}
// If a volume is pending and WFFC, it doesn't exist in Portworx.
// No need of querying it.
if !isPendingWFFC {
if volumeName != "" && !isPendingWFFC {
volumeNameList = append(volumeNameList, volumeName)
}
}
Expand Down
139 changes: 139 additions & 0 deletions drivers/volume/portworx/portworx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,145 @@ func TestGetPodVolumesPendingPVCs(t *testing.T) {
require.Len(t, volumesWFFC, 0, "volumesWFFC should not be found")
}

func TestGetPodVolumesProjectedVolumes(t *testing.T) {
// Setup
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockDriver := mockosd.NewMockVolumeDriver(mockCtrl)
mockCache := mockcache.NewMockSharedInformerCache(mockCtrl)
p := setup(mockDriver, mockCache)

// Create a sample pod with volumes
pod := &v1.Pod{
Spec: v1.PodSpec{
Volumes: []v1.Volume{
{
Name: "volume1",
VolumeSource: v1.VolumeSource{
PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
ClaimName: "claim1",
},
},
},
{
Name: "projected",
VolumeSource: v1.VolumeSource{
Projected: &v1.ProjectedVolumeSource{
Sources: []v1.VolumeProjection{
{
ServiceAccountToken: &v1.ServiceAccountTokenProjection{
Audience: "aud1",
},
},
},
},
},
},
},
},
}

createPVAndPVC("claim1", "ns1", "pv1", v1.ClaimBound)

bindingMode := storagev1.VolumeBindingImmediate
mockCache.EXPECT().GetStorageClass(scName).Return(&storagev1.StorageClass{
ObjectMeta: metav1.ObjectMeta{
Name: scName,
},
VolumeBindingMode: &bindingMode,
}, nil).Times(1)

mockDriver.EXPECT().Enumerate(
&api.VolumeLocator{
VolumeIds: []string{"pv1"},
}, nil).Return([]*api.Volume{
{
Id: "id1",
Locator: &api.VolumeLocator{
Name: "pv1",
},
Spec: &api.VolumeSpec{},
},
}, nil)

// Call the GetPodVolumes function
volumes, volumesWFFC, err := p.GetPodVolumes(&pod.Spec, "ns1", true)
require.NoError(t, err, "failed to get pod volumes")
require.Len(t, volumes, 1, "incorrect volume count")
require.Len(t, volumesWFFC, 0, "volumesWFFC should not be found")
}

func TestGetPodVolumesEphemeralVolumes(t *testing.T) {
// Setup
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockDriver := mockosd.NewMockVolumeDriver(mockCtrl)
mockCache := mockcache.NewMockSharedInformerCache(mockCtrl)
p := setup(mockDriver, mockCache)

ephemeralScName := "sc1"
// Create a sample pod with volumes
pod := &v1.Pod{
Spec: v1.PodSpec{
Volumes: []v1.Volume{
{
Name: "volume1",
VolumeSource: v1.VolumeSource{
PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
ClaimName: "claim1",
},
},
},
{
Name: "ephemeral",
VolumeSource: v1.VolumeSource{
Ephemeral: &v1.EphemeralVolumeSource{
VolumeClaimTemplate: &v1.PersistentVolumeClaimTemplate{
ObjectMeta: metav1.ObjectMeta{
Name: "claim2",
},
Spec: v1.PersistentVolumeClaimSpec{
StorageClassName: &ephemeralScName,
},
},
},
},
},
},
},
}

createPVAndPVC("claim1", "ns1", "pv1", v1.ClaimBound)
createPVAndPVC("claim2", "ns1", "pv2", v1.ClaimBound)

bindingMode := storagev1.VolumeBindingImmediate
mockCache.EXPECT().GetStorageClass(scName).Return(&storagev1.StorageClass{
ObjectMeta: metav1.ObjectMeta{
Name: scName,
},
VolumeBindingMode: &bindingMode,
}, nil).Times(1)

mockDriver.EXPECT().Enumerate(
&api.VolumeLocator{
VolumeIds: []string{"pv1"},
}, nil).Return([]*api.Volume{
{
Id: "id1",
Locator: &api.VolumeLocator{
Name: "pv1",
},
Spec: &api.VolumeSpec{},
},
}, nil)

// Call the GetPodVolumes function
volumes, volumesWFFC, err := p.GetPodVolumes(&pod.Spec, "ns1", true)
require.NoError(t, err, "failed to get pod volumes")
require.Len(t, volumes, 1, "incorrect volume count")
require.Len(t, volumesWFFC, 0, "volumesWFFC should not be found")
}

func createPVAndPVC(name, namespace, pvName string, status v1.PersistentVolumeClaimPhase) {
sc := scName
core.Instance().CreatePersistentVolumeClaim(&v1.PersistentVolumeClaim{
Expand Down

0 comments on commit db52d37

Please sign in to comment.