8000 cli/config/configfile: various test cleanups · docker/cli@1e9575e · GitHub
[go: up one dir, main page]

Skip to content

Commit 1e9575e

Browse files
committed
cli/config/configfile: various test cleanups
- use var/const blocks when declaring a list of variables - use const where possible TestCheckKubernetesConfigurationRaiseAnErrorOnInvalidValue: - use keys when assigning values - make sure test is dereferenced in the loop - use subtests Signed-off-by: Sebastiaan van Stijn <github@gone.nl> (cherry picked from commit be327a4) Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent c98e9c4 commit 1e9575e

File tree

1 file changed

+98
-83
lines changed

1 file changed

+98
-83
lines changed

cli/config/configfile/file_test.go

Lines changed: 98 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,19 @@ func TestEncodeAuth(t *testing.T) {
2727
}
2828

2929
func TestProxyConfig(t *testing.T) {
30-
httpProxy := "http://proxy.mycorp.example.com:3128"
31-
httpsProxy := "https://user:password@proxy.mycorp.example.com:3129"
32-
ftpProxy := "http://ftpproxy.mycorp.example.com:21"
33-
noProxy := "*.intra.mycorp.example.com"
34-
defaultProxyConfig := ProxyConfig{
35-
HTTPProxy: httpProxy,
36-
HTTPSProxy: httpsProxy,
37-
FTPProxy: ftpProxy,
38-
NoProxy: noProxy,
39-
}
30+
var (
31+
httpProxy = "http://proxy.mycorp.example.com:3128"
32+
httpsProxy = "https://user:password@proxy.mycorp.example.com:3129"
33+
ftpProxy = "http://ftpproxy.mycorp.example.com:21"
34+
noProxy = "*.intra.mycorp.example.com"
35+
36+
defaultProxyConfig = ProxyConfig{
37+
HTTPProxy: httpProxy,
38+
HTTPSProxy: httpsProxy,
39+
FTPProxy: ftpProxy,
40+
NoProxy: noProxy,
41+
}
42+
)
4043

4144
cfg := ConfigFile{
4245
Proxies: map[string]ProxyConfig{
@@ -59,18 +62,21 @@ func TestProxyConfig(t *testing.T) {
5962
}
6063

6164
func TestProxyConfigOverride(t *testing.T) {
62-
httpProxy := "http://proxy.mycorp.example.com:3128"
63-
overrideHTTPProxy := "http://proxy.example.com:3128"
64-
overrideNoProxy := ""
65-
httpsProxy := "https://user:password@proxy.mycorp.example.com:3129"
66-
ftpProxy := "http://ftpproxy.mycorp.example.com:21"
67-
noProxy := "*.intra.mycorp.example.com"
68-
defaultProxyConfig := ProxyConfig{
69-
HTTPProxy: httpProxy,
70-
HTTPSProxy: httpsProxy,
71-
FTPProxy: ftpProxy,
72-
NoProxy: noProxy,
73-
}
65+
var (
66+
httpProxy = "http://proxy.mycorp.example.com:3128"
67+
httpProxyOverride = "http://proxy.example.com:3128"
68+
httpsProxy = "https://user:password@proxy.mycorp.example.com:3129"
69+
ftpProxy = "http://ftpproxy.mycorp.example.com:21"
70+
noProxy = "*.intra.mycorp.example.com"
71+
noProxyOverride = ""
72+
73+
defaultProxyConfig = ProxyConfig{
74+
HTTPProxy: httpProxy,
75+
HTTPSProxy: httpsProxy,
76+
FTPProxy: ftpProxy,
77+
NoProxy: noProxy,
78+
}
79+
)
7480

7581
cfg := ConfigFile{
7682
Proxies: map[string]ProxyConfig{
@@ -84,46 +90,49 @@ func TestProxyConfigOverride(t *testing.T) {
8490
}
8591

8692
ropts := map[string]*string{
87-
"HTTP_PROXY": clone(overrideHTTPProxy),
88-
"NO_PROXY": clone(overrideNoProxy),
93+
"HTTP_PROXY": clone(httpProxyOverride),
94+
"NO_PROXY": clone(noProxyOverride),
8995
}
9096
proxyConfig := cfg.ParseProxyConfig("/var/run/docker.sock", ropts)
9197
expected := map[string]*string{
92-
"HTTP_PROXY": &overrideHTTPProxy,
98+
"HTTP_PROXY": &httpProxyOverride,
9399
"http_proxy": &httpProxy,
94100
"HTTPS_PROXY": &httpsProxy,
95101
"https_proxy": &httpsProxy,
96102
"FTP_PROXY": &ftpProxy,
97103
"ftp_proxy": &ftpProxy,
98-
"NO_PROXY": &overrideNoProxy,
104+
"NO_PROXY": &noProxyOverride,
99105
"no_proxy": &noProxy,
100106
}
101107
assert.Check(t, is.DeepEqual(expected, proxyConfig))
102108
}
103109

104110
func TestProxyConfigPerHost(t *testing.T) {
105-
httpProxy := "http://proxy.mycorp.example.com:3128"
106-
httpsProxy := "https://user:password@proxy.mycorp.example.com:3129"
107-
ftpProxy := "http://ftpproxy.mycorp.example.com:21"
108-
noProxy := "*.intra.mycorp.example.com"
109-
110-
extHTTPProxy := "http://proxy.example.com:3128"
111-
extHTTPSProxy := "https://user:password@proxy.example.com:3129"
112-
extFTPProxy := "http://ftpproxy.example.com:21"
113-
extNoProxy := "*.intra.example.com"
114-
115-
defaultProxyConfig := ProxyConfig{
116-
HTTPProxy: httpProxy,
117-
HTTPSProxy: httpsProxy,
118-
FTPProxy: ftpProxy,
119-
NoProxy: noProxy,
120-
}
121-
externalProxyConfig := ProxyConfig{
122-
HTTPProxy: extHTTPProxy,
123-
HTTPSProxy: extHTTPSProxy,
124-
FTPProxy: extFTPProxy,
125-
NoProxy: extNoProxy,
126-
}
111+
var (
112+
httpProxy = "http://proxy.mycorp.example.com:3128"
113+
httpsProxy = "https://user:password@proxy.mycorp.example.com:3129"
114+
ftpProxy = "http://ftpproxy.mycorp.example.com:21"
115+
noProxy = "*.intra.mycorp.example.com"
116+
117+
extHTTPProxy = "http://proxy.example.com:3128"
118+
extHTTPSProxy = "https://user:password@proxy.example.com:3129"
119+
extFTPProxy = "http://ftpproxy.example.com:21"
120+
extNoProxy = "*.intra.example.com"
121+
122+
defaultProxyConfig = ProxyConfig{
123+
HTTPProxy: httpProxy,
124+
HTTPSProxy: httpsProxy,
125+
FTPProxy: ftpProxy,
126+
NoProxy: noProxy,
127+
}
128+
129+
externalProxyConfig = ProxyConfig{
130+
HTTPProxy: extHTTPProxy,
131+
HTTPSProxy: extHTTPSProxy,
132+
FTPProxy: extFTPProxy,
133+
NoProxy: extNoProxy,
134+
}
135+
)
127136

128137
cfg := ConfigFile{
129138
Proxies: map[string]ProxyConfig{
@@ -226,9 +235,11 @@ func TestGetAllCredentialsCredsStore(t *testing.T) {
226235
}
227236

228237
func TestGetAllCredentialsCredHelper(t *testing.T) {
229-
testCredHelperSuffix := "test_cred_helper"
230-
testCredHelperRegistryHostname := "credhelper.com"
231-
testExtraCredHelperRegistryHostname := "somethingweird.com"
238+
const (
239+
testCredHelperSuffix = "test_cred_helper"
240+
testCredHelperRegistryHostname = "credhelper.com"
241+
testExtraCredHelperRegistryHostname = "somethingweird.com"
242+
)
232243

233244
unexpectedCredHelperAuth := types.AuthConfig{
234245
Username: "file_store_user",
@@ -265,9 +276,11 @@ func TestGetAllCredentialsCredHelper(t *testing.T) {
265276
}
266277

267278
func TestGetAllCredentialsFileStoreAndCredHelper(t *testing.T) {
268-
testFileStoreRegistryHostname := "example.com"
269-
testCredHelperSuffix := "test_cred_helper"
270-
testCredHelperRegistryHostname := "credhelper.com"
279+
const (
280+
testFileStoreRegistryHostname = "example.com"
281+
testCredHelperSuffix = "test_cred_helper"
282+
testCredHelperRegistryHostname = "credhelper.com"
283+
)
271284

272285
expectedFileStoreAuth := types.AuthConfig{
273286
Username: "file_store_user",
@@ -301,10 +314,12 @@ func TestGetAllCredentialsFileStoreAndCredHelper(t *testing.T) {
301314
}
302315

303316
func TestGetAllCredentialsCredStoreAndCredHelper(t *testing.T) {
304-
testCredStoreSuffix := "test_creds_store"
305-
testCredStoreRegistryHostname := "credstore.com"
306-
testCredHelperSuffix := "test_cred_helper"
307-
testCredHelperRegistryHostname := "credhelper.com"
317+
const (
318+
testCredStoreSuffix = "test_creds_store"
319+
testCredStoreRegistryHostname = "credstore.com"
320+
testCredHelperSuffix = "test_cred_helper"
321+
testCredHelperRegistryHostname = "credhelper.com"
322+
)
308323

309324
configFile := New("filename")
310325
configFile.CredentialsStore = testCredStoreSuffix
@@ -343,9 +358,11 @@ func TestGetAllCredentialsCredStoreAndCredHelper(t *testing.T) {
343358
}
344359

345360
func TestGetAllCredentialsCredHelperOverridesDefaultStore(t *testing.T) {
346-
testCredStoreSuffix := "test_creds_store"
347-
testCredHelperSuffix := "test_cred_helper"
348-
testRegistryHostname := "example.com"
361+
const (
362+
testCredStoreSuffix = "test_creds_store"
363+
testCredHelperSuffix = "test_cred_helper"
364+
testRegistryHostname = "example.com"
365+
)
349366

350367
configFile := New("filename")
351368
configFile.CredentialsStore = testCredStoreSuffix
@@ -424,38 +441,36 @@ func TestCheckKubernetesConfigurationRaiseAnErrorOnInvalidValue(t *testing.T) {
424441
expectError bool
425442
}{
426443
{
427-
"no kubernetes config is valid",
428-
nil,
429-
false,
444+
name: "no kubernetes config is valid",
430445
},
431446
{
432-
"enabled is valid",
433-
&KubernetesConfig{AllNamespaces: "enabled"},
434-
false,
447+
name: "enabled is valid",
448+
config: &KubernetesConfig{AllNamespaces: "enabled"},
435449
},
436450
{
437-
"disabled is valid",
438-
&KubernetesConfig{AllNamespaces: "disabled"},
439-
false,
451+
name: "disabled is valid",
452+
config: &KubernetesConfig{AllNamespaces: "disabled"},
440453
},
441454
{
442-
"empty string is valid",
443-
&KubernetesConfig{AllNamespaces: ""},
444-
false,
455+
name: "empty string is valid",
456+
config: &KubernetesConfig{AllNamespaces: ""},
445457
},
446458
{
447-
"other value is invalid",
448-
&KubernetesConfig{AllNamespaces: "unknown"},
449-
true,
459+
name: "other value is invalid",
460+
config: &KubernetesConfig{AllNamespaces: "unknown"},
461+
expectError: true,
450462
},
451463
}
452-
for _, test := range testCases {
453-
err := checkKubernetesConfiguration(test.config)
454-
if test.expectError {
455-
assert.Assert(t, err != nil, test.name)
456-
} else {
457-
assert.NilError(t, err, test.name)
458-
}
464+
for _, tc := range testCases {
465+
test := tc
466+
t.Run(test.name, func(t *testing.T) {
467+
err := checkKubernetesConfiguration(test.config)
468+
if test.expectError {
469+
assert.Assert(t, err != nil, test.name)
470+
} else {
471+
assert.NilError(t, err, test.name)
472+
}
473+
})
459474
}
460475
}
461476

0 commit comments

Comments
 (0)
0