summaryrefslogtreecommitdiff
path: root/registry/auth_test.go
blob: eca83931af6cd0a5c45fa4f11d9d3d7fadf87e19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package registry // import "github.com/docker/docker/registry"

import (
	"testing"

	"github.com/docker/docker/api/types/registry"
	"gotest.tools/v3/assert"
)

func buildAuthConfigs() map[string]registry.AuthConfig {
	authConfigs := map[string]registry.AuthConfig{}

	for _, reg := range []string{"testIndex", IndexServer} {
		authConfigs[reg] = registry.AuthConfig{
			Username: "docker-user",
			Password: "docker-pass",
		}
	}

	return authConfigs
}

func TestResolveAuthConfigIndexServer(t *testing.T) {
	authConfigs := buildAuthConfigs()
	indexConfig := authConfigs[IndexServer]

	officialIndex := &registry.IndexInfo{
		Official: true,
	}
	privateIndex := &registry.IndexInfo{
		Official: false,
	}

	resolved := ResolveAuthConfig(authConfigs, officialIndex)
	assert.Equal(t, resolved, indexConfig, "Expected ResolveAuthConfig to return IndexServer")

	resolved = ResolveAuthConfig(authConfigs, privateIndex)
	assert.Check(t, resolved != indexConfig, "Expected ResolveAuthConfig to not return IndexServer")
}

func TestResolveAuthConfigFullURL(t *testing.T) {
	authConfigs := buildAuthConfigs()

	registryAuth := registry.AuthConfig{
		Username: "foo-user",
		Password: "foo-pass",
	}
	localAuth := registry.AuthConfig{
		Username: "bar-user",
		Password: "bar-pass",
	}
	officialAuth := registry.AuthConfig{
		Username: "baz-user",
		Password: "baz-pass",
	}
	authConfigs[IndexServer] = officialAuth

	expectedAuths := map[string]registry.AuthConfig{
		"registry.example.com": registryAuth,
		"localhost:8000":       localAuth,
		"example.com":          localAuth,
	}

	validRegistries := map[string][]string{
		"registry.example.com": {
			"https://registry.example.com/v1/",
			"http://registry.example.com/v1/",
			"registry.example.com",
			"registry.example.com/v1/",
		},
		"localhost:8000": {
			"https://localhost:8000/v1/",
			"http://localhost:8000/v1/",
			"localhost:8000",
			"localhost:8000/v1/",
		},
		"example.com": {
			"https://example.com/v1/",
			"http://example.com/v1/",
			"example.com",
			"example.com/v1/",
		},
	}

	for configKey, registries := range validRegistries {
		configured, ok := expectedAuths[configKey]
		if !ok {
			t.Fail()
		}
		index := &registry.IndexInfo{
			Name: configKey,
		}
		for _, reg := range registries {
			authConfigs[reg] = configured
			resolved := ResolveAuthConfig(authConfigs, index)
			if resolved.Username != configured.Username || resolved.Password != configured.Password {
				t.Errorf("%s -> %v != %v\n", reg, resolved, configured)
			}
			delete(authConfigs, reg)
			resolved = ResolveAuthConfig(authConfigs, index)
			if resolved.Username == configured.Username || resolved.Password == configured.Password {
				t.Errorf("%s -> %v == %v\n", reg, resolved, configured)
			}
		}
	}
}