From ca5478a458b1f671236d7e2a03333f6748dd02ba Mon Sep 17 00:00:00 2001 From: Nishant Dhungel Date: Mon, 27 Mar 2023 17:19:15 -0700 Subject: [PATCH 1/8] Added unit test for getting Secret Volume --- 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 d66259a8..a7182617 100644 --- a/pkg/provider/aci_volumes_test.go +++ b/pkg/provider/aci_volumes_test.go @@ -18,6 +18,7 @@ import ( "gotest.tools/assert" is "gotest.tools/assert/cmp" v1 "k8s.io/api/core/v1" + errors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -490,3 +491,115 @@ func TestCreatePodWithCSIVolume(t *testing.T) { }) } } + +func TestGetSecretVolume(t *testing.T) { + fakeVolumeSecret := "fake-volume-secret" + secretVolumeName := "SecretVolume" + secretName := "api-key" + + fakeSecret := v1.Secret{ + ObjectMeta: metav1.ObjectMeta{ + Name: fakeVolumeSecret, + Namespace: podNamespace, + }, + Data: map[string][]byte{ + azureFileStorageAccountName: []byte("azureFileStorageAccountName"), + azureFileStorageAccountKey: []byte("azureFileStorageAccountKey")}, + } + + setOptional := new(bool) + *setOptional = false + + fakePodVolumes := []v1.Volume{ + { + Name: emptyVolumeName, + VolumeSource: v1.VolumeSource{ + EmptyDir: &v1.EmptyDirVolumeSource{}, + }, + }, + { + Name: secretVolumeName, + VolumeSource: v1.VolumeSource{ + Secret: &v1.SecretVolumeSource{ + SecretName: secretName, + Items: []v1.KeyToPath{ + { + Key: "azureFileStorageAccountName", + Path: "azureFileStorageAccountName", + }, + }, + Optional: setOptional, + }, + }, + }, + } + + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + + aciMocks := createNewACIMock() + + cases := []struct { + description string + callSecretMocks func(secretMock *MockSecretLister) + expectedError error + }{ + { + description: "Secret is nil and returns error while Optional is set to true", + callSecretMocks: func(secretMock *MockSecretLister) { + for _, volume := range fakePodVolumes { + if volume.Name == secretVolumeName { + mockSecretNamespaceLister := NewMockSecretNamespaceLister(mockCtrl) + secretMock.EXPECT().Secrets(podNamespace).Return(mockSecretNamespaceLister) + mockSecretNamespaceLister.EXPECT().Get(volume.Secret.SecretName).Return(nil, errors.NewNotFound(v1.Resource("secret"), secretName)) + } + } + }, + expectedError: fmt.Errorf("secret %s is required by Pod %s and does not exist", secretName, podName), + }, + { + description: "Secret returns a valid value", + callSecretMocks: func(secretMock *MockSecretLister) { + for _, volume := range fakePodVolumes { + if volume.Name == secretVolumeName { + mockSecretNamespaceLister := NewMockSecretNamespaceLister(mockCtrl) + secretMock.EXPECT().Secrets(podNamespace).Return(mockSecretNamespaceLister) + mockSecretNamespaceLister.EXPECT().Get(volume.Secret.SecretName).Return(&fakeSecret, nil) + } + } + }, + expectedError: nil, + }, + } + + for _, tc := range cases { + t.Run(tc.description, func(t *testing.T) { + mockSecretLister := NewMockSecretLister(mockCtrl) + + pod := testsutil.CreatePodObj(podName, podNamespace) + tc.callSecretMocks(mockSecretLister) + + pod.Spec.Volumes = fakePodVolumes + + provider, err := createTestProvider(aciMocks, NewMockConfigMapLister(mockCtrl), + mockSecretLister, NewMockPodLister(mockCtrl)) + if err != nil { + t.Fatal("Unable to create test provider", err) + } + + volumes,err := provider.getVolumes(context.Background(), pod) + + if tc.expectedError == nil { + azureStorageAccountName := base64.StdEncoding.EncodeToString([]byte("azureFileStorageAccountName")) + azureStorageAccountKey := base64.StdEncoding.EncodeToString([]byte("azureFileStorageAccountKey")) + assert.NilError(t, tc.expectedError, err) + assert.DeepEqual(t, *volumes[1].Secret[azureFileStorageAccountName], azureStorageAccountName) + assert.DeepEqual(t, *volumes[1].Secret[azureFileStorageAccountKey], azureStorageAccountKey) + } else { + assert.Equal(t, tc.expectedError.Error(), err.Error()) + } + + }) + } + +} \ No newline at end of file From 6c768fd13fcbaf559a9bcf0e21edcf1605564704 Mon Sep 17 00:00:00 2001 From: Nishant Dhungel Date: Mon, 27 Mar 2023 18:27:33 -0700 Subject: [PATCH 2/8] Fixed typo on description --- pkg/provider/aci_volumes_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/provider/aci_volumes_test.go b/pkg/provider/aci_volumes_test.go index a7182617..3786e047 100644 --- a/pkg/provider/aci_volumes_test.go +++ b/pkg/provider/aci_volumes_test.go @@ -545,7 +545,7 @@ func TestGetSecretVolume(t *testing.T) { expectedError error }{ { - description: "Secret is nil and returns error while Optional is set to true", + description: "Secret is nil and returns error while Optional is set to false", callSecretMocks: func(secretMock *MockSecretLister) { for _, volume := range fakePodVolumes { if volume.Name == secretVolumeName { From 37be2f860826188fdd8e0a6f14827c20d96612c4 Mon Sep 17 00:00:00 2001 From: Nishant Dhungel Date: Tue, 28 Mar 2023 09:37:37 -0700 Subject: [PATCH 3/8] Changed method name reflecting what's tested --- pkg/provider/aci_volumes_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/provider/aci_volumes_test.go b/pkg/provider/aci_volumes_test.go index 3786e047..966bee3a 100644 --- a/pkg/provider/aci_volumes_test.go +++ b/pkg/provider/aci_volumes_test.go @@ -492,7 +492,7 @@ func TestCreatePodWithCSIVolume(t *testing.T) { } } -func TestGetSecretVolume(t *testing.T) { +func TestGetVolumesForSecretVolume(t *testing.T) { fakeVolumeSecret := "fake-volume-secret" secretVolumeName := "SecretVolume" secretName := "api-key" From 56b2bf08e7e7d23de4d7c7348c7857fa50335187 Mon Sep 17 00:00:00 2001 From: Nishant Dhungel Date: Tue, 28 Mar 2023 09:41:08 -0700 Subject: [PATCH 4/8] Added new line in the end --- pkg/provider/aci_volumes_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/provider/aci_volumes_test.go b/pkg/provider/aci_volumes_test.go index 966bee3a..98eb912a 100644 --- a/pkg/provider/aci_volumes_test.go +++ b/pkg/provider/aci_volumes_test.go @@ -602,4 +602,4 @@ func TestGetVolumesForSecretVolume(t *testing.T) { }) } -} \ No newline at end of file +} From bf22fcde07c14f3ccdc56bc9f1c7ffc343db7d36 Mon Sep 17 00:00:00 2001 From: Nishant Dhungel Date: Mon, 27 Mar 2023 17:19:15 -0700 Subject: [PATCH 5/8] Added unit test for getting Secret Volume --- pkg/provider/aci_volumes_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/provider/aci_volumes_test.go b/pkg/provider/aci_volumes_test.go index 98eb912a..10c80aec 100644 --- a/pkg/provider/aci_volumes_test.go +++ b/pkg/provider/aci_volumes_test.go @@ -492,7 +492,7 @@ func TestCreatePodWithCSIVolume(t *testing.T) { } } -func TestGetVolumesForSecretVolume(t *testing.T) { +func TestGetSecretVolume(t *testing.T) { fakeVolumeSecret := "fake-volume-secret" secretVolumeName := "SecretVolume" secretName := "api-key" @@ -545,7 +545,7 @@ func TestGetVolumesForSecretVolume(t *testing.T) { expectedError error }{ { - description: "Secret is nil and returns error while Optional is set to false", + description: "Secret is nil and returns error while Optional is set to true", callSecretMocks: func(secretMock *MockSecretLister) { for _, volume := range fakePodVolumes { if volume.Name == secretVolumeName { From e39c86296060ad2c246d7b80a2502263cfc51d49 Mon Sep 17 00:00:00 2001 From: Nishant Dhungel Date: Mon, 27 Mar 2023 18:27:33 -0700 Subject: [PATCH 6/8] Fixed typo on description --- pkg/provider/aci_volumes_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/provider/aci_volumes_test.go b/pkg/provider/aci_volumes_test.go index 10c80aec..0be876e7 100644 --- a/pkg/provider/aci_volumes_test.go +++ b/pkg/provider/aci_volumes_test.go @@ -545,7 +545,7 @@ func TestGetSecretVolume(t *testing.T) { expectedError error }{ { - description: "Secret is nil and returns error while Optional is set to true", + description: "Secret is nil and returns error while Optional is set to false", callSecretMocks: func(secretMock *MockSecretLister) { for _, volume := range fakePodVolumes { if volume.Name == secretVolumeName { From ec18a72ffaeea7407b4760191a474f8e21e54043 Mon Sep 17 00:00:00 2001 From: Nishant Dhungel Date: Tue, 28 Mar 2023 09:37:37 -0700 Subject: [PATCH 7/8] Changed method name reflecting what's tested --- pkg/provider/aci_volumes_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/provider/aci_volumes_test.go b/pkg/provider/aci_volumes_test.go index 0be876e7..98eb912a 100644 --- a/pkg/provider/aci_volumes_test.go +++ b/pkg/provider/aci_volumes_test.go @@ -492,7 +492,7 @@ func TestCreatePodWithCSIVolume(t *testing.T) { } } -func TestGetSecretVolume(t *testing.T) { +func TestGetVolumesForSecretVolume(t *testing.T) { fakeVolumeSecret := "fake-volume-secret" secretVolumeName := "SecretVolume" secretName := "api-key" From 475e534d89572daa535f790ab01231c988f39c4d Mon Sep 17 00:00:00 2001 From: Nishant Dhungel Date: Wed, 5 Apr 2023 11:56:08 -0700 Subject: [PATCH 8/8] Added unit test for decode function in auth.go --- pkg/auth/auth_test.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/pkg/auth/auth_test.go b/pkg/auth/auth_test.go index f565676a..4287e962 100644 --- a/pkg/auth/auth_test.go +++ b/pkg/auth/auth_test.go @@ -247,3 +247,34 @@ func TestSetAuthConfigWithEnvVariablesOnly(t *testing.T) { } assert.Check(t, azConfig.Authorizer != nil, "Authorizer should be nil") } + +func TestDecode(t *testing.T) { + testCases := []struct { + desc string + input []byte + expectedOutput string + }{ + { + desc: "Testing Decode for UTF16LittleIndian Encoding", + input: []byte("\xFF\xFE\x68\x00\x65\x00\x6C\x00\x6C\x00\x6F\x00"), + expectedOutput: string([]byte("\x68\x65\x6C\x6C\x6F")), + }, + { + desc: "Testing Decode for UTF16BigIndian Encoding", + input: []byte("\xFE\xFF\x00\x68\x00\x65\x00\x6C\x00\x6C\x00\x6F"), + expectedOutput: string([]byte("\x68\x65\x6C\x6C\x6F")), + }, + { + desc: "Testing Decode for Unknown Encoding", + input: []byte("hello"), + expectedOutput: string([]byte("hello")), + }, + } + + authentication := Authentication{} + + for _, tc := range testCases { + decodedValue, _ := authentication.decode(tc.input) + assert.Equal(t, tc.expectedOutput, string(decodedValue), tc.desc) + } +}