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