Skip to content

Commit

Permalink
test: unit test for makeRegistryCredentialFromDockerConfig (#496)
Browse files Browse the repository at this point in the history
  • Loading branch information
smritidahal653 authored Mar 23, 2023
1 parent 25e72e1 commit c932764
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions pkg/provider/aci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit c932764

Please sign in to comment.