From c9327646979453c1526cd7faec75ba8b167ce45b Mon Sep 17 00:00:00 2001 From: Smriti Dahal <93288516+smritidahal653@users.noreply.github.com> Date: Thu, 23 Mar 2023 13:49:56 -0700 Subject: [PATCH] test: unit test for makeRegistryCredentialFromDockerConfig (#496) --- pkg/provider/aci_test.go | 59 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/pkg/provider/aci_test.go b/pkg/provider/aci_test.go index 646550a9..3e094477 100644 --- a/pkg/provider/aci_test.go +++ b/pkg/provider/aci_test.go @@ -14,6 +14,7 @@ import ( "time" azaciv2 "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerinstance/armcontainerinstance/v2" + "github.com/cpuguy83/dockercfg" "github.com/golang/mock/gomock" "github.com/google/uuid" "github.com/virtual-kubelet/azure-aci/pkg/auth" @@ -113,6 +114,64 @@ func TestMakeRegistryCredential(t *testing.T) { } } +// Test make registry credential from docker config +func TestMakeRegistryCredentialFromDockerConfig(t *testing.T) { + server := "server-" + uuid.New().String() + username := "user-" + uuid.New().String() + password := "pass-" + uuid.New().String() + authConfig := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", username, password))) + + tt := []struct { + name string + authConfig dockercfg.AuthConfig + shouldFail bool + failMessage string + }{ + { + "Valid username and password", + dockercfg.AuthConfig{Username: username, Password: password}, + false, + "", + }, + { + "Username and password can be decoded from authConfig", + dockercfg.AuthConfig{Username: username, Auth: authConfig}, + false, + "", + }, + { + "No Username", + dockercfg.AuthConfig{}, + true, + "no username present in auth config for server", + }, + { + "Invalid Auth", + dockercfg.AuthConfig{Username: username, Auth: base64.StdEncoding.EncodeToString([]byte("123"))}, + true, + "error decoding docker auth", + }, + } + + for _, tc := range tt { + t.Run(tc.name, func(t *testing.T) { + cred, err := makeRegistryCredentialFromDockerConfig(server, tc.authConfig) + + if tc.shouldFail { + assert.Check(t, err != nil, "conversion should fail") + assert.Check(t, strings.Contains(err.Error(), tc.failMessage), "failed message is not expected") + return + } + + assert.Check(t, err, "conversion should not fail") + assert.Check(t, cred != nil, "credential should not be nil") + assert.Check(t, is.Equal(server, *cred.Server), "server doesn't match") + assert.Check(t, is.Equal(username, *cred.Username), "username doesn't match") + assert.Check(t, is.Equal(password, *cred.Password), "password doesn't match") + }) + } +} + // Tests create pod without resource spec func TestCreatePodWithoutResourceSpec(t *testing.T) { podName := "pod-" + uuid.New().String()