-
Notifications
You must be signed in to change notification settings - Fork 2
/
env_test.go
271 lines (248 loc) · 7.28 KB
/
env_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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package main
import (
"errors"
"testing"
)
func TestGetHostname(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "Full URL with scheme",
input: "https://example.com/path",
expected: "example.com",
},
{
name: "Full URL without scheme",
input: "example.com/path",
expected: "example.com",
},
{
name: "Simple domain with scheme",
input: "https://example.com",
expected: "example.com",
},
{
name: "Simple domain without scheme",
input: "example.com",
expected: "example.com",
},
{
name: "Full URL without scheme",
input: "example.com/path",
expected: "example.com",
},
{
name: "Full URL with scheme and hyphen",
input: "https://example-hyphen.com/path",
expected: "example-hyphen.com",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual, err := getHostname(tt.input)
if err != nil {
t.Error(err)
}
if actual != tt.expected {
t.Errorf("Get(%v) actual = (%v), expected (%v)", tt.input, actual, tt.expected)
}
})
}
}
func TestGetEnvVariables(t *testing.T) {
type args struct {
labels []string
offset int
}
type output struct {
envUsername string
envPassword string
}
tests := []struct {
name string
input args
expected output
}{
{
name: "Negative Offset",
input: args{labels: []string{"repo", "example", "com"}, offset: -1},
expected: output{envUsername: "DOCKER_repo_example_com_USR", envPassword: "DOCKER_repo_example_com_PSW"},
},
{
name: "Offset 0",
input: args{labels: []string{"repo", "example", "com"}, offset: 0},
expected: output{envUsername: "DOCKER_repo_example_com_USR", envPassword: "DOCKER_repo_example_com_PSW"},
},
{
name: "Offset 1",
input: args{labels: []string{"repo", "example", "com"}, offset: 1},
expected: output{envUsername: "DOCKER_example_com_USR", envPassword: "DOCKER_example_com_PSW"},
},
{
name: "Offset 2",
input: args{labels: []string{"repo", "example", "com"}, offset: 2},
expected: output{envUsername: "DOCKER_com_USR", envPassword: "DOCKER_com_PSW"},
},
{
name: "Fallback",
input: args{labels: []string{"repo", "example", "com"}, offset: 3},
expected: output{envUsername: "DOCKER__USR", envPassword: "DOCKER__PSW"},
},
{
name: "Overflow Offset",
input: args{labels: []string{"repo", "example", "com"}, offset: 4},
expected: output{envUsername: "DOCKER__USR", envPassword: "DOCKER__PSW"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actualEnvUsername, actualEnvPassword := getEnvVariables(tt.input.labels, tt.input.offset)
if actualEnvUsername != tt.expected.envUsername || actualEnvPassword != tt.expected.envPassword {
t.Errorf("Get(%v) actual = (%v, %v), expected (%v, %v)", tt.input, actualEnvUsername, actualEnvPassword, tt.expected.envUsername, tt.expected.envPassword)
}
})
}
}
func TestGetEnvCredentials(t *testing.T) {
type output struct {
username string
password string
found bool
}
tests := []struct {
name string
input string
expected output
}{
{
name: "Exact match",
input: "example.com",
expected: output{username: "u", password: "p", found: true},
},
{
name: "Subdomain",
input: "repo.example.com",
expected: output{username: "u", password: "p", found: true},
},
{
name: "Hyphen-domain",
input: "repo-example.com",
expected: output{username: "", password: "", found: false},
},
{
name: "Different domain",
input: "example.net",
expected: output{username: "", password: "", found: false},
},
}
t.Setenv("DOCKER_example_com_USR", "u")
t.Setenv("DOCKER_example_com_PSW", "p")
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actualUsername, actualPassword, actualFound := getEnvCredentials(tt.input)
if actualUsername != tt.expected.username || actualPassword != tt.expected.password || actualFound != tt.expected.found {
t.Errorf("getEnvCredentials(%v) actual = (%v, %v, %v), expected (%v, %v, %v)", tt.input, actualUsername, actualPassword, actualFound, tt.expected.username, tt.expected.password, tt.expected.found)
}
})
}
}
func TestEnvGet(t *testing.T) {
type output struct {
username string
password string
err error
}
tests := []struct {
name string
input string
expected output
}{
{
name: "Domain with creds",
input: "https://example.com",
expected: output{username: "u1", password: "p1", err: nil},
},
{
name: "Domain without creds",
input: "https://example.net",
expected: output{username: "", password: "", err: nil},
},
{
name: "Subdomain with creds",
input: "https://repo.example.com",
expected: output{username: "u2", password: "p2", err: nil},
},
{
name: "Subdomain without creds",
input: "https://other.example.com",
expected: output{username: "u1", password: "p1", err: nil},
},
{
name: "Hyphen-domain with creds",
input: "https://repo-example.com",
expected: output{username: "u2", password: "p2", err: nil},
},
{
name: "Hyphen-domain without creds",
input: "https://other-example.com",
expected: output{username: "", password: "", err: nil},
},
{
name: "GitHub Container Registry",
input: "https://ghcr.io",
expected: output{username: "", password: "t1", err: nil},
},
}
e := Env{}
t.Setenv("DOCKER_example_com_USR", "u1")
t.Setenv("DOCKER_example_com_PSW", "p1")
t.Setenv("DOCKER_repo_example_com_USR", "u2")
t.Setenv("DOCKER_repo_example_com_PSW", "p2")
t.Setenv("GITHUB_TOKEN", "t1")
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actualUsername, actualPassword, actualErr := e.Get(tt.input)
if actualUsername != tt.expected.username || actualPassword != tt.expected.password || actualErr != tt.expected.err {
t.Errorf("Get(%v) actual = (%v, %v, %v), expected (%v, %v, %v)", tt.input, actualUsername, actualPassword, actualErr, tt.expected.username, tt.expected.password, tt.expected.err)
}
})
}
}
func TestEnvNotSupportedMethods(t *testing.T) {
e := Env{}
t.Run("Add is not supported", func(t *testing.T) {
actualErr := e.Add(nil)
if !errors.Is(actualErr, &NotSupportedError{}) {
t.Errorf("Add() actual = (%v), expected (%v)", actualErr, &NotSupportedError{})
}
})
t.Run("Add is ignored with IGNORE_DOCKER_LOGIN", func(t *testing.T) {
t.Setenv(envIgnoreLogin, "1")
actualErr := e.Add(nil)
if actualErr != nil {
t.Errorf("Add() actual = (%v), expected (%v)", actualErr, nil)
}
})
t.Run("Delete is not supported", func(t *testing.T) {
actualErr := e.Delete("")
if !errors.Is(actualErr, &NotSupportedError{}) {
t.Errorf("Delete() actual = (%v), expected (%v)", actualErr, &NotSupportedError{})
}
})
t.Run("Delete is ignored with IGNORE_DOCKER_LOGIN", func(t *testing.T) {
t.Setenv(envIgnoreLogin, "1")
actualErr := e.Delete("")
if actualErr != nil {
t.Errorf("Delete() actual = (%v), expected (%v)", actualErr, nil)
}
})
t.Run("List is not supported", func(t *testing.T) {
_, actualErr := e.List()
if !errors.Is(actualErr, &NotSupportedError{}) {
t.Errorf("List() actual = (%v), expected (%v)", actualErr, &NotSupportedError{})
}
})
}