client: remove transport package

This package doesn't really seem to do anything of real interest.
Removing it and replacing with a few helper functions. Most of this was
maintaining a fork of ctxhttp to support a mock that was unnecessary.

We could probably do with a further refactor of the client interface.
There is a lot of confusion of between transport, http layer and
application layer that makes for some awkward code. This change
improves the situation to the point where no breaking changes are
introduced.

Signed-off-by: Stephen J Day <stephen.day@docker.com>
This commit is contained in:
Stephen J Day 2016-09-08 20:44:25 -07:00
parent c6f96cb8b4
commit c648e163eb
86 changed files with 276 additions and 533 deletions

View File

@ -15,7 +15,7 @@ import (
func TestCheckpointCreateError(t *testing.T) { func TestCheckpointCreateError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
err := client.CheckpointCreate(context.Background(), "nothing", types.CheckpointCreateOptions{ err := client.CheckpointCreate(context.Background(), "nothing", types.CheckpointCreateOptions{
CheckpointID: "noting", CheckpointID: "noting",
@ -33,7 +33,7 @@ func TestCheckpointCreate(t *testing.T) {
expectedURL := "/containers/container_id/checkpoints" expectedURL := "/containers/container_id/checkpoints"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -13,7 +13,7 @@ import (
func TestCheckpointDeleteError(t *testing.T) { func TestCheckpointDeleteError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
err := client.CheckpointDelete(context.Background(), "container_id", "checkpoint_id") err := client.CheckpointDelete(context.Background(), "container_id", "checkpoint_id")
@ -26,7 +26,7 @@ func TestCheckpointDelete(t *testing.T) {
expectedURL := "/containers/container_id/checkpoints/checkpoint_id" expectedURL := "/containers/container_id/checkpoints/checkpoint_id"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -15,7 +15,7 @@ import (
func TestCheckpointListError(t *testing.T) { func TestCheckpointListError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.CheckpointList(context.Background(), "container_id") _, err := client.CheckpointList(context.Background(), "container_id")
@ -28,7 +28,7 @@ func TestCheckpointList(t *testing.T) {
expectedURL := "/containers/container_id/checkpoints" expectedURL := "/containers/container_id/checkpoints"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -8,7 +8,7 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/docker/docker/client/transport" "github.com/docker/go-connections/sockets"
"github.com/docker/go-connections/tlsconfig" "github.com/docker/go-connections/tlsconfig"
) )
@ -26,8 +26,8 @@ type Client struct {
addr string addr string
// basePath holds the path to prepend to the requests. // basePath holds the path to prepend to the requests.
basePath string basePath string
// transport is the interface to send request with, it implements transport.Client. // client used to send and receive http requests.
transport transport.Client client *http.Client
// version of the server to talk to. // version of the server to talk to.
version string version string
// custom http headers configured by users. // custom http headers configured by users.
@ -86,9 +86,15 @@ func NewClient(host string, version string, client *http.Client, httpHeaders map
return nil, err return nil, err
} }
transport, err := transport.NewTransportWithHTTP(proto, addr, client) if client == nil {
if err != nil { client = &http.Client{}
return nil, err }
if client.Transport == nil {
// setup the transport, if not already present
transport := new(http.Transport)
sockets.ConfigureTransport(transport, proto, addr)
client.Transport = transport
} }
return &Client{ return &Client{
@ -96,7 +102,7 @@ func NewClient(host string, version string, client *http.Client, httpHeaders map
proto: proto, proto: proto,
addr: addr, addr: addr,
basePath: basePath, basePath: basePath,
transport: transport, client: client,
version: version, version: version,
customHTTPHeaders: httpHeaders, customHTTPHeaders: httpHeaders,
}, nil }, nil

View File

@ -2,48 +2,17 @@ package client
import ( import (
"bytes" "bytes"
"crypto/tls"
"encoding/json" "encoding/json"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/client/transport"
) )
type mockClient struct { func newMockClient(doer func(*http.Request) (*http.Response, error)) *http.Client {
do func(*http.Request) (*http.Response, error) return &http.Client{
} Transport: transportFunc(doer),
// TLSConfig returns the TLS configuration.
func (m *mockClient) TLSConfig() *tls.Config {
return &tls.Config{}
}
// Scheme returns protocol scheme to use.
func (m *mockClient) Scheme() string {
return "http"
}
// Secure returns true if there is a TLS configuration.
func (m *mockClient) Secure() bool {
return false
}
// NewMockClient returns a mocked client that runs the function supplied as `client.Do` call
func newMockClient(tlsConfig *tls.Config, doer func(*http.Request) (*http.Response, error)) transport.Client {
if tlsConfig != nil {
panic("this actually gets set!")
} }
return &mockClient{
do: doer,
}
}
// Do executes the supplied function for the mock.
func (m mockClient) Do(req *http.Request) (*http.Response, error) {
return m.do(req)
} }
func errorMock(statusCode int, message string) func(req *http.Request) (*http.Response, error) { func errorMock(statusCode int, message string) func(req *http.Request) (*http.Response, error) {

View File

@ -173,7 +173,7 @@ func TestParseHost(t *testing.T) {
func TestUpdateClientVersion(t *testing.T) { func TestUpdateClientVersion(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
splitQuery := strings.Split(req.URL.Path, "/") splitQuery := strings.Split(req.URL.Path, "/")
queryVersion := splitQuery[1] queryVersion := splitQuery[1]
b, err := json.Marshal(types.Version{ b, err := json.Marshal(types.Version{

View File

@ -15,7 +15,7 @@ import (
func TestContainerCommitError(t *testing.T) { func TestContainerCommitError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.ContainerCommit(context.Background(), "nothing", types.ContainerCommitOptions{}) _, err := client.ContainerCommit(context.Background(), "nothing", types.ContainerCommitOptions{})
if err == nil || err.Error() != "Error response from daemon: Server error" { if err == nil || err.Error() != "Error response from daemon: Server error" {
@ -34,7 +34,7 @@ func TestContainerCommit(t *testing.T) {
expectedChanges := []string{"change1", "change2"} expectedChanges := []string{"change1", "change2"}
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -17,7 +17,7 @@ import (
func TestContainerStatPathError(t *testing.T) { func TestContainerStatPathError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.ContainerStatPath(context.Background(), "container_id", "path") _, err := client.ContainerStatPath(context.Background(), "container_id", "path")
if err == nil || err.Error() != "Error response from daemon: Server error" { if err == nil || err.Error() != "Error response from daemon: Server error" {
@ -27,7 +27,7 @@ func TestContainerStatPathError(t *testing.T) {
func TestContainerStatPathNoHeaderError(t *testing.T) { func TestContainerStatPathNoHeaderError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
return &http.Response{ return &http.Response{
StatusCode: http.StatusOK, StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))), Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
@ -44,7 +44,7 @@ func TestContainerStatPath(t *testing.T) {
expectedURL := "/containers/container_id/archive" expectedURL := "/containers/container_id/archive"
expectedPath := "path/to/file" expectedPath := "path/to/file"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }
@ -87,7 +87,7 @@ func TestContainerStatPath(t *testing.T) {
func TestCopyToContainerError(t *testing.T) { func TestCopyToContainerError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
err := client.CopyToContainer(context.Background(), "container_id", "path/to/file", bytes.NewReader([]byte("")), types.CopyToContainerOptions{}) err := client.CopyToContainer(context.Background(), "container_id", "path/to/file", bytes.NewReader([]byte("")), types.CopyToContainerOptions{})
if err == nil || err.Error() != "Error response from daemon: Server error" { if err == nil || err.Error() != "Error response from daemon: Server error" {
@ -97,7 +97,7 @@ func TestCopyToContainerError(t *testing.T) {
func TestCopyToContainerNotStatusOKError(t *testing.T) { func TestCopyToContainerNotStatusOKError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusNoContent, "No content")), client: newMockClient(errorMock(http.StatusNoContent, "No content")),
} }
err := client.CopyToContainer(context.Background(), "container_id", "path/to/file", bytes.NewReader([]byte("")), types.CopyToContainerOptions{}) err := client.CopyToContainer(context.Background(), "container_id", "path/to/file", bytes.NewReader([]byte("")), types.CopyToContainerOptions{})
if err == nil || err.Error() != "unexpected status code from daemon: 204" { if err == nil || err.Error() != "unexpected status code from daemon: 204" {
@ -109,7 +109,7 @@ func TestCopyToContainer(t *testing.T) {
expectedURL := "/containers/container_id/archive" expectedURL := "/containers/container_id/archive"
expectedPath := "path/to/file" expectedPath := "path/to/file"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }
@ -153,7 +153,7 @@ func TestCopyToContainer(t *testing.T) {
func TestCopyFromContainerError(t *testing.T) { func TestCopyFromContainerError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, _, err := client.CopyFromContainer(context.Background(), "container_id", "path/to/file") _, _, err := client.CopyFromContainer(context.Background(), "container_id", "path/to/file")
if err == nil || err.Error() != "Error response from daemon: Server error" { if err == nil || err.Error() != "Error response from daemon: Server error" {
@ -163,7 +163,7 @@ func TestCopyFromContainerError(t *testing.T) {
func TestCopyFromContainerNotStatusOKError(t *testing.T) { func TestCopyFromContainerNotStatusOKError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusNoContent, "No content")), client: newMockClient(errorMock(http.StatusNoContent, "No content")),
} }
_, _, err := client.CopyFromContainer(context.Background(), "container_id", "path/to/file") _, _, err := client.CopyFromContainer(context.Background(), "container_id", "path/to/file")
if err == nil || err.Error() != "unexpected status code from daemon: 204" { if err == nil || err.Error() != "unexpected status code from daemon: 204" {
@ -173,7 +173,7 @@ func TestCopyFromContainerNotStatusOKError(t *testing.T) {
func TestCopyFromContainerNoHeaderError(t *testing.T) { func TestCopyFromContainerNoHeaderError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
return &http.Response{ return &http.Response{
StatusCode: http.StatusOK, StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))), Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
@ -190,7 +190,7 @@ func TestCopyFromContainer(t *testing.T) {
expectedURL := "/containers/container_id/archive" expectedURL := "/containers/container_id/archive"
expectedPath := "path/to/file" expectedPath := "path/to/file"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -16,7 +16,7 @@ import (
func TestContainerCreateError(t *testing.T) { func TestContainerCreateError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.ContainerCreate(context.Background(), nil, nil, nil, "nothing") _, err := client.ContainerCreate(context.Background(), nil, nil, nil, "nothing")
if err == nil || err.Error() != "Error response from daemon: Server error" { if err == nil || err.Error() != "Error response from daemon: Server error" {
@ -25,7 +25,7 @@ func TestContainerCreateError(t *testing.T) {
// 404 doesn't automagitally means an unknown image // 404 doesn't automagitally means an unknown image
client = &Client{ client = &Client{
transport: newMockClient(nil, errorMock(http.StatusNotFound, "Server error")), client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
} }
_, err = client.ContainerCreate(context.Background(), nil, nil, nil, "nothing") _, err = client.ContainerCreate(context.Background(), nil, nil, nil, "nothing")
if err == nil || err.Error() != "Error response from daemon: Server error" { if err == nil || err.Error() != "Error response from daemon: Server error" {
@ -35,7 +35,7 @@ func TestContainerCreateError(t *testing.T) {
func TestContainerCreateImageNotFound(t *testing.T) { func TestContainerCreateImageNotFound(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusNotFound, "No such image")), client: newMockClient(errorMock(http.StatusNotFound, "No such image")),
} }
_, err := client.ContainerCreate(context.Background(), &container.Config{Image: "unknown_image"}, nil, nil, "unknown") _, err := client.ContainerCreate(context.Background(), &container.Config{Image: "unknown_image"}, nil, nil, "unknown")
if err == nil || !IsErrImageNotFound(err) { if err == nil || !IsErrImageNotFound(err) {
@ -46,7 +46,7 @@ func TestContainerCreateImageNotFound(t *testing.T) {
func TestContainerCreateWithName(t *testing.T) { func TestContainerCreateWithName(t *testing.T) {
expectedURL := "/containers/create" expectedURL := "/containers/create"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -15,7 +15,7 @@ import (
func TestContainerDiffError(t *testing.T) { func TestContainerDiffError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.ContainerDiff(context.Background(), "nothing") _, err := client.ContainerDiff(context.Background(), "nothing")
if err == nil || err.Error() != "Error response from daemon: Server error" { if err == nil || err.Error() != "Error response from daemon: Server error" {
@ -27,7 +27,7 @@ func TestContainerDiffError(t *testing.T) {
func TestContainerDiff(t *testing.T) { func TestContainerDiff(t *testing.T) {
expectedURL := "/containers/container_id/changes" expectedURL := "/containers/container_id/changes"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -16,7 +16,7 @@ import (
func TestContainerExecCreateError(t *testing.T) { func TestContainerExecCreateError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.ContainerExecCreate(context.Background(), "container_id", types.ExecConfig{}) _, err := client.ContainerExecCreate(context.Background(), "container_id", types.ExecConfig{})
if err == nil || err.Error() != "Error response from daemon: Server error" { if err == nil || err.Error() != "Error response from daemon: Server error" {
@ -27,7 +27,7 @@ func TestContainerExecCreateError(t *testing.T) {
func TestContainerExecCreate(t *testing.T) { func TestContainerExecCreate(t *testing.T) {
expectedURL := "/containers/container_id/exec" expectedURL := "/containers/container_id/exec"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL)
} }
@ -71,7 +71,7 @@ func TestContainerExecCreate(t *testing.T) {
func TestContainerExecStartError(t *testing.T) { func TestContainerExecStartError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
err := client.ContainerExecStart(context.Background(), "nothing", types.ExecStartCheck{}) err := client.ContainerExecStart(context.Background(), "nothing", types.ExecStartCheck{})
if err == nil || err.Error() != "Error response from daemon: Server error" { if err == nil || err.Error() != "Error response from daemon: Server error" {
@ -82,7 +82,7 @@ func TestContainerExecStartError(t *testing.T) {
func TestContainerExecStart(t *testing.T) { func TestContainerExecStart(t *testing.T) {
expectedURL := "/exec/exec_id/start" expectedURL := "/exec/exec_id/start"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }
@ -115,7 +115,7 @@ func TestContainerExecStart(t *testing.T) {
func TestContainerExecInspectError(t *testing.T) { func TestContainerExecInspectError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.ContainerExecInspect(context.Background(), "nothing") _, err := client.ContainerExecInspect(context.Background(), "nothing")
if err == nil || err.Error() != "Error response from daemon: Server error" { if err == nil || err.Error() != "Error response from daemon: Server error" {
@ -126,7 +126,7 @@ func TestContainerExecInspectError(t *testing.T) {
func TestContainerExecInspect(t *testing.T) { func TestContainerExecInspect(t *testing.T) {
expectedURL := "/exec/exec_id/json" expectedURL := "/exec/exec_id/json"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -13,7 +13,7 @@ import (
func TestContainerExportError(t *testing.T) { func TestContainerExportError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.ContainerExport(context.Background(), "nothing") _, err := client.ContainerExport(context.Background(), "nothing")
if err == nil || err.Error() != "Error response from daemon: Server error" { if err == nil || err.Error() != "Error response from daemon: Server error" {
@ -24,7 +24,7 @@ func TestContainerExportError(t *testing.T) {
func TestContainerExport(t *testing.T) { func TestContainerExport(t *testing.T) {
expectedURL := "/containers/container_id/export" expectedURL := "/containers/container_id/export"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(r *http.Request) (*http.Response, error) { client: newMockClient(func(r *http.Request) (*http.Response, error) {
if !strings.HasPrefix(r.URL.Path, expectedURL) { if !strings.HasPrefix(r.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, r.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, r.URL)
} }

View File

@ -15,7 +15,7 @@ import (
func TestContainerInspectError(t *testing.T) { func TestContainerInspectError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.ContainerInspect(context.Background(), "nothing") _, err := client.ContainerInspect(context.Background(), "nothing")
@ -26,7 +26,7 @@ func TestContainerInspectError(t *testing.T) {
func TestContainerInspectContainerNotFound(t *testing.T) { func TestContainerInspectContainerNotFound(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusNotFound, "Server error")), client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
} }
_, err := client.ContainerInspect(context.Background(), "unknown") _, err := client.ContainerInspect(context.Background(), "unknown")
@ -38,7 +38,7 @@ func TestContainerInspectContainerNotFound(t *testing.T) {
func TestContainerInspect(t *testing.T) { func TestContainerInspect(t *testing.T) {
expectedURL := "/containers/container_id/json" expectedURL := "/containers/container_id/json"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }
@ -76,7 +76,7 @@ func TestContainerInspect(t *testing.T) {
func TestContainerInspectNode(t *testing.T) { func TestContainerInspectNode(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
content, err := json.Marshal(types.ContainerJSON{ content, err := json.Marshal(types.ContainerJSON{
ContainerJSONBase: &types.ContainerJSONBase{ ContainerJSONBase: &types.ContainerJSONBase{
ID: "container_id", ID: "container_id",

View File

@ -13,7 +13,7 @@ import (
func TestContainerKillError(t *testing.T) { func TestContainerKillError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
err := client.ContainerKill(context.Background(), "nothing", "SIGKILL") err := client.ContainerKill(context.Background(), "nothing", "SIGKILL")
if err == nil || err.Error() != "Error response from daemon: Server error" { if err == nil || err.Error() != "Error response from daemon: Server error" {
@ -24,7 +24,7 @@ func TestContainerKillError(t *testing.T) {
func TestContainerKill(t *testing.T) { func TestContainerKill(t *testing.T) {
expectedURL := "/containers/container_id/kill" expectedURL := "/containers/container_id/kill"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -16,7 +16,7 @@ import (
func TestContainerListError(t *testing.T) { func TestContainerListError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.ContainerList(context.Background(), types.ContainerListOptions{}) _, err := client.ContainerList(context.Background(), types.ContainerListOptions{})
if err == nil || err.Error() != "Error response from daemon: Server error" { if err == nil || err.Error() != "Error response from daemon: Server error" {
@ -28,7 +28,7 @@ func TestContainerList(t *testing.T) {
expectedURL := "/containers/json" expectedURL := "/containers/json"
expectedFilters := `{"before":{"container":true},"label":{"label1":true,"label2":true}}` expectedFilters := `{"before":{"container":true},"label":{"label1":true,"label2":true}}`
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -19,7 +19,7 @@ import (
func TestContainerLogsError(t *testing.T) { func TestContainerLogsError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.ContainerLogs(context.Background(), "container_id", types.ContainerLogsOptions{}) _, err := client.ContainerLogs(context.Background(), "container_id", types.ContainerLogsOptions{})
if err == nil || err.Error() != "Error response from daemon: Server error" { if err == nil || err.Error() != "Error response from daemon: Server error" {
@ -83,7 +83,7 @@ func TestContainerLogs(t *testing.T) {
} }
for _, logCase := range cases { for _, logCase := range cases {
client := &Client{ client := &Client{
transport: newMockClient(nil, func(r *http.Request) (*http.Response, error) { client: newMockClient(func(r *http.Request) (*http.Response, error) {
if !strings.HasPrefix(r.URL.Path, expectedURL) { if !strings.HasPrefix(r.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, r.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, r.URL)
} }

View File

@ -13,7 +13,7 @@ import (
func TestContainerPauseError(t *testing.T) { func TestContainerPauseError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
err := client.ContainerPause(context.Background(), "nothing") err := client.ContainerPause(context.Background(), "nothing")
if err == nil || err.Error() != "Error response from daemon: Server error" { if err == nil || err.Error() != "Error response from daemon: Server error" {
@ -24,7 +24,7 @@ func TestContainerPauseError(t *testing.T) {
func TestContainerPause(t *testing.T) { func TestContainerPause(t *testing.T) {
expectedURL := "/containers/container_id/pause" expectedURL := "/containers/container_id/pause"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -14,7 +14,7 @@ import (
func TestContainerRemoveError(t *testing.T) { func TestContainerRemoveError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
err := client.ContainerRemove(context.Background(), "container_id", types.ContainerRemoveOptions{}) err := client.ContainerRemove(context.Background(), "container_id", types.ContainerRemoveOptions{})
if err == nil || err.Error() != "Error response from daemon: Server error" { if err == nil || err.Error() != "Error response from daemon: Server error" {
@ -25,7 +25,7 @@ func TestContainerRemoveError(t *testing.T) {
func TestContainerRemove(t *testing.T) { func TestContainerRemove(t *testing.T) {
expectedURL := "/containers/container_id" expectedURL := "/containers/container_id"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -13,7 +13,7 @@ import (
func TestContainerRenameError(t *testing.T) { func TestContainerRenameError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
err := client.ContainerRename(context.Background(), "nothing", "newNothing") err := client.ContainerRename(context.Background(), "nothing", "newNothing")
if err == nil || err.Error() != "Error response from daemon: Server error" { if err == nil || err.Error() != "Error response from daemon: Server error" {
@ -24,7 +24,7 @@ func TestContainerRenameError(t *testing.T) {
func TestContainerRename(t *testing.T) { func TestContainerRename(t *testing.T) {
expectedURL := "/containers/container_id/rename" expectedURL := "/containers/container_id/rename"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -14,7 +14,7 @@ import (
func TestContainerResizeError(t *testing.T) { func TestContainerResizeError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
err := client.ContainerResize(context.Background(), "container_id", types.ResizeOptions{}) err := client.ContainerResize(context.Background(), "container_id", types.ResizeOptions{})
if err == nil || err.Error() != "Error response from daemon: Server error" { if err == nil || err.Error() != "Error response from daemon: Server error" {
@ -24,7 +24,7 @@ func TestContainerResizeError(t *testing.T) {
func TestContainerExecResizeError(t *testing.T) { func TestContainerExecResizeError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
err := client.ContainerExecResize(context.Background(), "exec_id", types.ResizeOptions{}) err := client.ContainerExecResize(context.Background(), "exec_id", types.ResizeOptions{})
if err == nil || err.Error() != "Error response from daemon: Server error" { if err == nil || err.Error() != "Error response from daemon: Server error" {
@ -34,7 +34,7 @@ func TestContainerExecResizeError(t *testing.T) {
func TestContainerResize(t *testing.T) { func TestContainerResize(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, resizeTransport("/containers/container_id/resize")), client: newMockClient(resizeTransport("/containers/container_id/resize")),
} }
err := client.ContainerResize(context.Background(), "container_id", types.ResizeOptions{ err := client.ContainerResize(context.Background(), "container_id", types.ResizeOptions{
@ -48,7 +48,7 @@ func TestContainerResize(t *testing.T) {
func TestContainerExecResize(t *testing.T) { func TestContainerExecResize(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, resizeTransport("/exec/exec_id/resize")), client: newMockClient(resizeTransport("/exec/exec_id/resize")),
} }
err := client.ContainerExecResize(context.Background(), "exec_id", types.ResizeOptions{ err := client.ContainerExecResize(context.Background(), "exec_id", types.ResizeOptions{

View File

@ -14,7 +14,7 @@ import (
func TestContainerRestartError(t *testing.T) { func TestContainerRestartError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
timeout := 0 * time.Second timeout := 0 * time.Second
err := client.ContainerRestart(context.Background(), "nothing", &timeout) err := client.ContainerRestart(context.Background(), "nothing", &timeout)
@ -26,7 +26,7 @@ func TestContainerRestartError(t *testing.T) {
func TestContainerRestart(t *testing.T) { func TestContainerRestart(t *testing.T) {
expectedURL := "/containers/container_id/restart" expectedURL := "/containers/container_id/restart"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -16,7 +16,7 @@ import (
func TestContainerStartError(t *testing.T) { func TestContainerStartError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
err := client.ContainerStart(context.Background(), "nothing", types.ContainerStartOptions{}) err := client.ContainerStart(context.Background(), "nothing", types.ContainerStartOptions{})
if err == nil || err.Error() != "Error response from daemon: Server error" { if err == nil || err.Error() != "Error response from daemon: Server error" {
@ -27,7 +27,7 @@ func TestContainerStartError(t *testing.T) {
func TestContainerStart(t *testing.T) { func TestContainerStart(t *testing.T) {
expectedURL := "/containers/container_id/start" expectedURL := "/containers/container_id/start"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -13,7 +13,7 @@ import (
func TestContainerStatsError(t *testing.T) { func TestContainerStatsError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.ContainerStats(context.Background(), "nothing", false) _, err := client.ContainerStats(context.Background(), "nothing", false)
if err == nil || err.Error() != "Error response from daemon: Server error" { if err == nil || err.Error() != "Error response from daemon: Server error" {
@ -37,7 +37,7 @@ func TestContainerStats(t *testing.T) {
} }
for _, c := range cases { for _, c := range cases {
client := &Client{ client := &Client{
transport: newMockClient(nil, func(r *http.Request) (*http.Response, error) { client: newMockClient(func(r *http.Request) (*http.Response, error) {
if !strings.HasPrefix(r.URL.Path, expectedURL) { if !strings.HasPrefix(r.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, r.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, r.URL)
} }

View File

@ -14,7 +14,7 @@ import (
func TestContainerStopError(t *testing.T) { func TestContainerStopError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
timeout := 0 * time.Second timeout := 0 * time.Second
err := client.ContainerStop(context.Background(), "nothing", &timeout) err := client.ContainerStop(context.Background(), "nothing", &timeout)
@ -26,7 +26,7 @@ func TestContainerStopError(t *testing.T) {
func TestContainerStop(t *testing.T) { func TestContainerStop(t *testing.T) {
expectedURL := "/containers/container_id/stop" expectedURL := "/containers/container_id/stop"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -16,7 +16,7 @@ import (
func TestContainerTopError(t *testing.T) { func TestContainerTopError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.ContainerTop(context.Background(), "nothing", []string{}) _, err := client.ContainerTop(context.Background(), "nothing", []string{})
if err == nil || err.Error() != "Error response from daemon: Server error" { if err == nil || err.Error() != "Error response from daemon: Server error" {
@ -33,7 +33,7 @@ func TestContainerTop(t *testing.T) {
expectedTitles := []string{"title1", "title2"} expectedTitles := []string{"title1", "title2"}
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -13,7 +13,7 @@ import (
func TestContainerUnpauseError(t *testing.T) { func TestContainerUnpauseError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
err := client.ContainerUnpause(context.Background(), "nothing") err := client.ContainerUnpause(context.Background(), "nothing")
if err == nil || err.Error() != "Error response from daemon: Server error" { if err == nil || err.Error() != "Error response from daemon: Server error" {
@ -24,7 +24,7 @@ func TestContainerUnpauseError(t *testing.T) {
func TestContainerUnpause(t *testing.T) { func TestContainerUnpause(t *testing.T) {
expectedURL := "/containers/container_id/unpause" expectedURL := "/containers/container_id/unpause"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -16,7 +16,7 @@ import (
func TestContainerUpdateError(t *testing.T) { func TestContainerUpdateError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.ContainerUpdate(context.Background(), "nothing", container.UpdateConfig{}) _, err := client.ContainerUpdate(context.Background(), "nothing", container.UpdateConfig{})
if err == nil || err.Error() != "Error response from daemon: Server error" { if err == nil || err.Error() != "Error response from daemon: Server error" {
@ -28,7 +28,7 @@ func TestContainerUpdate(t *testing.T) {
expectedURL := "/containers/container_id/update" expectedURL := "/containers/container_id/update"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -18,7 +18,7 @@ import (
func TestContainerWaitError(t *testing.T) { func TestContainerWaitError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
code, err := client.ContainerWait(context.Background(), "nothing") code, err := client.ContainerWait(context.Background(), "nothing")
if err == nil || err.Error() != "Error response from daemon: Server error" { if err == nil || err.Error() != "Error response from daemon: Server error" {
@ -32,7 +32,7 @@ func TestContainerWaitError(t *testing.T) {
func TestContainerWait(t *testing.T) { func TestContainerWait(t *testing.T) {
expectedURL := "/containers/container_id/wait" expectedURL := "/containers/container_id/wait"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -34,7 +34,7 @@ func TestEventsErrorInOptions(t *testing.T) {
} }
for _, e := range errorCases { for _, e := range errorCases {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.Events(context.Background(), e.options) _, err := client.Events(context.Background(), e.options)
if err == nil || !strings.Contains(err.Error(), e.expectedError) { if err == nil || !strings.Contains(err.Error(), e.expectedError) {
@ -45,7 +45,7 @@ func TestEventsErrorInOptions(t *testing.T) {
func TestEventsErrorFromServer(t *testing.T) { func TestEventsErrorFromServer(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.Events(context.Background(), types.EventsOptions{}) _, err := client.Events(context.Background(), types.EventsOptions{})
if err == nil || err.Error() != "Error response from daemon: Server error" { if err == nil || err.Error() != "Error response from daemon: Server error" {
@ -93,7 +93,7 @@ func TestEvents(t *testing.T) {
for _, eventsCase := range eventsCases { for _, eventsCase := range eventsCases {
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -47,7 +47,12 @@ func (cli *Client) postHijacked(ctx context.Context, path string, query url.Valu
req.Header.Set("Connection", "Upgrade") req.Header.Set("Connection", "Upgrade")
req.Header.Set("Upgrade", "tcp") req.Header.Set("Upgrade", "tcp")
conn, err := dial(cli.proto, cli.addr, cli.transport.TLSConfig()) tlsConfig, err := resolveTLSConfig(cli.client.Transport)
if err != nil {
return types.HijackedResponse{}, err
}
conn, err := dial(cli.proto, cli.addr, tlsConfig)
if err != nil { if err != nil {
if strings.Contains(err.Error(), "connection refused") { if strings.Contains(err.Error(), "connection refused") {
return types.HijackedResponse{}, fmt.Errorf("Cannot connect to the Docker daemon. Is 'docker daemon' running on this host?") return types.HijackedResponse{}, fmt.Errorf("Cannot connect to the Docker daemon. Is 'docker daemon' running on this host?")

View File

@ -18,7 +18,7 @@ import (
func TestImageBuildError(t *testing.T) { func TestImageBuildError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.ImageBuild(context.Background(), nil, types.ImageBuildOptions{}) _, err := client.ImageBuild(context.Background(), nil, types.ImageBuildOptions{})
if err == nil || err.Error() != "Error response from daemon: Server error" { if err == nil || err.Error() != "Error response from daemon: Server error" {
@ -157,7 +157,7 @@ func TestImageBuild(t *testing.T) {
for _, buildCase := range buildCases { for _, buildCase := range buildCases {
expectedURL := "/build" expectedURL := "/build"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(r *http.Request) (*http.Response, error) { client: newMockClient(func(r *http.Request) (*http.Response, error) {
if !strings.HasPrefix(r.URL.Path, expectedURL) { if !strings.HasPrefix(r.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, r.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, r.URL)
} }

View File

@ -15,7 +15,7 @@ import (
func TestImageCreateError(t *testing.T) { func TestImageCreateError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.ImageCreate(context.Background(), "reference", types.ImageCreateOptions{}) _, err := client.ImageCreate(context.Background(), "reference", types.ImageCreateOptions{})
if err == nil || err.Error() != "Error response from daemon: Server error" { if err == nil || err.Error() != "Error response from daemon: Server error" {
@ -30,7 +30,7 @@ func TestImageCreate(t *testing.T) {
expectedReference := fmt.Sprintf("%s@%s", expectedImage, expectedTag) expectedReference := fmt.Sprintf("%s@%s", expectedImage, expectedTag)
expectedRegistryAuth := "eyJodHRwczovL2luZGV4LmRvY2tlci5pby92MS8iOnsiYXV0aCI6ImRHOTBid289IiwiZW1haWwiOiJqb2huQGRvZS5jb20ifX0=" expectedRegistryAuth := "eyJodHRwczovL2luZGV4LmRvY2tlci5pby92MS8iOnsiYXV0aCI6ImRHOTBid289IiwiZW1haWwiOiJqb2huQGRvZS5jb20ifX0="
client := &Client{ client := &Client{
transport: newMockClient(nil, func(r *http.Request) (*http.Response, error) { client: newMockClient(func(r *http.Request) (*http.Response, error) {
if !strings.HasPrefix(r.URL.Path, expectedURL) { if !strings.HasPrefix(r.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, r.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, r.URL)
} }

View File

@ -15,7 +15,7 @@ import (
func TestImageHistoryError(t *testing.T) { func TestImageHistoryError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.ImageHistory(context.Background(), "nothing") _, err := client.ImageHistory(context.Background(), "nothing")
if err == nil || err.Error() != "Error response from daemon: Server error" { if err == nil || err.Error() != "Error response from daemon: Server error" {
@ -26,7 +26,7 @@ func TestImageHistoryError(t *testing.T) {
func TestImageHistory(t *testing.T) { func TestImageHistory(t *testing.T) {
expectedURL := "/images/image_id/history" expectedURL := "/images/image_id/history"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(r *http.Request) (*http.Response, error) { client: newMockClient(func(r *http.Request) (*http.Response, error) {
if !strings.HasPrefix(r.URL.Path, expectedURL) { if !strings.HasPrefix(r.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, r.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, r.URL)
} }

View File

@ -15,7 +15,7 @@ import (
func TestImageImportError(t *testing.T) { func TestImageImportError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.ImageImport(context.Background(), types.ImageImportSource{}, "image:tag", types.ImageImportOptions{}) _, err := client.ImageImport(context.Background(), types.ImageImportSource{}, "image:tag", types.ImageImportOptions{})
if err == nil || err.Error() != "Error response from daemon: Server error" { if err == nil || err.Error() != "Error response from daemon: Server error" {
@ -26,7 +26,7 @@ func TestImageImportError(t *testing.T) {
func TestImageImport(t *testing.T) { func TestImageImport(t *testing.T) {
expectedURL := "/images/create" expectedURL := "/images/create"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(r *http.Request) (*http.Response, error) { client: newMockClient(func(r *http.Request) (*http.Response, error) {
if !strings.HasPrefix(r.URL.Path, expectedURL) { if !strings.HasPrefix(r.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, r.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, r.URL)
} }

View File

@ -16,7 +16,7 @@ import (
func TestImageInspectError(t *testing.T) { func TestImageInspectError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, _, err := client.ImageInspectWithRaw(context.Background(), "nothing") _, _, err := client.ImageInspectWithRaw(context.Background(), "nothing")
@ -27,7 +27,7 @@ func TestImageInspectError(t *testing.T) {
func TestImageInspectImageNotFound(t *testing.T) { func TestImageInspectImageNotFound(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusNotFound, "Server error")), client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
} }
_, _, err := client.ImageInspectWithRaw(context.Background(), "unknown") _, _, err := client.ImageInspectWithRaw(context.Background(), "unknown")
@ -40,7 +40,7 @@ func TestImageInspect(t *testing.T) {
expectedURL := "/images/image_id/json" expectedURL := "/images/image_id/json"
expectedTags := []string{"tag1", "tag2"} expectedTags := []string{"tag1", "tag2"}
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -16,7 +16,7 @@ import (
func TestImageListError(t *testing.T) { func TestImageListError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.ImageList(context.Background(), types.ImageListOptions{}) _, err := client.ImageList(context.Background(), types.ImageListOptions{})
@ -82,7 +82,7 @@ func TestImageList(t *testing.T) {
} }
for _, listCase := range listCases { for _, listCase := range listCases {
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -13,7 +13,7 @@ import (
func TestImageLoadError(t *testing.T) { func TestImageLoadError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.ImageLoad(context.Background(), nil, true) _, err := client.ImageLoad(context.Background(), nil, true)
@ -51,7 +51,7 @@ func TestImageLoad(t *testing.T) {
} }
for _, loadCase := range loadCases { for _, loadCase := range loadCases {
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -15,7 +15,7 @@ import (
func TestImagePullReferenceParseError(t *testing.T) { func TestImagePullReferenceParseError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
return nil, nil return nil, nil
}), }),
} }
@ -28,7 +28,7 @@ func TestImagePullReferenceParseError(t *testing.T) {
func TestImagePullAnyError(t *testing.T) { func TestImagePullAnyError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.ImagePull(context.Background(), "myimage", types.ImagePullOptions{}) _, err := client.ImagePull(context.Background(), "myimage", types.ImagePullOptions{})
if err == nil || err.Error() != "Error response from daemon: Server error" { if err == nil || err.Error() != "Error response from daemon: Server error" {
@ -38,7 +38,7 @@ func TestImagePullAnyError(t *testing.T) {
func TestImagePullStatusUnauthorizedError(t *testing.T) { func TestImagePullStatusUnauthorizedError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusUnauthorized, "Unauthorized error")), client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")),
} }
_, err := client.ImagePull(context.Background(), "myimage", types.ImagePullOptions{}) _, err := client.ImagePull(context.Background(), "myimage", types.ImagePullOptions{})
if err == nil || err.Error() != "Error response from daemon: Unauthorized error" { if err == nil || err.Error() != "Error response from daemon: Unauthorized error" {
@ -48,7 +48,7 @@ func TestImagePullStatusUnauthorizedError(t *testing.T) {
func TestImagePullWithUnauthorizedErrorAndPrivilegeFuncError(t *testing.T) { func TestImagePullWithUnauthorizedErrorAndPrivilegeFuncError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusUnauthorized, "Unauthorized error")), client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")),
} }
privilegeFunc := func() (string, error) { privilegeFunc := func() (string, error) {
return "", fmt.Errorf("Error requesting privilege") return "", fmt.Errorf("Error requesting privilege")
@ -63,7 +63,7 @@ func TestImagePullWithUnauthorizedErrorAndPrivilegeFuncError(t *testing.T) {
func TestImagePullWithUnauthorizedErrorAndAnotherUnauthorizedError(t *testing.T) { func TestImagePullWithUnauthorizedErrorAndAnotherUnauthorizedError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusUnauthorized, "Unauthorized error")), client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")),
} }
privilegeFunc := func() (string, error) { privilegeFunc := func() (string, error) {
return "a-auth-header", nil return "a-auth-header", nil
@ -79,7 +79,7 @@ func TestImagePullWithUnauthorizedErrorAndAnotherUnauthorizedError(t *testing.T)
func TestImagePullWithPrivilegedFuncNoError(t *testing.T) { func TestImagePullWithPrivilegedFuncNoError(t *testing.T) {
expectedURL := "/images/create" expectedURL := "/images/create"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }
@ -163,7 +163,7 @@ func TestImagePullWithoutErrors(t *testing.T) {
} }
for _, pullCase := range pullCases { for _, pullCase := range pullCases {
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -15,7 +15,7 @@ import (
func TestImagePushReferenceError(t *testing.T) { func TestImagePushReferenceError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
return nil, nil return nil, nil
}), }),
} }
@ -33,7 +33,7 @@ func TestImagePushReferenceError(t *testing.T) {
func TestImagePushAnyError(t *testing.T) { func TestImagePushAnyError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.ImagePush(context.Background(), "myimage", types.ImagePushOptions{}) _, err := client.ImagePush(context.Background(), "myimage", types.ImagePushOptions{})
if err == nil || err.Error() != "Error response from daemon: Server error" { if err == nil || err.Error() != "Error response from daemon: Server error" {
@ -43,7 +43,7 @@ func TestImagePushAnyError(t *testing.T) {
func TestImagePushStatusUnauthorizedError(t *testing.T) { func TestImagePushStatusUnauthorizedError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusUnauthorized, "Unauthorized error")), client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")),
} }
_, err := client.ImagePush(context.Background(), "myimage", types.ImagePushOptions{}) _, err := client.ImagePush(context.Background(), "myimage", types.ImagePushOptions{})
if err == nil || err.Error() != "Error response from daemon: Unauthorized error" { if err == nil || err.Error() != "Error response from daemon: Unauthorized error" {
@ -53,7 +53,7 @@ func TestImagePushStatusUnauthorizedError(t *testing.T) {
func TestImagePushWithUnauthorizedErrorAndPrivilegeFuncError(t *testing.T) { func TestImagePushWithUnauthorizedErrorAndPrivilegeFuncError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusUnauthorized, "Unauthorized error")), client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")),
} }
privilegeFunc := func() (string, error) { privilegeFunc := func() (string, error) {
return "", fmt.Errorf("Error requesting privilege") return "", fmt.Errorf("Error requesting privilege")
@ -68,7 +68,7 @@ func TestImagePushWithUnauthorizedErrorAndPrivilegeFuncError(t *testing.T) {
func TestImagePushWithUnauthorizedErrorAndAnotherUnauthorizedError(t *testing.T) { func TestImagePushWithUnauthorizedErrorAndAnotherUnauthorizedError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusUnauthorized, "Unauthorized error")), client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")),
} }
privilegeFunc := func() (string, error) { privilegeFunc := func() (string, error) {
return "a-auth-header", nil return "a-auth-header", nil
@ -84,7 +84,7 @@ func TestImagePushWithUnauthorizedErrorAndAnotherUnauthorizedError(t *testing.T)
func TestImagePushWithPrivilegedFuncNoError(t *testing.T) { func TestImagePushWithPrivilegedFuncNoError(t *testing.T) {
expectedURL := "/images/myimage/push" expectedURL := "/images/myimage/push"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }
@ -149,7 +149,7 @@ func TestImagePushWithoutErrors(t *testing.T) {
} }
for _, pullCase := range pullCases { for _, pullCase := range pullCases {
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
expectedURL := fmt.Sprintf(expectedURLFormat, pullCase.expectedImage) expectedURL := fmt.Sprintf(expectedURLFormat, pullCase.expectedImage)
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)

View File

@ -15,7 +15,7 @@ import (
func TestImageRemoveError(t *testing.T) { func TestImageRemoveError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.ImageRemove(context.Background(), "image_id", types.ImageRemoveOptions{}) _, err := client.ImageRemove(context.Background(), "image_id", types.ImageRemoveOptions{})
@ -49,7 +49,7 @@ func TestImageRemove(t *testing.T) {
} }
for _, removeCase := range removeCases { for _, removeCase := range removeCases {
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -15,7 +15,7 @@ import (
func TestImageSaveError(t *testing.T) { func TestImageSaveError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.ImageSave(context.Background(), []string{"nothing"}) _, err := client.ImageSave(context.Background(), []string{"nothing"})
if err == nil || err.Error() != "Error response from daemon: Server error" { if err == nil || err.Error() != "Error response from daemon: Server error" {
@ -26,7 +26,7 @@ func TestImageSaveError(t *testing.T) {
func TestImageSave(t *testing.T) { func TestImageSave(t *testing.T) {
expectedURL := "/images/get" expectedURL := "/images/get"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(r *http.Request) (*http.Response, error) { client: newMockClient(func(r *http.Request) (*http.Response, error) {
if !strings.HasPrefix(r.URL.Path, expectedURL) { if !strings.HasPrefix(r.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, r.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, r.URL)
} }

View File

@ -18,7 +18,7 @@ import (
func TestImageSearchAnyError(t *testing.T) { func TestImageSearchAnyError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.ImageSearch(context.Background(), "some-image", types.ImageSearchOptions{}) _, err := client.ImageSearch(context.Background(), "some-image", types.ImageSearchOptions{})
if err == nil || err.Error() != "Error response from daemon: Server error" { if err == nil || err.Error() != "Error response from daemon: Server error" {
@ -28,7 +28,7 @@ func TestImageSearchAnyError(t *testing.T) {
func TestImageSearchStatusUnauthorizedError(t *testing.T) { func TestImageSearchStatusUnauthorizedError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusUnauthorized, "Unauthorized error")), client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")),
} }
_, err := client.ImageSearch(context.Background(), "some-image", types.ImageSearchOptions{}) _, err := client.ImageSearch(context.Background(), "some-image", types.ImageSearchOptions{})
if err == nil || err.Error() != "Error response from daemon: Unauthorized error" { if err == nil || err.Error() != "Error response from daemon: Unauthorized error" {
@ -38,7 +38,7 @@ func TestImageSearchStatusUnauthorizedError(t *testing.T) {
func TestImageSearchWithUnauthorizedErrorAndPrivilegeFuncError(t *testing.T) { func TestImageSearchWithUnauthorizedErrorAndPrivilegeFuncError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusUnauthorized, "Unauthorized error")), client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")),
} }
privilegeFunc := func() (string, error) { privilegeFunc := func() (string, error) {
return "", fmt.Errorf("Error requesting privilege") return "", fmt.Errorf("Error requesting privilege")
@ -53,7 +53,7 @@ func TestImageSearchWithUnauthorizedErrorAndPrivilegeFuncError(t *testing.T) {
func TestImageSearchWithUnauthorizedErrorAndAnotherUnauthorizedError(t *testing.T) { func TestImageSearchWithUnauthorizedErrorAndAnotherUnauthorizedError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusUnauthorized, "Unauthorized error")), client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")),
} }
privilegeFunc := func() (string, error) { privilegeFunc := func() (string, error) {
return "a-auth-header", nil return "a-auth-header", nil
@ -69,7 +69,7 @@ func TestImageSearchWithUnauthorizedErrorAndAnotherUnauthorizedError(t *testing.
func TestImageSearchWithPrivilegedFuncNoError(t *testing.T) { func TestImageSearchWithPrivilegedFuncNoError(t *testing.T) {
expectedURL := "/images/search" expectedURL := "/images/search"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }
@ -126,7 +126,7 @@ func TestImageSearchWithoutErrors(t *testing.T) {
expectedFilters := `{"is-automated":{"true":true},"stars":{"3":true}}` expectedFilters := `{"is-automated":{"true":true},"stars":{"3":true}}`
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -13,7 +13,7 @@ import (
func TestImageTagError(t *testing.T) { func TestImageTagError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
err := client.ImageTag(context.Background(), "image_id", "repo:tag") err := client.ImageTag(context.Background(), "image_id", "repo:tag")
@ -26,7 +26,7 @@ func TestImageTagError(t *testing.T) {
// of distribution/reference package. // of distribution/reference package.
func TestImageTagInvalidReference(t *testing.T) { func TestImageTagInvalidReference(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
err := client.ImageTag(context.Background(), "image_id", "aa/asdf$$^/aa") err := client.ImageTag(context.Background(), "image_id", "aa/asdf$$^/aa")
@ -93,7 +93,7 @@ func TestImageTag(t *testing.T) {
} }
for _, tagCase := range tagCases { for _, tagCase := range tagCases {
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -15,7 +15,7 @@ import (
func TestInfoServerError(t *testing.T) { func TestInfoServerError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.Info(context.Background()) _, err := client.Info(context.Background())
if err == nil || err.Error() != "Error response from daemon: Server error" { if err == nil || err.Error() != "Error response from daemon: Server error" {
@ -25,7 +25,7 @@ func TestInfoServerError(t *testing.T) {
func TestInfoInvalidResponseJSONError(t *testing.T) { func TestInfoInvalidResponseJSONError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
return &http.Response{ return &http.Response{
StatusCode: http.StatusOK, StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader([]byte("invalid json"))), Body: ioutil.NopCloser(bytes.NewReader([]byte("invalid json"))),
@ -41,7 +41,7 @@ func TestInfoInvalidResponseJSONError(t *testing.T) {
func TestInfo(t *testing.T) { func TestInfo(t *testing.T) {
expectedURL := "/info" expectedURL := "/info"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -17,7 +17,7 @@ import (
func TestNetworkConnectError(t *testing.T) { func TestNetworkConnectError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
err := client.NetworkConnect(context.Background(), "network_id", "container_id", nil) err := client.NetworkConnect(context.Background(), "network_id", "container_id", nil)
@ -30,7 +30,7 @@ func TestNetworkConnectEmptyNilEndpointSettings(t *testing.T) {
expectedURL := "/networks/network_id/connect" expectedURL := "/networks/network_id/connect"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }
@ -69,7 +69,7 @@ func TestNetworkConnect(t *testing.T) {
expectedURL := "/networks/network_id/connect" expectedURL := "/networks/network_id/connect"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -15,7 +15,7 @@ import (
func TestNetworkCreateError(t *testing.T) { func TestNetworkCreateError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.NetworkCreate(context.Background(), "mynetwork", types.NetworkCreate{}) _, err := client.NetworkCreate(context.Background(), "mynetwork", types.NetworkCreate{})
@ -28,7 +28,7 @@ func TestNetworkCreate(t *testing.T) {
expectedURL := "/networks/create" expectedURL := "/networks/create"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -15,7 +15,7 @@ import (
func TestNetworkDisconnectError(t *testing.T) { func TestNetworkDisconnectError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
err := client.NetworkDisconnect(context.Background(), "network_id", "container_id", false) err := client.NetworkDisconnect(context.Background(), "network_id", "container_id", false)
@ -28,7 +28,7 @@ func TestNetworkDisconnect(t *testing.T) {
expectedURL := "/networks/network_id/disconnect" expectedURL := "/networks/network_id/disconnect"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -15,7 +15,7 @@ import (
func TestNetworkInspectError(t *testing.T) { func TestNetworkInspectError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.NetworkInspect(context.Background(), "nothing") _, err := client.NetworkInspect(context.Background(), "nothing")
@ -26,7 +26,7 @@ func TestNetworkInspectError(t *testing.T) {
func TestNetworkInspectContainerNotFound(t *testing.T) { func TestNetworkInspectContainerNotFound(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusNotFound, "Server error")), client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
} }
_, err := client.NetworkInspect(context.Background(), "unknown") _, err := client.NetworkInspect(context.Background(), "unknown")
@ -38,7 +38,7 @@ func TestNetworkInspectContainerNotFound(t *testing.T) {
func TestNetworkInspect(t *testing.T) { func TestNetworkInspect(t *testing.T) {
expectedURL := "/networks/network_id" expectedURL := "/networks/network_id"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -16,7 +16,7 @@ import (
func TestNetworkListError(t *testing.T) { func TestNetworkListError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.NetworkList(context.Background(), types.NetworkListOptions{ _, err := client.NetworkList(context.Background(), types.NetworkListOptions{
@ -69,7 +69,7 @@ func TestNetworkList(t *testing.T) {
for _, listCase := range listCases { for _, listCase := range listCases {
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -13,7 +13,7 @@ import (
func TestNetworkRemoveError(t *testing.T) { func TestNetworkRemoveError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
err := client.NetworkRemove(context.Background(), "network_id") err := client.NetworkRemove(context.Background(), "network_id")
@ -26,7 +26,7 @@ func TestNetworkRemove(t *testing.T) {
expectedURL := "/networks/network_id" expectedURL := "/networks/network_id"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -15,7 +15,7 @@ import (
func TestNodeInspectError(t *testing.T) { func TestNodeInspectError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, _, err := client.NodeInspectWithRaw(context.Background(), "nothing") _, _, err := client.NodeInspectWithRaw(context.Background(), "nothing")
@ -26,7 +26,7 @@ func TestNodeInspectError(t *testing.T) {
func TestNodeInspectNodeNotFound(t *testing.T) { func TestNodeInspectNodeNotFound(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusNotFound, "Server error")), client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
} }
_, _, err := client.NodeInspectWithRaw(context.Background(), "unknown") _, _, err := client.NodeInspectWithRaw(context.Background(), "unknown")
@ -38,7 +38,7 @@ func TestNodeInspectNodeNotFound(t *testing.T) {
func TestNodeInspect(t *testing.T) { func TestNodeInspect(t *testing.T) {
expectedURL := "/nodes/node_id" expectedURL := "/nodes/node_id"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -17,7 +17,7 @@ import (
func TestNodeListError(t *testing.T) { func TestNodeListError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.NodeList(context.Background(), types.NodeListOptions{}) _, err := client.NodeList(context.Background(), types.NodeListOptions{})
@ -54,7 +54,7 @@ func TestNodeList(t *testing.T) {
} }
for _, listCase := range listCases { for _, listCase := range listCases {
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -15,7 +15,7 @@ import (
func TestNodeRemoveError(t *testing.T) { func TestNodeRemoveError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
err := client.NodeRemove(context.Background(), "node_id", types.NodeRemoveOptions{Force: false}) err := client.NodeRemove(context.Background(), "node_id", types.NodeRemoveOptions{Force: false})
@ -42,7 +42,7 @@ func TestNodeRemove(t *testing.T) {
for _, removeCase := range removeCases { for _, removeCase := range removeCases {
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -15,7 +15,7 @@ import (
func TestNodeUpdateError(t *testing.T) { func TestNodeUpdateError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
err := client.NodeUpdate(context.Background(), "node_id", swarm.Version{}, swarm.NodeSpec{}) err := client.NodeUpdate(context.Background(), "node_id", swarm.Version{}, swarm.NodeSpec{})
@ -28,7 +28,7 @@ func TestNodeUpdate(t *testing.T) {
expectedURL := "/nodes/node_id/update" expectedURL := "/nodes/node_id/update"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -15,7 +15,7 @@ import (
func TestPluginDisableError(t *testing.T) { func TestPluginDisableError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
err := client.PluginDisable(context.Background(), "plugin_name") err := client.PluginDisable(context.Background(), "plugin_name")
@ -28,7 +28,7 @@ func TestPluginDisable(t *testing.T) {
expectedURL := "/plugins/plugin_name/disable" expectedURL := "/plugins/plugin_name/disable"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -15,7 +15,7 @@ import (
func TestPluginEnableError(t *testing.T) { func TestPluginEnableError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
err := client.PluginEnable(context.Background(), "plugin_name") err := client.PluginEnable(context.Background(), "plugin_name")
@ -28,7 +28,7 @@ func TestPluginEnable(t *testing.T) {
expectedURL := "/plugins/plugin_name/enable" expectedURL := "/plugins/plugin_name/enable"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -17,7 +17,7 @@ import (
func TestPluginInspectError(t *testing.T) { func TestPluginInspectError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, _, err := client.PluginInspectWithRaw(context.Background(), "nothing") _, _, err := client.PluginInspectWithRaw(context.Background(), "nothing")
@ -29,7 +29,7 @@ func TestPluginInspectError(t *testing.T) {
func TestPluginInspect(t *testing.T) { func TestPluginInspect(t *testing.T) {
expectedURL := "/plugins/plugin_name" expectedURL := "/plugins/plugin_name"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -17,7 +17,7 @@ import (
func TestPluginListError(t *testing.T) { func TestPluginListError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.PluginList(context.Background()) _, err := client.PluginList(context.Background())
@ -29,7 +29,7 @@ func TestPluginListError(t *testing.T) {
func TestPluginList(t *testing.T) { func TestPluginList(t *testing.T) {
expectedURL := "/plugins" expectedURL := "/plugins"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -15,7 +15,7 @@ import (
func TestPluginPushError(t *testing.T) { func TestPluginPushError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
err := client.PluginPush(context.Background(), "plugin_name", "") err := client.PluginPush(context.Background(), "plugin_name", "")
@ -28,7 +28,7 @@ func TestPluginPush(t *testing.T) {
expectedURL := "/plugins/plugin_name" expectedURL := "/plugins/plugin_name"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -17,7 +17,7 @@ import (
func TestPluginRemoveError(t *testing.T) { func TestPluginRemoveError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
err := client.PluginRemove(context.Background(), "plugin_name", types.PluginRemoveOptions{}) err := client.PluginRemove(context.Background(), "plugin_name", types.PluginRemoveOptions{})
@ -30,7 +30,7 @@ func TestPluginRemove(t *testing.T) {
expectedURL := "/plugins/plugin_name" expectedURL := "/plugins/plugin_name"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -15,7 +15,7 @@ import (
func TestPluginSetError(t *testing.T) { func TestPluginSetError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
err := client.PluginSet(context.Background(), "plugin_name", []string{}) err := client.PluginSet(context.Background(), "plugin_name", []string{})
@ -28,7 +28,7 @@ func TestPluginSet(t *testing.T) {
expectedURL := "/plugins/plugin_name/set" expectedURL := "/plugins/plugin_name/set"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -13,9 +13,9 @@ import (
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/versions" "github.com/docker/docker/api/types/versions"
"github.com/docker/docker/client/transport/cancellable"
"github.com/pkg/errors" "github.com/pkg/errors"
"golang.org/x/net/context" "golang.org/x/net/context"
"golang.org/x/net/context/ctxhttp"
) )
// serverResponse is a wrapper for http API responses. // serverResponse is a wrapper for http API responses.
@ -98,20 +98,27 @@ func (cli *Client) sendClientRequest(ctx context.Context, method, path string, q
// need a valid and meaningful host name. (See #189) // need a valid and meaningful host name. (See #189)
req.Host = "docker" req.Host = "docker"
} }
scheme, err := resolveScheme(cli.client.Transport)
if err != nil {
return serverResp, err
}
req.URL.Host = cli.addr req.URL.Host = cli.addr
req.URL.Scheme = cli.transport.Scheme() req.URL.Scheme = scheme
if expectedPayload && req.Header.Get("Content-Type") == "" { if expectedPayload && req.Header.Get("Content-Type") == "" {
req.Header.Set("Content-Type", "text/plain") req.Header.Set("Content-Type", "text/plain")
} }
resp, err := cancellable.Do(ctx, cli.transport, req) resp, err := ctxhttp.Do(ctx, cli.client, req)
if err != nil { if err != nil {
if !cli.transport.Secure() && strings.Contains(err.Error(), "malformed HTTP response") {
if scheme == "https" && strings.Contains(err.Error(), "malformed HTTP response") {
return serverResp, fmt.Errorf("%v.\n* Are you trying to connect to a TLS-enabled daemon without TLS?", err) return serverResp, fmt.Errorf("%v.\n* Are you trying to connect to a TLS-enabled daemon without TLS?", err)
} }
if cli.transport.Secure() && strings.Contains(err.Error(), "bad certificate") { if scheme == "https" && strings.Contains(err.Error(), "bad certificate") {
return serverResp, fmt.Errorf("The server probably has client authentication (--tlsverify) enabled. Please check your TLS client certification settings: %v", err) return serverResp, fmt.Errorf("The server probably has client authentication (--tlsverify) enabled. Please check your TLS client certification settings: %v", err)
} }

View File

@ -50,7 +50,7 @@ func TestSetHostHeader(t *testing.T) {
} }
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, testURL) { if !strings.HasPrefix(req.URL.Path, testURL) {
return nil, fmt.Errorf("Test Case #%d: Expected URL %q, got %q", c, testURL, req.URL) return nil, fmt.Errorf("Test Case #%d: Expected URL %q, got %q", c, testURL, req.URL)
} }
@ -65,6 +65,7 @@ func TestSetHostHeader(t *testing.T) {
Body: ioutil.NopCloser(bytes.NewReader(([]byte("")))), Body: ioutil.NopCloser(bytes.NewReader(([]byte("")))),
}, nil }, nil
}), }),
proto: proto, proto: proto,
addr: addr, addr: addr,
basePath: basePath, basePath: basePath,
@ -82,7 +83,7 @@ func TestSetHostHeader(t *testing.T) {
// errors returned as JSON // errors returned as JSON
func TestPlainTextError(t *testing.T) { func TestPlainTextError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, plainTextErrorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(plainTextErrorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.ContainerList(context.Background(), types.ContainerListOptions{}) _, err := client.ContainerList(context.Background(), types.ContainerListOptions{})
if err == nil || err.Error() != "Error response from daemon: Server error" { if err == nil || err.Error() != "Error response from daemon: Server error" {

View File

@ -16,7 +16,7 @@ import (
func TestServiceCreateError(t *testing.T) { func TestServiceCreateError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.ServiceCreate(context.Background(), swarm.ServiceSpec{}, types.ServiceCreateOptions{}) _, err := client.ServiceCreate(context.Background(), swarm.ServiceSpec{}, types.ServiceCreateOptions{})
if err == nil || err.Error() != "Error response from daemon: Server error" { if err == nil || err.Error() != "Error response from daemon: Server error" {
@ -27,7 +27,7 @@ func TestServiceCreateError(t *testing.T) {
func TestServiceCreate(t *testing.T) { func TestServiceCreate(t *testing.T) {
expectedURL := "/services/create" expectedURL := "/services/create"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -15,7 +15,7 @@ import (
func TestServiceInspectError(t *testing.T) { func TestServiceInspectError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, _, err := client.ServiceInspectWithRaw(context.Background(), "nothing") _, _, err := client.ServiceInspectWithRaw(context.Background(), "nothing")
@ -26,7 +26,7 @@ func TestServiceInspectError(t *testing.T) {
func TestServiceInspectServiceNotFound(t *testing.T) { func TestServiceInspectServiceNotFound(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusNotFound, "Server error")), client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
} }
_, _, err := client.ServiceInspectWithRaw(context.Background(), "unknown") _, _, err := client.ServiceInspectWithRaw(context.Background(), "unknown")
@ -38,7 +38,7 @@ func TestServiceInspectServiceNotFound(t *testing.T) {
func TestServiceInspect(t *testing.T) { func TestServiceInspect(t *testing.T) {
expectedURL := "/services/service_id" expectedURL := "/services/service_id"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -17,7 +17,7 @@ import (
func TestServiceListError(t *testing.T) { func TestServiceListError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.ServiceList(context.Background(), types.ServiceListOptions{}) _, err := client.ServiceList(context.Background(), types.ServiceListOptions{})
@ -54,7 +54,7 @@ func TestServiceList(t *testing.T) {
} }
for _, listCase := range listCases { for _, listCase := range listCases {
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -13,7 +13,7 @@ import (
func TestServiceRemoveError(t *testing.T) { func TestServiceRemoveError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
err := client.ServiceRemove(context.Background(), "service_id") err := client.ServiceRemove(context.Background(), "service_id")
@ -26,7 +26,7 @@ func TestServiceRemove(t *testing.T) {
expectedURL := "/services/service_id" expectedURL := "/services/service_id"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -16,7 +16,7 @@ import (
func TestServiceUpdateError(t *testing.T) { func TestServiceUpdateError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
err := client.ServiceUpdate(context.Background(), "service_id", swarm.Version{}, swarm.ServiceSpec{}, types.ServiceUpdateOptions{}) err := client.ServiceUpdate(context.Background(), "service_id", swarm.Version{}, swarm.ServiceSpec{}, types.ServiceUpdateOptions{})
@ -51,7 +51,7 @@ func TestServiceUpdate(t *testing.T) {
for _, updateCase := range updateCases { for _, updateCase := range updateCases {
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -15,7 +15,7 @@ import (
func TestSwarmInitError(t *testing.T) { func TestSwarmInitError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.SwarmInit(context.Background(), swarm.InitRequest{}) _, err := client.SwarmInit(context.Background(), swarm.InitRequest{})
@ -28,7 +28,7 @@ func TestSwarmInit(t *testing.T) {
expectedURL := "/swarm/init" expectedURL := "/swarm/init"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -15,7 +15,7 @@ import (
func TestSwarmInspectError(t *testing.T) { func TestSwarmInspectError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.SwarmInspect(context.Background()) _, err := client.SwarmInspect(context.Background())
@ -27,7 +27,7 @@ func TestSwarmInspectError(t *testing.T) {
func TestSwarmInspect(t *testing.T) { func TestSwarmInspect(t *testing.T) {
expectedURL := "/swarm" expectedURL := "/swarm"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -15,7 +15,7 @@ import (
func TestSwarmJoinError(t *testing.T) { func TestSwarmJoinError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
err := client.SwarmJoin(context.Background(), swarm.JoinRequest{}) err := client.SwarmJoin(context.Background(), swarm.JoinRequest{})
@ -28,7 +28,7 @@ func TestSwarmJoin(t *testing.T) {
expectedURL := "/swarm/join" expectedURL := "/swarm/join"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -13,7 +13,7 @@ import (
func TestSwarmLeaveError(t *testing.T) { func TestSwarmLeaveError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
err := client.SwarmLeave(context.Background(), false) err := client.SwarmLeave(context.Background(), false)
@ -40,7 +40,7 @@ func TestSwarmLeave(t *testing.T) {
for _, leaveCase := range leaveCases { for _, leaveCase := range leaveCases {
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -15,7 +15,7 @@ import (
func TestSwarmUpdateError(t *testing.T) { func TestSwarmUpdateError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
err := client.SwarmUpdate(context.Background(), swarm.Version{}, swarm.Spec{}, swarm.UpdateFlags{}) err := client.SwarmUpdate(context.Background(), swarm.Version{}, swarm.Spec{}, swarm.UpdateFlags{})
@ -28,7 +28,7 @@ func TestSwarmUpdate(t *testing.T) {
expectedURL := "/swarm/update" expectedURL := "/swarm/update"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -15,7 +15,7 @@ import (
func TestTaskInspectError(t *testing.T) { func TestTaskInspectError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, _, err := client.TaskInspectWithRaw(context.Background(), "nothing") _, _, err := client.TaskInspectWithRaw(context.Background(), "nothing")
@ -27,7 +27,7 @@ func TestTaskInspectError(t *testing.T) {
func TestTaskInspect(t *testing.T) { func TestTaskInspect(t *testing.T) {
expectedURL := "/tasks/task_id" expectedURL := "/tasks/task_id"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -17,7 +17,7 @@ import (
func TestTaskListError(t *testing.T) { func TestTaskListError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.TaskList(context.Background(), types.TaskListOptions{}) _, err := client.TaskList(context.Background(), types.TaskListOptions{})
@ -54,7 +54,7 @@ func TestTaskList(t *testing.T) {
} }
for _, listCase := range listCases { for _, listCase := range listCases {
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

51
transport.go Normal file
View File

@ -0,0 +1,51 @@
package client
import (
"crypto/tls"
"errors"
"net/http"
)
var errTLSConfigUnavailable = errors.New("TLSConfig unavailable")
// transportFunc allows us to inject a mock transport for testing. We define it
// here so we can detect the tlsconfig and return nil for only this type.
type transportFunc func(*http.Request) (*http.Response, error)
func (tf transportFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return tf(req)
}
// resolveTLSConfig attempts to resolve the tls configuration from the
// RoundTripper.
func resolveTLSConfig(transport http.RoundTripper) (*tls.Config, error) {
switch tr := transport.(type) {
case *http.Transport:
return tr.TLSClientConfig, nil
case transportFunc:
return nil, nil // detect this type for testing.
default:
return nil, errTLSConfigUnavailable
}
}
// resolveScheme detects a tls config on the transport and returns the
// appropriate http scheme.
//
// TODO(stevvooe): This isn't really the right way to write clients in Go.
// `NewClient` should probably only take an `*http.Client` and work from there.
// Unfortunately, the model of having a host-ish/url-thingy as the connection
// string has us confusing protocol and transport layers. We continue doing
// this to avoid breaking existing clients but this should be addressed.
func resolveScheme(transport http.RoundTripper) (string, error) {
c, err := resolveTLSConfig(transport)
if err != nil {
return "", err
}
if c != nil {
return "https", nil
}
return "http", nil
}

View File

@ -1,27 +0,0 @@
Copyright (c) 2009 The Go Authors. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View File

@ -1,23 +0,0 @@
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build go1.5
package cancellable
import (
"net/http"
"github.com/docker/docker/client/transport"
)
func canceler(client transport.Sender, req *http.Request) func() {
// TODO(djd): Respect any existing value of req.Cancel.
ch := make(chan struct{})
req.Cancel = ch
return func() {
close(ch)
}
}

View File

@ -1,27 +0,0 @@
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build !go1.5
package cancellable
import (
"net/http"
"github.com/docker/docker/client/transport"
)
type requestCanceler interface {
CancelRequest(*http.Request)
}
func canceler(client transport.Sender, req *http.Request) func() {
rc, ok := client.(requestCanceler)
if !ok {
return func() {}
}
return func() {
rc.CancelRequest(req)
}
}

View File

@ -1,115 +0,0 @@
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package cancellable provides helper function to cancel http requests.
package cancellable
import (
"io"
"net/http"
"sync"
"github.com/docker/docker/client/transport"
"golang.org/x/net/context"
)
func nop() {}
var (
testHookContextDoneBeforeHeaders = nop
testHookDoReturned = nop
testHookDidBodyClose = nop
)
// Do sends an HTTP request with the provided transport.Sender and returns an HTTP response.
// If the client is nil, http.DefaultClient is used.
// If the context is canceled or times out, ctx.Err() will be returned.
//
// FORK INFORMATION:
//
// This function deviates from the upstream version in golang.org/x/net/context/ctxhttp by
// taking a Sender interface rather than a *http.Client directly. That allow us to use
// this function with mocked clients and hijacked connections.
func Do(ctx context.Context, client transport.Sender, req *http.Request) (*http.Response, error) {
if client == nil {
client = http.DefaultClient
}
// Request cancelation changed in Go 1.5, see canceler.go and canceler_go14.go.
cancel := canceler(client, req)
type responseAndError struct {
resp *http.Response
err error
}
result := make(chan responseAndError, 1)
go func() {
resp, err := client.Do(req)
testHookDoReturned()
result <- responseAndError{resp, err}
}()
var resp *http.Response
select {
case <-ctx.Done():
testHookContextDoneBeforeHeaders()
cancel()
// Clean up after the goroutine calling client.Do:
go func() {
if r := <-result; r.resp != nil && r.resp.Body != nil {
testHookDidBodyClose()
r.resp.Body.Close()
}
}()
return nil, ctx.Err()
case r := <-result:
var err error
resp, err = r.resp, r.err
if err != nil {
return resp, err
}
}
c := make(chan struct{})
go func() {
select {
case <-ctx.Done():
cancel()
case <-c:
// The response's Body is closed.
}
}()
resp.Body = &notifyingReader{ReadCloser: resp.Body, notify: c}
return resp, nil
}
// notifyingReader is an io.ReadCloser that closes the notify channel after
// Close is called or a Read fails on the underlying ReadCloser.
type notifyingReader struct {
io.ReadCloser
notify chan<- struct{}
notifyOnce sync.Once
}
func (r *notifyingReader) Read(p []byte) (int, error) {
n, err := r.ReadCloser.Read(p)
if err != nil {
r.notifyOnce.Do(func() {
close(r.notify)
})
}
return n, err
}
func (r *notifyingReader) Close() error {
err := r.ReadCloser.Close()
r.notifyOnce.Do(func() {
close(r.notify)
})
return err
}

View File

@ -1,47 +0,0 @@
package transport
import (
"crypto/tls"
"net/http"
)
// Sender is an interface that clients must implement
// to be able to send requests to a remote connection.
type Sender interface {
// Do sends request to a remote endpoint.
Do(*http.Request) (*http.Response, error)
}
// Client is an interface that abstracts all remote connections.
type Client interface {
Sender
// Secure tells whether the connection is secure or not.
Secure() bool
// Scheme returns the connection protocol the client uses.
Scheme() string
// TLSConfig returns any TLS configuration the client uses.
TLSConfig() *tls.Config
}
// tlsInfo returns information about the TLS configuration.
type tlsInfo struct {
tlsConfig *tls.Config
}
// TLSConfig returns the TLS configuration.
func (t *tlsInfo) TLSConfig() *tls.Config {
return t.tlsConfig
}
// Scheme returns protocol scheme to use.
func (t *tlsInfo) Scheme() string {
if t.tlsConfig != nil {
return "https"
}
return "http"
}
// Secure returns true if there is a TLS configuration.
func (t *tlsInfo) Secure() bool {
return t.tlsConfig != nil
}

View File

@ -1,57 +0,0 @@
// Package transport provides function to send request to remote endpoints.
package transport
import (
"fmt"
"net/http"
"github.com/docker/go-connections/sockets"
)
// apiTransport holds information about the http transport to connect with the API.
type apiTransport struct {
*http.Client
*tlsInfo
transport *http.Transport
}
// NewTransportWithHTTP creates a new transport based on the provided proto, address and http client.
// It uses Docker's default http transport configuration if the client is nil.
// It does not modify the client's transport if it's not nil.
func NewTransportWithHTTP(proto, addr string, client *http.Client) (Client, error) {
var transport *http.Transport
if client != nil {
tr, ok := client.Transport.(*http.Transport)
if !ok {
return nil, fmt.Errorf("unable to verify TLS configuration, invalid transport %v", client.Transport)
}
transport = tr
} else {
transport = defaultTransport(proto, addr)
client = &http.Client{
Transport: transport,
}
}
return &apiTransport{
Client: client,
tlsInfo: &tlsInfo{transport.TLSClientConfig},
transport: transport,
}, nil
}
// CancelRequest stops a request execution.
func (a *apiTransport) CancelRequest(req *http.Request) {
a.transport.CancelRequest(req)
}
// defaultTransport creates a new http.Transport with Docker's
// default transport configuration.
func defaultTransport(proto, addr string) *http.Transport {
tr := new(http.Transport)
sockets.ConfigureTransport(tr, proto, addr)
return tr
}
var _ Client = &apiTransport{}

View File

@ -15,7 +15,7 @@ import (
func TestVolumeCreateError(t *testing.T) { func TestVolumeCreateError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.VolumeCreate(context.Background(), types.VolumeCreateRequest{}) _, err := client.VolumeCreate(context.Background(), types.VolumeCreateRequest{})
@ -28,7 +28,7 @@ func TestVolumeCreate(t *testing.T) {
expectedURL := "/volumes/create" expectedURL := "/volumes/create"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -15,7 +15,7 @@ import (
func TestVolumeInspectError(t *testing.T) { func TestVolumeInspectError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.VolumeInspect(context.Background(), "nothing") _, err := client.VolumeInspect(context.Background(), "nothing")
@ -26,7 +26,7 @@ func TestVolumeInspectError(t *testing.T) {
func TestVolumeInspectNotFound(t *testing.T) { func TestVolumeInspectNotFound(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusNotFound, "Server error")), client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
} }
_, err := client.VolumeInspect(context.Background(), "unknown") _, err := client.VolumeInspect(context.Background(), "unknown")
@ -38,7 +38,7 @@ func TestVolumeInspectNotFound(t *testing.T) {
func TestVolumeInspect(t *testing.T) { func TestVolumeInspect(t *testing.T) {
expectedURL := "/volumes/volume_id" expectedURL := "/volumes/volume_id"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -16,7 +16,7 @@ import (
func TestVolumeListError(t *testing.T) { func TestVolumeListError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
_, err := client.VolumeList(context.Background(), filters.NewArgs()) _, err := client.VolumeList(context.Background(), filters.NewArgs())
@ -59,7 +59,7 @@ func TestVolumeList(t *testing.T) {
for _, listCase := range listCases { for _, listCase := range listCases {
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }

View File

@ -13,7 +13,7 @@ import (
func TestVolumeRemoveError(t *testing.T) { func TestVolumeRemoveError(t *testing.T) {
client := &Client{ client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
} }
err := client.VolumeRemove(context.Background(), "volume_id", false) err := client.VolumeRemove(context.Background(), "volume_id", false)
@ -26,7 +26,7 @@ func TestVolumeRemove(t *testing.T) {
expectedURL := "/volumes/volume_id" expectedURL := "/volumes/volume_id"
client := &Client{ client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) { if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
} }