Browse code

integ-cli: also preserve SystemRoot env var

Windows CI fails to dial remote test host over tcp in the test cases where
we clear environment variables during `exec(dockerBinary, ...)` in the
absence of `SystemRoot` environment variable (typically points to `c:\windows`).

This fixes tests:
- `TestRunEnvironmentErase`
- `TestRunEnvironmentOverride`

Signed-off-by: Ahmet Alp Balkan <ahmetalpbalkan@gmail.com>

Ahmet Alp Balkan authored on 2015/03/07 18:03:44
Showing 3 changed files
... ...
@@ -9,7 +9,7 @@ import (
9 9
 
10 10
 func TestCliProxyDisableProxyUnixSock(t *testing.T) {
11 11
 	cmd := exec.Command(dockerBinary, "info")
12
-	cmd.Env = appendDockerHostEnv([]string{"HTTP_PROXY=http://127.0.0.1:9999"})
12
+	cmd.Env = appendBaseEnv([]string{"HTTP_PROXY=http://127.0.0.1:9999"})
13 13
 
14 14
 	if out, _, err := runCommandWithOutput(cmd); err != nil {
15 15
 		t.Fatal(err, out)
... ...
@@ -869,7 +869,7 @@ func TestRunEnvironmentErase(t *testing.T) {
869 869
 	defer deleteAllContainers()
870 870
 
871 871
 	cmd := exec.Command(dockerBinary, "run", "-e", "FOO", "-e", "HOSTNAME", "busybox", "env")
872
-	cmd.Env = appendDockerHostEnv([]string{})
872
+	cmd.Env = appendBaseEnv([]string{})
873 873
 
874 874
 	out, _, err := runCommandWithOutput(cmd)
875 875
 	if err != nil {
... ...
@@ -908,7 +908,7 @@ func TestRunEnvironmentOverride(t *testing.T) {
908 908
 	defer deleteAllContainers()
909 909
 
910 910
 	cmd := exec.Command(dockerBinary, "run", "-e", "HOSTNAME", "-e", "HOME=/root2", "busybox", "env")
911
-	cmd.Env = appendDockerHostEnv([]string{"HOSTNAME=bar"})
911
+	cmd.Env = appendBaseEnv([]string{"HOSTNAME=bar"})
912 912
 
913 913
 	out, _, err := runCommandWithOutput(cmd)
914 914
 	if err != nil {
... ...
@@ -919,12 +919,23 @@ func setupRegistry(t *testing.T) func() {
919 919
 	return func() { reg.Close() }
920 920
 }
921 921
 
922
-// appendDockerHostEnv adds given env slice DOCKER_HOST value if set in the
923
-// environment. Useful when environment is cleared but we want to preserve DOCKER_HOST
924
-// to execute tests against a remote daemon.
925
-func appendDockerHostEnv(env []string) []string {
926
-	if dockerHost := os.Getenv("DOCKER_HOST"); dockerHost != "" {
927
-		env = append(env, fmt.Sprintf("DOCKER_HOST=%s", dockerHost))
922
+// appendBaseEnv appends the minimum set of environment variables to exec the
923
+// docker cli binary for testing with correct configuration to the given env
924
+// list.
925
+func appendBaseEnv(env []string) []string {
926
+	preserveList := []string{
927
+		// preserve remote test host
928
+		"DOCKER_HOST",
929
+
930
+		// windows: requires preserving SystemRoot, otherwise dial tcp fails
931
+		// with "GetAddrInfoW: A non-recoverable error occurred during a database lookup."
932
+		"SystemRoot",
933
+	}
934
+
935
+	for _, key := range preserveList {
936
+		if val := os.Getenv(key); val != "" {
937
+			env = append(env, fmt.Sprintf("%s=%s", key, val))
938
+		}
928 939
 	}
929 940
 	return env
930 941
 }