2016-12-25 14:31:52 -05:00
|
|
|
package configfile
|
|
|
|
|
|
|
|
import (
|
2017-01-31 00:15:51 -05:00
|
|
|
"fmt"
|
2016-12-25 14:31:52 -05:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2017-01-31 00:15:51 -05:00
|
|
|
"github.com/stretchr/testify/assert"
|
2017-06-22 12:03:58 -04:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-12-25 14:31:52 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestEncodeAuth(t *testing.T) {
|
|
|
|
newAuthConfig := &types.AuthConfig{Username: "ken", Password: "test"}
|
|
|
|
authStr := encodeAuth(newAuthConfig)
|
2017-06-22 12:03:58 -04:00
|
|
|
|
|
|
|
expected := &types.AuthConfig{}
|
2016-12-25 14:31:52 -05:00
|
|
|
var err error
|
2017-06-22 12:03:58 -04:00
|
|
|
expected.Username, expected.Password, err = decodeAuth(authStr)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, expected, newAuthConfig)
|
2016-12-25 14:31:52 -05:00
|
|
|
}
|
2017-01-31 00:15:51 -05:00
|
|
|
|
|
|
|
func TestProxyConfig(t *testing.T) {
|
|
|
|
httpProxy := "http://proxy.mycorp.com:3128"
|
|
|
|
httpsProxy := "https://user:password@proxy.mycorp.com:3129"
|
|
|
|
ftpProxy := "http://ftpproxy.mycorp.com:21"
|
|
|
|
noProxy := "*.intra.mycorp.com"
|
|
|
|
defaultProxyConfig := ProxyConfig{
|
|
|
|
HTTPProxy: httpProxy,
|
|
|
|
HTTPSProxy: httpsProxy,
|
|
|
|
FTPProxy: ftpProxy,
|
|
|
|
NoProxy: noProxy,
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg := ConfigFile{
|
|
|
|
Proxies: map[string]ProxyConfig{
|
|
|
|
"default": defaultProxyConfig,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
proxyConfig := cfg.ParseProxyConfig("/var/run/docker.sock", []string{})
|
|
|
|
expected := map[string]*string{
|
|
|
|
"HTTP_PROXY": &httpProxy,
|
|
|
|
"http_proxy": &httpProxy,
|
|
|
|
"HTTPS_PROXY": &httpsProxy,
|
|
|
|
"https_proxy": &httpsProxy,
|
|
|
|
"FTP_PROXY": &ftpProxy,
|
|
|
|
"ftp_proxy": &ftpProxy,
|
|
|
|
"NO_PROXY": &noProxy,
|
|
|
|
"no_proxy": &noProxy,
|
|
|
|
}
|
|
|
|
assert.Equal(t, expected, proxyConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProxyConfigOverride(t *testing.T) {
|
|
|
|
httpProxy := "http://proxy.mycorp.com:3128"
|
|
|
|
overrideHTTPProxy := "http://proxy.example.com:3128"
|
|
|
|
overrideNoProxy := ""
|
|
|
|
httpsProxy := "https://user:password@proxy.mycorp.com:3129"
|
|
|
|
ftpProxy := "http://ftpproxy.mycorp.com:21"
|
|
|
|
noProxy := "*.intra.mycorp.com"
|
|
|
|
defaultProxyConfig := ProxyConfig{
|
|
|
|
HTTPProxy: httpProxy,
|
|
|
|
HTTPSProxy: httpsProxy,
|
|
|
|
FTPProxy: ftpProxy,
|
|
|
|
NoProxy: noProxy,
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg := ConfigFile{
|
|
|
|
Proxies: map[string]ProxyConfig{
|
|
|
|
"default": defaultProxyConfig,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ropts := []string{
|
|
|
|
fmt.Sprintf("HTTP_PROXY=%s", overrideHTTPProxy),
|
|
|
|
"NO_PROXY=",
|
|
|
|
}
|
|
|
|
proxyConfig := cfg.ParseProxyConfig("/var/run/docker.sock", ropts)
|
|
|
|
expected := map[string]*string{
|
|
|
|
"HTTP_PROXY": &overrideHTTPProxy,
|
|
|
|
"http_proxy": &httpProxy,
|
|
|
|
"HTTPS_PROXY": &httpsProxy,
|
|
|
|
"https_proxy": &httpsProxy,
|
|
|
|
"FTP_PROXY": &ftpProxy,
|
|
|
|
"ftp_proxy": &ftpProxy,
|
|
|
|
"NO_PROXY": &overrideNoProxy,
|
|
|
|
"no_proxy": &noProxy,
|
|
|
|
}
|
|
|
|
assert.Equal(t, expected, proxyConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProxyConfigPerHost(t *testing.T) {
|
|
|
|
httpProxy := "http://proxy.mycorp.com:3128"
|
|
|
|
httpsProxy := "https://user:password@proxy.mycorp.com:3129"
|
|
|
|
ftpProxy := "http://ftpproxy.mycorp.com:21"
|
|
|
|
noProxy := "*.intra.mycorp.com"
|
|
|
|
|
|
|
|
extHTTPProxy := "http://proxy.example.com:3128"
|
|
|
|
extHTTPSProxy := "https://user:password@proxy.example.com:3129"
|
|
|
|
extFTPProxy := "http://ftpproxy.example.com:21"
|
|
|
|
extNoProxy := "*.intra.example.com"
|
|
|
|
|
|
|
|
defaultProxyConfig := ProxyConfig{
|
|
|
|
HTTPProxy: httpProxy,
|
|
|
|
HTTPSProxy: httpsProxy,
|
|
|
|
FTPProxy: ftpProxy,
|
|
|
|
NoProxy: noProxy,
|
|
|
|
}
|
|
|
|
externalProxyConfig := ProxyConfig{
|
|
|
|
HTTPProxy: extHTTPProxy,
|
|
|
|
HTTPSProxy: extHTTPSProxy,
|
|
|
|
FTPProxy: extFTPProxy,
|
|
|
|
NoProxy: extNoProxy,
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg := ConfigFile{
|
|
|
|
Proxies: map[string]ProxyConfig{
|
|
|
|
"default": defaultProxyConfig,
|
|
|
|
"tcp://example.docker.com:2376": externalProxyConfig,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
proxyConfig := cfg.ParseProxyConfig("tcp://example.docker.com:2376", []string{})
|
|
|
|
expected := map[string]*string{
|
|
|
|
"HTTP_PROXY": &extHTTPProxy,
|
|
|
|
"http_proxy": &extHTTPProxy,
|
|
|
|
"HTTPS_PROXY": &extHTTPSProxy,
|
|
|
|
"https_proxy": &extHTTPSProxy,
|
|
|
|
"FTP_PROXY": &extFTPProxy,
|
|
|
|
"ftp_proxy": &extFTPProxy,
|
|
|
|
"NO_PROXY": &extNoProxy,
|
|
|
|
"no_proxy": &extNoProxy,
|
|
|
|
}
|
|
|
|
assert.Equal(t, expected, proxyConfig)
|
|
|
|
}
|
2017-06-12 14:36:49 -04:00
|
|
|
|
|
|
|
func TestConfigFile(t *testing.T) {
|
|
|
|
configFilename := "configFilename"
|
2017-06-27 10:31:38 -04:00
|
|
|
configFile := New(configFilename)
|
2017-06-12 14:36:49 -04:00
|
|
|
|
|
|
|
assert.Equal(t, configFilename, configFile.Filename)
|
|
|
|
}
|
2017-06-22 12:03:58 -04:00
|
|
|
|
|
|
|
func TestGetAllCredentials(t *testing.T) {
|
2017-06-27 10:31:38 -04:00
|
|
|
configFile := New("filename")
|
2017-06-22 12:03:58 -04:00
|
|
|
exampleAuth := types.AuthConfig{
|
|
|
|
Username: "user",
|
|
|
|
Password: "pass",
|
|
|
|
}
|
|
|
|
configFile.AuthConfigs["example.com/foo"] = exampleAuth
|
|
|
|
|
|
|
|
authConfigs, err := configFile.GetAllCredentials()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expected := make(map[string]types.AuthConfig)
|
|
|
|
expected["example.com/foo"] = exampleAuth
|
|
|
|
assert.Equal(t, expected, authConfigs)
|
|
|
|
}
|