Use TLS for tests if needed

Signed-off-by: Christopher Crone <christopher.crone@docker.com>
This commit is contained in:
Christopher Crone 2017-09-08 17:17:28 +02:00
parent dc6ddfa841
commit 0bdba0e91a
8 changed files with 22 additions and 17 deletions

View file

@ -1372,8 +1372,7 @@ func (s *DockerSuite) TestContainerAPICreateNoHostConfig118(c *check.C) {
Image: "busybox",
}
var httpClient *http.Client
cli, err := client.NewClient(daemonHost(), "v1.18", httpClient, map[string]string{})
cli, err := NewEnvClientWithVersion("v1.18")
_, err = cli.ContainerCreate(context.Background(), &config, &containertypes.HostConfig{}, &networktypes.NetworkingConfig{}, "")
c.Assert(err, checker.IsNil)

View file

@ -179,8 +179,7 @@ func (s *DockerSuite) TestAPIImagesSizeCompatibility(c *check.C) {
Labels map[string]string
}
var httpClient *http.Client
cli, err = client.NewClient(daemonHost(), "v1.24", httpClient, nil)
cli, err = NewEnvClientWithVersion("v1.24")
c.Assert(err, checker.IsNil)
defer cli.Close()

View file

@ -4,9 +4,7 @@ package main
import (
"encoding/json"
"net/http"
"github.com/docker/docker/client"
"github.com/docker/docker/integration-cli/checker"
"github.com/go-check/check"
"golang.org/x/net/context"
@ -19,8 +17,7 @@ func (s *DockerSuite) TestInspectAPICpusetInConfigPre120(c *check.C) {
name := "cpusetinconfig-pre120"
dockerCmd(c, "run", "--name", name, "--cpuset-cpus", "0", "busybox", "true")
var httpClient *http.Client
cli, err := client.NewClient(daemonHost(), "v1.19", httpClient, nil)
cli, err := NewEnvClientWithVersion("v1.19")
c.Assert(err, checker.IsNil)
defer cli.Close()
_, body, err := cli.ContainerInspectWithRaw(context.Background(), name, false)

View file

@ -1,11 +1,9 @@
package main
import (
"net/http"
"strings"
"time"
"github.com/docker/docker/client"
"github.com/docker/docker/integration-cli/checker"
"github.com/docker/docker/integration-cli/cli"
"github.com/go-check/check"
@ -131,8 +129,7 @@ func (s *DockerSuite) TestKillStoppedContainerAPIPre120(c *check.C) {
testRequires(c, DaemonIsLinux) // Windows only supports 1.25 or later
runSleepingContainer(c, "--name", "docker-kill-test-api", "-d")
dockerCmd(c, "stop", "docker-kill-test-api")
var httpClient *http.Client
cli, err := client.NewClient(daemonHost(), "v1.19", httpClient, nil)
cli, err := NewEnvClientWithVersion("v1.19")
c.Assert(err, check.IsNil)
defer cli.Close()
err = cli.ContainerKill(context.Background(), "docker-kill-test-api", "SIGKILL")

View file

@ -4127,7 +4127,7 @@ func (s *DockerSuite) TestRunRm(c *check.C) {
// Test that auto-remove is performed by the client on API versions that do not support daemon-side api-remove (API < 1.25)
func (s *DockerSuite) TestRunRmPre125Api(c *check.C) {
name := "miss-me-when-im-gone"
envs := appendBaseEnv(false, "DOCKER_API_VERSION=1.24")
envs := appendBaseEnv(os.Getenv("DOCKER_TLS_VERIFY") != "", "DOCKER_API_VERSION=1.24")
cli.Docker(cli.Args("run", "--name="+name, "--rm", "busybox"), cli.WithEnvironmentVariables(envs...)).Assert(c, icmd.Success)
cli.Docker(cli.Inspect(name), cli.Format(".name")).Assert(c, icmd.Expected{

View file

@ -6,7 +6,6 @@ import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"
@ -373,8 +372,7 @@ func waitInspectWithArgs(name, expr, expected string, timeout time.Duration, arg
}
func getInspectBody(c *check.C, version, id string) []byte {
var httpClient *http.Client
cli, err := client.NewClient(daemonHost(), version, httpClient, nil)
cli, err := NewEnvClientWithVersion(version)
c.Assert(err, check.IsNil)
defer cli.Close()
_, body, err := cli.ContainerInspectWithRaw(context.Background(), id, false)

View file

@ -129,7 +129,11 @@ func New(host, endpoint string, modifiers ...func(*http.Request) error) (*http.R
return nil, fmt.Errorf("could not create new request: %v", err)
}
req.URL.Scheme = "http"
if os.Getenv("DOCKER_TLS_VERIFY") != "" {
req.URL.Scheme = "https"
} else {
req.URL.Scheme = "http"
}
req.URL.Host = addr
for _, config := range modifiers {

View file

@ -183,3 +183,14 @@ func RemoveOutputForExistingElements(output string, existing []string) string {
res := RemoveLinesForExistingElements(strings.Split(output, "\n"), existing)
return strings.Join(res, "\n")
}
// NewEnvClientWithVersion returns a docker client with a specified version.
// See: github.com/docker/docker/client `NewEnvClient()`
func NewEnvClientWithVersion(version string) (*client.Client, error) {
cli, err := client.NewEnvClient()
if err != nil {
return nil, err
}
cli.NegotiateAPIVersionPing(types.Ping{APIVersion: version})
return cli, nil
}