Browse code

Use TLS for tests if needed

Signed-off-by: Christopher Crone <christopher.crone@docker.com>

Christopher Crone authored on 2017/09/09 00:17:28
Showing 8 changed files
... ...
@@ -1372,8 +1372,7 @@ func (s *DockerSuite) TestContainerAPICreateNoHostConfig118(c *check.C) {
1372 1372
 		Image: "busybox",
1373 1373
 	}
1374 1374
 
1375
-	var httpClient *http.Client
1376
-	cli, err := client.NewClient(daemonHost(), "v1.18", httpClient, map[string]string{})
1375
+	cli, err := NewEnvClientWithVersion("v1.18")
1377 1376
 
1378 1377
 	_, err = cli.ContainerCreate(context.Background(), &config, &containertypes.HostConfig{}, &networktypes.NetworkingConfig{}, "")
1379 1378
 	c.Assert(err, checker.IsNil)
... ...
@@ -179,8 +179,7 @@ func (s *DockerSuite) TestAPIImagesSizeCompatibility(c *check.C) {
179 179
 		Labels      map[string]string
180 180
 	}
181 181
 
182
-	var httpClient *http.Client
183
-	cli, err = client.NewClient(daemonHost(), "v1.24", httpClient, nil)
182
+	cli, err = NewEnvClientWithVersion("v1.24")
184 183
 	c.Assert(err, checker.IsNil)
185 184
 	defer cli.Close()
186 185
 
... ...
@@ -4,9 +4,7 @@ package main
4 4
 
5 5
 import (
6 6
 	"encoding/json"
7
-	"net/http"
8 7
 
9
-	"github.com/docker/docker/client"
10 8
 	"github.com/docker/docker/integration-cli/checker"
11 9
 	"github.com/go-check/check"
12 10
 	"golang.org/x/net/context"
... ...
@@ -19,8 +17,7 @@ func (s *DockerSuite) TestInspectAPICpusetInConfigPre120(c *check.C) {
19 19
 
20 20
 	name := "cpusetinconfig-pre120"
21 21
 	dockerCmd(c, "run", "--name", name, "--cpuset-cpus", "0", "busybox", "true")
22
-	var httpClient *http.Client
23
-	cli, err := client.NewClient(daemonHost(), "v1.19", httpClient, nil)
22
+	cli, err := NewEnvClientWithVersion("v1.19")
24 23
 	c.Assert(err, checker.IsNil)
25 24
 	defer cli.Close()
26 25
 	_, body, err := cli.ContainerInspectWithRaw(context.Background(), name, false)
... ...
@@ -1,11 +1,9 @@
1 1
 package main
2 2
 
3 3
 import (
4
-	"net/http"
5 4
 	"strings"
6 5
 	"time"
7 6
 
8
-	"github.com/docker/docker/client"
9 7
 	"github.com/docker/docker/integration-cli/checker"
10 8
 	"github.com/docker/docker/integration-cli/cli"
11 9
 	"github.com/go-check/check"
... ...
@@ -131,8 +129,7 @@ func (s *DockerSuite) TestKillStoppedContainerAPIPre120(c *check.C) {
131 131
 	testRequires(c, DaemonIsLinux) // Windows only supports 1.25 or later
132 132
 	runSleepingContainer(c, "--name", "docker-kill-test-api", "-d")
133 133
 	dockerCmd(c, "stop", "docker-kill-test-api")
134
-	var httpClient *http.Client
135
-	cli, err := client.NewClient(daemonHost(), "v1.19", httpClient, nil)
134
+	cli, err := NewEnvClientWithVersion("v1.19")
136 135
 	c.Assert(err, check.IsNil)
137 136
 	defer cli.Close()
138 137
 	err = cli.ContainerKill(context.Background(), "docker-kill-test-api", "SIGKILL")
... ...
@@ -4127,7 +4127,7 @@ func (s *DockerSuite) TestRunRm(c *check.C) {
4127 4127
 // Test that auto-remove is performed by the client on API versions that do not support daemon-side api-remove (API < 1.25)
4128 4128
 func (s *DockerSuite) TestRunRmPre125Api(c *check.C) {
4129 4129
 	name := "miss-me-when-im-gone"
4130
-	envs := appendBaseEnv(false, "DOCKER_API_VERSION=1.24")
4130
+	envs := appendBaseEnv(os.Getenv("DOCKER_TLS_VERIFY") != "", "DOCKER_API_VERSION=1.24")
4131 4131
 	cli.Docker(cli.Args("run", "--name="+name, "--rm", "busybox"), cli.WithEnvironmentVariables(envs...)).Assert(c, icmd.Success)
4132 4132
 
4133 4133
 	cli.Docker(cli.Inspect(name), cli.Format(".name")).Assert(c, icmd.Expected{
... ...
@@ -6,7 +6,6 @@ import (
6 6
 	"fmt"
7 7
 	"io"
8 8
 	"io/ioutil"
9
-	"net/http"
10 9
 	"os"
11 10
 	"path"
12 11
 	"path/filepath"
... ...
@@ -373,8 +372,7 @@ func waitInspectWithArgs(name, expr, expected string, timeout time.Duration, arg
373 373
 }
374 374
 
375 375
 func getInspectBody(c *check.C, version, id string) []byte {
376
-	var httpClient *http.Client
377
-	cli, err := client.NewClient(daemonHost(), version, httpClient, nil)
376
+	cli, err := NewEnvClientWithVersion(version)
378 377
 	c.Assert(err, check.IsNil)
379 378
 	defer cli.Close()
380 379
 	_, body, err := cli.ContainerInspectWithRaw(context.Background(), id, false)
... ...
@@ -129,7 +129,11 @@ func New(host, endpoint string, modifiers ...func(*http.Request) error) (*http.R
129 129
 		return nil, fmt.Errorf("could not create new request: %v", err)
130 130
 	}
131 131
 
132
-	req.URL.Scheme = "http"
132
+	if os.Getenv("DOCKER_TLS_VERIFY") != "" {
133
+		req.URL.Scheme = "https"
134
+	} else {
135
+		req.URL.Scheme = "http"
136
+	}
133 137
 	req.URL.Host = addr
134 138
 
135 139
 	for _, config := range modifiers {
... ...
@@ -183,3 +183,14 @@ func RemoveOutputForExistingElements(output string, existing []string) string {
183 183
 	res := RemoveLinesForExistingElements(strings.Split(output, "\n"), existing)
184 184
 	return strings.Join(res, "\n")
185 185
 }
186
+
187
+// NewEnvClientWithVersion returns a docker client with a specified version.
188
+// See: github.com/docker/docker/client `NewEnvClient()`
189
+func NewEnvClientWithVersion(version string) (*client.Client, error) {
190
+	cli, err := client.NewEnvClient()
191
+	if err != nil {
192
+		return nil, err
193
+	}
194
+	cli.NegotiateAPIVersionPing(types.Ping{APIVersion: version})
195
+	return cli, nil
196
+}