Skip to content

Commit

Permalink
test: Added Unit Test for GetVolumes case: ConfigMapsVolume (#501)
Browse files Browse the repository at this point in the history
  • Loading branch information
nidhunge authored Mar 28, 2023
1 parent bc92cae commit 3dadd9f
Showing 1 changed file with 113 additions and 0 deletions.
113 changes: 113 additions & 0 deletions pkg/provider/aci_volumes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,3 +603,116 @@ func TestGetVolumesForSecretVolume(t *testing.T) {
}

}

func TestGetVolumesForConfigMapVolume(t *testing.T) {
configMapVolumeName := "ConfigMapVolume"
configMapName := "fake-root-ca.crt"

fakeConfigMap := v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: configMapName,
Namespace: podNamespace,
},
Data: map[string]string{
configMapName: "fake-ca-data",
"foo": "bar",
},
}

setOptional := new(bool)
*setOptional = false

fakePodVolumes := []v1.Volume{
{
Name: emptyVolumeName,
VolumeSource: v1.VolumeSource{
EmptyDir: &v1.EmptyDirVolumeSource{},
},
},
{
Name: configMapVolumeName,
VolumeSource: v1.VolumeSource{
ConfigMap: &v1.ConfigMapVolumeSource{
LocalObjectReference: v1.LocalObjectReference{
Name: configMapName,
},
Items: []v1.KeyToPath{
{
Key: configMapName,
Path: "ca.crt",
},
},
Optional: setOptional,
},
},
},
}

mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

aciMocks := createNewACIMock()

cases := []struct {
description string
callConfigMapMocks func(configMapMock *MockConfigMapLister)
expectedError error
}{
{
description: "ConfigMap is nil and returns error while Optional is set to false",
callConfigMapMocks: func(configMapMock *MockConfigMapLister) {
for _, volume := range fakePodVolumes {
if volume.Name == configMapVolumeName {
mockConfigMapNamespaceLister := NewMockConfigMapNamespaceLister(mockCtrl)
configMapMock.EXPECT().ConfigMaps(podNamespace).Return(mockConfigMapNamespaceLister)
mockConfigMapNamespaceLister.EXPECT().Get(volume.ConfigMap.Name).Return(nil, errors.NewNotFound(v1.Resource("ConfigMap"), configMapName))
}
}
},
expectedError: fmt.Errorf("ConfigMap %s is required by Pod %s and does not exist", configMapName, podName),
},
{
description: "Secret returns a valid value",
callConfigMapMocks: func(configMapMock *MockConfigMapLister) {
for _, volume := range fakePodVolumes {
if volume.Name == configMapVolumeName {
mockConfigMapNamespaceLister := NewMockConfigMapNamespaceLister(mockCtrl)
configMapMock.EXPECT().ConfigMaps(podNamespace).Return(mockConfigMapNamespaceLister)
mockConfigMapNamespaceLister.EXPECT().Get(volume.ConfigMap.Name).Return(&fakeConfigMap, nil)
}
}
},
expectedError: nil,
},
}

for _, tc := range cases {
t.Run(tc.description, func(t *testing.T) {
mockConfigMapLister := NewMockConfigMapLister(mockCtrl)

pod := testsutil.CreatePodObj(podName, podNamespace)
tc.callConfigMapMocks(mockConfigMapLister)

pod.Spec.Volumes = fakePodVolumes

provider, err := createTestProvider(aciMocks, mockConfigMapLister,
NewMockSecretLister(mockCtrl), NewMockPodLister(mockCtrl))
if err != nil {
t.Fatal("Unable to create test provider", err)
}

volumes,err := provider.getVolumes(context.Background(), pod)

if tc.expectedError == nil {
assert.NilError(t, tc.expectedError, err)

fakeCaConfigData := base64.StdEncoding.EncodeToString([]byte("fake-ca-data"))
assert.DeepEqual(t, *volumes[1].Secret[configMapName], fakeCaConfigData)
} else {
assert.Equal(t, tc.expectedError.Error(), err.Error())
}

})
}

}

0 comments on commit 3dadd9f

Please sign in to comment.