Skip to content

Commit

Permalink
Add test cases for path based registry login and logout
Browse files Browse the repository at this point in the history
The functions `SetCredentials` (for login) as well as
`RemoveAuthentication` (for logout) already feature support for path
based registries. This patch adds unit tests to ensure that the support
will not break in the future.

Signed-off-by: Sascha Grunert <[email protected]>
  • Loading branch information
saschagrunert committed Jul 1, 2021
1 parent bca1df8 commit 8fd6290
Showing 1 changed file with 136 additions and 0 deletions.
136 changes: 136 additions & 0 deletions pkg/docker/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -675,3 +676,138 @@ func TestAuthKeysForRef(t *testing.T) {
require.Equal(t, tc.expected, result, tc.name)
}
}

func TestSetCredentials(t *testing.T) {
for _, tc := range []struct {
registries []string
assert func(dockerConfigFile)
}{
{
registries: []string{"quay.io"},
assert: func(auth dockerConfigFile) {
require.Len(t, auth.AuthConfigs, 1)
require.NotEmpty(t, auth.AuthConfigs["quay.io"].Auth)
},
},
{
registries: []string{"quay.io/a/b/c/d/image"},
assert: func(auth dockerConfigFile) {
require.Len(t, auth.AuthConfigs, 1)
require.NotEmpty(t, auth.AuthConfigs["quay.io/a/b/c/d/image"].Auth)
},
},
{
registries: []string{
"quay.io/a/b/c",
"quay.io/a/b",
"quay.io/a",
"quay.io",
"my-registry.local",
"my-registry.local",
},
assert: func(auth dockerConfigFile) {
require.Len(t, auth.AuthConfigs, 5)
require.NotEmpty(t, auth.AuthConfigs["quay.io/a/b/c"].Auth)
require.NotEmpty(t, auth.AuthConfigs["quay.io/a/b"].Auth)
require.NotEmpty(t, auth.AuthConfigs["quay.io/a"].Auth)
require.NotEmpty(t, auth.AuthConfigs["quay.io"].Auth)
require.NotEmpty(t, auth.AuthConfigs["my-registry.local"].Auth)
},
},
} {
tmpFile, err := ioutil.TempFile("", "auth.json.set")
require.NoError(t, err)
_, err = tmpFile.WriteString("{}")
require.NoError(t, err)
defer os.RemoveAll(tmpFile.Name())
sys := &types.SystemContext{AuthFilePath: tmpFile.Name()}

for _, registry := range tc.registries {
_, err := SetCredentials(sys, registry, "username", "password")
require.NoError(t, err)
}

auth, err := readJSONFile(tmpFile.Name(), false)
require.NoError(t, err)

tc.assert(auth)
}
}

func TestRemoveAuthentication(t *testing.T) {
testAuth := dockerAuthConfig{Auth: "ZXhhbXBsZTpvcmc="}
for _, tc := range []struct {
config dockerConfigFile
registries []string
assert func(dockerConfigFile)
}{
{
config: dockerConfigFile{
AuthConfigs: map[string]dockerAuthConfig{
"quay.io": testAuth,
},
},
registries: []string{"quay.io"},
assert: func(auth dockerConfigFile) {
require.Len(t, auth.AuthConfigs, 0)
},
},
{
config: dockerConfigFile{
AuthConfigs: map[string]dockerAuthConfig{
"quay.io": testAuth,
"my-registry.local": testAuth,
},
},
registries: []string{"my-registry.local"},
assert: func(auth dockerConfigFile) {
require.Len(t, auth.AuthConfigs, 1)
require.NotEmpty(t, auth.AuthConfigs["quay.io"].Auth)
},
},
{
config: dockerConfigFile{
AuthConfigs: map[string]dockerAuthConfig{
"quay.io/a/b/c": testAuth,
"quay.io/a/b": testAuth,
"quay.io/a": testAuth,
"quay.io": testAuth,
"my-registry.local": testAuth,
},
},
registries: []string{
"quay.io/a/b",
"quay.io",
"my-registry.local",
},
assert: func(auth dockerConfigFile) {
require.Len(t, auth.AuthConfigs, 2)
require.NotEmpty(t, auth.AuthConfigs["quay.io/a/b/c"].Auth)
require.NotEmpty(t, auth.AuthConfigs["quay.io/a"].Auth)
},
},
} {

content, err := json.Marshal(&tc.config)
require.NoError(t, err)

tmpFile, err := ioutil.TempFile("", "auth.json")
require.NoError(t, err)
defer os.RemoveAll(tmpFile.Name())

_, err = tmpFile.Write(content)
require.NoError(t, err)

sys := &types.SystemContext{AuthFilePath: tmpFile.Name()}

for _, registry := range tc.registries {
err := RemoveAuthentication(sys, registry)
require.NoError(t, err)
}

auth, err := readJSONFile(tmpFile.Name(), false)
require.NoError(t, err)

tc.assert(auth)
}
}

0 comments on commit 8fd6290

Please sign in to comment.