github.com/damirazo/docker@v1.9.0/integration-cli/docker_cli_config_test.go (about) 1 package main 2 3 import ( 4 "io/ioutil" 5 "net/http" 6 "net/http/httptest" 7 "os" 8 "os/exec" 9 "path/filepath" 10 "runtime" 11 12 "github.com/docker/docker/autogen/dockerversion" 13 "github.com/docker/docker/pkg/homedir" 14 "github.com/go-check/check" 15 ) 16 17 func (s *DockerSuite) TestConfigHttpHeader(c *check.C) { 18 testRequires(c, UnixCli) // Can't set/unset HOME on windows right now 19 // We either need a level of Go that supports Unsetenv (for cases 20 // when HOME/USERPROFILE isn't set), or we need to be able to use 21 // os/user but user.Current() only works if we aren't statically compiling 22 23 var headers map[string][]string 24 25 server := httptest.NewServer(http.HandlerFunc( 26 func(w http.ResponseWriter, r *http.Request) { 27 headers = r.Header 28 })) 29 defer server.Close() 30 31 homeKey := homedir.Key() 32 homeVal := homedir.Get() 33 tmpDir, err := ioutil.TempDir("", "fake-home") 34 c.Assert(err, check.IsNil) 35 defer os.RemoveAll(tmpDir) 36 37 dotDocker := filepath.Join(tmpDir, ".docker") 38 os.Mkdir(dotDocker, 0600) 39 tmpCfg := filepath.Join(dotDocker, "config.json") 40 41 defer func() { os.Setenv(homeKey, homeVal) }() 42 os.Setenv(homeKey, tmpDir) 43 44 data := `{ 45 "HttpHeaders": { "MyHeader": "MyValue" } 46 }` 47 48 err = ioutil.WriteFile(tmpCfg, []byte(data), 0600) 49 if err != nil { 50 c.Fatalf("Err creating file(%s): %v", tmpCfg, err) 51 } 52 53 cmd := exec.Command(dockerBinary, "-H="+server.URL[7:], "ps") 54 out, _, _ := runCommandWithOutput(cmd) 55 56 if headers["User-Agent"] == nil { 57 c.Fatalf("Missing User-Agent: %q\nout:%v", headers, out) 58 } 59 60 if headers["User-Agent"][0] != "Docker-Client/"+dockerversion.VERSION+" ("+runtime.GOOS+")" { 61 c.Fatalf("Badly formatted User-Agent: %q\nout:%v", headers, out) 62 } 63 64 if headers["Myheader"] == nil || headers["Myheader"][0] != "MyValue" { 65 c.Fatalf("Missing/bad header: %q\nout:%v", headers, out) 66 } 67 } 68 69 func (s *DockerSuite) TestConfigDir(c *check.C) { 70 cDir, err := ioutil.TempDir("", "fake-home") 71 c.Assert(err, check.IsNil) 72 defer os.RemoveAll(cDir) 73 74 // First make sure pointing to empty dir doesn't generate an error 75 out, rc := dockerCmd(c, "--config", cDir, "ps") 76 77 if rc != 0 { 78 c.Fatalf("ps1 didn't work:\nrc:%d\nout%s", rc, out) 79 } 80 81 // Test with env var too 82 cmd := exec.Command(dockerBinary, "ps") 83 cmd.Env = append(os.Environ(), "DOCKER_CONFIG="+cDir) 84 out, rc, err = runCommandWithOutput(cmd) 85 86 if rc != 0 || err != nil { 87 c.Fatalf("ps2 didn't work:\nrc:%d\nout%s\nerr:%v", rc, out, err) 88 } 89 90 // Start a server so we can check to see if the config file was 91 // loaded properly 92 var headers map[string][]string 93 94 server := httptest.NewServer(http.HandlerFunc( 95 func(w http.ResponseWriter, r *http.Request) { 96 headers = r.Header 97 })) 98 defer server.Close() 99 100 // Create a dummy config file in our new config dir 101 data := `{ 102 "HttpHeaders": { "MyHeader": "MyValue" } 103 }` 104 105 tmpCfg := filepath.Join(cDir, "config.json") 106 err = ioutil.WriteFile(tmpCfg, []byte(data), 0600) 107 if err != nil { 108 c.Fatalf("Err creating file(%s): %v", tmpCfg, err) 109 } 110 111 cmd = exec.Command(dockerBinary, "--config", cDir, "-H="+server.URL[7:], "ps") 112 out, _, _ = runCommandWithOutput(cmd) 113 114 if headers["Myheader"] == nil || headers["Myheader"][0] != "MyValue" { 115 c.Fatalf("ps3 - Missing header: %q\nout:%v", headers, out) 116 } 117 118 // Reset headers and try again using env var this time 119 headers = map[string][]string{} 120 cmd = exec.Command(dockerBinary, "-H="+server.URL[7:], "ps") 121 cmd.Env = append(os.Environ(), "DOCKER_CONFIG="+cDir) 122 out, _, _ = runCommandWithOutput(cmd) 123 124 if headers["Myheader"] == nil || headers["Myheader"][0] != "MyValue" { 125 c.Fatalf("ps4 - Missing header: %q\nout:%v", headers, out) 126 } 127 128 // Reset headers and make sure flag overrides the env var 129 headers = map[string][]string{} 130 cmd = exec.Command(dockerBinary, "--config", cDir, "-H="+server.URL[7:], "ps") 131 cmd.Env = append(os.Environ(), "DOCKER_CONFIG=MissingDir") 132 out, _, _ = runCommandWithOutput(cmd) 133 134 if headers["Myheader"] == nil || headers["Myheader"][0] != "MyValue" { 135 c.Fatalf("ps5 - Missing header: %q\nout:%v", headers, out) 136 } 137 138 // Reset headers and make sure flag overrides the env var. 139 // Almost same as previous but make sure the "MissingDir" isn't 140 // ignore - we don't want to default back to the env var. 141 headers = map[string][]string{} 142 cmd = exec.Command(dockerBinary, "--config", "MissingDir", "-H="+server.URL[7:], "ps") 143 cmd.Env = append(os.Environ(), "DOCKER_CONFIG="+cDir) 144 out, _, _ = runCommandWithOutput(cmd) 145 146 if headers["Myheader"] != nil { 147 c.Fatalf("ps6 - Headers are there but shouldn't be: %q\nout:%v", headers, out) 148 } 149 150 }