forked from krystal/guvnor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker_test.go
90 lines (84 loc) · 1.91 KB
/
docker_test.go
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
package guvnor
import (
"testing"
"github.com/docker/docker/api/types"
"github.com/stretchr/testify/assert"
)
func Test_getIndexforImage(t *testing.T) {
tests := []struct {
name string
image string
want string
}{
{
name: "fully qualified",
image: "ghcr.io/strideynet/fizz:buzz",
want: "ghcr.io",
},
{
name: "short",
image: "ubuntu",
want: "docker.io",
},
}
for _, tt := range tests {
t.Run(t.Name(), func(t *testing.T) {
got, err := getIndexForImage(tt.image)
assert.NoError(t, err)
assert.Equal(t, tt.want, got)
})
}
}
func Test_extractAuthCfg(t *testing.T) {
tests := []struct {
name string
dockerCfg []byte
want *types.AuthConfig
wantErr string
}{
{
name: "blank docker config",
dockerCfg: []byte("{}"),
wantErr: "no auth configured for registry",
},
{
name: "no auth configured for registry",
wantErr: "no auth configured for registry",
dockerCfg: []byte(`{"auths":{}}`),
},
{
name: "user/password provided",
dockerCfg: []byte(`{"auths":{"test.com":{"username":"user","password":"pass"}}}`),
want: &types.AuthConfig{
ServerAddress: "test.com",
Username: "user",
Password: "pass",
},
},
{
name: "auth provided",
dockerCfg: []byte(`{"auths":{"test.com":{"auth":"dXNlcjpwYXNz"}}}`),
want: &types.AuthConfig{
ServerAddress: "test.com",
Username: "user",
Password: "pass",
},
},
{
name: "malformed auth provided",
dockerCfg: []byte(`{"auths":{"test.com":{"auth":"dXNlcjpwYXNzOmhlaA=="}}}`),
wantErr: "auth string malformed, expected 2 parts",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := extractAuthCfg(tt.dockerCfg, "test.com")
if tt.wantErr != "" {
assert.EqualError(t, err, tt.wantErr)
} else {
assert.NoError(t, err)
}
assert.Equal(t, tt.want, got)
})
}
}