From 3dadd9ff3a015977a15bb597372f1da3046856c3 Mon Sep 17 00:00:00 2001 From: nidhunge <43793123+nidhunge@users.noreply.github.com> Date: Tue, 28 Mar 2023 15:18:04 -0700 Subject: [PATCH] test: Added Unit Test for GetVolumes case: ConfigMapsVolume (#501) --- pkg/provider/aci_volumes_test.go | 113 +++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) diff --git a/pkg/provider/aci_volumes_test.go b/pkg/provider/aci_volumes_test.go index 98eb912a..d1796131 100644 --- a/pkg/provider/aci_volumes_test.go +++ b/pkg/provider/aci_volumes_test.go @@ -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()) + } + + }) + } + +}