2015-04-01 18:39:37 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-06-04 13:29:57 -04:00
|
|
|
"runtime"
|
2015-04-01 18:39:37 -04:00
|
|
|
|
2016-11-02 20:43:32 -04:00
|
|
|
"github.com/docker/docker/api"
|
2015-11-09 13:32:46 -05:00
|
|
|
"github.com/docker/docker/dockerversion"
|
2016-12-30 12:23:00 -05:00
|
|
|
"github.com/docker/docker/integration-cli/checker"
|
2015-04-01 18:39:37 -04:00
|
|
|
"github.com/docker/docker/pkg/homedir"
|
2017-01-05 13:08:24 -05:00
|
|
|
icmd "github.com/docker/docker/pkg/testutil/cmd"
|
2015-04-18 12:46:47 -04:00
|
|
|
"github.com/go-check/check"
|
2015-04-01 18:39:37 -04:00
|
|
|
)
|
|
|
|
|
[nit] integration-cli: obey Go's naming convention
No substantial code change.
- Api --> API
- Cli --> CLI
- Http, Https --> HTTP, HTTPS
- Id --> ID
- Uid,Gid,Pid --> UID,PID,PID
- Ipam --> IPAM
- Tls --> TLS (TestDaemonNoTlsCliTlsVerifyWithEnv --> TestDaemonTLSVerifyIssue13964)
Didn't touch in this commit:
- Git: because it is officially "Git": https://git-scm.com/
- Tar: because it is officially "Tar": https://www.gnu.org/software/tar/
- Cpu, Nat, Mac, Ipc, Shm: for keeping a consistency with existing production code (not changable, for compatibility)
Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
2016-09-27 21:50:12 -04:00
|
|
|
func (s *DockerSuite) TestConfigHTTPHeader(c *check.C) {
|
2015-04-18 12:46:47 -04:00
|
|
|
testRequires(c, UnixCli) // Can't set/unset HOME on windows right now
|
2015-04-01 18:39:37 -04:00
|
|
|
// We either need a level of Go that supports Unsetenv (for cases
|
|
|
|
// when HOME/USERPROFILE isn't set), or we need to be able to use
|
|
|
|
// os/user but user.Current() only works if we aren't statically compiling
|
|
|
|
|
|
|
|
var headers map[string][]string
|
|
|
|
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
2016-11-02 20:43:32 -04:00
|
|
|
w.Header().Set("API-Version", api.DefaultVersion)
|
2015-04-01 18:39:37 -04:00
|
|
|
headers = r.Header
|
|
|
|
}))
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
homeKey := homedir.Key()
|
|
|
|
homeVal := homedir.Get()
|
2015-09-01 10:00:40 -04:00
|
|
|
tmpDir, err := ioutil.TempDir("", "fake-home")
|
2015-10-21 19:32:16 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-04-01 18:39:37 -04:00
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
|
|
|
dotDocker := filepath.Join(tmpDir, ".docker")
|
|
|
|
os.Mkdir(dotDocker, 0600)
|
|
|
|
tmpCfg := filepath.Join(dotDocker, "config.json")
|
|
|
|
|
|
|
|
defer func() { os.Setenv(homeKey, homeVal) }()
|
|
|
|
os.Setenv(homeKey, tmpDir)
|
|
|
|
|
|
|
|
data := `{
|
|
|
|
"HttpHeaders": { "MyHeader": "MyValue" }
|
|
|
|
}`
|
|
|
|
|
2015-09-01 10:00:40 -04:00
|
|
|
err = ioutil.WriteFile(tmpCfg, []byte(data), 0600)
|
2015-10-21 19:32:16 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-04-01 18:39:37 -04:00
|
|
|
|
2017-01-05 06:38:34 -05:00
|
|
|
result := icmd.RunCommand(dockerBinary, "-H="+server.URL[7:], "ps")
|
2017-01-05 13:08:24 -05:00
|
|
|
result.Assert(c, icmd.Expected{
|
|
|
|
ExitCode: 1,
|
|
|
|
Error: "exit status 1",
|
|
|
|
})
|
2015-04-01 18:39:37 -04:00
|
|
|
|
2015-10-21 19:32:16 -04:00
|
|
|
c.Assert(headers["User-Agent"], checker.NotNil, check.Commentf("Missing User-Agent"))
|
2015-06-04 13:29:57 -04:00
|
|
|
|
2017-01-05 06:38:34 -05:00
|
|
|
c.Assert(headers["User-Agent"][0], checker.Equals, "Docker-Client/"+dockerversion.Version+" ("+runtime.GOOS+")", check.Commentf("Badly formatted User-Agent,out:%v", result.Combined()))
|
2015-10-21 19:32:16 -04:00
|
|
|
|
|
|
|
c.Assert(headers["Myheader"], checker.NotNil)
|
2017-01-05 06:38:34 -05:00
|
|
|
c.Assert(headers["Myheader"][0], checker.Equals, "MyValue", check.Commentf("Missing/bad header,out:%v", result.Combined()))
|
2015-06-04 13:29:57 -04:00
|
|
|
|
2015-04-01 18:39:37 -04:00
|
|
|
}
|
2015-04-28 11:00:18 -04:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestConfigDir(c *check.C) {
|
2015-09-01 10:00:40 -04:00
|
|
|
cDir, err := ioutil.TempDir("", "fake-home")
|
2015-10-21 19:32:16 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-09-01 10:00:40 -04:00
|
|
|
defer os.RemoveAll(cDir)
|
2015-04-28 11:00:18 -04:00
|
|
|
|
|
|
|
// First make sure pointing to empty dir doesn't generate an error
|
2015-10-21 19:32:16 -04:00
|
|
|
dockerCmd(c, "--config", cDir, "ps")
|
2015-04-28 11:00:18 -04:00
|
|
|
|
|
|
|
// Test with env var too
|
2017-01-05 13:08:24 -05:00
|
|
|
icmd.RunCmd(icmd.Cmd{
|
2017-01-05 06:38:34 -05:00
|
|
|
Command: []string{dockerBinary, "ps"},
|
2017-01-05 13:08:24 -05:00
|
|
|
Env: appendBaseEnv(true, "DOCKER_CONFIG="+cDir),
|
2017-01-05 06:38:34 -05:00
|
|
|
}).Assert(c, icmd.Success)
|
2015-04-28 11:00:18 -04:00
|
|
|
|
|
|
|
// Start a server so we can check to see if the config file was
|
|
|
|
// loaded properly
|
|
|
|
var headers map[string][]string
|
|
|
|
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
headers = r.Header
|
|
|
|
}))
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
// Create a dummy config file in our new config dir
|
|
|
|
data := `{
|
|
|
|
"HttpHeaders": { "MyHeader": "MyValue" }
|
|
|
|
}`
|
|
|
|
|
|
|
|
tmpCfg := filepath.Join(cDir, "config.json")
|
|
|
|
err = ioutil.WriteFile(tmpCfg, []byte(data), 0600)
|
2015-10-21 19:32:16 -04:00
|
|
|
c.Assert(err, checker.IsNil, check.Commentf("Err creating file"))
|
2015-04-28 11:00:18 -04:00
|
|
|
|
2016-02-24 17:59:11 -05:00
|
|
|
env := appendBaseEnv(false)
|
|
|
|
|
2017-01-05 06:38:34 -05:00
|
|
|
icmd.RunCmd(icmd.Cmd{
|
2017-01-05 13:08:24 -05:00
|
|
|
Command: []string{dockerBinary, "--config", cDir, "-H=" + server.URL[7:], "ps"},
|
|
|
|
Env: env,
|
|
|
|
}).Assert(c, icmd.Expected{
|
|
|
|
ExitCode: 1,
|
|
|
|
Error: "exit status 1",
|
2017-01-05 06:38:34 -05:00
|
|
|
})
|
2015-10-21 19:32:16 -04:00
|
|
|
c.Assert(headers["Myheader"], checker.NotNil)
|
2017-01-05 13:08:24 -05:00
|
|
|
c.Assert(headers["Myheader"][0], checker.Equals, "MyValue", check.Commentf("ps3 - Missing header"))
|
2015-04-28 11:00:18 -04:00
|
|
|
|
|
|
|
// Reset headers and try again using env var this time
|
|
|
|
headers = map[string][]string{}
|
2017-01-05 06:38:34 -05:00
|
|
|
icmd.RunCmd(icmd.Cmd{
|
2017-01-05 13:08:24 -05:00
|
|
|
Command: []string{dockerBinary, "--config", cDir, "-H=" + server.URL[7:], "ps"},
|
|
|
|
Env: append(env, "DOCKER_CONFIG="+cDir),
|
|
|
|
}).Assert(c, icmd.Expected{
|
|
|
|
ExitCode: 1,
|
2017-01-05 06:38:34 -05:00
|
|
|
})
|
2015-10-21 19:32:16 -04:00
|
|
|
c.Assert(headers["Myheader"], checker.NotNil)
|
2017-01-05 13:08:24 -05:00
|
|
|
c.Assert(headers["Myheader"][0], checker.Equals, "MyValue", check.Commentf("ps4 - Missing header"))
|
2015-04-28 11:00:18 -04:00
|
|
|
|
2017-01-05 06:38:34 -05:00
|
|
|
// FIXME(vdemeester) should be a unit test
|
2015-04-28 11:00:18 -04:00
|
|
|
// Reset headers and make sure flag overrides the env var
|
|
|
|
headers = map[string][]string{}
|
2017-01-05 06:38:34 -05:00
|
|
|
icmd.RunCmd(icmd.Cmd{
|
2017-01-05 13:08:24 -05:00
|
|
|
Command: []string{dockerBinary, "--config", cDir, "-H=" + server.URL[7:], "ps"},
|
|
|
|
Env: append(env, "DOCKER_CONFIG=MissingDir"),
|
|
|
|
}).Assert(c, icmd.Expected{
|
|
|
|
ExitCode: 1,
|
2017-01-05 06:38:34 -05:00
|
|
|
})
|
2015-10-21 19:32:16 -04:00
|
|
|
c.Assert(headers["Myheader"], checker.NotNil)
|
2017-01-05 13:08:24 -05:00
|
|
|
c.Assert(headers["Myheader"][0], checker.Equals, "MyValue", check.Commentf("ps5 - Missing header"))
|
2015-04-28 11:00:18 -04:00
|
|
|
|
2017-01-05 06:38:34 -05:00
|
|
|
// FIXME(vdemeester) should be a unit test
|
2015-04-28 11:00:18 -04:00
|
|
|
// Reset headers and make sure flag overrides the env var.
|
|
|
|
// Almost same as previous but make sure the "MissingDir" isn't
|
|
|
|
// ignore - we don't want to default back to the env var.
|
|
|
|
headers = map[string][]string{}
|
2017-01-05 06:38:34 -05:00
|
|
|
icmd.RunCmd(icmd.Cmd{
|
2017-01-05 13:08:24 -05:00
|
|
|
Command: []string{dockerBinary, "--config", "MissingDir", "-H=" + server.URL[7:], "ps"},
|
|
|
|
Env: append(env, "DOCKER_CONFIG="+cDir),
|
|
|
|
}).Assert(c, icmd.Expected{
|
|
|
|
ExitCode: 1,
|
|
|
|
Error: "exit status 1",
|
2017-01-05 06:38:34 -05:00
|
|
|
})
|
2015-04-28 11:00:18 -04:00
|
|
|
|
2017-01-05 13:08:24 -05:00
|
|
|
c.Assert(headers["Myheader"], checker.IsNil, check.Commentf("ps6 - Headers shouldn't be the expected value"))
|
2015-04-28 11:00:18 -04:00
|
|
|
}
|