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>
This commit is contained in:
Sebastiaan van Stijn 2021-04-30 09:43:13 +02:00
parent f3886f354a
commit be327a4f0f
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
1 changed files with 96 additions and 81 deletions

View File

@ -27,16 +27,19 @@ func TestEncodeAuth(t *testing.T) {
} }
func TestProxyConfig(t *testing.T) { func TestProxyConfig(t *testing.T) {
httpProxy := "http://proxy.mycorp.example.com:3128" var (
httpsProxy := "https://user:password@proxy.mycorp.example.com:3129" httpProxy = "http://proxy.mycorp.example.com:3128"
ftpProxy := "http://ftpproxy.mycorp.example.com:21" httpsProxy = "https://user:password@proxy.mycorp.example.com:3129"
noProxy := "*.intra.mycorp.example.com" ftpProxy = "http://ftpproxy.mycorp.example.com:21"
defaultProxyConfig := ProxyConfig{ noProxy = "*.intra.mycorp.example.com"
defaultProxyConfig = ProxyConfig{
HTTPProxy: httpProxy, HTTPProxy: httpProxy,
HTTPSProxy: httpsProxy, HTTPSProxy: httpsProxy,
FTPProxy: ftpProxy, FTPProxy: ftpProxy,
NoProxy: noProxy, NoProxy: noProxy,
} }
)
cfg := ConfigFile{ cfg := ConfigFile{
Proxies: map[string]ProxyConfig{ Proxies: map[string]ProxyConfig{
@ -59,18 +62,21 @@ func TestProxyConfig(t *testing.T) {
} }
func TestProxyConfigOverride(t *testing.T) { func TestProxyConfigOverride(t *testing.T) {
httpProxy := "http://proxy.mycorp.example.com:3128" var (
overrideHTTPProxy := "http://proxy.example.com:3128" httpProxy = "http://proxy.mycorp.example.com:3128"
overrideNoProxy := "" httpProxyOverride = "http://proxy.example.com:3128"
httpsProxy := "https://user:password@proxy.mycorp.example.com:3129" httpsProxy = "https://user:password@proxy.mycorp.example.com:3129"
ftpProxy := "http://ftpproxy.mycorp.example.com:21" ftpProxy = "http://ftpproxy.mycorp.example.com:21"
noProxy := "*.intra.mycorp.example.com" noProxy = "*.intra.mycorp.example.com"
defaultProxyConfig := ProxyConfig{ noProxyOverride = ""
defaultProxyConfig = ProxyConfig{
HTTPProxy: httpProxy, HTTPProxy: httpProxy,
HTTPSProxy: httpsProxy, HTTPSProxy: httpsProxy,
FTPProxy: ftpProxy, FTPProxy: ftpProxy,
NoProxy: noProxy, NoProxy: noProxy,
} }
)
cfg := ConfigFile{ cfg := ConfigFile{
Proxies: map[string]ProxyConfig{ Proxies: map[string]ProxyConfig{
@ -84,46 +90,49 @@ func TestProxyConfigOverride(t *testing.T) {
} }
ropts := map[string]*string{ ropts := map[string]*string{
"HTTP_PROXY": clone(overrideHTTPProxy), "HTTP_PROXY": clone(httpProxyOverride),
"NO_PROXY": clone(overrideNoProxy), "NO_PROXY": clone(noProxyOverride),
} }
proxyConfig := cfg.ParseProxyConfig("/var/run/docker.sock", ropts) proxyConfig := cfg.ParseProxyConfig("/var/run/docker.sock", ropts)
expected := map[string]*string{ expected := map[string]*string{
"HTTP_PROXY": &overrideHTTPProxy, "HTTP_PROXY": &httpProxyOverride,
"http_proxy": &httpProxy, "http_proxy": &httpProxy,
"HTTPS_PROXY": &httpsProxy, "HTTPS_PROXY": &httpsProxy,
"https_proxy": &httpsProxy, "https_proxy": &httpsProxy,
"FTP_PROXY": &ftpProxy, "FTP_PROXY": &ftpProxy,
"ftp_proxy": &ftpProxy, "ftp_proxy": &ftpProxy,
"NO_PROXY": &overrideNoProxy, "NO_PROXY": &noProxyOverride,
"no_proxy": &noProxy, "no_proxy": &noProxy,
} }
assert.Check(t, is.DeepEqual(expected, proxyConfig)) assert.Check(t, is.DeepEqual(expected, proxyConfig))
} }
func TestProxyConfigPerHost(t *testing.T) { func TestProxyConfigPerHost(t *testing.T) {
httpProxy := "http://proxy.mycorp.example.com:3128" var (
httpsProxy := "https://user:password@proxy.mycorp.example.com:3129" httpProxy = "http://proxy.mycorp.example.com:3128"
ftpProxy := "http://ftpproxy.mycorp.example.com:21" httpsProxy = "https://user:password@proxy.mycorp.example.com:3129"
noProxy := "*.intra.mycorp.example.com" ftpProxy = "http://ftpproxy.mycorp.example.com:21"
noProxy = "*.intra.mycorp.example.com"
extHTTPProxy := "http://proxy.example.com:3128" extHTTPProxy = "http://proxy.example.com:3128"
extHTTPSProxy := "https://user:password@proxy.example.com:3129" extHTTPSProxy = "https://user:password@proxy.example.com:3129"
extFTPProxy := "http://ftpproxy.example.com:21" extFTPProxy = "http://ftpproxy.example.com:21"
extNoProxy := "*.intra.example.com" extNoProxy = "*.intra.example.com"
defaultProxyConfig := ProxyConfig{ defaultProxyConfig = ProxyConfig{
HTTPProxy: httpProxy, HTTPProxy: httpProxy,
HTTPSProxy: httpsProxy, HTTPSProxy: httpsProxy,
FTPProxy: ftpProxy, FTPProxy: ftpProxy,
NoProxy: noProxy, NoProxy: noProxy,
} }
externalProxyConfig := ProxyConfig{
externalProxyConfig = ProxyConfig{
HTTPProxy: extHTTPProxy, HTTPProxy: extHTTPProxy,
HTTPSProxy: extHTTPSProxy, HTTPSProxy: extHTTPSProxy,
FTPProxy: extFTPProxy, FTPProxy: extFTPProxy,
NoProxy: extNoProxy, NoProxy: extNoProxy,
} }
)
cfg := ConfigFile{ cfg := ConfigFile{
Proxies: map[string]ProxyConfig{ Proxies: map[string]ProxyConfig{
@ -226,9 +235,11 @@ func TestGetAllCredentialsCredsStore(t *testing.T) {
} }
func TestGetAllCredentialsCredHelper(t *testing.T) { func TestGetAllCredentialsCredHelper(t *testing.T) {
testCredHelperSuffix := "test_cred_helper" const (
testCredHelperRegistryHostname := "credhelper.com" testCredHelperSuffix = "test_cred_helper"
testExtraCredHelperRegistryHostname := "somethingweird.com" testCredHelperRegistryHostname = "credhelper.com"
testExtraCredHelperRegistryHostname = "somethingweird.com"
)
unexpectedCredHelperAuth := types.AuthConfig{ unexpectedCredHelperAuth := types.AuthConfig{
Username: "file_store_user", Username: "file_store_user",
@ -265,9 +276,11 @@ func TestGetAllCredentialsCredHelper(t *testing.T) {
} }
func TestGetAllCredentialsFileStoreAndCredHelper(t *testing.T) { func TestGetAllCredentialsFileStoreAndCredHelper(t *testing.T) {
testFileStoreRegistryHostname := "example.com" const (
testCredHelperSuffix := "test_cred_helper" testFileStoreRegistryHostname = "example.com"
testCredHelperRegistryHostname := "credhelper.com" testCredHelperSuffix = "test_cred_helper"
testCredHelperRegistryHostname = "credhelper.com"
)
expectedFileStoreAuth := types.AuthConfig{ expectedFileStoreAuth := types.AuthConfig{
Username: "file_store_user", Username: "file_store_user",
@ -301,10 +314,12 @@ func TestGetAllCredentialsFileStoreAndCredHelper(t *testing.T) {
} }
func TestGetAllCredentialsCredStoreAndCredHelper(t *testing.T) { func TestGetAllCredentialsCredStoreAndCredHelper(t *testing.T) {
testCredStoreSuffix := "test_creds_store" const (
testCredStoreRegistryHostname := "credstore.com" testCredStoreSuffix = "test_creds_store"
testCredHelperSuffix := "test_cred_helper" testCredStoreRegistryHostname = "credstore.com"
testCredHelperRegistryHostname := "credhelper.com" testCredHelperSuffix = "test_cred_helper"
testCredHelperRegistryHostname = "credhelper.com"
)
configFile := New("filename") configFile := New("filename")
configFile.CredentialsStore = testCredStoreSuffix configFile.CredentialsStore = testCredStoreSuffix
@ -343,9 +358,11 @@ func TestGetAllCredentialsCredStoreAndCredHelper(t *testing.T) {
} }
func TestGetAllCredentialsCredHelperOverridesDefaultStore(t *testing.T) { func TestGetAllCredentialsCredHelperOverridesDefaultStore(t *testing.T) {
testCredStoreSuffix := "test_creds_store" const (
testCredHelperSuffix := "test_cred_helper" testCredStoreSuffix = "test_creds_store"
testRegistryHostname := "example.com" testCredHelperSuffix = "test_cred_helper"
testRegistryHostname = "example.com"
)
configFile := New("filename") configFile := New("filename")
configFile.CredentialsStore = testCredStoreSuffix configFile.CredentialsStore = testCredStoreSuffix
@ -424,38 +441,36 @@ func TestCheckKubernetesConfigurationRaiseAnErrorOnInvalidValue(t *testing.T) {
expectError bool expectError bool
}{ }{
{ {
"no kubernetes config is valid", name: "no kubernetes config is valid",
nil,
false,
}, },
{ {
"enabled is valid", name: "enabled is valid",
&KubernetesConfig{AllNamespaces: "enabled"}, config: &KubernetesConfig{AllNamespaces: "enabled"},
false,
}, },
{ {
"disabled is valid", name: "disabled is valid",
&KubernetesConfig{AllNamespaces: "disabled"}, config: &KubernetesConfig{AllNamespaces: "disabled"},
false,
}, },
{ {
"empty string is valid", name: "empty string is valid",
&KubernetesConfig{AllNamespaces: ""}, config: &KubernetesConfig{AllNamespaces: ""},
false,
}, },
{ {
"other value is invalid", name: "other value is invalid",
&KubernetesConfig{AllNamespaces: "unknown"}, config: &KubernetesConfig{AllNamespaces: "unknown"},
true, expectError: true,
}, },
} }
for _, test := range testCases { for _, tc := range testCases {
test := tc
t.Run(test.name, func(t *testing.T) {
err := checkKubernetesConfiguration(test.config) err := checkKubernetesConfiguration(test.config)
if test.expectError { if test.expectError {
assert.Assert(t, err != nil, test.name) assert.Assert(t, err != nil, test.name)
} else { } else {
assert.NilError(t, err, test.name) assert.NilError(t, err, test.name)
} }
})
} }
} }