add better generate
Signed-off-by: Jess Frazelle <acidburn@microsoft.com>
This commit is contained in:
parent
3fc6abf56b
commit
cdd93563f5
5655 changed files with 1187011 additions and 392 deletions
35
vendor/github.com/docker/docker-ce/components/engine/client/README.md
generated
vendored
Normal file
35
vendor/github.com/docker/docker-ce/components/engine/client/README.md
generated
vendored
Normal file
|
@ -0,0 +1,35 @@
|
|||
# Go client for the Docker Engine API
|
||||
|
||||
The `docker` command uses this package to communicate with the daemon. It can also be used by your own Go applications to do anything the command-line interface does – running containers, pulling images, managing swarms, etc.
|
||||
|
||||
For example, to list running containers (the equivalent of `docker ps`):
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/client"
|
||||
)
|
||||
|
||||
func main() {
|
||||
cli, err := client.NewEnvClient()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
for _, container := range containers {
|
||||
fmt.Printf("%s %s\n", container.ID[:10], container.Image)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
[Full documentation is available on GoDoc.](https://godoc.org/github.com/docker/docker/client)
|
30
vendor/github.com/docker/docker-ce/components/engine/client/build_prune.go
generated
vendored
Normal file
30
vendor/github.com/docker/docker-ce/components/engine/client/build_prune.go
generated
vendored
Normal file
|
@ -0,0 +1,30 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// BuildCachePrune requests the daemon to delete unused cache data
|
||||
func (cli *Client) BuildCachePrune(ctx context.Context) (*types.BuildCachePruneReport, error) {
|
||||
if err := cli.NewVersionError("1.31", "build prune"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
report := types.BuildCachePruneReport{}
|
||||
|
||||
serverResp, err := cli.post(ctx, "/build/prune", nil, nil, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer ensureReaderClosed(serverResp)
|
||||
|
||||
if err := json.NewDecoder(serverResp.body).Decode(&report); err != nil {
|
||||
return nil, fmt.Errorf("Error retrieving disk usage: %v", err)
|
||||
}
|
||||
|
||||
return &report, nil
|
||||
}
|
13
vendor/github.com/docker/docker-ce/components/engine/client/checkpoint_create.go
generated
vendored
Normal file
13
vendor/github.com/docker/docker-ce/components/engine/client/checkpoint_create.go
generated
vendored
Normal file
|
@ -0,0 +1,13 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"github.com/docker/docker/api/types"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// CheckpointCreate creates a checkpoint from the given container with the given name
|
||||
func (cli *Client) CheckpointCreate(ctx context.Context, container string, options types.CheckpointCreateOptions) error {
|
||||
resp, err := cli.post(ctx, "/containers/"+container+"/checkpoints", nil, options, nil)
|
||||
ensureReaderClosed(resp)
|
||||
return err
|
||||
}
|
73
vendor/github.com/docker/docker-ce/components/engine/client/checkpoint_create_test.go
generated
vendored
Normal file
73
vendor/github.com/docker/docker-ce/components/engine/client/checkpoint_create_test.go
generated
vendored
Normal file
|
@ -0,0 +1,73 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestCheckpointCreateError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
err := client.CheckpointCreate(context.Background(), "nothing", types.CheckpointCreateOptions{
|
||||
CheckpointID: "noting",
|
||||
Exit: true,
|
||||
})
|
||||
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server Error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckpointCreate(t *testing.T) {
|
||||
expectedContainerID := "container_id"
|
||||
expectedCheckpointID := "checkpoint_id"
|
||||
expectedURL := "/containers/container_id/checkpoints"
|
||||
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
|
||||
if req.Method != "POST" {
|
||||
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
|
||||
}
|
||||
|
||||
createOptions := &types.CheckpointCreateOptions{}
|
||||
if err := json.NewDecoder(req.Body).Decode(createOptions); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if createOptions.CheckpointID != expectedCheckpointID {
|
||||
return nil, fmt.Errorf("expected CheckpointID to be 'checkpoint_id', got %v", createOptions.CheckpointID)
|
||||
}
|
||||
|
||||
if !createOptions.Exit {
|
||||
return nil, fmt.Errorf("expected Exit to be true")
|
||||
}
|
||||
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
|
||||
err := client.CheckpointCreate(context.Background(), expectedContainerID, types.CheckpointCreateOptions{
|
||||
CheckpointID: expectedCheckpointID,
|
||||
Exit: true,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
20
vendor/github.com/docker/docker-ce/components/engine/client/checkpoint_delete.go
generated
vendored
Normal file
20
vendor/github.com/docker/docker-ce/components/engine/client/checkpoint_delete.go
generated
vendored
Normal file
|
@ -0,0 +1,20 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// CheckpointDelete deletes the checkpoint with the given name from the given container
|
||||
func (cli *Client) CheckpointDelete(ctx context.Context, containerID string, options types.CheckpointDeleteOptions) error {
|
||||
query := url.Values{}
|
||||
if options.CheckpointDir != "" {
|
||||
query.Set("dir", options.CheckpointDir)
|
||||
}
|
||||
|
||||
resp, err := cli.delete(ctx, "/containers/"+containerID+"/checkpoints/"+options.CheckpointID, query, nil)
|
||||
ensureReaderClosed(resp)
|
||||
return err
|
||||
}
|
54
vendor/github.com/docker/docker-ce/components/engine/client/checkpoint_delete_test.go
generated
vendored
Normal file
54
vendor/github.com/docker/docker-ce/components/engine/client/checkpoint_delete_test.go
generated
vendored
Normal file
|
@ -0,0 +1,54 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestCheckpointDeleteError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
|
||||
err := client.CheckpointDelete(context.Background(), "container_id", types.CheckpointDeleteOptions{
|
||||
CheckpointID: "checkpoint_id",
|
||||
})
|
||||
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server Error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckpointDelete(t *testing.T) {
|
||||
expectedURL := "/containers/container_id/checkpoints/checkpoint_id"
|
||||
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
if req.Method != "DELETE" {
|
||||
return nil, fmt.Errorf("expected DELETE method, got %s", req.Method)
|
||||
}
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
|
||||
err := client.CheckpointDelete(context.Background(), "container_id", types.CheckpointDeleteOptions{
|
||||
CheckpointID: "checkpoint_id",
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
28
vendor/github.com/docker/docker-ce/components/engine/client/checkpoint_list.go
generated
vendored
Normal file
28
vendor/github.com/docker/docker-ce/components/engine/client/checkpoint_list.go
generated
vendored
Normal file
|
@ -0,0 +1,28 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/url"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// CheckpointList returns the checkpoints of the given container in the docker host
|
||||
func (cli *Client) CheckpointList(ctx context.Context, container string, options types.CheckpointListOptions) ([]types.Checkpoint, error) {
|
||||
var checkpoints []types.Checkpoint
|
||||
|
||||
query := url.Values{}
|
||||
if options.CheckpointDir != "" {
|
||||
query.Set("dir", options.CheckpointDir)
|
||||
}
|
||||
|
||||
resp, err := cli.get(ctx, "/containers/"+container+"/checkpoints", query, nil)
|
||||
if err != nil {
|
||||
return checkpoints, wrapResponseError(err, resp, "container", container)
|
||||
}
|
||||
|
||||
err = json.NewDecoder(resp.body).Decode(&checkpoints)
|
||||
ensureReaderClosed(resp)
|
||||
return checkpoints, err
|
||||
}
|
68
vendor/github.com/docker/docker-ce/components/engine/client/checkpoint_list_test.go
generated
vendored
Normal file
68
vendor/github.com/docker/docker-ce/components/engine/client/checkpoint_list_test.go
generated
vendored
Normal file
|
@ -0,0 +1,68 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestCheckpointListError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
|
||||
_, err := client.CheckpointList(context.Background(), "container_id", types.CheckpointListOptions{})
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server Error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckpointList(t *testing.T) {
|
||||
expectedURL := "/containers/container_id/checkpoints"
|
||||
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
content, err := json.Marshal([]types.Checkpoint{
|
||||
{
|
||||
Name: "checkpoint",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader(content)),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
|
||||
checkpoints, err := client.CheckpointList(context.Background(), "container_id", types.CheckpointListOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(checkpoints) != 1 {
|
||||
t.Fatalf("expected 1 checkpoint, got %v", checkpoints)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckpointListContainerNotFound(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
|
||||
}
|
||||
|
||||
_, err := client.CheckpointList(context.Background(), "unknown", types.CheckpointListOptions{})
|
||||
if err == nil || !IsErrNotFound(err) {
|
||||
t.Fatalf("expected a containerNotFound error, got %v", err)
|
||||
}
|
||||
}
|
397
vendor/github.com/docker/docker-ce/components/engine/client/client.go
generated
vendored
Normal file
397
vendor/github.com/docker/docker-ce/components/engine/client/client.go
generated
vendored
Normal file
|
@ -0,0 +1,397 @@
|
|||
/*
|
||||
Package client is a Go client for the Docker Engine API.
|
||||
|
||||
For more information about the Engine API, see the documentation:
|
||||
https://docs.docker.com/engine/reference/api/
|
||||
|
||||
Usage
|
||||
|
||||
You use the library by creating a client object and calling methods on it. The
|
||||
client can be created either from environment variables with NewEnvClient, or
|
||||
configured manually with NewClient.
|
||||
|
||||
For example, to list running containers (the equivalent of "docker ps"):
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/client"
|
||||
)
|
||||
|
||||
func main() {
|
||||
cli, err := client.NewEnvClient()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
for _, container := range containers {
|
||||
fmt.Printf("%s %s\n", container.ID[:10], container.Image)
|
||||
}
|
||||
}
|
||||
|
||||
*/
|
||||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/docker/docker/api"
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/versions"
|
||||
"github.com/docker/go-connections/sockets"
|
||||
"github.com/docker/go-connections/tlsconfig"
|
||||
"github.com/pkg/errors"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// ErrRedirect is the error returned by checkRedirect when the request is non-GET.
|
||||
var ErrRedirect = errors.New("unexpected redirect in response")
|
||||
|
||||
// Client is the API client that performs all operations
|
||||
// against a docker server.
|
||||
type Client struct {
|
||||
// scheme sets the scheme for the client
|
||||
scheme string
|
||||
// host holds the server address to connect to
|
||||
host string
|
||||
// proto holds the client protocol i.e. unix.
|
||||
proto string
|
||||
// addr holds the client address.
|
||||
addr string
|
||||
// basePath holds the path to prepend to the requests.
|
||||
basePath string
|
||||
// client used to send and receive http requests.
|
||||
client *http.Client
|
||||
// version of the server to talk to.
|
||||
version string
|
||||
// custom http headers configured by users.
|
||||
customHTTPHeaders map[string]string
|
||||
// manualOverride is set to true when the version was set by users.
|
||||
manualOverride bool
|
||||
}
|
||||
|
||||
// CheckRedirect specifies the policy for dealing with redirect responses:
|
||||
// If the request is non-GET return `ErrRedirect`. Otherwise use the last response.
|
||||
//
|
||||
// Go 1.8 changes behavior for HTTP redirects (specifically 301, 307, and 308) in the client .
|
||||
// The Docker client (and by extension docker API client) can be made to to send a request
|
||||
// like POST /containers//start where what would normally be in the name section of the URL is empty.
|
||||
// This triggers an HTTP 301 from the daemon.
|
||||
// In go 1.8 this 301 will be converted to a GET request, and ends up getting a 404 from the daemon.
|
||||
// This behavior change manifests in the client in that before the 301 was not followed and
|
||||
// the client did not generate an error, but now results in a message like Error response from daemon: page not found.
|
||||
func CheckRedirect(req *http.Request, via []*http.Request) error {
|
||||
if via[0].Method == http.MethodGet {
|
||||
return http.ErrUseLastResponse
|
||||
}
|
||||
return ErrRedirect
|
||||
}
|
||||
|
||||
// NewEnvClient initializes a new API client based on environment variables.
|
||||
// See FromEnv for a list of support environment variables.
|
||||
//
|
||||
// Deprecated: use NewClientWithOpts(FromEnv)
|
||||
func NewEnvClient() (*Client, error) {
|
||||
return NewClientWithOpts(FromEnv)
|
||||
}
|
||||
|
||||
// FromEnv configures the client with values from environment variables.
|
||||
//
|
||||
// Supported environment variables:
|
||||
// DOCKER_HOST to set the url to the docker server.
|
||||
// DOCKER_API_VERSION to set the version of the API to reach, leave empty for latest.
|
||||
// DOCKER_CERT_PATH to load the TLS certificates from.
|
||||
// DOCKER_TLS_VERIFY to enable or disable TLS verification, off by default.
|
||||
func FromEnv(c *Client) error {
|
||||
if dockerCertPath := os.Getenv("DOCKER_CERT_PATH"); dockerCertPath != "" {
|
||||
options := tlsconfig.Options{
|
||||
CAFile: filepath.Join(dockerCertPath, "ca.pem"),
|
||||
CertFile: filepath.Join(dockerCertPath, "cert.pem"),
|
||||
KeyFile: filepath.Join(dockerCertPath, "key.pem"),
|
||||
InsecureSkipVerify: os.Getenv("DOCKER_TLS_VERIFY") == "",
|
||||
}
|
||||
tlsc, err := tlsconfig.Client(options)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.client = &http.Client{
|
||||
Transport: &http.Transport{TLSClientConfig: tlsc},
|
||||
CheckRedirect: CheckRedirect,
|
||||
}
|
||||
}
|
||||
|
||||
if host := os.Getenv("DOCKER_HOST"); host != "" {
|
||||
if err := WithHost(host)(c); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if version := os.Getenv("DOCKER_API_VERSION"); version != "" {
|
||||
c.version = version
|
||||
c.manualOverride = true
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// WithTLSClientConfig applies a tls config to the client transport.
|
||||
func WithTLSClientConfig(cacertPath, certPath, keyPath string) func(*Client) error {
|
||||
return func(c *Client) error {
|
||||
opts := tlsconfig.Options{
|
||||
CAFile: cacertPath,
|
||||
CertFile: certPath,
|
||||
KeyFile: keyPath,
|
||||
ExclusiveRootPools: true,
|
||||
}
|
||||
config, err := tlsconfig.Client(opts)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to create tls config")
|
||||
}
|
||||
if transport, ok := c.client.Transport.(*http.Transport); ok {
|
||||
transport.TLSClientConfig = config
|
||||
return nil
|
||||
}
|
||||
return errors.Errorf("cannot apply tls config to transport: %T", c.client.Transport)
|
||||
}
|
||||
}
|
||||
|
||||
// WithDialer applies the dialer.DialContext to the client transport. This can be
|
||||
// used to set the Timeout and KeepAlive settings of the client.
|
||||
func WithDialer(dialer *net.Dialer) func(*Client) error {
|
||||
return func(c *Client) error {
|
||||
if transport, ok := c.client.Transport.(*http.Transport); ok {
|
||||
transport.DialContext = dialer.DialContext
|
||||
return nil
|
||||
}
|
||||
return errors.Errorf("cannot apply dialer to transport: %T", c.client.Transport)
|
||||
}
|
||||
}
|
||||
|
||||
// WithVersion overrides the client version with the specified one
|
||||
func WithVersion(version string) func(*Client) error {
|
||||
return func(c *Client) error {
|
||||
c.version = version
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithHost overrides the client host with the specified one.
|
||||
func WithHost(host string) func(*Client) error {
|
||||
return func(c *Client) error {
|
||||
hostURL, err := ParseHostURL(host)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.host = host
|
||||
c.proto = hostURL.Scheme
|
||||
c.addr = hostURL.Host
|
||||
c.basePath = hostURL.Path
|
||||
if transport, ok := c.client.Transport.(*http.Transport); ok {
|
||||
return sockets.ConfigureTransport(transport, c.proto, c.addr)
|
||||
}
|
||||
return errors.Errorf("cannot apply host to transport: %T", c.client.Transport)
|
||||
}
|
||||
}
|
||||
|
||||
// WithHTTPClient overrides the client http client with the specified one
|
||||
func WithHTTPClient(client *http.Client) func(*Client) error {
|
||||
return func(c *Client) error {
|
||||
if client != nil {
|
||||
c.client = client
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithHTTPHeaders overrides the client default http headers
|
||||
func WithHTTPHeaders(headers map[string]string) func(*Client) error {
|
||||
return func(c *Client) error {
|
||||
c.customHTTPHeaders = headers
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// NewClientWithOpts initializes a new API client with default values. It takes functors
|
||||
// to modify values when creating it, like `NewClientWithOpts(WithVersion(…))`
|
||||
// It also initializes the custom http headers to add to each request.
|
||||
//
|
||||
// It won't send any version information if the version number is empty. It is
|
||||
// highly recommended that you set a version or your client may break if the
|
||||
// server is upgraded.
|
||||
func NewClientWithOpts(ops ...func(*Client) error) (*Client, error) {
|
||||
client, err := defaultHTTPClient(DefaultDockerHost)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c := &Client{
|
||||
host: DefaultDockerHost,
|
||||
version: api.DefaultVersion,
|
||||
scheme: "http",
|
||||
client: client,
|
||||
proto: defaultProto,
|
||||
addr: defaultAddr,
|
||||
}
|
||||
|
||||
for _, op := range ops {
|
||||
if err := op(c); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if _, ok := c.client.Transport.(http.RoundTripper); !ok {
|
||||
return nil, fmt.Errorf("unable to verify TLS configuration, invalid transport %v", c.client.Transport)
|
||||
}
|
||||
tlsConfig := resolveTLSConfig(c.client.Transport)
|
||||
if tlsConfig != nil {
|
||||
// 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.
|
||||
c.scheme = "https"
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func defaultHTTPClient(host string) (*http.Client, error) {
|
||||
url, err := ParseHostURL(host)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
transport := new(http.Transport)
|
||||
sockets.ConfigureTransport(transport, url.Scheme, url.Host)
|
||||
return &http.Client{
|
||||
Transport: transport,
|
||||
CheckRedirect: CheckRedirect,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// NewClient initializes a new API client for the given host and API version.
|
||||
// It uses the given http client as transport.
|
||||
// It also initializes the custom http headers to add to each request.
|
||||
//
|
||||
// It won't send any version information if the version number is empty. It is
|
||||
// highly recommended that you set a version or your client may break if the
|
||||
// server is upgraded.
|
||||
// Deprecated: use NewClientWithOpts
|
||||
func NewClient(host string, version string, client *http.Client, httpHeaders map[string]string) (*Client, error) {
|
||||
return NewClientWithOpts(WithHost(host), WithVersion(version), WithHTTPClient(client), WithHTTPHeaders(httpHeaders))
|
||||
}
|
||||
|
||||
// Close the transport used by the client
|
||||
func (cli *Client) Close() error {
|
||||
if t, ok := cli.client.Transport.(*http.Transport); ok {
|
||||
t.CloseIdleConnections()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// getAPIPath returns the versioned request path to call the api.
|
||||
// It appends the query parameters to the path if they are not empty.
|
||||
func (cli *Client) getAPIPath(p string, query url.Values) string {
|
||||
var apiPath string
|
||||
if cli.version != "" {
|
||||
v := strings.TrimPrefix(cli.version, "v")
|
||||
apiPath = path.Join(cli.basePath, "/v"+v, p)
|
||||
} else {
|
||||
apiPath = path.Join(cli.basePath, p)
|
||||
}
|
||||
return (&url.URL{Path: apiPath, RawQuery: query.Encode()}).String()
|
||||
}
|
||||
|
||||
// ClientVersion returns the API version used by this client.
|
||||
func (cli *Client) ClientVersion() string {
|
||||
return cli.version
|
||||
}
|
||||
|
||||
// NegotiateAPIVersion queries the API and updates the version to match the
|
||||
// API version. Any errors are silently ignored.
|
||||
func (cli *Client) NegotiateAPIVersion(ctx context.Context) {
|
||||
ping, _ := cli.Ping(ctx)
|
||||
cli.NegotiateAPIVersionPing(ping)
|
||||
}
|
||||
|
||||
// NegotiateAPIVersionPing updates the client version to match the Ping.APIVersion
|
||||
// if the ping version is less than the default version.
|
||||
func (cli *Client) NegotiateAPIVersionPing(p types.Ping) {
|
||||
if cli.manualOverride {
|
||||
return
|
||||
}
|
||||
|
||||
// try the latest version before versioning headers existed
|
||||
if p.APIVersion == "" {
|
||||
p.APIVersion = "1.24"
|
||||
}
|
||||
|
||||
// if the client is not initialized with a version, start with the latest supported version
|
||||
if cli.version == "" {
|
||||
cli.version = api.DefaultVersion
|
||||
}
|
||||
|
||||
// if server version is lower than the client version, downgrade
|
||||
if versions.LessThan(p.APIVersion, cli.version) {
|
||||
cli.version = p.APIVersion
|
||||
}
|
||||
}
|
||||
|
||||
// DaemonHost returns the host address used by the client
|
||||
func (cli *Client) DaemonHost() string {
|
||||
return cli.host
|
||||
}
|
||||
|
||||
// ParseHostURL parses a url string, validates the string is a host url, and
|
||||
// returns the parsed URL
|
||||
func ParseHostURL(host string) (*url.URL, error) {
|
||||
protoAddrParts := strings.SplitN(host, "://", 2)
|
||||
if len(protoAddrParts) == 1 {
|
||||
return nil, fmt.Errorf("unable to parse docker host `%s`", host)
|
||||
}
|
||||
|
||||
var basePath string
|
||||
proto, addr := protoAddrParts[0], protoAddrParts[1]
|
||||
if proto == "tcp" {
|
||||
parsed, err := url.Parse("tcp://" + addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
addr = parsed.Host
|
||||
basePath = parsed.Path
|
||||
}
|
||||
return &url.URL{
|
||||
Scheme: proto,
|
||||
Host: addr,
|
||||
Path: basePath,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// CustomHTTPHeaders returns the custom http headers stored by the client.
|
||||
func (cli *Client) CustomHTTPHeaders() map[string]string {
|
||||
m := make(map[string]string)
|
||||
for k, v := range cli.customHTTPHeaders {
|
||||
m[k] = v
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// SetCustomHTTPHeaders that will be set on every HTTP request made by the client.
|
||||
// Deprecated: use WithHTTPHeaders when creating the client.
|
||||
func (cli *Client) SetCustomHTTPHeaders(headers map[string]string) {
|
||||
cli.customHTTPHeaders = headers
|
||||
}
|
53
vendor/github.com/docker/docker-ce/components/engine/client/client_mock_test.go
generated
vendored
Normal file
53
vendor/github.com/docker/docker-ce/components/engine/client/client_mock_test.go
generated
vendored
Normal file
|
@ -0,0 +1,53 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
)
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
func newMockClient(doer func(*http.Request) (*http.Response, error)) *http.Client {
|
||||
return &http.Client{
|
||||
Transport: transportFunc(doer),
|
||||
}
|
||||
}
|
||||
|
||||
func errorMock(statusCode int, message string) func(req *http.Request) (*http.Response, error) {
|
||||
return func(req *http.Request) (*http.Response, error) {
|
||||
header := http.Header{}
|
||||
header.Set("Content-Type", "application/json")
|
||||
|
||||
body, err := json.Marshal(&types.ErrorResponse{
|
||||
Message: message,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &http.Response{
|
||||
StatusCode: statusCode,
|
||||
Body: ioutil.NopCloser(bytes.NewReader(body)),
|
||||
Header: header,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func plainTextErrorMock(statusCode int, message string) func(req *http.Request) (*http.Response, error) {
|
||||
return func(req *http.Request) (*http.Response, error) {
|
||||
return &http.Response{
|
||||
StatusCode: statusCode,
|
||||
Body: ioutil.NopCloser(bytes.NewReader([]byte(message))),
|
||||
}, nil
|
||||
}
|
||||
}
|
322
vendor/github.com/docker/docker-ce/components/engine/client/client_test.go
generated
vendored
Normal file
322
vendor/github.com/docker/docker-ce/components/engine/client/client_test.go
generated
vendored
Normal file
|
@ -0,0 +1,322 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"runtime"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/docker/api"
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/internal/testutil"
|
||||
"github.com/gotestyourself/gotestyourself/assert"
|
||||
is "github.com/gotestyourself/gotestyourself/assert/cmp"
|
||||
"github.com/gotestyourself/gotestyourself/env"
|
||||
"github.com/gotestyourself/gotestyourself/skip"
|
||||
)
|
||||
|
||||
func TestNewEnvClient(t *testing.T) {
|
||||
skip.If(t, runtime.GOOS == "windows")
|
||||
|
||||
testcases := []struct {
|
||||
doc string
|
||||
envs map[string]string
|
||||
expectedError string
|
||||
expectedVersion string
|
||||
}{
|
||||
{
|
||||
doc: "default api version",
|
||||
envs: map[string]string{},
|
||||
expectedVersion: api.DefaultVersion,
|
||||
},
|
||||
{
|
||||
doc: "invalid cert path",
|
||||
envs: map[string]string{
|
||||
"DOCKER_CERT_PATH": "invalid/path",
|
||||
},
|
||||
expectedError: "Could not load X509 key pair: open invalid/path/cert.pem: no such file or directory",
|
||||
},
|
||||
{
|
||||
doc: "default api version with cert path",
|
||||
envs: map[string]string{
|
||||
"DOCKER_CERT_PATH": "testdata/",
|
||||
},
|
||||
expectedVersion: api.DefaultVersion,
|
||||
},
|
||||
{
|
||||
doc: "default api version with cert path and tls verify",
|
||||
envs: map[string]string{
|
||||
"DOCKER_CERT_PATH": "testdata/",
|
||||
"DOCKER_TLS_VERIFY": "1",
|
||||
},
|
||||
expectedVersion: api.DefaultVersion,
|
||||
},
|
||||
{
|
||||
doc: "default api version with cert path and host",
|
||||
envs: map[string]string{
|
||||
"DOCKER_CERT_PATH": "testdata/",
|
||||
"DOCKER_HOST": "https://notaunixsocket",
|
||||
},
|
||||
expectedVersion: api.DefaultVersion,
|
||||
},
|
||||
{
|
||||
doc: "invalid docker host",
|
||||
envs: map[string]string{
|
||||
"DOCKER_HOST": "host",
|
||||
},
|
||||
expectedError: "unable to parse docker host `host`",
|
||||
},
|
||||
{
|
||||
doc: "invalid docker host, with good format",
|
||||
envs: map[string]string{
|
||||
"DOCKER_HOST": "invalid://url",
|
||||
},
|
||||
expectedVersion: api.DefaultVersion,
|
||||
},
|
||||
{
|
||||
doc: "override api version",
|
||||
envs: map[string]string{
|
||||
"DOCKER_API_VERSION": "1.22",
|
||||
},
|
||||
expectedVersion: "1.22",
|
||||
},
|
||||
}
|
||||
|
||||
defer env.PatchAll(t, nil)()
|
||||
for _, c := range testcases {
|
||||
env.PatchAll(t, c.envs)
|
||||
apiclient, err := NewEnvClient()
|
||||
if c.expectedError != "" {
|
||||
assert.Check(t, is.Error(err, c.expectedError), c.doc)
|
||||
} else {
|
||||
assert.Check(t, err, c.doc)
|
||||
version := apiclient.ClientVersion()
|
||||
assert.Check(t, is.Equal(c.expectedVersion, version), c.doc)
|
||||
}
|
||||
|
||||
if c.envs["DOCKER_TLS_VERIFY"] != "" {
|
||||
// pedantic checking that this is handled correctly
|
||||
tr := apiclient.client.Transport.(*http.Transport)
|
||||
assert.Assert(t, tr.TLSClientConfig != nil, c.doc)
|
||||
assert.Check(t, is.Equal(tr.TLSClientConfig.InsecureSkipVerify, false), c.doc)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAPIPath(t *testing.T) {
|
||||
testcases := []struct {
|
||||
version string
|
||||
path string
|
||||
query url.Values
|
||||
expected string
|
||||
}{
|
||||
{"", "/containers/json", nil, "/containers/json"},
|
||||
{"", "/containers/json", url.Values{}, "/containers/json"},
|
||||
{"", "/containers/json", url.Values{"s": []string{"c"}}, "/containers/json?s=c"},
|
||||
{"1.22", "/containers/json", nil, "/v1.22/containers/json"},
|
||||
{"1.22", "/containers/json", url.Values{}, "/v1.22/containers/json"},
|
||||
{"1.22", "/containers/json", url.Values{"s": []string{"c"}}, "/v1.22/containers/json?s=c"},
|
||||
{"v1.22", "/containers/json", nil, "/v1.22/containers/json"},
|
||||
{"v1.22", "/containers/json", url.Values{}, "/v1.22/containers/json"},
|
||||
{"v1.22", "/containers/json", url.Values{"s": []string{"c"}}, "/v1.22/containers/json?s=c"},
|
||||
{"v1.22", "/networks/kiwl$%^", nil, "/v1.22/networks/kiwl$%25%5E"},
|
||||
}
|
||||
|
||||
for _, testcase := range testcases {
|
||||
c := Client{version: testcase.version, basePath: "/"}
|
||||
actual := c.getAPIPath(testcase.path, testcase.query)
|
||||
assert.Check(t, is.Equal(actual, testcase.expected))
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseHostURL(t *testing.T) {
|
||||
testcases := []struct {
|
||||
host string
|
||||
expected *url.URL
|
||||
expectedErr string
|
||||
}{
|
||||
{
|
||||
host: "",
|
||||
expectedErr: "unable to parse docker host",
|
||||
},
|
||||
{
|
||||
host: "foobar",
|
||||
expectedErr: "unable to parse docker host",
|
||||
},
|
||||
{
|
||||
host: "foo://bar",
|
||||
expected: &url.URL{Scheme: "foo", Host: "bar"},
|
||||
},
|
||||
{
|
||||
host: "tcp://localhost:2476",
|
||||
expected: &url.URL{Scheme: "tcp", Host: "localhost:2476"},
|
||||
},
|
||||
{
|
||||
host: "tcp://localhost:2476/path",
|
||||
expected: &url.URL{Scheme: "tcp", Host: "localhost:2476", Path: "/path"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, testcase := range testcases {
|
||||
actual, err := ParseHostURL(testcase.host)
|
||||
if testcase.expectedErr != "" {
|
||||
testutil.ErrorContains(t, err, testcase.expectedErr)
|
||||
}
|
||||
assert.Check(t, is.DeepEqual(testcase.expected, actual))
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewEnvClientSetsDefaultVersion(t *testing.T) {
|
||||
defer env.PatchAll(t, map[string]string{
|
||||
"DOCKER_HOST": "",
|
||||
"DOCKER_API_VERSION": "",
|
||||
"DOCKER_TLS_VERIFY": "",
|
||||
"DOCKER_CERT_PATH": "",
|
||||
})()
|
||||
|
||||
client, err := NewEnvClient()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assert.Check(t, is.Equal(client.version, api.DefaultVersion))
|
||||
|
||||
expected := "1.22"
|
||||
os.Setenv("DOCKER_API_VERSION", expected)
|
||||
client, err = NewEnvClient()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assert.Check(t, is.Equal(expected, client.version))
|
||||
}
|
||||
|
||||
// TestNegotiateAPIVersionEmpty asserts that client.Client can
|
||||
// negotiate a compatible APIVersion when omitted
|
||||
func TestNegotiateAPIVersionEmpty(t *testing.T) {
|
||||
defer env.PatchAll(t, map[string]string{"DOCKER_API_VERSION": ""})
|
||||
|
||||
client, err := NewEnvClient()
|
||||
assert.NilError(t, err)
|
||||
|
||||
ping := types.Ping{
|
||||
APIVersion: "",
|
||||
OSType: "linux",
|
||||
Experimental: false,
|
||||
}
|
||||
|
||||
// set our version to something new
|
||||
client.version = "1.25"
|
||||
|
||||
// if no version from server, expect the earliest
|
||||
// version before APIVersion was implemented
|
||||
expected := "1.24"
|
||||
|
||||
// test downgrade
|
||||
client.NegotiateAPIVersionPing(ping)
|
||||
assert.Check(t, is.Equal(expected, client.version))
|
||||
}
|
||||
|
||||
// TestNegotiateAPIVersion asserts that client.Client can
|
||||
// negotiate a compatible APIVersion with the server
|
||||
func TestNegotiateAPIVersion(t *testing.T) {
|
||||
client, err := NewEnvClient()
|
||||
assert.NilError(t, err)
|
||||
|
||||
expected := "1.21"
|
||||
ping := types.Ping{
|
||||
APIVersion: expected,
|
||||
OSType: "linux",
|
||||
Experimental: false,
|
||||
}
|
||||
|
||||
// set our version to something new
|
||||
client.version = "1.22"
|
||||
|
||||
// test downgrade
|
||||
client.NegotiateAPIVersionPing(ping)
|
||||
assert.Check(t, is.Equal(expected, client.version))
|
||||
|
||||
// set the client version to something older, and verify that we keep the
|
||||
// original setting.
|
||||
expected = "1.20"
|
||||
client.version = expected
|
||||
client.NegotiateAPIVersionPing(ping)
|
||||
assert.Check(t, is.Equal(expected, client.version))
|
||||
|
||||
}
|
||||
|
||||
// TestNegotiateAPIVersionOverride asserts that we honor
|
||||
// the environment variable DOCKER_API_VERSION when negotiating versions
|
||||
func TestNegotiateAPVersionOverride(t *testing.T) {
|
||||
expected := "9.99"
|
||||
defer env.PatchAll(t, map[string]string{"DOCKER_API_VERSION": expected})()
|
||||
|
||||
client, err := NewEnvClient()
|
||||
assert.NilError(t, err)
|
||||
|
||||
ping := types.Ping{
|
||||
APIVersion: "1.24",
|
||||
OSType: "linux",
|
||||
Experimental: false,
|
||||
}
|
||||
|
||||
// test that we honored the env var
|
||||
client.NegotiateAPIVersionPing(ping)
|
||||
assert.Check(t, is.Equal(expected, client.version))
|
||||
}
|
||||
|
||||
type roundTripFunc func(*http.Request) (*http.Response, error)
|
||||
|
||||
func (rtf roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
return rtf(req)
|
||||
}
|
||||
|
||||
type bytesBufferClose struct {
|
||||
*bytes.Buffer
|
||||
}
|
||||
|
||||
func (bbc bytesBufferClose) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestClientRedirect(t *testing.T) {
|
||||
client := &http.Client{
|
||||
CheckRedirect: CheckRedirect,
|
||||
Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) {
|
||||
if req.URL.String() == "/bla" {
|
||||
return &http.Response{StatusCode: 404}, nil
|
||||
}
|
||||
return &http.Response{
|
||||
StatusCode: 301,
|
||||
Header: map[string][]string{"Location": {"/bla"}},
|
||||
Body: bytesBufferClose{bytes.NewBuffer(nil)},
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
|
||||
cases := []struct {
|
||||
httpMethod string
|
||||
expectedErr *url.Error
|
||||
statusCode int
|
||||
}{
|
||||
{http.MethodGet, nil, 301},
|
||||
{http.MethodPost, &url.Error{Op: "Post", URL: "/bla", Err: ErrRedirect}, 301},
|
||||
{http.MethodPut, &url.Error{Op: "Put", URL: "/bla", Err: ErrRedirect}, 301},
|
||||
{http.MethodDelete, &url.Error{Op: "Delete", URL: "/bla", Err: ErrRedirect}, 301},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
req, err := http.NewRequest(tc.httpMethod, "/redirectme", nil)
|
||||
assert.Check(t, err)
|
||||
resp, err := client.Do(req)
|
||||
assert.Check(t, is.Equal(tc.statusCode, resp.StatusCode))
|
||||
if tc.expectedErr == nil {
|
||||
assert.Check(t, is.Nil(err))
|
||||
} else {
|
||||
urlError, ok := err.(*url.Error)
|
||||
assert.Assert(t, ok, "%T is not *url.Error", err)
|
||||
assert.Check(t, is.Equal(*tc.expectedErr, *urlError))
|
||||
}
|
||||
}
|
||||
}
|
9
vendor/github.com/docker/docker-ce/components/engine/client/client_unix.go
generated
vendored
Normal file
9
vendor/github.com/docker/docker-ce/components/engine/client/client_unix.go
generated
vendored
Normal file
|
@ -0,0 +1,9 @@
|
|||
// +build linux freebsd openbsd darwin
|
||||
|
||||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
// DefaultDockerHost defines os specific default if DOCKER_HOST is unset
|
||||
const DefaultDockerHost = "unix:///var/run/docker.sock"
|
||||
|
||||
const defaultProto = "unix"
|
||||
const defaultAddr = "/var/run/docker.sock"
|
7
vendor/github.com/docker/docker-ce/components/engine/client/client_windows.go
generated
vendored
Normal file
7
vendor/github.com/docker/docker-ce/components/engine/client/client_windows.go
generated
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
// DefaultDockerHost defines os specific default if DOCKER_HOST is unset
|
||||
const DefaultDockerHost = "npipe:////./pipe/docker_engine"
|
||||
|
||||
const defaultProto = "npipe"
|
||||
const defaultAddr = "//./pipe/docker_engine"
|
25
vendor/github.com/docker/docker-ce/components/engine/client/config_create.go
generated
vendored
Normal file
25
vendor/github.com/docker/docker-ce/components/engine/client/config_create.go
generated
vendored
Normal file
|
@ -0,0 +1,25 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/swarm"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// ConfigCreate creates a new Config.
|
||||
func (cli *Client) ConfigCreate(ctx context.Context, config swarm.ConfigSpec) (types.ConfigCreateResponse, error) {
|
||||
var response types.ConfigCreateResponse
|
||||
if err := cli.NewVersionError("1.30", "config create"); err != nil {
|
||||
return response, err
|
||||
}
|
||||
resp, err := cli.post(ctx, "/configs/create", nil, config, nil)
|
||||
if err != nil {
|
||||
return response, err
|
||||
}
|
||||
|
||||
err = json.NewDecoder(resp.body).Decode(&response)
|
||||
ensureReaderClosed(resp)
|
||||
return response, err
|
||||
}
|
70
vendor/github.com/docker/docker-ce/components/engine/client/config_create_test.go
generated
vendored
Normal file
70
vendor/github.com/docker/docker-ce/components/engine/client/config_create_test.go
generated
vendored
Normal file
|
@ -0,0 +1,70 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/swarm"
|
||||
"github.com/gotestyourself/gotestyourself/assert"
|
||||
is "github.com/gotestyourself/gotestyourself/assert/cmp"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestConfigCreateUnsupported(t *testing.T) {
|
||||
client := &Client{
|
||||
version: "1.29",
|
||||
client: &http.Client{},
|
||||
}
|
||||
_, err := client.ConfigCreate(context.Background(), swarm.ConfigSpec{})
|
||||
assert.Check(t, is.Error(err, `"config create" requires API version 1.30, but the Docker daemon API version is 1.29`))
|
||||
}
|
||||
|
||||
func TestConfigCreateError(t *testing.T) {
|
||||
client := &Client{
|
||||
version: "1.30",
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
_, err := client.ConfigCreate(context.Background(), swarm.ConfigSpec{})
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server Error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigCreate(t *testing.T) {
|
||||
expectedURL := "/v1.30/configs/create"
|
||||
client := &Client{
|
||||
version: "1.30",
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
if req.Method != "POST" {
|
||||
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
|
||||
}
|
||||
b, err := json.Marshal(types.ConfigCreateResponse{
|
||||
ID: "test_config",
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusCreated,
|
||||
Body: ioutil.NopCloser(bytes.NewReader(b)),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
|
||||
r, err := client.ConfigCreate(context.Background(), swarm.ConfigSpec{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if r.ID != "test_config" {
|
||||
t.Fatalf("expected `test_config`, got %s", r.ID)
|
||||
}
|
||||
}
|
36
vendor/github.com/docker/docker-ce/components/engine/client/config_inspect.go
generated
vendored
Normal file
36
vendor/github.com/docker/docker-ce/components/engine/client/config_inspect.go
generated
vendored
Normal file
|
@ -0,0 +1,36 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/docker/docker/api/types/swarm"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// ConfigInspectWithRaw returns the config information with raw data
|
||||
func (cli *Client) ConfigInspectWithRaw(ctx context.Context, id string) (swarm.Config, []byte, error) {
|
||||
if id == "" {
|
||||
return swarm.Config{}, nil, objectNotFoundError{object: "config", id: id}
|
||||
}
|
||||
if err := cli.NewVersionError("1.30", "config inspect"); err != nil {
|
||||
return swarm.Config{}, nil, err
|
||||
}
|
||||
resp, err := cli.get(ctx, "/configs/"+id, nil, nil)
|
||||
if err != nil {
|
||||
return swarm.Config{}, nil, wrapResponseError(err, resp, "config", id)
|
||||
}
|
||||
defer ensureReaderClosed(resp)
|
||||
|
||||
body, err := ioutil.ReadAll(resp.body)
|
||||
if err != nil {
|
||||
return swarm.Config{}, nil, err
|
||||
}
|
||||
|
||||
var config swarm.Config
|
||||
rdr := bytes.NewReader(body)
|
||||
err = json.NewDecoder(rdr).Decode(&config)
|
||||
|
||||
return config, body, err
|
||||
}
|
103
vendor/github.com/docker/docker-ce/components/engine/client/config_inspect_test.go
generated
vendored
Normal file
103
vendor/github.com/docker/docker-ce/components/engine/client/config_inspect_test.go
generated
vendored
Normal file
|
@ -0,0 +1,103 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/docker/api/types/swarm"
|
||||
"github.com/gotestyourself/gotestyourself/assert"
|
||||
is "github.com/gotestyourself/gotestyourself/assert/cmp"
|
||||
"github.com/pkg/errors"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestConfigInspectNotFound(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
|
||||
}
|
||||
|
||||
_, _, err := client.ConfigInspectWithRaw(context.Background(), "unknown")
|
||||
if err == nil || !IsErrNotFound(err) {
|
||||
t.Fatalf("expected a NotFoundError error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigInspectWithEmptyID(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
return nil, errors.New("should not make request")
|
||||
}),
|
||||
}
|
||||
_, _, err := client.ConfigInspectWithRaw(context.Background(), "")
|
||||
if !IsErrNotFound(err) {
|
||||
t.Fatalf("Expected NotFoundError, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigInspectUnsupported(t *testing.T) {
|
||||
client := &Client{
|
||||
version: "1.29",
|
||||
client: &http.Client{},
|
||||
}
|
||||
_, _, err := client.ConfigInspectWithRaw(context.Background(), "nothing")
|
||||
assert.Check(t, is.Error(err, `"config inspect" requires API version 1.30, but the Docker daemon API version is 1.29`))
|
||||
}
|
||||
|
||||
func TestConfigInspectError(t *testing.T) {
|
||||
client := &Client{
|
||||
version: "1.30",
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
|
||||
_, _, err := client.ConfigInspectWithRaw(context.Background(), "nothing")
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server Error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigInspectConfigNotFound(t *testing.T) {
|
||||
client := &Client{
|
||||
version: "1.30",
|
||||
client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
|
||||
}
|
||||
|
||||
_, _, err := client.ConfigInspectWithRaw(context.Background(), "unknown")
|
||||
if err == nil || !IsErrNotFound(err) {
|
||||
t.Fatalf("expected a configNotFoundError error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigInspect(t *testing.T) {
|
||||
expectedURL := "/v1.30/configs/config_id"
|
||||
client := &Client{
|
||||
version: "1.30",
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
content, err := json.Marshal(swarm.Config{
|
||||
ID: "config_id",
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader(content)),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
|
||||
configInspect, _, err := client.ConfigInspectWithRaw(context.Background(), "config_id")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if configInspect.ID != "config_id" {
|
||||
t.Fatalf("expected `config_id`, got %s", configInspect.ID)
|
||||
}
|
||||
}
|
38
vendor/github.com/docker/docker-ce/components/engine/client/config_list.go
generated
vendored
Normal file
38
vendor/github.com/docker/docker-ce/components/engine/client/config_list.go
generated
vendored
Normal file
|
@ -0,0 +1,38 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/url"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
"github.com/docker/docker/api/types/swarm"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// ConfigList returns the list of configs.
|
||||
func (cli *Client) ConfigList(ctx context.Context, options types.ConfigListOptions) ([]swarm.Config, error) {
|
||||
if err := cli.NewVersionError("1.30", "config list"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
query := url.Values{}
|
||||
|
||||
if options.Filters.Len() > 0 {
|
||||
filterJSON, err := filters.ToJSON(options.Filters)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
query.Set("filters", filterJSON)
|
||||
}
|
||||
|
||||
resp, err := cli.get(ctx, "/configs", query, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var configs []swarm.Config
|
||||
err = json.NewDecoder(resp.body).Decode(&configs)
|
||||
ensureReaderClosed(resp)
|
||||
return configs, err
|
||||
}
|
107
vendor/github.com/docker/docker-ce/components/engine/client/config_list_test.go
generated
vendored
Normal file
107
vendor/github.com/docker/docker-ce/components/engine/client/config_list_test.go
generated
vendored
Normal file
|
@ -0,0 +1,107 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
"github.com/docker/docker/api/types/swarm"
|
||||
"github.com/gotestyourself/gotestyourself/assert"
|
||||
is "github.com/gotestyourself/gotestyourself/assert/cmp"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestConfigListUnsupported(t *testing.T) {
|
||||
client := &Client{
|
||||
version: "1.29",
|
||||
client: &http.Client{},
|
||||
}
|
||||
_, err := client.ConfigList(context.Background(), types.ConfigListOptions{})
|
||||
assert.Check(t, is.Error(err, `"config list" requires API version 1.30, but the Docker daemon API version is 1.29`))
|
||||
}
|
||||
|
||||
func TestConfigListError(t *testing.T) {
|
||||
client := &Client{
|
||||
version: "1.30",
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
|
||||
_, err := client.ConfigList(context.Background(), types.ConfigListOptions{})
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server Error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigList(t *testing.T) {
|
||||
expectedURL := "/v1.30/configs"
|
||||
|
||||
filters := filters.NewArgs()
|
||||
filters.Add("label", "label1")
|
||||
filters.Add("label", "label2")
|
||||
|
||||
listCases := []struct {
|
||||
options types.ConfigListOptions
|
||||
expectedQueryParams map[string]string
|
||||
}{
|
||||
{
|
||||
options: types.ConfigListOptions{},
|
||||
expectedQueryParams: map[string]string{
|
||||
"filters": "",
|
||||
},
|
||||
},
|
||||
{
|
||||
options: types.ConfigListOptions{
|
||||
Filters: filters,
|
||||
},
|
||||
expectedQueryParams: map[string]string{
|
||||
"filters": `{"label":{"label1":true,"label2":true}}`,
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, listCase := range listCases {
|
||||
client := &Client{
|
||||
version: "1.30",
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
query := req.URL.Query()
|
||||
for key, expected := range listCase.expectedQueryParams {
|
||||
actual := query.Get(key)
|
||||
if actual != expected {
|
||||
return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
|
||||
}
|
||||
}
|
||||
content, err := json.Marshal([]swarm.Config{
|
||||
{
|
||||
ID: "config_id1",
|
||||
},
|
||||
{
|
||||
ID: "config_id2",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader(content)),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
|
||||
configs, err := client.ConfigList(context.Background(), listCase.options)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(configs) != 2 {
|
||||
t.Fatalf("expected 2 configs, got %v", configs)
|
||||
}
|
||||
}
|
||||
}
|
13
vendor/github.com/docker/docker-ce/components/engine/client/config_remove.go
generated
vendored
Normal file
13
vendor/github.com/docker/docker-ce/components/engine/client/config_remove.go
generated
vendored
Normal file
|
@ -0,0 +1,13 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import "golang.org/x/net/context"
|
||||
|
||||
// ConfigRemove removes a Config.
|
||||
func (cli *Client) ConfigRemove(ctx context.Context, id string) error {
|
||||
if err := cli.NewVersionError("1.30", "config remove"); err != nil {
|
||||
return err
|
||||
}
|
||||
resp, err := cli.delete(ctx, "/configs/"+id, nil, nil)
|
||||
ensureReaderClosed(resp)
|
||||
return wrapResponseError(err, resp, "config", id)
|
||||
}
|
60
vendor/github.com/docker/docker-ce/components/engine/client/config_remove_test.go
generated
vendored
Normal file
60
vendor/github.com/docker/docker-ce/components/engine/client/config_remove_test.go
generated
vendored
Normal file
|
@ -0,0 +1,60 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/gotestyourself/gotestyourself/assert"
|
||||
is "github.com/gotestyourself/gotestyourself/assert/cmp"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestConfigRemoveUnsupported(t *testing.T) {
|
||||
client := &Client{
|
||||
version: "1.29",
|
||||
client: &http.Client{},
|
||||
}
|
||||
err := client.ConfigRemove(context.Background(), "config_id")
|
||||
assert.Check(t, is.Error(err, `"config remove" requires API version 1.30, but the Docker daemon API version is 1.29`))
|
||||
}
|
||||
|
||||
func TestConfigRemoveError(t *testing.T) {
|
||||
client := &Client{
|
||||
version: "1.30",
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
|
||||
err := client.ConfigRemove(context.Background(), "config_id")
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server Error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigRemove(t *testing.T) {
|
||||
expectedURL := "/v1.30/configs/config_id"
|
||||
|
||||
client := &Client{
|
||||
version: "1.30",
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
if req.Method != "DELETE" {
|
||||
return nil, fmt.Errorf("expected DELETE method, got %s", req.Method)
|
||||
}
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader([]byte("body"))),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
|
||||
err := client.ConfigRemove(context.Background(), "config_id")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
21
vendor/github.com/docker/docker-ce/components/engine/client/config_update.go
generated
vendored
Normal file
21
vendor/github.com/docker/docker-ce/components/engine/client/config_update.go
generated
vendored
Normal file
|
@ -0,0 +1,21 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"strconv"
|
||||
|
||||
"github.com/docker/docker/api/types/swarm"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// ConfigUpdate attempts to update a Config
|
||||
func (cli *Client) ConfigUpdate(ctx context.Context, id string, version swarm.Version, config swarm.ConfigSpec) error {
|
||||
if err := cli.NewVersionError("1.30", "config update"); err != nil {
|
||||
return err
|
||||
}
|
||||
query := url.Values{}
|
||||
query.Set("version", strconv.FormatUint(version.Index, 10))
|
||||
resp, err := cli.post(ctx, "/configs/"+id+"/update", query, config, nil)
|
||||
ensureReaderClosed(resp)
|
||||
return err
|
||||
}
|
61
vendor/github.com/docker/docker-ce/components/engine/client/config_update_test.go
generated
vendored
Normal file
61
vendor/github.com/docker/docker-ce/components/engine/client/config_update_test.go
generated
vendored
Normal file
|
@ -0,0 +1,61 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/docker/api/types/swarm"
|
||||
"github.com/gotestyourself/gotestyourself/assert"
|
||||
is "github.com/gotestyourself/gotestyourself/assert/cmp"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestConfigUpdateUnsupported(t *testing.T) {
|
||||
client := &Client{
|
||||
version: "1.29",
|
||||
client: &http.Client{},
|
||||
}
|
||||
err := client.ConfigUpdate(context.Background(), "config_id", swarm.Version{}, swarm.ConfigSpec{})
|
||||
assert.Check(t, is.Error(err, `"config update" requires API version 1.30, but the Docker daemon API version is 1.29`))
|
||||
}
|
||||
|
||||
func TestConfigUpdateError(t *testing.T) {
|
||||
client := &Client{
|
||||
version: "1.30",
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
|
||||
err := client.ConfigUpdate(context.Background(), "config_id", swarm.Version{}, swarm.ConfigSpec{})
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server Error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigUpdate(t *testing.T) {
|
||||
expectedURL := "/v1.30/configs/config_id/update"
|
||||
|
||||
client := &Client{
|
||||
version: "1.30",
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
if req.Method != "POST" {
|
||||
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
|
||||
}
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader([]byte("body"))),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
|
||||
err := client.ConfigUpdate(context.Background(), "config_id", swarm.Version{}, swarm.ConfigSpec{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
57
vendor/github.com/docker/docker-ce/components/engine/client/container_attach.go
generated
vendored
Normal file
57
vendor/github.com/docker/docker-ce/components/engine/client/container_attach.go
generated
vendored
Normal file
|
@ -0,0 +1,57 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// ContainerAttach attaches a connection to a container in the server.
|
||||
// It returns a types.HijackedConnection with the hijacked connection
|
||||
// and the a reader to get output. It's up to the called to close
|
||||
// the hijacked connection by calling types.HijackedResponse.Close.
|
||||
//
|
||||
// The stream format on the response will be in one of two formats:
|
||||
//
|
||||
// If the container is using a TTY, there is only a single stream (stdout), and
|
||||
// data is copied directly from the container output stream, no extra
|
||||
// multiplexing or headers.
|
||||
//
|
||||
// If the container is *not* using a TTY, streams for stdout and stderr are
|
||||
// multiplexed.
|
||||
// The format of the multiplexed stream is as follows:
|
||||
//
|
||||
// [8]byte{STREAM_TYPE, 0, 0, 0, SIZE1, SIZE2, SIZE3, SIZE4}[]byte{OUTPUT}
|
||||
//
|
||||
// STREAM_TYPE can be 1 for stdout and 2 for stderr
|
||||
//
|
||||
// SIZE1, SIZE2, SIZE3, and SIZE4 are four bytes of uint32 encoded as big endian.
|
||||
// This is the size of OUTPUT.
|
||||
//
|
||||
// You can use github.com/docker/docker/pkg/stdcopy.StdCopy to demultiplex this
|
||||
// stream.
|
||||
func (cli *Client) ContainerAttach(ctx context.Context, container string, options types.ContainerAttachOptions) (types.HijackedResponse, error) {
|
||||
query := url.Values{}
|
||||
if options.Stream {
|
||||
query.Set("stream", "1")
|
||||
}
|
||||
if options.Stdin {
|
||||
query.Set("stdin", "1")
|
||||
}
|
||||
if options.Stdout {
|
||||
query.Set("stdout", "1")
|
||||
}
|
||||
if options.Stderr {
|
||||
query.Set("stderr", "1")
|
||||
}
|
||||
if options.DetachKeys != "" {
|
||||
query.Set("detachKeys", options.DetachKeys)
|
||||
}
|
||||
if options.Logs {
|
||||
query.Set("logs", "1")
|
||||
}
|
||||
|
||||
headers := map[string][]string{"Content-Type": {"text/plain"}}
|
||||
return cli.postHijacked(ctx, "/containers/"+container+"/attach", query, nil, headers)
|
||||
}
|
55
vendor/github.com/docker/docker-ce/components/engine/client/container_commit.go
generated
vendored
Normal file
55
vendor/github.com/docker/docker-ce/components/engine/client/container_commit.go
generated
vendored
Normal file
|
@ -0,0 +1,55 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/url"
|
||||
|
||||
"github.com/docker/distribution/reference"
|
||||
"github.com/docker/docker/api/types"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// ContainerCommit applies changes into a container and creates a new tagged image.
|
||||
func (cli *Client) ContainerCommit(ctx context.Context, container string, options types.ContainerCommitOptions) (types.IDResponse, error) {
|
||||
var repository, tag string
|
||||
if options.Reference != "" {
|
||||
ref, err := reference.ParseNormalizedNamed(options.Reference)
|
||||
if err != nil {
|
||||
return types.IDResponse{}, err
|
||||
}
|
||||
|
||||
if _, isCanonical := ref.(reference.Canonical); isCanonical {
|
||||
return types.IDResponse{}, errors.New("refusing to create a tag with a digest reference")
|
||||
}
|
||||
ref = reference.TagNameOnly(ref)
|
||||
|
||||
if tagged, ok := ref.(reference.Tagged); ok {
|
||||
tag = tagged.Tag()
|
||||
}
|
||||
repository = reference.FamiliarName(ref)
|
||||
}
|
||||
|
||||
query := url.Values{}
|
||||
query.Set("container", container)
|
||||
query.Set("repo", repository)
|
||||
query.Set("tag", tag)
|
||||
query.Set("comment", options.Comment)
|
||||
query.Set("author", options.Author)
|
||||
for _, change := range options.Changes {
|
||||
query.Add("changes", change)
|
||||
}
|
||||
if !options.Pause {
|
||||
query.Set("pause", "0")
|
||||
}
|
||||
|
||||
var response types.IDResponse
|
||||
resp, err := cli.post(ctx, "/commit", query, options.Config, nil)
|
||||
if err != nil {
|
||||
return response, err
|
||||
}
|
||||
|
||||
err = json.NewDecoder(resp.body).Decode(&response)
|
||||
ensureReaderClosed(resp)
|
||||
return response, err
|
||||
}
|
96
vendor/github.com/docker/docker-ce/components/engine/client/container_commit_test.go
generated
vendored
Normal file
96
vendor/github.com/docker/docker-ce/components/engine/client/container_commit_test.go
generated
vendored
Normal file
|
@ -0,0 +1,96 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestContainerCommitError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
_, err := client.ContainerCommit(context.Background(), "nothing", types.ContainerCommitOptions{})
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server Error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerCommit(t *testing.T) {
|
||||
expectedURL := "/commit"
|
||||
expectedContainerID := "container_id"
|
||||
specifiedReference := "repository_name:tag"
|
||||
expectedRepositoryName := "repository_name"
|
||||
expectedTag := "tag"
|
||||
expectedComment := "comment"
|
||||
expectedAuthor := "author"
|
||||
expectedChanges := []string{"change1", "change2"}
|
||||
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
query := req.URL.Query()
|
||||
containerID := query.Get("container")
|
||||
if containerID != expectedContainerID {
|
||||
return nil, fmt.Errorf("container id not set in URL query properly. Expected '%s', got %s", expectedContainerID, containerID)
|
||||
}
|
||||
repo := query.Get("repo")
|
||||
if repo != expectedRepositoryName {
|
||||
return nil, fmt.Errorf("container repo not set in URL query properly. Expected '%s', got %s", expectedRepositoryName, repo)
|
||||
}
|
||||
tag := query.Get("tag")
|
||||
if tag != expectedTag {
|
||||
return nil, fmt.Errorf("container tag not set in URL query properly. Expected '%s', got %s'", expectedTag, tag)
|
||||
}
|
||||
comment := query.Get("comment")
|
||||
if comment != expectedComment {
|
||||
return nil, fmt.Errorf("container comment not set in URL query properly. Expected '%s', got %s'", expectedComment, comment)
|
||||
}
|
||||
author := query.Get("author")
|
||||
if author != expectedAuthor {
|
||||
return nil, fmt.Errorf("container author not set in URL query properly. Expected '%s', got %s'", expectedAuthor, author)
|
||||
}
|
||||
pause := query.Get("pause")
|
||||
if pause != "0" {
|
||||
return nil, fmt.Errorf("container pause not set in URL query properly. Expected 'true', got %v'", pause)
|
||||
}
|
||||
changes := query["changes"]
|
||||
if len(changes) != len(expectedChanges) {
|
||||
return nil, fmt.Errorf("expected container changes size to be '%d', got %d", len(expectedChanges), len(changes))
|
||||
}
|
||||
b, err := json.Marshal(types.IDResponse{
|
||||
ID: "new_container_id",
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader(b)),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
|
||||
r, err := client.ContainerCommit(context.Background(), expectedContainerID, types.ContainerCommitOptions{
|
||||
Reference: specifiedReference,
|
||||
Comment: expectedComment,
|
||||
Author: expectedAuthor,
|
||||
Changes: expectedChanges,
|
||||
Pause: false,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if r.ID != "new_container_id" {
|
||||
t.Fatalf("expected `new_container_id`, got %s", r.ID)
|
||||
}
|
||||
}
|
102
vendor/github.com/docker/docker-ce/components/engine/client/container_copy.go
generated
vendored
Normal file
102
vendor/github.com/docker/docker-ce/components/engine/client/container_copy.go
generated
vendored
Normal file
|
@ -0,0 +1,102 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
)
|
||||
|
||||
// ContainerStatPath returns Stat information about a path inside the container filesystem.
|
||||
func (cli *Client) ContainerStatPath(ctx context.Context, containerID, path string) (types.ContainerPathStat, error) {
|
||||
query := url.Values{}
|
||||
query.Set("path", filepath.ToSlash(path)) // Normalize the paths used in the API.
|
||||
|
||||
urlStr := "/containers/" + containerID + "/archive"
|
||||
response, err := cli.head(ctx, urlStr, query, nil)
|
||||
if err != nil {
|
||||
return types.ContainerPathStat{}, wrapResponseError(err, response, "container:path", containerID+":"+path)
|
||||
}
|
||||
defer ensureReaderClosed(response)
|
||||
return getContainerPathStatFromHeader(response.header)
|
||||
}
|
||||
|
||||
// CopyToContainer copies content into the container filesystem.
|
||||
// Note that `content` must be a Reader for a TAR archive
|
||||
func (cli *Client) CopyToContainer(ctx context.Context, containerID, dstPath string, content io.Reader, options types.CopyToContainerOptions) error {
|
||||
query := url.Values{}
|
||||
query.Set("path", filepath.ToSlash(dstPath)) // Normalize the paths used in the API.
|
||||
// Do not allow for an existing directory to be overwritten by a non-directory and vice versa.
|
||||
if !options.AllowOverwriteDirWithFile {
|
||||
query.Set("noOverwriteDirNonDir", "true")
|
||||
}
|
||||
|
||||
if options.CopyUIDGID {
|
||||
query.Set("copyUIDGID", "true")
|
||||
}
|
||||
|
||||
apiPath := "/containers/" + containerID + "/archive"
|
||||
|
||||
response, err := cli.putRaw(ctx, apiPath, query, content, nil)
|
||||
if err != nil {
|
||||
return wrapResponseError(err, response, "container:path", containerID+":"+dstPath)
|
||||
}
|
||||
defer ensureReaderClosed(response)
|
||||
|
||||
if response.statusCode != http.StatusOK {
|
||||
return fmt.Errorf("unexpected status code from daemon: %d", response.statusCode)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CopyFromContainer gets the content from the container and returns it as a Reader
|
||||
// for a TAR archive to manipulate it in the host. It's up to the caller to close the reader.
|
||||
func (cli *Client) CopyFromContainer(ctx context.Context, containerID, srcPath string) (io.ReadCloser, types.ContainerPathStat, error) {
|
||||
query := make(url.Values, 1)
|
||||
query.Set("path", filepath.ToSlash(srcPath)) // Normalize the paths used in the API.
|
||||
|
||||
apiPath := "/containers/" + containerID + "/archive"
|
||||
response, err := cli.get(ctx, apiPath, query, nil)
|
||||
if err != nil {
|
||||
return nil, types.ContainerPathStat{}, wrapResponseError(err, response, "container:path", containerID+":"+srcPath)
|
||||
}
|
||||
|
||||
if response.statusCode != http.StatusOK {
|
||||
return nil, types.ContainerPathStat{}, fmt.Errorf("unexpected status code from daemon: %d", response.statusCode)
|
||||
}
|
||||
|
||||
// In order to get the copy behavior right, we need to know information
|
||||
// about both the source and the destination. The response headers include
|
||||
// stat info about the source that we can use in deciding exactly how to
|
||||
// copy it locally. Along with the stat info about the local destination,
|
||||
// we have everything we need to handle the multiple possibilities there
|
||||
// can be when copying a file/dir from one location to another file/dir.
|
||||
stat, err := getContainerPathStatFromHeader(response.header)
|
||||
if err != nil {
|
||||
return nil, stat, fmt.Errorf("unable to get resource stat from response: %s", err)
|
||||
}
|
||||
return response.body, stat, err
|
||||
}
|
||||
|
||||
func getContainerPathStatFromHeader(header http.Header) (types.ContainerPathStat, error) {
|
||||
var stat types.ContainerPathStat
|
||||
|
||||
encodedStat := header.Get("X-Docker-Container-Path-Stat")
|
||||
statDecoder := base64.NewDecoder(base64.StdEncoding, strings.NewReader(encodedStat))
|
||||
|
||||
err := json.NewDecoder(statDecoder).Decode(&stat)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("unable to decode container path stat header: %s", err)
|
||||
}
|
||||
|
||||
return stat, err
|
||||
}
|
274
vendor/github.com/docker/docker-ce/components/engine/client/container_copy_test.go
generated
vendored
Normal file
274
vendor/github.com/docker/docker-ce/components/engine/client/container_copy_test.go
generated
vendored
Normal file
|
@ -0,0 +1,274 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
)
|
||||
|
||||
func TestContainerStatPathError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
_, err := client.ContainerStatPath(context.Background(), "container_id", "path")
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerStatPathNotFoundError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusNotFound, "Not found")),
|
||||
}
|
||||
_, err := client.ContainerStatPath(context.Background(), "container_id", "path")
|
||||
if !IsErrNotFound(err) {
|
||||
t.Fatalf("expected a not found error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerStatPathNoHeaderError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
_, err := client.ContainerStatPath(context.Background(), "container_id", "path/to/file")
|
||||
if err == nil {
|
||||
t.Fatalf("expected an error, got nothing")
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerStatPath(t *testing.T) {
|
||||
expectedURL := "/containers/container_id/archive"
|
||||
expectedPath := "path/to/file"
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
if req.Method != "HEAD" {
|
||||
return nil, fmt.Errorf("expected HEAD method, got %s", req.Method)
|
||||
}
|
||||
query := req.URL.Query()
|
||||
path := query.Get("path")
|
||||
if path != expectedPath {
|
||||
return nil, fmt.Errorf("path not set in URL query properly")
|
||||
}
|
||||
content, err := json.Marshal(types.ContainerPathStat{
|
||||
Name: "name",
|
||||
Mode: 0700,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
base64PathStat := base64.StdEncoding.EncodeToString(content)
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
|
||||
Header: http.Header{
|
||||
"X-Docker-Container-Path-Stat": []string{base64PathStat},
|
||||
},
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
stat, err := client.ContainerStatPath(context.Background(), "container_id", expectedPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if stat.Name != "name" {
|
||||
t.Fatalf("expected container path stat name to be 'name', got '%s'", stat.Name)
|
||||
}
|
||||
if stat.Mode != 0700 {
|
||||
t.Fatalf("expected container path stat mode to be 0700, got '%v'", stat.Mode)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCopyToContainerError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
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" {
|
||||
t.Fatalf("expected a Server error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCopyToContainerNotFoundError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusNotFound, "Not found")),
|
||||
}
|
||||
err := client.CopyToContainer(context.Background(), "container_id", "path/to/file", bytes.NewReader([]byte("")), types.CopyToContainerOptions{})
|
||||
if !IsErrNotFound(err) {
|
||||
t.Fatalf("expected a not found error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCopyToContainerNotStatusOKError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusNoContent, "No content")),
|
||||
}
|
||||
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" {
|
||||
t.Fatalf("expected an unexpected status code error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCopyToContainer(t *testing.T) {
|
||||
expectedURL := "/containers/container_id/archive"
|
||||
expectedPath := "path/to/file"
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
if req.Method != "PUT" {
|
||||
return nil, fmt.Errorf("expected PUT method, got %s", req.Method)
|
||||
}
|
||||
query := req.URL.Query()
|
||||
path := query.Get("path")
|
||||
if path != expectedPath {
|
||||
return nil, fmt.Errorf("path not set in URL query properly, expected '%s', got %s", expectedPath, path)
|
||||
}
|
||||
noOverwriteDirNonDir := query.Get("noOverwriteDirNonDir")
|
||||
if noOverwriteDirNonDir != "true" {
|
||||
return nil, fmt.Errorf("noOverwriteDirNonDir not set in URL query properly, expected true, got %s", noOverwriteDirNonDir)
|
||||
}
|
||||
|
||||
content, err := ioutil.ReadAll(req.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := req.Body.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if string(content) != "content" {
|
||||
return nil, fmt.Errorf("expected content to be 'content', got %s", string(content))
|
||||
}
|
||||
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
err := client.CopyToContainer(context.Background(), "container_id", expectedPath, bytes.NewReader([]byte("content")), types.CopyToContainerOptions{
|
||||
AllowOverwriteDirWithFile: false,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCopyFromContainerError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
_, _, err := client.CopyFromContainer(context.Background(), "container_id", "path/to/file")
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCopyFromContainerNotFoundError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusNotFound, "Not found")),
|
||||
}
|
||||
_, _, err := client.CopyFromContainer(context.Background(), "container_id", "path/to/file")
|
||||
if !IsErrNotFound(err) {
|
||||
t.Fatalf("expected a not found error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCopyFromContainerNotStatusOKError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusNoContent, "No content")),
|
||||
}
|
||||
_, _, err := client.CopyFromContainer(context.Background(), "container_id", "path/to/file")
|
||||
if err == nil || err.Error() != "unexpected status code from daemon: 204" {
|
||||
t.Fatalf("expected an unexpected status code error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCopyFromContainerNoHeaderError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
_, _, err := client.CopyFromContainer(context.Background(), "container_id", "path/to/file")
|
||||
if err == nil {
|
||||
t.Fatalf("expected an error, got nothing")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCopyFromContainer(t *testing.T) {
|
||||
expectedURL := "/containers/container_id/archive"
|
||||
expectedPath := "path/to/file"
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
if req.Method != "GET" {
|
||||
return nil, fmt.Errorf("expected GET method, got %s", req.Method)
|
||||
}
|
||||
query := req.URL.Query()
|
||||
path := query.Get("path")
|
||||
if path != expectedPath {
|
||||
return nil, fmt.Errorf("path not set in URL query properly, expected '%s', got %s", expectedPath, path)
|
||||
}
|
||||
|
||||
headercontent, err := json.Marshal(types.ContainerPathStat{
|
||||
Name: "name",
|
||||
Mode: 0700,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
base64PathStat := base64.StdEncoding.EncodeToString(headercontent)
|
||||
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader([]byte("content"))),
|
||||
Header: http.Header{
|
||||
"X-Docker-Container-Path-Stat": []string{base64PathStat},
|
||||
},
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
r, stat, err := client.CopyFromContainer(context.Background(), "container_id", expectedPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if stat.Name != "name" {
|
||||
t.Fatalf("expected container path stat name to be 'name', got '%s'", stat.Name)
|
||||
}
|
||||
if stat.Mode != 0700 {
|
||||
t.Fatalf("expected container path stat mode to be 0700, got '%v'", stat.Mode)
|
||||
}
|
||||
content, err := ioutil.ReadAll(r)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := r.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(content) != "content" {
|
||||
t.Fatalf("expected content to be 'content', got %s", string(content))
|
||||
}
|
||||
}
|
56
vendor/github.com/docker/docker-ce/components/engine/client/container_create.go
generated
vendored
Normal file
56
vendor/github.com/docker/docker-ce/components/engine/client/container_create.go
generated
vendored
Normal file
|
@ -0,0 +1,56 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/docker/docker/api/types/network"
|
||||
"github.com/docker/docker/api/types/versions"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
type configWrapper struct {
|
||||
*container.Config
|
||||
HostConfig *container.HostConfig
|
||||
NetworkingConfig *network.NetworkingConfig
|
||||
}
|
||||
|
||||
// ContainerCreate creates a new container based in the given configuration.
|
||||
// It can be associated with a name, but it's not mandatory.
|
||||
func (cli *Client) ContainerCreate(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, containerName string) (container.ContainerCreateCreatedBody, error) {
|
||||
var response container.ContainerCreateCreatedBody
|
||||
|
||||
if err := cli.NewVersionError("1.25", "stop timeout"); config != nil && config.StopTimeout != nil && err != nil {
|
||||
return response, err
|
||||
}
|
||||
|
||||
// When using API 1.24 and under, the client is responsible for removing the container
|
||||
if hostConfig != nil && versions.LessThan(cli.ClientVersion(), "1.25") {
|
||||
hostConfig.AutoRemove = false
|
||||
}
|
||||
|
||||
query := url.Values{}
|
||||
if containerName != "" {
|
||||
query.Set("name", containerName)
|
||||
}
|
||||
|
||||
body := configWrapper{
|
||||
Config: config,
|
||||
HostConfig: hostConfig,
|
||||
NetworkingConfig: networkingConfig,
|
||||
}
|
||||
|
||||
serverResp, err := cli.post(ctx, "/containers/create", query, body, nil)
|
||||
if err != nil {
|
||||
if serverResp.statusCode == 404 && strings.Contains(err.Error(), "No such image") {
|
||||
return response, objectNotFoundError{object: "image", id: config.Image}
|
||||
}
|
||||
return response, err
|
||||
}
|
||||
|
||||
err = json.NewDecoder(serverResp.body).Decode(&response)
|
||||
ensureReaderClosed(serverResp)
|
||||
return response, err
|
||||
}
|
118
vendor/github.com/docker/docker-ce/components/engine/client/container_create_test.go
generated
vendored
Normal file
118
vendor/github.com/docker/docker-ce/components/engine/client/container_create_test.go
generated
vendored
Normal file
|
@ -0,0 +1,118 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestContainerCreateError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
_, err := client.ContainerCreate(context.Background(), nil, nil, nil, "nothing")
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server Error while testing StatusInternalServerError, got %v", err)
|
||||
}
|
||||
|
||||
// 404 doesn't automatically means an unknown image
|
||||
client = &Client{
|
||||
client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
|
||||
}
|
||||
_, err = client.ContainerCreate(context.Background(), nil, nil, nil, "nothing")
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server Error while testing StatusNotFound, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerCreateImageNotFound(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusNotFound, "No such image")),
|
||||
}
|
||||
_, err := client.ContainerCreate(context.Background(), &container.Config{Image: "unknown_image"}, nil, nil, "unknown")
|
||||
if err == nil || !IsErrNotFound(err) {
|
||||
t.Fatalf("expected an imageNotFound error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerCreateWithName(t *testing.T) {
|
||||
expectedURL := "/containers/create"
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
name := req.URL.Query().Get("name")
|
||||
if name != "container_name" {
|
||||
return nil, fmt.Errorf("container name not set in URL query properly. Expected `container_name`, got %s", name)
|
||||
}
|
||||
b, err := json.Marshal(container.ContainerCreateCreatedBody{
|
||||
ID: "container_id",
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader(b)),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
|
||||
r, err := client.ContainerCreate(context.Background(), nil, nil, nil, "container_name")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if r.ID != "container_id" {
|
||||
t.Fatalf("expected `container_id`, got %s", r.ID)
|
||||
}
|
||||
}
|
||||
|
||||
// TestContainerCreateAutoRemove validates that a client using API 1.24 always disables AutoRemove. When using API 1.25
|
||||
// or up, AutoRemove should not be disabled.
|
||||
func TestContainerCreateAutoRemove(t *testing.T) {
|
||||
autoRemoveValidator := func(expectedValue bool) func(req *http.Request) (*http.Response, error) {
|
||||
return func(req *http.Request) (*http.Response, error) {
|
||||
var config configWrapper
|
||||
|
||||
if err := json.NewDecoder(req.Body).Decode(&config); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if config.HostConfig.AutoRemove != expectedValue {
|
||||
return nil, fmt.Errorf("expected AutoRemove to be %v, got %v", expectedValue, config.HostConfig.AutoRemove)
|
||||
}
|
||||
b, err := json.Marshal(container.ContainerCreateCreatedBody{
|
||||
ID: "container_id",
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader(b)),
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
client := &Client{
|
||||
client: newMockClient(autoRemoveValidator(false)),
|
||||
version: "1.24",
|
||||
}
|
||||
if _, err := client.ContainerCreate(context.Background(), nil, &container.HostConfig{AutoRemove: true}, nil, ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
client = &Client{
|
||||
client: newMockClient(autoRemoveValidator(true)),
|
||||
version: "1.25",
|
||||
}
|
||||
if _, err := client.ContainerCreate(context.Background(), nil, &container.HostConfig{AutoRemove: true}, nil, ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
23
vendor/github.com/docker/docker-ce/components/engine/client/container_diff.go
generated
vendored
Normal file
23
vendor/github.com/docker/docker-ce/components/engine/client/container_diff.go
generated
vendored
Normal file
|
@ -0,0 +1,23 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/url"
|
||||
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// ContainerDiff shows differences in a container filesystem since it was started.
|
||||
func (cli *Client) ContainerDiff(ctx context.Context, containerID string) ([]container.ContainerChangeResponseItem, error) {
|
||||
var changes []container.ContainerChangeResponseItem
|
||||
|
||||
serverResp, err := cli.get(ctx, "/containers/"+containerID+"/changes", url.Values{}, nil)
|
||||
if err != nil {
|
||||
return changes, err
|
||||
}
|
||||
|
||||
err = json.NewDecoder(serverResp.body).Decode(&changes)
|
||||
ensureReaderClosed(serverResp)
|
||||
return changes, err
|
||||
}
|
61
vendor/github.com/docker/docker-ce/components/engine/client/container_diff_test.go
generated
vendored
Normal file
61
vendor/github.com/docker/docker-ce/components/engine/client/container_diff_test.go
generated
vendored
Normal file
|
@ -0,0 +1,61 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestContainerDiffError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
_, err := client.ContainerDiff(context.Background(), "nothing")
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server Error, got %v", err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestContainerDiff(t *testing.T) {
|
||||
expectedURL := "/containers/container_id/changes"
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
b, err := json.Marshal([]container.ContainerChangeResponseItem{
|
||||
{
|
||||
Kind: 0,
|
||||
Path: "/path/1",
|
||||
},
|
||||
{
|
||||
Kind: 1,
|
||||
Path: "/path/2",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader(b)),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
|
||||
changes, err := client.ContainerDiff(context.Background(), "container_id")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(changes) != 2 {
|
||||
t.Fatalf("expected an array of 2 changes, got %v", changes)
|
||||
}
|
||||
}
|
54
vendor/github.com/docker/docker-ce/components/engine/client/container_exec.go
generated
vendored
Normal file
54
vendor/github.com/docker/docker-ce/components/engine/client/container_exec.go
generated
vendored
Normal file
|
@ -0,0 +1,54 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// ContainerExecCreate creates a new exec configuration to run an exec process.
|
||||
func (cli *Client) ContainerExecCreate(ctx context.Context, container string, config types.ExecConfig) (types.IDResponse, error) {
|
||||
var response types.IDResponse
|
||||
|
||||
if err := cli.NewVersionError("1.25", "env"); len(config.Env) != 0 && err != nil {
|
||||
return response, err
|
||||
}
|
||||
|
||||
resp, err := cli.post(ctx, "/containers/"+container+"/exec", nil, config, nil)
|
||||
if err != nil {
|
||||
return response, err
|
||||
}
|
||||
err = json.NewDecoder(resp.body).Decode(&response)
|
||||
ensureReaderClosed(resp)
|
||||
return response, err
|
||||
}
|
||||
|
||||
// ContainerExecStart starts an exec process already created in the docker host.
|
||||
func (cli *Client) ContainerExecStart(ctx context.Context, execID string, config types.ExecStartCheck) error {
|
||||
resp, err := cli.post(ctx, "/exec/"+execID+"/start", nil, config, nil)
|
||||
ensureReaderClosed(resp)
|
||||
return err
|
||||
}
|
||||
|
||||
// ContainerExecAttach attaches a connection to an exec process in the server.
|
||||
// It returns a types.HijackedConnection with the hijacked connection
|
||||
// and the a reader to get output. It's up to the called to close
|
||||
// the hijacked connection by calling types.HijackedResponse.Close.
|
||||
func (cli *Client) ContainerExecAttach(ctx context.Context, execID string, config types.ExecStartCheck) (types.HijackedResponse, error) {
|
||||
headers := map[string][]string{"Content-Type": {"application/json"}}
|
||||
return cli.postHijacked(ctx, "/exec/"+execID+"/start", nil, config, headers)
|
||||
}
|
||||
|
||||
// ContainerExecInspect returns information about a specific exec process on the docker host.
|
||||
func (cli *Client) ContainerExecInspect(ctx context.Context, execID string) (types.ContainerExecInspect, error) {
|
||||
var response types.ContainerExecInspect
|
||||
resp, err := cli.get(ctx, "/exec/"+execID+"/json", nil, nil)
|
||||
if err != nil {
|
||||
return response, err
|
||||
}
|
||||
|
||||
err = json.NewDecoder(resp.body).Decode(&response)
|
||||
ensureReaderClosed(resp)
|
||||
return response, err
|
||||
}
|
157
vendor/github.com/docker/docker-ce/components/engine/client/container_exec_test.go
generated
vendored
Normal file
157
vendor/github.com/docker/docker-ce/components/engine/client/container_exec_test.go
generated
vendored
Normal file
|
@ -0,0 +1,157 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
)
|
||||
|
||||
func TestContainerExecCreateError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
_, err := client.ContainerExecCreate(context.Background(), "container_id", types.ExecConfig{})
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server Error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerExecCreate(t *testing.T) {
|
||||
expectedURL := "/containers/container_id/exec"
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
if req.Method != "POST" {
|
||||
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
|
||||
}
|
||||
// FIXME validate the content is the given ExecConfig ?
|
||||
if err := req.ParseForm(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
execConfig := &types.ExecConfig{}
|
||||
if err := json.NewDecoder(req.Body).Decode(execConfig); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if execConfig.User != "user" {
|
||||
return nil, fmt.Errorf("expected an execConfig with User == 'user', got %v", execConfig)
|
||||
}
|
||||
b, err := json.Marshal(types.IDResponse{
|
||||
ID: "exec_id",
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader(b)),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
|
||||
r, err := client.ContainerExecCreate(context.Background(), "container_id", types.ExecConfig{
|
||||
User: "user",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if r.ID != "exec_id" {
|
||||
t.Fatalf("expected `exec_id`, got %s", r.ID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerExecStartError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
err := client.ContainerExecStart(context.Background(), "nothing", types.ExecStartCheck{})
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server Error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerExecStart(t *testing.T) {
|
||||
expectedURL := "/exec/exec_id/start"
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
if err := req.ParseForm(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
execStartCheck := &types.ExecStartCheck{}
|
||||
if err := json.NewDecoder(req.Body).Decode(execStartCheck); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if execStartCheck.Tty || !execStartCheck.Detach {
|
||||
return nil, fmt.Errorf("expected execStartCheck{Detach:true,Tty:false}, got %v", execStartCheck)
|
||||
}
|
||||
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
|
||||
err := client.ContainerExecStart(context.Background(), "exec_id", types.ExecStartCheck{
|
||||
Detach: true,
|
||||
Tty: false,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerExecInspectError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
_, err := client.ContainerExecInspect(context.Background(), "nothing")
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server Error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerExecInspect(t *testing.T) {
|
||||
expectedURL := "/exec/exec_id/json"
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
b, err := json.Marshal(types.ContainerExecInspect{
|
||||
ExecID: "exec_id",
|
||||
ContainerID: "container_id",
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader(b)),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
|
||||
inspect, err := client.ContainerExecInspect(context.Background(), "exec_id")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if inspect.ExecID != "exec_id" {
|
||||
t.Fatalf("expected ExecID to be `exec_id`, got %s", inspect.ExecID)
|
||||
}
|
||||
if inspect.ContainerID != "container_id" {
|
||||
t.Fatalf("expected ContainerID `container_id`, got %s", inspect.ContainerID)
|
||||
}
|
||||
}
|
20
vendor/github.com/docker/docker-ce/components/engine/client/container_export.go
generated
vendored
Normal file
20
vendor/github.com/docker/docker-ce/components/engine/client/container_export.go
generated
vendored
Normal file
|
@ -0,0 +1,20 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/url"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// ContainerExport retrieves the raw contents of a container
|
||||
// and returns them as an io.ReadCloser. It's up to the caller
|
||||
// to close the stream.
|
||||
func (cli *Client) ContainerExport(ctx context.Context, containerID string) (io.ReadCloser, error) {
|
||||
serverResp, err := cli.get(ctx, "/containers/"+containerID+"/export", url.Values{}, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return serverResp.body, nil
|
||||
}
|
50
vendor/github.com/docker/docker-ce/components/engine/client/container_export_test.go
generated
vendored
Normal file
50
vendor/github.com/docker/docker-ce/components/engine/client/container_export_test.go
generated
vendored
Normal file
|
@ -0,0 +1,50 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestContainerExportError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
_, err := client.ContainerExport(context.Background(), "nothing")
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server Error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerExport(t *testing.T) {
|
||||
expectedURL := "/containers/container_id/export"
|
||||
client := &Client{
|
||||
client: newMockClient(func(r *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(r.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, r.URL)
|
||||
}
|
||||
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader([]byte("response"))),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
body, err := client.ContainerExport(context.Background(), "container_id")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer body.Close()
|
||||
content, err := ioutil.ReadAll(body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(content) != "response" {
|
||||
t.Fatalf("expected response to contain 'response', got %s", string(content))
|
||||
}
|
||||
}
|
53
vendor/github.com/docker/docker-ce/components/engine/client/container_inspect.go
generated
vendored
Normal file
53
vendor/github.com/docker/docker-ce/components/engine/client/container_inspect.go
generated
vendored
Normal file
|
@ -0,0 +1,53 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"net/url"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// ContainerInspect returns the container information.
|
||||
func (cli *Client) ContainerInspect(ctx context.Context, containerID string) (types.ContainerJSON, error) {
|
||||
if containerID == "" {
|
||||
return types.ContainerJSON{}, objectNotFoundError{object: "container", id: containerID}
|
||||
}
|
||||
serverResp, err := cli.get(ctx, "/containers/"+containerID+"/json", nil, nil)
|
||||
if err != nil {
|
||||
return types.ContainerJSON{}, wrapResponseError(err, serverResp, "container", containerID)
|
||||
}
|
||||
|
||||
var response types.ContainerJSON
|
||||
err = json.NewDecoder(serverResp.body).Decode(&response)
|
||||
ensureReaderClosed(serverResp)
|
||||
return response, err
|
||||
}
|
||||
|
||||
// ContainerInspectWithRaw returns the container information and its raw representation.
|
||||
func (cli *Client) ContainerInspectWithRaw(ctx context.Context, containerID string, getSize bool) (types.ContainerJSON, []byte, error) {
|
||||
if containerID == "" {
|
||||
return types.ContainerJSON{}, nil, objectNotFoundError{object: "container", id: containerID}
|
||||
}
|
||||
query := url.Values{}
|
||||
if getSize {
|
||||
query.Set("size", "1")
|
||||
}
|
||||
serverResp, err := cli.get(ctx, "/containers/"+containerID+"/json", query, nil)
|
||||
if err != nil {
|
||||
return types.ContainerJSON{}, nil, wrapResponseError(err, serverResp, "container", containerID)
|
||||
}
|
||||
defer ensureReaderClosed(serverResp)
|
||||
|
||||
body, err := ioutil.ReadAll(serverResp.body)
|
||||
if err != nil {
|
||||
return types.ContainerJSON{}, nil, err
|
||||
}
|
||||
|
||||
var response types.ContainerJSON
|
||||
rdr := bytes.NewReader(body)
|
||||
err = json.NewDecoder(rdr).Decode(&response)
|
||||
return response, body, err
|
||||
}
|
138
vendor/github.com/docker/docker-ce/components/engine/client/container_inspect_test.go
generated
vendored
Normal file
138
vendor/github.com/docker/docker-ce/components/engine/client/container_inspect_test.go
generated
vendored
Normal file
|
@ -0,0 +1,138 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/pkg/errors"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestContainerInspectError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
|
||||
_, err := client.ContainerInspect(context.Background(), "nothing")
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server Error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerInspectContainerNotFound(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
|
||||
}
|
||||
|
||||
_, err := client.ContainerInspect(context.Background(), "unknown")
|
||||
if err == nil || !IsErrNotFound(err) {
|
||||
t.Fatalf("expected a containerNotFound error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerInspectWithEmptyID(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
return nil, errors.New("should not make request")
|
||||
}),
|
||||
}
|
||||
_, _, err := client.ContainerInspectWithRaw(context.Background(), "", true)
|
||||
if !IsErrNotFound(err) {
|
||||
t.Fatalf("Expected NotFoundError, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerInspect(t *testing.T) {
|
||||
expectedURL := "/containers/container_id/json"
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
content, err := json.Marshal(types.ContainerJSON{
|
||||
ContainerJSONBase: &types.ContainerJSONBase{
|
||||
ID: "container_id",
|
||||
Image: "image",
|
||||
Name: "name",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader(content)),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
|
||||
r, err := client.ContainerInspect(context.Background(), "container_id")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if r.ID != "container_id" {
|
||||
t.Fatalf("expected `container_id`, got %s", r.ID)
|
||||
}
|
||||
if r.Image != "image" {
|
||||
t.Fatalf("expected `image`, got %s", r.Image)
|
||||
}
|
||||
if r.Name != "name" {
|
||||
t.Fatalf("expected `name`, got %s", r.Name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerInspectNode(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
content, err := json.Marshal(types.ContainerJSON{
|
||||
ContainerJSONBase: &types.ContainerJSONBase{
|
||||
ID: "container_id",
|
||||
Image: "image",
|
||||
Name: "name",
|
||||
Node: &types.ContainerNode{
|
||||
ID: "container_node_id",
|
||||
Addr: "container_node",
|
||||
Labels: map[string]string{"foo": "bar"},
|
||||
},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader(content)),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
|
||||
r, err := client.ContainerInspect(context.Background(), "container_id")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if r.ID != "container_id" {
|
||||
t.Fatalf("expected `container_id`, got %s", r.ID)
|
||||
}
|
||||
if r.Image != "image" {
|
||||
t.Fatalf("expected `image`, got %s", r.Image)
|
||||
}
|
||||
if r.Name != "name" {
|
||||
t.Fatalf("expected `name`, got %s", r.Name)
|
||||
}
|
||||
if r.Node.ID != "container_node_id" {
|
||||
t.Fatalf("expected `container_node_id`, got %s", r.Node.ID)
|
||||
}
|
||||
if r.Node.Addr != "container_node" {
|
||||
t.Fatalf("expected `container_node`, got %s", r.Node.Addr)
|
||||
}
|
||||
foo, ok := r.Node.Labels["foo"]
|
||||
if foo != "bar" || !ok {
|
||||
t.Fatalf("expected `bar` for label `foo`")
|
||||
}
|
||||
}
|
17
vendor/github.com/docker/docker-ce/components/engine/client/container_kill.go
generated
vendored
Normal file
17
vendor/github.com/docker/docker-ce/components/engine/client/container_kill.go
generated
vendored
Normal file
|
@ -0,0 +1,17 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// ContainerKill terminates the container process but does not remove the container from the docker host.
|
||||
func (cli *Client) ContainerKill(ctx context.Context, containerID, signal string) error {
|
||||
query := url.Values{}
|
||||
query.Set("signal", signal)
|
||||
|
||||
resp, err := cli.post(ctx, "/containers/"+containerID+"/kill", query, nil, nil)
|
||||
ensureReaderClosed(resp)
|
||||
return err
|
||||
}
|
46
vendor/github.com/docker/docker-ce/components/engine/client/container_kill_test.go
generated
vendored
Normal file
46
vendor/github.com/docker/docker-ce/components/engine/client/container_kill_test.go
generated
vendored
Normal file
|
@ -0,0 +1,46 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestContainerKillError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
err := client.ContainerKill(context.Background(), "nothing", "SIGKILL")
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server Error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerKill(t *testing.T) {
|
||||
expectedURL := "/containers/container_id/kill"
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
signal := req.URL.Query().Get("signal")
|
||||
if signal != "SIGKILL" {
|
||||
return nil, fmt.Errorf("signal not set in URL query properly. Expected 'SIGKILL', got %s", signal)
|
||||
}
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
|
||||
err := client.ContainerKill(context.Background(), "container_id", "SIGKILL")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
56
vendor/github.com/docker/docker-ce/components/engine/client/container_list.go
generated
vendored
Normal file
56
vendor/github.com/docker/docker-ce/components/engine/client/container_list.go
generated
vendored
Normal file
|
@ -0,0 +1,56 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/url"
|
||||
"strconv"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// ContainerList returns the list of containers in the docker host.
|
||||
func (cli *Client) ContainerList(ctx context.Context, options types.ContainerListOptions) ([]types.Container, error) {
|
||||
query := url.Values{}
|
||||
|
||||
if options.All {
|
||||
query.Set("all", "1")
|
||||
}
|
||||
|
||||
if options.Limit != -1 {
|
||||
query.Set("limit", strconv.Itoa(options.Limit))
|
||||
}
|
||||
|
||||
if options.Since != "" {
|
||||
query.Set("since", options.Since)
|
||||
}
|
||||
|
||||
if options.Before != "" {
|
||||
query.Set("before", options.Before)
|
||||
}
|
||||
|
||||
if options.Size {
|
||||
query.Set("size", "1")
|
||||
}
|
||||
|
||||
if options.Filters.Len() > 0 {
|
||||
filterJSON, err := filters.ToParamWithVersion(cli.version, options.Filters)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
query.Set("filters", filterJSON)
|
||||
}
|
||||
|
||||
resp, err := cli.get(ctx, "/containers/json", query, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var containers []types.Container
|
||||
err = json.NewDecoder(resp.body).Decode(&containers)
|
||||
ensureReaderClosed(resp)
|
||||
return containers, err
|
||||
}
|
96
vendor/github.com/docker/docker-ce/components/engine/client/container_list_test.go
generated
vendored
Normal file
96
vendor/github.com/docker/docker-ce/components/engine/client/container_list_test.go
generated
vendored
Normal file
|
@ -0,0 +1,96 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestContainerListError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
_, err := client.ContainerList(context.Background(), types.ContainerListOptions{})
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server Error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerList(t *testing.T) {
|
||||
expectedURL := "/containers/json"
|
||||
expectedFilters := `{"before":{"container":true},"label":{"label1":true,"label2":true}}`
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
query := req.URL.Query()
|
||||
all := query.Get("all")
|
||||
if all != "1" {
|
||||
return nil, fmt.Errorf("all not set in URL query properly. Expected '1', got %s", all)
|
||||
}
|
||||
limit := query.Get("limit")
|
||||
if limit != "0" {
|
||||
return nil, fmt.Errorf("limit should have not be present in query. Expected '0', got %s", limit)
|
||||
}
|
||||
since := query.Get("since")
|
||||
if since != "container" {
|
||||
return nil, fmt.Errorf("since not set in URL query properly. Expected 'container', got %s", since)
|
||||
}
|
||||
before := query.Get("before")
|
||||
if before != "" {
|
||||
return nil, fmt.Errorf("before should have not be present in query, go %s", before)
|
||||
}
|
||||
size := query.Get("size")
|
||||
if size != "1" {
|
||||
return nil, fmt.Errorf("size not set in URL query properly. Expected '1', got %s", size)
|
||||
}
|
||||
filters := query.Get("filters")
|
||||
if filters != expectedFilters {
|
||||
return nil, fmt.Errorf("expected filters incoherent '%v' with actual filters %v", expectedFilters, filters)
|
||||
}
|
||||
|
||||
b, err := json.Marshal([]types.Container{
|
||||
{
|
||||
ID: "container_id1",
|
||||
},
|
||||
{
|
||||
ID: "container_id2",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader(b)),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
|
||||
filters := filters.NewArgs()
|
||||
filters.Add("label", "label1")
|
||||
filters.Add("label", "label2")
|
||||
filters.Add("before", "container")
|
||||
containers, err := client.ContainerList(context.Background(), types.ContainerListOptions{
|
||||
Size: true,
|
||||
All: true,
|
||||
Since: "container",
|
||||
Filters: filters,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(containers) != 2 {
|
||||
t.Fatalf("expected 2 containers, got %v", containers)
|
||||
}
|
||||
}
|
80
vendor/github.com/docker/docker-ce/components/engine/client/container_logs.go
generated
vendored
Normal file
80
vendor/github.com/docker/docker-ce/components/engine/client/container_logs.go
generated
vendored
Normal file
|
@ -0,0 +1,80 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
timetypes "github.com/docker/docker/api/types/time"
|
||||
)
|
||||
|
||||
// ContainerLogs returns the logs generated by a container in an io.ReadCloser.
|
||||
// It's up to the caller to close the stream.
|
||||
//
|
||||
// The stream format on the response will be in one of two formats:
|
||||
//
|
||||
// If the container is using a TTY, there is only a single stream (stdout), and
|
||||
// data is copied directly from the container output stream, no extra
|
||||
// multiplexing or headers.
|
||||
//
|
||||
// If the container is *not* using a TTY, streams for stdout and stderr are
|
||||
// multiplexed.
|
||||
// The format of the multiplexed stream is as follows:
|
||||
//
|
||||
// [8]byte{STREAM_TYPE, 0, 0, 0, SIZE1, SIZE2, SIZE3, SIZE4}[]byte{OUTPUT}
|
||||
//
|
||||
// STREAM_TYPE can be 1 for stdout and 2 for stderr
|
||||
//
|
||||
// SIZE1, SIZE2, SIZE3, and SIZE4 are four bytes of uint32 encoded as big endian.
|
||||
// This is the size of OUTPUT.
|
||||
//
|
||||
// You can use github.com/docker/docker/pkg/stdcopy.StdCopy to demultiplex this
|
||||
// stream.
|
||||
func (cli *Client) ContainerLogs(ctx context.Context, container string, options types.ContainerLogsOptions) (io.ReadCloser, error) {
|
||||
query := url.Values{}
|
||||
if options.ShowStdout {
|
||||
query.Set("stdout", "1")
|
||||
}
|
||||
|
||||
if options.ShowStderr {
|
||||
query.Set("stderr", "1")
|
||||
}
|
||||
|
||||
if options.Since != "" {
|
||||
ts, err := timetypes.GetTimestamp(options.Since, time.Now())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
query.Set("since", ts)
|
||||
}
|
||||
|
||||
if options.Until != "" {
|
||||
ts, err := timetypes.GetTimestamp(options.Until, time.Now())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
query.Set("until", ts)
|
||||
}
|
||||
|
||||
if options.Timestamps {
|
||||
query.Set("timestamps", "1")
|
||||
}
|
||||
|
||||
if options.Details {
|
||||
query.Set("details", "1")
|
||||
}
|
||||
|
||||
if options.Follow {
|
||||
query.Set("follow", "1")
|
||||
}
|
||||
query.Set("tail", options.Tail)
|
||||
|
||||
resp, err := cli.get(ctx, "/containers/"+container+"/logs", query, nil)
|
||||
if err != nil {
|
||||
return nil, wrapResponseError(err, resp, "container", container)
|
||||
}
|
||||
return resp.body, nil
|
||||
}
|
157
vendor/github.com/docker/docker-ce/components/engine/client/container_logs_test.go
generated
vendored
Normal file
157
vendor/github.com/docker/docker-ce/components/engine/client/container_logs_test.go
generated
vendored
Normal file
|
@ -0,0 +1,157 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/internal/testutil"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestContainerLogsNotFoundError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusNotFound, "Not found")),
|
||||
}
|
||||
_, err := client.ContainerLogs(context.Background(), "container_id", types.ContainerLogsOptions{})
|
||||
if !IsErrNotFound(err) {
|
||||
t.Fatalf("expected a not found error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerLogsError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
_, err := client.ContainerLogs(context.Background(), "container_id", types.ContainerLogsOptions{})
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server Error, got %v", err)
|
||||
}
|
||||
_, err = client.ContainerLogs(context.Background(), "container_id", types.ContainerLogsOptions{
|
||||
Since: "2006-01-02TZ",
|
||||
})
|
||||
testutil.ErrorContains(t, err, `parsing time "2006-01-02TZ"`)
|
||||
_, err = client.ContainerLogs(context.Background(), "container_id", types.ContainerLogsOptions{
|
||||
Until: "2006-01-02TZ",
|
||||
})
|
||||
testutil.ErrorContains(t, err, `parsing time "2006-01-02TZ"`)
|
||||
}
|
||||
|
||||
func TestContainerLogs(t *testing.T) {
|
||||
expectedURL := "/containers/container_id/logs"
|
||||
cases := []struct {
|
||||
options types.ContainerLogsOptions
|
||||
expectedQueryParams map[string]string
|
||||
}{
|
||||
{
|
||||
expectedQueryParams: map[string]string{
|
||||
"tail": "",
|
||||
},
|
||||
},
|
||||
{
|
||||
options: types.ContainerLogsOptions{
|
||||
Tail: "any",
|
||||
},
|
||||
expectedQueryParams: map[string]string{
|
||||
"tail": "any",
|
||||
},
|
||||
},
|
||||
{
|
||||
options: types.ContainerLogsOptions{
|
||||
ShowStdout: true,
|
||||
ShowStderr: true,
|
||||
Timestamps: true,
|
||||
Details: true,
|
||||
Follow: true,
|
||||
},
|
||||
expectedQueryParams: map[string]string{
|
||||
"tail": "",
|
||||
"stdout": "1",
|
||||
"stderr": "1",
|
||||
"timestamps": "1",
|
||||
"details": "1",
|
||||
"follow": "1",
|
||||
},
|
||||
},
|
||||
{
|
||||
options: types.ContainerLogsOptions{
|
||||
// An complete invalid date, timestamp or go duration will be
|
||||
// passed as is
|
||||
Since: "invalid but valid",
|
||||
},
|
||||
expectedQueryParams: map[string]string{
|
||||
"tail": "",
|
||||
"since": "invalid but valid",
|
||||
},
|
||||
},
|
||||
{
|
||||
options: types.ContainerLogsOptions{
|
||||
// An complete invalid date, timestamp or go duration will be
|
||||
// passed as is
|
||||
Until: "invalid but valid",
|
||||
},
|
||||
expectedQueryParams: map[string]string{
|
||||
"tail": "",
|
||||
"until": "invalid but valid",
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, logCase := range cases {
|
||||
client := &Client{
|
||||
client: newMockClient(func(r *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(r.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, r.URL)
|
||||
}
|
||||
// Check query parameters
|
||||
query := r.URL.Query()
|
||||
for key, expected := range logCase.expectedQueryParams {
|
||||
actual := query.Get(key)
|
||||
if actual != expected {
|
||||
return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
|
||||
}
|
||||
}
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader([]byte("response"))),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
body, err := client.ContainerLogs(context.Background(), "container_id", logCase.options)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer body.Close()
|
||||
content, err := ioutil.ReadAll(body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(content) != "response" {
|
||||
t.Fatalf("expected response to contain 'response', got %s", string(content))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func ExampleClient_ContainerLogs_withTimeout() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
client, _ := NewEnvClient()
|
||||
reader, err := client.ContainerLogs(ctx, "container_id", types.ContainerLogsOptions{})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
_, err = io.Copy(os.Stdout, reader)
|
||||
if err != nil && err != io.EOF {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
10
vendor/github.com/docker/docker-ce/components/engine/client/container_pause.go
generated
vendored
Normal file
10
vendor/github.com/docker/docker-ce/components/engine/client/container_pause.go
generated
vendored
Normal file
|
@ -0,0 +1,10 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import "golang.org/x/net/context"
|
||||
|
||||
// ContainerPause pauses the main process of a given container without terminating it.
|
||||
func (cli *Client) ContainerPause(ctx context.Context, containerID string) error {
|
||||
resp, err := cli.post(ctx, "/containers/"+containerID+"/pause", nil, nil, nil)
|
||||
ensureReaderClosed(resp)
|
||||
return err
|
||||
}
|
41
vendor/github.com/docker/docker-ce/components/engine/client/container_pause_test.go
generated
vendored
Normal file
41
vendor/github.com/docker/docker-ce/components/engine/client/container_pause_test.go
generated
vendored
Normal file
|
@ -0,0 +1,41 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestContainerPauseError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
err := client.ContainerPause(context.Background(), "nothing")
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server Error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerPause(t *testing.T) {
|
||||
expectedURL := "/containers/container_id/pause"
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
err := client.ContainerPause(context.Background(), "container_id")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
36
vendor/github.com/docker/docker-ce/components/engine/client/container_prune.go
generated
vendored
Normal file
36
vendor/github.com/docker/docker-ce/components/engine/client/container_prune.go
generated
vendored
Normal file
|
@ -0,0 +1,36 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// ContainersPrune requests the daemon to delete unused data
|
||||
func (cli *Client) ContainersPrune(ctx context.Context, pruneFilters filters.Args) (types.ContainersPruneReport, error) {
|
||||
var report types.ContainersPruneReport
|
||||
|
||||
if err := cli.NewVersionError("1.25", "container prune"); err != nil {
|
||||
return report, err
|
||||
}
|
||||
|
||||
query, err := getFiltersQuery(pruneFilters)
|
||||
if err != nil {
|
||||
return report, err
|
||||
}
|
||||
|
||||
serverResp, err := cli.post(ctx, "/containers/prune", query, nil, nil)
|
||||
if err != nil {
|
||||
return report, err
|
||||
}
|
||||
defer ensureReaderClosed(serverResp)
|
||||
|
||||
if err := json.NewDecoder(serverResp.body).Decode(&report); err != nil {
|
||||
return report, fmt.Errorf("Error retrieving disk usage: %v", err)
|
||||
}
|
||||
|
||||
return report, nil
|
||||
}
|
125
vendor/github.com/docker/docker-ce/components/engine/client/container_prune_test.go
generated
vendored
Normal file
125
vendor/github.com/docker/docker-ce/components/engine/client/container_prune_test.go
generated
vendored
Normal file
|
@ -0,0 +1,125 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
"github.com/gotestyourself/gotestyourself/assert"
|
||||
is "github.com/gotestyourself/gotestyourself/assert/cmp"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestContainersPruneError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
version: "1.25",
|
||||
}
|
||||
|
||||
filters := filters.NewArgs()
|
||||
|
||||
_, err := client.ContainersPrune(context.Background(), filters)
|
||||
assert.Check(t, is.Error(err, "Error response from daemon: Server error"))
|
||||
}
|
||||
|
||||
func TestContainersPrune(t *testing.T) {
|
||||
expectedURL := "/v1.25/containers/prune"
|
||||
|
||||
danglingFilters := filters.NewArgs()
|
||||
danglingFilters.Add("dangling", "true")
|
||||
|
||||
noDanglingFilters := filters.NewArgs()
|
||||
noDanglingFilters.Add("dangling", "false")
|
||||
|
||||
danglingUntilFilters := filters.NewArgs()
|
||||
danglingUntilFilters.Add("dangling", "true")
|
||||
danglingUntilFilters.Add("until", "2016-12-15T14:00")
|
||||
|
||||
labelFilters := filters.NewArgs()
|
||||
labelFilters.Add("dangling", "true")
|
||||
labelFilters.Add("label", "label1=foo")
|
||||
labelFilters.Add("label", "label2!=bar")
|
||||
|
||||
listCases := []struct {
|
||||
filters filters.Args
|
||||
expectedQueryParams map[string]string
|
||||
}{
|
||||
{
|
||||
filters: filters.Args{},
|
||||
expectedQueryParams: map[string]string{
|
||||
"until": "",
|
||||
"filter": "",
|
||||
"filters": "",
|
||||
},
|
||||
},
|
||||
{
|
||||
filters: danglingFilters,
|
||||
expectedQueryParams: map[string]string{
|
||||
"until": "",
|
||||
"filter": "",
|
||||
"filters": `{"dangling":{"true":true}}`,
|
||||
},
|
||||
},
|
||||
{
|
||||
filters: danglingUntilFilters,
|
||||
expectedQueryParams: map[string]string{
|
||||
"until": "",
|
||||
"filter": "",
|
||||
"filters": `{"dangling":{"true":true},"until":{"2016-12-15T14:00":true}}`,
|
||||
},
|
||||
},
|
||||
{
|
||||
filters: noDanglingFilters,
|
||||
expectedQueryParams: map[string]string{
|
||||
"until": "",
|
||||
"filter": "",
|
||||
"filters": `{"dangling":{"false":true}}`,
|
||||
},
|
||||
},
|
||||
{
|
||||
filters: labelFilters,
|
||||
expectedQueryParams: map[string]string{
|
||||
"until": "",
|
||||
"filter": "",
|
||||
"filters": `{"dangling":{"true":true},"label":{"label1=foo":true,"label2!=bar":true}}`,
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, listCase := range listCases {
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
query := req.URL.Query()
|
||||
for key, expected := range listCase.expectedQueryParams {
|
||||
actual := query.Get(key)
|
||||
assert.Check(t, is.Equal(expected, actual))
|
||||
}
|
||||
content, err := json.Marshal(types.ContainersPruneReport{
|
||||
ContainersDeleted: []string{"container_id1", "container_id2"},
|
||||
SpaceReclaimed: 9999,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader(content)),
|
||||
}, nil
|
||||
}),
|
||||
version: "1.25",
|
||||
}
|
||||
|
||||
report, err := client.ContainersPrune(context.Background(), listCase.filters)
|
||||
assert.Check(t, err)
|
||||
assert.Check(t, is.Len(report.ContainersDeleted, 2))
|
||||
assert.Check(t, is.Equal(uint64(9999), report.SpaceReclaimed))
|
||||
}
|
||||
}
|
27
vendor/github.com/docker/docker-ce/components/engine/client/container_remove.go
generated
vendored
Normal file
27
vendor/github.com/docker/docker-ce/components/engine/client/container_remove.go
generated
vendored
Normal file
|
@ -0,0 +1,27 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// ContainerRemove kills and removes a container from the docker host.
|
||||
func (cli *Client) ContainerRemove(ctx context.Context, containerID string, options types.ContainerRemoveOptions) error {
|
||||
query := url.Values{}
|
||||
if options.RemoveVolumes {
|
||||
query.Set("v", "1")
|
||||
}
|
||||
if options.RemoveLinks {
|
||||
query.Set("link", "1")
|
||||
}
|
||||
|
||||
if options.Force {
|
||||
query.Set("force", "1")
|
||||
}
|
||||
|
||||
resp, err := cli.delete(ctx, "/containers/"+containerID, query, nil)
|
||||
ensureReaderClosed(resp)
|
||||
return wrapResponseError(err, resp, "container", containerID)
|
||||
}
|
66
vendor/github.com/docker/docker-ce/components/engine/client/container_remove_test.go
generated
vendored
Normal file
66
vendor/github.com/docker/docker-ce/components/engine/client/container_remove_test.go
generated
vendored
Normal file
|
@ -0,0 +1,66 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/gotestyourself/gotestyourself/assert"
|
||||
is "github.com/gotestyourself/gotestyourself/assert/cmp"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestContainerRemoveError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
err := client.ContainerRemove(context.Background(), "container_id", types.ContainerRemoveOptions{})
|
||||
assert.Check(t, is.Error(err, "Error response from daemon: Server error"))
|
||||
}
|
||||
|
||||
func TestContainerRemoveNotFoundError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusNotFound, "missing")),
|
||||
}
|
||||
err := client.ContainerRemove(context.Background(), "container_id", types.ContainerRemoveOptions{})
|
||||
assert.Check(t, is.Error(err, "Error: No such container: container_id"))
|
||||
assert.Check(t, IsErrNotFound(err))
|
||||
}
|
||||
|
||||
func TestContainerRemove(t *testing.T) {
|
||||
expectedURL := "/containers/container_id"
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
query := req.URL.Query()
|
||||
volume := query.Get("v")
|
||||
if volume != "1" {
|
||||
return nil, fmt.Errorf("v (volume) not set in URL query properly. Expected '1', got %s", volume)
|
||||
}
|
||||
force := query.Get("force")
|
||||
if force != "1" {
|
||||
return nil, fmt.Errorf("force not set in URL query properly. Expected '1', got %s", force)
|
||||
}
|
||||
link := query.Get("link")
|
||||
if link != "" {
|
||||
return nil, fmt.Errorf("link should have not be present in query, go %s", link)
|
||||
}
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
|
||||
err := client.ContainerRemove(context.Background(), "container_id", types.ContainerRemoveOptions{
|
||||
RemoveVolumes: true,
|
||||
Force: true,
|
||||
})
|
||||
assert.Check(t, err)
|
||||
}
|
16
vendor/github.com/docker/docker-ce/components/engine/client/container_rename.go
generated
vendored
Normal file
16
vendor/github.com/docker/docker-ce/components/engine/client/container_rename.go
generated
vendored
Normal file
|
@ -0,0 +1,16 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// ContainerRename changes the name of a given container.
|
||||
func (cli *Client) ContainerRename(ctx context.Context, containerID, newContainerName string) error {
|
||||
query := url.Values{}
|
||||
query.Set("name", newContainerName)
|
||||
resp, err := cli.post(ctx, "/containers/"+containerID+"/rename", query, nil, nil)
|
||||
ensureReaderClosed(resp)
|
||||
return err
|
||||
}
|
46
vendor/github.com/docker/docker-ce/components/engine/client/container_rename_test.go
generated
vendored
Normal file
46
vendor/github.com/docker/docker-ce/components/engine/client/container_rename_test.go
generated
vendored
Normal file
|
@ -0,0 +1,46 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestContainerRenameError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
err := client.ContainerRename(context.Background(), "nothing", "newNothing")
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server Error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerRename(t *testing.T) {
|
||||
expectedURL := "/containers/container_id/rename"
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
name := req.URL.Query().Get("name")
|
||||
if name != "newName" {
|
||||
return nil, fmt.Errorf("name not set in URL query properly. Expected 'newName', got %s", name)
|
||||
}
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
|
||||
err := client.ContainerRename(context.Background(), "container_id", "newName")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
29
vendor/github.com/docker/docker-ce/components/engine/client/container_resize.go
generated
vendored
Normal file
29
vendor/github.com/docker/docker-ce/components/engine/client/container_resize.go
generated
vendored
Normal file
|
@ -0,0 +1,29 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"strconv"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// ContainerResize changes the size of the tty for a container.
|
||||
func (cli *Client) ContainerResize(ctx context.Context, containerID string, options types.ResizeOptions) error {
|
||||
return cli.resize(ctx, "/containers/"+containerID, options.Height, options.Width)
|
||||
}
|
||||
|
||||
// ContainerExecResize changes the size of the tty for an exec process running inside a container.
|
||||
func (cli *Client) ContainerExecResize(ctx context.Context, execID string, options types.ResizeOptions) error {
|
||||
return cli.resize(ctx, "/exec/"+execID, options.Height, options.Width)
|
||||
}
|
||||
|
||||
func (cli *Client) resize(ctx context.Context, basePath string, height, width uint) error {
|
||||
query := url.Values{}
|
||||
query.Set("h", strconv.Itoa(int(height)))
|
||||
query.Set("w", strconv.Itoa(int(width)))
|
||||
|
||||
resp, err := cli.post(ctx, basePath+"/resize", query, nil, nil)
|
||||
ensureReaderClosed(resp)
|
||||
return err
|
||||
}
|
82
vendor/github.com/docker/docker-ce/components/engine/client/container_resize_test.go
generated
vendored
Normal file
82
vendor/github.com/docker/docker-ce/components/engine/client/container_resize_test.go
generated
vendored
Normal file
|
@ -0,0 +1,82 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestContainerResizeError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
err := client.ContainerResize(context.Background(), "container_id", types.ResizeOptions{})
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server Error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerExecResizeError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
err := client.ContainerExecResize(context.Background(), "exec_id", types.ResizeOptions{})
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server Error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerResize(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(resizeTransport("/containers/container_id/resize")),
|
||||
}
|
||||
|
||||
err := client.ContainerResize(context.Background(), "container_id", types.ResizeOptions{
|
||||
Height: 500,
|
||||
Width: 600,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerExecResize(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(resizeTransport("/exec/exec_id/resize")),
|
||||
}
|
||||
|
||||
err := client.ContainerExecResize(context.Background(), "exec_id", types.ResizeOptions{
|
||||
Height: 500,
|
||||
Width: 600,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func resizeTransport(expectedURL string) func(req *http.Request) (*http.Response, error) {
|
||||
return func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
query := req.URL.Query()
|
||||
h := query.Get("h")
|
||||
if h != "500" {
|
||||
return nil, fmt.Errorf("h not set in URL query properly. Expected '500', got %s", h)
|
||||
}
|
||||
w := query.Get("w")
|
||||
if w != "600" {
|
||||
return nil, fmt.Errorf("w not set in URL query properly. Expected '600', got %s", w)
|
||||
}
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
|
||||
}, nil
|
||||
}
|
||||
}
|
22
vendor/github.com/docker/docker-ce/components/engine/client/container_restart.go
generated
vendored
Normal file
22
vendor/github.com/docker/docker-ce/components/engine/client/container_restart.go
generated
vendored
Normal file
|
@ -0,0 +1,22 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
timetypes "github.com/docker/docker/api/types/time"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// ContainerRestart stops and starts a container again.
|
||||
// It makes the daemon to wait for the container to be up again for
|
||||
// a specific amount of time, given the timeout.
|
||||
func (cli *Client) ContainerRestart(ctx context.Context, containerID string, timeout *time.Duration) error {
|
||||
query := url.Values{}
|
||||
if timeout != nil {
|
||||
query.Set("t", timetypes.DurationToSecondsString(*timeout))
|
||||
}
|
||||
resp, err := cli.post(ctx, "/containers/"+containerID+"/restart", query, nil, nil)
|
||||
ensureReaderClosed(resp)
|
||||
return err
|
||||
}
|
48
vendor/github.com/docker/docker-ce/components/engine/client/container_restart_test.go
generated
vendored
Normal file
48
vendor/github.com/docker/docker-ce/components/engine/client/container_restart_test.go
generated
vendored
Normal file
|
@ -0,0 +1,48 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestContainerRestartError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
timeout := 0 * time.Second
|
||||
err := client.ContainerRestart(context.Background(), "nothing", &timeout)
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server Error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerRestart(t *testing.T) {
|
||||
expectedURL := "/containers/container_id/restart"
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
t := req.URL.Query().Get("t")
|
||||
if t != "100" {
|
||||
return nil, fmt.Errorf("t (timeout) not set in URL query properly. Expected '100', got %s", t)
|
||||
}
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
timeout := 100 * time.Second
|
||||
err := client.ContainerRestart(context.Background(), "container_id", &timeout)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
24
vendor/github.com/docker/docker-ce/components/engine/client/container_start.go
generated
vendored
Normal file
24
vendor/github.com/docker/docker-ce/components/engine/client/container_start.go
generated
vendored
Normal file
|
@ -0,0 +1,24 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
)
|
||||
|
||||
// ContainerStart sends a request to the docker daemon to start a container.
|
||||
func (cli *Client) ContainerStart(ctx context.Context, containerID string, options types.ContainerStartOptions) error {
|
||||
query := url.Values{}
|
||||
if len(options.CheckpointID) != 0 {
|
||||
query.Set("checkpoint", options.CheckpointID)
|
||||
}
|
||||
if len(options.CheckpointDir) != 0 {
|
||||
query.Set("checkpoint-dir", options.CheckpointDir)
|
||||
}
|
||||
|
||||
resp, err := cli.post(ctx, "/containers/"+containerID+"/start", query, nil, nil)
|
||||
ensureReaderClosed(resp)
|
||||
return err
|
||||
}
|
58
vendor/github.com/docker/docker-ce/components/engine/client/container_start_test.go
generated
vendored
Normal file
58
vendor/github.com/docker/docker-ce/components/engine/client/container_start_test.go
generated
vendored
Normal file
|
@ -0,0 +1,58 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
)
|
||||
|
||||
func TestContainerStartError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
err := client.ContainerStart(context.Background(), "nothing", types.ContainerStartOptions{})
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server Error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerStart(t *testing.T) {
|
||||
expectedURL := "/containers/container_id/start"
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
// we're not expecting any payload, but if one is supplied, check it is valid.
|
||||
if req.Header.Get("Content-Type") == "application/json" {
|
||||
var startConfig interface{}
|
||||
if err := json.NewDecoder(req.Body).Decode(&startConfig); err != nil {
|
||||
return nil, fmt.Errorf("Unable to parse json: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
checkpoint := req.URL.Query().Get("checkpoint")
|
||||
if checkpoint != "checkpoint_id" {
|
||||
return nil, fmt.Errorf("checkpoint not set in URL query properly. Expected 'checkpoint_id', got %s", checkpoint)
|
||||
}
|
||||
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
|
||||
err := client.ContainerStart(context.Background(), "container_id", types.ContainerStartOptions{CheckpointID: "checkpoint_id"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
26
vendor/github.com/docker/docker-ce/components/engine/client/container_stats.go
generated
vendored
Normal file
26
vendor/github.com/docker/docker-ce/components/engine/client/container_stats.go
generated
vendored
Normal file
|
@ -0,0 +1,26 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// ContainerStats returns near realtime stats for a given container.
|
||||
// It's up to the caller to close the io.ReadCloser returned.
|
||||
func (cli *Client) ContainerStats(ctx context.Context, containerID string, stream bool) (types.ContainerStats, error) {
|
||||
query := url.Values{}
|
||||
query.Set("stream", "0")
|
||||
if stream {
|
||||
query.Set("stream", "1")
|
||||
}
|
||||
|
||||
resp, err := cli.get(ctx, "/containers/"+containerID+"/stats", query, nil)
|
||||
if err != nil {
|
||||
return types.ContainerStats{}, err
|
||||
}
|
||||
|
||||
osType := getDockerOS(resp.header.Get("Server"))
|
||||
return types.ContainerStats{Body: resp.body, OSType: osType}, err
|
||||
}
|
70
vendor/github.com/docker/docker-ce/components/engine/client/container_stats_test.go
generated
vendored
Normal file
70
vendor/github.com/docker/docker-ce/components/engine/client/container_stats_test.go
generated
vendored
Normal file
|
@ -0,0 +1,70 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestContainerStatsError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
_, err := client.ContainerStats(context.Background(), "nothing", false)
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server Error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerStats(t *testing.T) {
|
||||
expectedURL := "/containers/container_id/stats"
|
||||
cases := []struct {
|
||||
stream bool
|
||||
expectedStream string
|
||||
}{
|
||||
{
|
||||
expectedStream: "0",
|
||||
},
|
||||
{
|
||||
stream: true,
|
||||
expectedStream: "1",
|
||||
},
|
||||
}
|
||||
for _, c := range cases {
|
||||
client := &Client{
|
||||
client: newMockClient(func(r *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(r.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, r.URL)
|
||||
}
|
||||
|
||||
query := r.URL.Query()
|
||||
stream := query.Get("stream")
|
||||
if stream != c.expectedStream {
|
||||
return nil, fmt.Errorf("stream not set in URL query properly. Expected '%s', got %s", c.expectedStream, stream)
|
||||
}
|
||||
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader([]byte("response"))),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
resp, err := client.ContainerStats(context.Background(), "container_id", c.stream)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
content, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(content) != "response" {
|
||||
t.Fatalf("expected response to contain 'response', got %s", string(content))
|
||||
}
|
||||
}
|
||||
}
|
21
vendor/github.com/docker/docker-ce/components/engine/client/container_stop.go
generated
vendored
Normal file
21
vendor/github.com/docker/docker-ce/components/engine/client/container_stop.go
generated
vendored
Normal file
|
@ -0,0 +1,21 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
timetypes "github.com/docker/docker/api/types/time"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// ContainerStop stops a container without terminating the process.
|
||||
// The process is blocked until the container stops or the timeout expires.
|
||||
func (cli *Client) ContainerStop(ctx context.Context, containerID string, timeout *time.Duration) error {
|
||||
query := url.Values{}
|
||||
if timeout != nil {
|
||||
query.Set("t", timetypes.DurationToSecondsString(*timeout))
|
||||
}
|
||||
resp, err := cli.post(ctx, "/containers/"+containerID+"/stop", query, nil, nil)
|
||||
ensureReaderClosed(resp)
|
||||
return err
|
||||
}
|
48
vendor/github.com/docker/docker-ce/components/engine/client/container_stop_test.go
generated
vendored
Normal file
48
vendor/github.com/docker/docker-ce/components/engine/client/container_stop_test.go
generated
vendored
Normal file
|
@ -0,0 +1,48 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestContainerStopError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
timeout := 0 * time.Second
|
||||
err := client.ContainerStop(context.Background(), "nothing", &timeout)
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server Error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerStop(t *testing.T) {
|
||||
expectedURL := "/containers/container_id/stop"
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
t := req.URL.Query().Get("t")
|
||||
if t != "100" {
|
||||
return nil, fmt.Errorf("t (timeout) not set in URL query properly. Expected '100', got %s", t)
|
||||
}
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
timeout := 100 * time.Second
|
||||
err := client.ContainerStop(context.Background(), "container_id", &timeout)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
28
vendor/github.com/docker/docker-ce/components/engine/client/container_top.go
generated
vendored
Normal file
28
vendor/github.com/docker/docker-ce/components/engine/client/container_top.go
generated
vendored
Normal file
|
@ -0,0 +1,28 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// ContainerTop shows process information from within a container.
|
||||
func (cli *Client) ContainerTop(ctx context.Context, containerID string, arguments []string) (container.ContainerTopOKBody, error) {
|
||||
var response container.ContainerTopOKBody
|
||||
query := url.Values{}
|
||||
if len(arguments) > 0 {
|
||||
query.Set("ps_args", strings.Join(arguments, " "))
|
||||
}
|
||||
|
||||
resp, err := cli.get(ctx, "/containers/"+containerID+"/top", query, nil)
|
||||
if err != nil {
|
||||
return response, err
|
||||
}
|
||||
|
||||
err = json.NewDecoder(resp.body).Decode(&response)
|
||||
ensureReaderClosed(resp)
|
||||
return response, err
|
||||
}
|
74
vendor/github.com/docker/docker-ce/components/engine/client/container_top_test.go
generated
vendored
Normal file
74
vendor/github.com/docker/docker-ce/components/engine/client/container_top_test.go
generated
vendored
Normal file
|
@ -0,0 +1,74 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestContainerTopError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
_, err := client.ContainerTop(context.Background(), "nothing", []string{})
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server Error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerTop(t *testing.T) {
|
||||
expectedURL := "/containers/container_id/top"
|
||||
expectedProcesses := [][]string{
|
||||
{"p1", "p2"},
|
||||
{"p3"},
|
||||
}
|
||||
expectedTitles := []string{"title1", "title2"}
|
||||
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
query := req.URL.Query()
|
||||
args := query.Get("ps_args")
|
||||
if args != "arg1 arg2" {
|
||||
return nil, fmt.Errorf("args not set in URL query properly. Expected 'arg1 arg2', got %v", args)
|
||||
}
|
||||
|
||||
b, err := json.Marshal(container.ContainerTopOKBody{
|
||||
Processes: [][]string{
|
||||
{"p1", "p2"},
|
||||
{"p3"},
|
||||
},
|
||||
Titles: []string{"title1", "title2"},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader(b)),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
|
||||
processList, err := client.ContainerTop(context.Background(), "container_id", []string{"arg1", "arg2"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(expectedProcesses, processList.Processes) {
|
||||
t.Fatalf("Processes: expected %v, got %v", expectedProcesses, processList.Processes)
|
||||
}
|
||||
if !reflect.DeepEqual(expectedTitles, processList.Titles) {
|
||||
t.Fatalf("Titles: expected %v, got %v", expectedTitles, processList.Titles)
|
||||
}
|
||||
}
|
10
vendor/github.com/docker/docker-ce/components/engine/client/container_unpause.go
generated
vendored
Normal file
10
vendor/github.com/docker/docker-ce/components/engine/client/container_unpause.go
generated
vendored
Normal file
|
@ -0,0 +1,10 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import "golang.org/x/net/context"
|
||||
|
||||
// ContainerUnpause resumes the process execution within a container
|
||||
func (cli *Client) ContainerUnpause(ctx context.Context, containerID string) error {
|
||||
resp, err := cli.post(ctx, "/containers/"+containerID+"/unpause", nil, nil, nil)
|
||||
ensureReaderClosed(resp)
|
||||
return err
|
||||
}
|
41
vendor/github.com/docker/docker-ce/components/engine/client/container_unpause_test.go
generated
vendored
Normal file
41
vendor/github.com/docker/docker-ce/components/engine/client/container_unpause_test.go
generated
vendored
Normal file
|
@ -0,0 +1,41 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestContainerUnpauseError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
err := client.ContainerUnpause(context.Background(), "nothing")
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server Error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerUnpause(t *testing.T) {
|
||||
expectedURL := "/containers/container_id/unpause"
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
err := client.ContainerUnpause(context.Background(), "container_id")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
22
vendor/github.com/docker/docker-ce/components/engine/client/container_update.go
generated
vendored
Normal file
22
vendor/github.com/docker/docker-ce/components/engine/client/container_update.go
generated
vendored
Normal file
|
@ -0,0 +1,22 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// ContainerUpdate updates resources of a container
|
||||
func (cli *Client) ContainerUpdate(ctx context.Context, containerID string, updateConfig container.UpdateConfig) (container.ContainerUpdateOKBody, error) {
|
||||
var response container.ContainerUpdateOKBody
|
||||
serverResp, err := cli.post(ctx, "/containers/"+containerID+"/update", nil, updateConfig, nil)
|
||||
if err != nil {
|
||||
return response, err
|
||||
}
|
||||
|
||||
err = json.NewDecoder(serverResp.body).Decode(&response)
|
||||
|
||||
ensureReaderClosed(serverResp)
|
||||
return response, err
|
||||
}
|
58
vendor/github.com/docker/docker-ce/components/engine/client/container_update_test.go
generated
vendored
Normal file
58
vendor/github.com/docker/docker-ce/components/engine/client/container_update_test.go
generated
vendored
Normal file
|
@ -0,0 +1,58 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestContainerUpdateError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
_, err := client.ContainerUpdate(context.Background(), "nothing", container.UpdateConfig{})
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server Error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerUpdate(t *testing.T) {
|
||||
expectedURL := "/containers/container_id/update"
|
||||
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
|
||||
b, err := json.Marshal(container.ContainerUpdateOKBody{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader(b)),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
|
||||
_, err := client.ContainerUpdate(context.Background(), "container_id", container.UpdateConfig{
|
||||
Resources: container.Resources{
|
||||
CPUPeriod: 1,
|
||||
},
|
||||
RestartPolicy: container.RestartPolicy{
|
||||
Name: "always",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
84
vendor/github.com/docker/docker-ce/components/engine/client/container_wait.go
generated
vendored
Normal file
84
vendor/github.com/docker/docker-ce/components/engine/client/container_wait.go
generated
vendored
Normal file
|
@ -0,0 +1,84 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/url"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/docker/docker/api/types/versions"
|
||||
)
|
||||
|
||||
// ContainerWait waits until the specified container is in a certain state
|
||||
// indicated by the given condition, either "not-running" (default),
|
||||
// "next-exit", or "removed".
|
||||
//
|
||||
// If this client's API version is before 1.30, condition is ignored and
|
||||
// ContainerWait will return immediately with the two channels, as the server
|
||||
// will wait as if the condition were "not-running".
|
||||
//
|
||||
// If this client's API version is at least 1.30, ContainerWait blocks until
|
||||
// the request has been acknowledged by the server (with a response header),
|
||||
// then returns two channels on which the caller can wait for the exit status
|
||||
// of the container or an error if there was a problem either beginning the
|
||||
// wait request or in getting the response. This allows the caller to
|
||||
// synchronize ContainerWait with other calls, such as specifying a
|
||||
// "next-exit" condition before issuing a ContainerStart request.
|
||||
func (cli *Client) ContainerWait(ctx context.Context, containerID string, condition container.WaitCondition) (<-chan container.ContainerWaitOKBody, <-chan error) {
|
||||
if versions.LessThan(cli.ClientVersion(), "1.30") {
|
||||
return cli.legacyContainerWait(ctx, containerID)
|
||||
}
|
||||
|
||||
resultC := make(chan container.ContainerWaitOKBody)
|
||||
errC := make(chan error, 1)
|
||||
|
||||
query := url.Values{}
|
||||
query.Set("condition", string(condition))
|
||||
|
||||
resp, err := cli.post(ctx, "/containers/"+containerID+"/wait", query, nil, nil)
|
||||
if err != nil {
|
||||
defer ensureReaderClosed(resp)
|
||||
errC <- err
|
||||
return resultC, errC
|
||||
}
|
||||
|
||||
go func() {
|
||||
defer ensureReaderClosed(resp)
|
||||
var res container.ContainerWaitOKBody
|
||||
if err := json.NewDecoder(resp.body).Decode(&res); err != nil {
|
||||
errC <- err
|
||||
return
|
||||
}
|
||||
|
||||
resultC <- res
|
||||
}()
|
||||
|
||||
return resultC, errC
|
||||
}
|
||||
|
||||
// legacyContainerWait returns immediately and doesn't have an option to wait
|
||||
// until the container is removed.
|
||||
func (cli *Client) legacyContainerWait(ctx context.Context, containerID string) (<-chan container.ContainerWaitOKBody, <-chan error) {
|
||||
resultC := make(chan container.ContainerWaitOKBody)
|
||||
errC := make(chan error)
|
||||
|
||||
go func() {
|
||||
resp, err := cli.post(ctx, "/containers/"+containerID+"/wait", nil, nil, nil)
|
||||
if err != nil {
|
||||
errC <- err
|
||||
return
|
||||
}
|
||||
defer ensureReaderClosed(resp)
|
||||
|
||||
var res container.ContainerWaitOKBody
|
||||
if err := json.NewDecoder(resp.body).Decode(&res); err != nil {
|
||||
errC <- err
|
||||
return
|
||||
}
|
||||
|
||||
resultC <- res
|
||||
}()
|
||||
|
||||
return resultC, errC
|
||||
}
|
74
vendor/github.com/docker/docker-ce/components/engine/client/container_wait_test.go
generated
vendored
Normal file
74
vendor/github.com/docker/docker-ce/components/engine/client/container_wait_test.go
generated
vendored
Normal file
|
@ -0,0 +1,74 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/docker/docker/api/types/container"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestContainerWaitError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
resultC, errC := client.ContainerWait(context.Background(), "nothing", "")
|
||||
select {
|
||||
case result := <-resultC:
|
||||
t.Fatalf("expected to not get a wait result, got %d", result.StatusCode)
|
||||
case err := <-errC:
|
||||
if err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server Error, got %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerWait(t *testing.T) {
|
||||
expectedURL := "/containers/container_id/wait"
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
b, err := json.Marshal(container.ContainerWaitOKBody{
|
||||
StatusCode: 15,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader(b)),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
|
||||
resultC, errC := client.ContainerWait(context.Background(), "container_id", "")
|
||||
select {
|
||||
case err := <-errC:
|
||||
t.Fatal(err)
|
||||
case result := <-resultC:
|
||||
if result.StatusCode != 15 {
|
||||
t.Fatalf("expected a status code equal to '15', got %d", result.StatusCode)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func ExampleClient_ContainerWait_withTimeout() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
client, _ := NewEnvClient()
|
||||
_, errC := client.ContainerWait(ctx, "container_id", "")
|
||||
if err := <-errC; err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
26
vendor/github.com/docker/docker-ce/components/engine/client/disk_usage.go
generated
vendored
Normal file
26
vendor/github.com/docker/docker-ce/components/engine/client/disk_usage.go
generated
vendored
Normal file
|
@ -0,0 +1,26 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// DiskUsage requests the current data usage from the daemon
|
||||
func (cli *Client) DiskUsage(ctx context.Context) (types.DiskUsage, error) {
|
||||
var du types.DiskUsage
|
||||
|
||||
serverResp, err := cli.get(ctx, "/system/df", nil, nil)
|
||||
if err != nil {
|
||||
return du, err
|
||||
}
|
||||
defer ensureReaderClosed(serverResp)
|
||||
|
||||
if err := json.NewDecoder(serverResp.body).Decode(&du); err != nil {
|
||||
return du, fmt.Errorf("Error retrieving disk usage: %v", err)
|
||||
}
|
||||
|
||||
return du, nil
|
||||
}
|
55
vendor/github.com/docker/docker-ce/components/engine/client/disk_usage_test.go
generated
vendored
Normal file
55
vendor/github.com/docker/docker-ce/components/engine/client/disk_usage_test.go
generated
vendored
Normal file
|
@ -0,0 +1,55 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestDiskUsageError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
_, err := client.DiskUsage(context.Background())
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server Error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiskUsage(t *testing.T) {
|
||||
expectedURL := "/system/df"
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
|
||||
du := types.DiskUsage{
|
||||
LayersSize: int64(100),
|
||||
Images: nil,
|
||||
Containers: nil,
|
||||
Volumes: nil,
|
||||
}
|
||||
|
||||
b, err := json.Marshal(du)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader(b)),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
if _, err := client.DiskUsage(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
38
vendor/github.com/docker/docker-ce/components/engine/client/distribution_inspect.go
generated
vendored
Normal file
38
vendor/github.com/docker/docker-ce/components/engine/client/distribution_inspect.go
generated
vendored
Normal file
|
@ -0,0 +1,38 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/url"
|
||||
|
||||
registrytypes "github.com/docker/docker/api/types/registry"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// DistributionInspect returns the image digest with full Manifest
|
||||
func (cli *Client) DistributionInspect(ctx context.Context, image, encodedRegistryAuth string) (registrytypes.DistributionInspect, error) {
|
||||
// Contact the registry to retrieve digest and platform information
|
||||
var distributionInspect registrytypes.DistributionInspect
|
||||
if image == "" {
|
||||
return distributionInspect, objectNotFoundError{object: "distribution", id: image}
|
||||
}
|
||||
|
||||
if err := cli.NewVersionError("1.30", "distribution inspect"); err != nil {
|
||||
return distributionInspect, err
|
||||
}
|
||||
var headers map[string][]string
|
||||
|
||||
if encodedRegistryAuth != "" {
|
||||
headers = map[string][]string{
|
||||
"X-Registry-Auth": {encodedRegistryAuth},
|
||||
}
|
||||
}
|
||||
|
||||
resp, err := cli.get(ctx, "/distribution/"+image+"/json", url.Values{}, headers)
|
||||
if err != nil {
|
||||
return distributionInspect, err
|
||||
}
|
||||
|
||||
err = json.NewDecoder(resp.body).Decode(&distributionInspect)
|
||||
ensureReaderClosed(resp)
|
||||
return distributionInspect, err
|
||||
}
|
32
vendor/github.com/docker/docker-ce/components/engine/client/distribution_inspect_test.go
generated
vendored
Normal file
32
vendor/github.com/docker/docker-ce/components/engine/client/distribution_inspect_test.go
generated
vendored
Normal file
|
@ -0,0 +1,32 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/gotestyourself/gotestyourself/assert"
|
||||
is "github.com/gotestyourself/gotestyourself/assert/cmp"
|
||||
"github.com/pkg/errors"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestDistributionInspectUnsupported(t *testing.T) {
|
||||
client := &Client{
|
||||
version: "1.29",
|
||||
client: &http.Client{},
|
||||
}
|
||||
_, err := client.DistributionInspect(context.Background(), "foobar:1.0", "")
|
||||
assert.Check(t, is.Error(err, `"distribution inspect" requires API version 1.30, but the Docker daemon API version is 1.29`))
|
||||
}
|
||||
|
||||
func TestDistributionInspectWithEmptyID(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
return nil, errors.New("should not make request")
|
||||
}),
|
||||
}
|
||||
_, err := client.DistributionInspect(context.Background(), "", "")
|
||||
if !IsErrNotFound(err) {
|
||||
t.Fatalf("Expected NotFoundError, got %v", err)
|
||||
}
|
||||
}
|
133
vendor/github.com/docker/docker-ce/components/engine/client/errors.go
generated
vendored
Normal file
133
vendor/github.com/docker/docker-ce/components/engine/client/errors.go
generated
vendored
Normal file
|
@ -0,0 +1,133 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"net/http"
|
||||
|
||||
"github.com/docker/docker/api/types/versions"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// errConnectionFailed implements an error returned when connection failed.
|
||||
type errConnectionFailed struct {
|
||||
host string
|
||||
}
|
||||
|
||||
// Error returns a string representation of an errConnectionFailed
|
||||
func (err errConnectionFailed) Error() string {
|
||||
if err.host == "" {
|
||||
return "Cannot connect to the Docker daemon. Is the docker daemon running on this host?"
|
||||
}
|
||||
return fmt.Sprintf("Cannot connect to the Docker daemon at %s. Is the docker daemon running?", err.host)
|
||||
}
|
||||
|
||||
// IsErrConnectionFailed returns true if the error is caused by connection failed.
|
||||
func IsErrConnectionFailed(err error) bool {
|
||||
_, ok := errors.Cause(err).(errConnectionFailed)
|
||||
return ok
|
||||
}
|
||||
|
||||
// ErrorConnectionFailed returns an error with host in the error message when connection to docker daemon failed.
|
||||
func ErrorConnectionFailed(host string) error {
|
||||
return errConnectionFailed{host: host}
|
||||
}
|
||||
|
||||
type notFound interface {
|
||||
error
|
||||
NotFound() bool // Is the error a NotFound error
|
||||
}
|
||||
|
||||
// IsErrNotFound returns true if the error is a NotFound error, which is returned
|
||||
// by the API when some object is not found.
|
||||
func IsErrNotFound(err error) bool {
|
||||
te, ok := err.(notFound)
|
||||
return ok && te.NotFound()
|
||||
}
|
||||
|
||||
type objectNotFoundError struct {
|
||||
object string
|
||||
id string
|
||||
}
|
||||
|
||||
func (e objectNotFoundError) NotFound() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (e objectNotFoundError) Error() string {
|
||||
return fmt.Sprintf("Error: No such %s: %s", e.object, e.id)
|
||||
}
|
||||
|
||||
func wrapResponseError(err error, resp serverResponse, object, id string) error {
|
||||
switch {
|
||||
case err == nil:
|
||||
return nil
|
||||
case resp.statusCode == http.StatusNotFound:
|
||||
return objectNotFoundError{object: object, id: id}
|
||||
case resp.statusCode == http.StatusNotImplemented:
|
||||
return notImplementedError{message: err.Error()}
|
||||
default:
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// unauthorizedError represents an authorization error in a remote registry.
|
||||
type unauthorizedError struct {
|
||||
cause error
|
||||
}
|
||||
|
||||
// Error returns a string representation of an unauthorizedError
|
||||
func (u unauthorizedError) Error() string {
|
||||
return u.cause.Error()
|
||||
}
|
||||
|
||||
// IsErrUnauthorized returns true if the error is caused
|
||||
// when a remote registry authentication fails
|
||||
func IsErrUnauthorized(err error) bool {
|
||||
_, ok := err.(unauthorizedError)
|
||||
return ok
|
||||
}
|
||||
|
||||
type pluginPermissionDenied struct {
|
||||
name string
|
||||
}
|
||||
|
||||
func (e pluginPermissionDenied) Error() string {
|
||||
return "Permission denied while installing plugin " + e.name
|
||||
}
|
||||
|
||||
// IsErrPluginPermissionDenied returns true if the error is caused
|
||||
// when a user denies a plugin's permissions
|
||||
func IsErrPluginPermissionDenied(err error) bool {
|
||||
_, ok := err.(pluginPermissionDenied)
|
||||
return ok
|
||||
}
|
||||
|
||||
type notImplementedError struct {
|
||||
message string
|
||||
}
|
||||
|
||||
func (e notImplementedError) Error() string {
|
||||
return e.message
|
||||
}
|
||||
|
||||
func (e notImplementedError) NotImplemented() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// IsErrNotImplemented returns true if the error is a NotImplemented error.
|
||||
// This is returned by the API when a requested feature has not been
|
||||
// implemented.
|
||||
func IsErrNotImplemented(err error) bool {
|
||||
te, ok := err.(notImplementedError)
|
||||
return ok && te.NotImplemented()
|
||||
}
|
||||
|
||||
// NewVersionError returns an error if the APIVersion required
|
||||
// if less than the current supported version
|
||||
func (cli *Client) NewVersionError(APIrequired, feature string) error {
|
||||
if cli.version != "" && versions.LessThan(cli.version, APIrequired) {
|
||||
return fmt.Errorf("%q requires API version %s, but the Docker daemon API version is %s", feature, APIrequired, cli.version)
|
||||
}
|
||||
return nil
|
||||
}
|
102
vendor/github.com/docker/docker-ce/components/engine/client/events.go
generated
vendored
Normal file
102
vendor/github.com/docker/docker-ce/components/engine/client/events.go
generated
vendored
Normal file
|
@ -0,0 +1,102 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/events"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
timetypes "github.com/docker/docker/api/types/time"
|
||||
)
|
||||
|
||||
// Events returns a stream of events in the daemon. It's up to the caller to close the stream
|
||||
// by cancelling the context. Once the stream has been completely read an io.EOF error will
|
||||
// be sent over the error channel. If an error is sent all processing will be stopped. It's up
|
||||
// to the caller to reopen the stream in the event of an error by reinvoking this method.
|
||||
func (cli *Client) Events(ctx context.Context, options types.EventsOptions) (<-chan events.Message, <-chan error) {
|
||||
|
||||
messages := make(chan events.Message)
|
||||
errs := make(chan error, 1)
|
||||
|
||||
started := make(chan struct{})
|
||||
go func() {
|
||||
defer close(errs)
|
||||
|
||||
query, err := buildEventsQueryParams(cli.version, options)
|
||||
if err != nil {
|
||||
close(started)
|
||||
errs <- err
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := cli.get(ctx, "/events", query, nil)
|
||||
if err != nil {
|
||||
close(started)
|
||||
errs <- err
|
||||
return
|
||||
}
|
||||
defer resp.body.Close()
|
||||
|
||||
decoder := json.NewDecoder(resp.body)
|
||||
|
||||
close(started)
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
errs <- ctx.Err()
|
||||
return
|
||||
default:
|
||||
var event events.Message
|
||||
if err := decoder.Decode(&event); err != nil {
|
||||
errs <- err
|
||||
return
|
||||
}
|
||||
|
||||
select {
|
||||
case messages <- event:
|
||||
case <-ctx.Done():
|
||||
errs <- ctx.Err()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
<-started
|
||||
|
||||
return messages, errs
|
||||
}
|
||||
|
||||
func buildEventsQueryParams(cliVersion string, options types.EventsOptions) (url.Values, error) {
|
||||
query := url.Values{}
|
||||
ref := time.Now()
|
||||
|
||||
if options.Since != "" {
|
||||
ts, err := timetypes.GetTimestamp(options.Since, ref)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
query.Set("since", ts)
|
||||
}
|
||||
|
||||
if options.Until != "" {
|
||||
ts, err := timetypes.GetTimestamp(options.Until, ref)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
query.Set("until", ts)
|
||||
}
|
||||
|
||||
if options.Filters.Len() > 0 {
|
||||
filterJSON, err := filters.ToParamWithVersion(cliVersion, options.Filters)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
query.Set("filters", filterJSON)
|
||||
}
|
||||
|
||||
return query, nil
|
||||
}
|
165
vendor/github.com/docker/docker-ce/components/engine/client/events_test.go
generated
vendored
Normal file
165
vendor/github.com/docker/docker-ce/components/engine/client/events_test.go
generated
vendored
Normal file
|
@ -0,0 +1,165 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/events"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
)
|
||||
|
||||
func TestEventsErrorInOptions(t *testing.T) {
|
||||
errorCases := []struct {
|
||||
options types.EventsOptions
|
||||
expectedError string
|
||||
}{
|
||||
{
|
||||
options: types.EventsOptions{
|
||||
Since: "2006-01-02TZ",
|
||||
},
|
||||
expectedError: `parsing time "2006-01-02TZ"`,
|
||||
},
|
||||
{
|
||||
options: types.EventsOptions{
|
||||
Until: "2006-01-02TZ",
|
||||
},
|
||||
expectedError: `parsing time "2006-01-02TZ"`,
|
||||
},
|
||||
}
|
||||
for _, e := range errorCases {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
_, errs := client.Events(context.Background(), e.options)
|
||||
err := <-errs
|
||||
if err == nil || !strings.Contains(err.Error(), e.expectedError) {
|
||||
t.Fatalf("expected an error %q, got %v", e.expectedError, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEventsErrorFromServer(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
_, errs := client.Events(context.Background(), types.EventsOptions{})
|
||||
err := <-errs
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server Error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEvents(t *testing.T) {
|
||||
|
||||
expectedURL := "/events"
|
||||
|
||||
filters := filters.NewArgs()
|
||||
filters.Add("type", events.ContainerEventType)
|
||||
expectedFiltersJSON := fmt.Sprintf(`{"type":{"%s":true}}`, events.ContainerEventType)
|
||||
|
||||
eventsCases := []struct {
|
||||
options types.EventsOptions
|
||||
events []events.Message
|
||||
expectedEvents map[string]bool
|
||||
expectedQueryParams map[string]string
|
||||
}{
|
||||
{
|
||||
options: types.EventsOptions{
|
||||
Filters: filters,
|
||||
},
|
||||
expectedQueryParams: map[string]string{
|
||||
"filters": expectedFiltersJSON,
|
||||
},
|
||||
events: []events.Message{},
|
||||
expectedEvents: make(map[string]bool),
|
||||
},
|
||||
{
|
||||
options: types.EventsOptions{
|
||||
Filters: filters,
|
||||
},
|
||||
expectedQueryParams: map[string]string{
|
||||
"filters": expectedFiltersJSON,
|
||||
},
|
||||
events: []events.Message{
|
||||
{
|
||||
Type: "container",
|
||||
ID: "1",
|
||||
Action: "create",
|
||||
},
|
||||
{
|
||||
Type: "container",
|
||||
ID: "2",
|
||||
Action: "die",
|
||||
},
|
||||
{
|
||||
Type: "container",
|
||||
ID: "3",
|
||||
Action: "create",
|
||||
},
|
||||
},
|
||||
expectedEvents: map[string]bool{
|
||||
"1": true,
|
||||
"2": true,
|
||||
"3": true,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, eventsCase := range eventsCases {
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
query := req.URL.Query()
|
||||
|
||||
for key, expected := range eventsCase.expectedQueryParams {
|
||||
actual := query.Get(key)
|
||||
if actual != expected {
|
||||
return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
buffer := new(bytes.Buffer)
|
||||
|
||||
for _, e := range eventsCase.events {
|
||||
b, _ := json.Marshal(e)
|
||||
buffer.Write(b)
|
||||
}
|
||||
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(buffer),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
|
||||
messages, errs := client.Events(context.Background(), eventsCase.options)
|
||||
|
||||
loop:
|
||||
for {
|
||||
select {
|
||||
case err := <-errs:
|
||||
if err != nil && err != io.EOF {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
break loop
|
||||
case e := <-messages:
|
||||
_, ok := eventsCase.expectedEvents[e.ID]
|
||||
if !ok {
|
||||
t.Fatalf("event received not expected with action %s & id %s", e.Action, e.ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
230
vendor/github.com/docker/docker-ce/components/engine/client/hijack.go
generated
vendored
Normal file
230
vendor/github.com/docker/docker-ce/components/engine/client/hijack.go
generated
vendored
Normal file
|
@ -0,0 +1,230 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/go-connections/sockets"
|
||||
"github.com/pkg/errors"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// tlsClientCon holds tls information and a dialed connection.
|
||||
type tlsClientCon struct {
|
||||
*tls.Conn
|
||||
rawConn net.Conn
|
||||
}
|
||||
|
||||
func (c *tlsClientCon) CloseWrite() error {
|
||||
// Go standard tls.Conn doesn't provide the CloseWrite() method so we do it
|
||||
// on its underlying connection.
|
||||
if conn, ok := c.rawConn.(types.CloseWriter); ok {
|
||||
return conn.CloseWrite()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// postHijacked sends a POST request and hijacks the connection.
|
||||
func (cli *Client) postHijacked(ctx context.Context, path string, query url.Values, body interface{}, headers map[string][]string) (types.HijackedResponse, error) {
|
||||
bodyEncoded, err := encodeData(body)
|
||||
if err != nil {
|
||||
return types.HijackedResponse{}, err
|
||||
}
|
||||
|
||||
apiPath := cli.getAPIPath(path, query)
|
||||
req, err := http.NewRequest("POST", apiPath, bodyEncoded)
|
||||
if err != nil {
|
||||
return types.HijackedResponse{}, err
|
||||
}
|
||||
req = cli.addHeaders(req, headers)
|
||||
|
||||
conn, err := cli.setupHijackConn(req, "tcp")
|
||||
if err != nil {
|
||||
return types.HijackedResponse{}, err
|
||||
}
|
||||
|
||||
return types.HijackedResponse{Conn: conn, Reader: bufio.NewReader(conn)}, err
|
||||
}
|
||||
|
||||
func tlsDial(network, addr string, config *tls.Config) (net.Conn, error) {
|
||||
return tlsDialWithDialer(new(net.Dialer), network, addr, config)
|
||||
}
|
||||
|
||||
// We need to copy Go's implementation of tls.Dial (pkg/cryptor/tls/tls.go) in
|
||||
// order to return our custom tlsClientCon struct which holds both the tls.Conn
|
||||
// object _and_ its underlying raw connection. The rationale for this is that
|
||||
// we need to be able to close the write end of the connection when attaching,
|
||||
// which tls.Conn does not provide.
|
||||
func tlsDialWithDialer(dialer *net.Dialer, network, addr string, config *tls.Config) (net.Conn, error) {
|
||||
// We want the Timeout and Deadline values from dialer to cover the
|
||||
// whole process: TCP connection and TLS handshake. This means that we
|
||||
// also need to start our own timers now.
|
||||
timeout := dialer.Timeout
|
||||
|
||||
if !dialer.Deadline.IsZero() {
|
||||
deadlineTimeout := time.Until(dialer.Deadline)
|
||||
if timeout == 0 || deadlineTimeout < timeout {
|
||||
timeout = deadlineTimeout
|
||||
}
|
||||
}
|
||||
|
||||
var errChannel chan error
|
||||
|
||||
if timeout != 0 {
|
||||
errChannel = make(chan error, 2)
|
||||
time.AfterFunc(timeout, func() {
|
||||
errChannel <- errors.New("")
|
||||
})
|
||||
}
|
||||
|
||||
proxyDialer, err := sockets.DialerFromEnvironment(dialer)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rawConn, err := proxyDialer.Dial(network, addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// When we set up a TCP connection for hijack, there could be long periods
|
||||
// of inactivity (a long running command with no output) that in certain
|
||||
// network setups may cause ECONNTIMEOUT, leaving the client in an unknown
|
||||
// state. Setting TCP KeepAlive on the socket connection will prohibit
|
||||
// ECONNTIMEOUT unless the socket connection truly is broken
|
||||
if tcpConn, ok := rawConn.(*net.TCPConn); ok {
|
||||
tcpConn.SetKeepAlive(true)
|
||||
tcpConn.SetKeepAlivePeriod(30 * time.Second)
|
||||
}
|
||||
|
||||
colonPos := strings.LastIndex(addr, ":")
|
||||
if colonPos == -1 {
|
||||
colonPos = len(addr)
|
||||
}
|
||||
hostname := addr[:colonPos]
|
||||
|
||||
// If no ServerName is set, infer the ServerName
|
||||
// from the hostname we're connecting to.
|
||||
if config.ServerName == "" {
|
||||
// Make a copy to avoid polluting argument or default.
|
||||
config = tlsConfigClone(config)
|
||||
config.ServerName = hostname
|
||||
}
|
||||
|
||||
conn := tls.Client(rawConn, config)
|
||||
|
||||
if timeout == 0 {
|
||||
err = conn.Handshake()
|
||||
} else {
|
||||
go func() {
|
||||
errChannel <- conn.Handshake()
|
||||
}()
|
||||
|
||||
err = <-errChannel
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
rawConn.Close()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// This is Docker difference with standard's crypto/tls package: returned a
|
||||
// wrapper which holds both the TLS and raw connections.
|
||||
return &tlsClientCon{conn, rawConn}, nil
|
||||
}
|
||||
|
||||
func dial(proto, addr string, tlsConfig *tls.Config) (net.Conn, error) {
|
||||
if tlsConfig != nil && proto != "unix" && proto != "npipe" {
|
||||
// Notice this isn't Go standard's tls.Dial function
|
||||
return tlsDial(proto, addr, tlsConfig)
|
||||
}
|
||||
if proto == "npipe" {
|
||||
return sockets.DialPipe(addr, 32*time.Second)
|
||||
}
|
||||
return net.Dial(proto, addr)
|
||||
}
|
||||
|
||||
func (cli *Client) setupHijackConn(req *http.Request, proto string) (net.Conn, error) {
|
||||
req.Host = cli.addr
|
||||
req.Header.Set("Connection", "Upgrade")
|
||||
req.Header.Set("Upgrade", proto)
|
||||
|
||||
conn, err := dial(cli.proto, cli.addr, resolveTLSConfig(cli.client.Transport))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "cannot connect to the Docker daemon. Is 'docker daemon' running on this host?")
|
||||
}
|
||||
|
||||
// When we set up a TCP connection for hijack, there could be long periods
|
||||
// of inactivity (a long running command with no output) that in certain
|
||||
// network setups may cause ECONNTIMEOUT, leaving the client in an unknown
|
||||
// state. Setting TCP KeepAlive on the socket connection will prohibit
|
||||
// ECONNTIMEOUT unless the socket connection truly is broken
|
||||
if tcpConn, ok := conn.(*net.TCPConn); ok {
|
||||
tcpConn.SetKeepAlive(true)
|
||||
tcpConn.SetKeepAlivePeriod(30 * time.Second)
|
||||
}
|
||||
|
||||
clientconn := httputil.NewClientConn(conn, nil)
|
||||
defer clientconn.Close()
|
||||
|
||||
// Server hijacks the connection, error 'connection closed' expected
|
||||
resp, err := clientconn.Do(req)
|
||||
if err != httputil.ErrPersistEOF {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if resp.StatusCode != http.StatusSwitchingProtocols {
|
||||
resp.Body.Close()
|
||||
return nil, fmt.Errorf("unable to upgrade to %s, received %d", proto, resp.StatusCode)
|
||||
}
|
||||
}
|
||||
|
||||
c, br := clientconn.Hijack()
|
||||
if br.Buffered() > 0 {
|
||||
// If there is buffered content, wrap the connection. We return an
|
||||
// object that implements CloseWrite iff the underlying connection
|
||||
// implements it.
|
||||
if _, ok := c.(types.CloseWriter); ok {
|
||||
c = &hijackedConnCloseWriter{c, br}
|
||||
} else {
|
||||
c = &hijackedConn{c, br}
|
||||
}
|
||||
} else {
|
||||
br.Reset(nil)
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// hijackedConn wraps a net.Conn and is returned by setupHijackConn in the case
|
||||
// that a) there was already buffered data in the http layer when Hijack() was
|
||||
// called, and b) the underlying net.Conn does *not* implement CloseWrite().
|
||||
// hijackedConn does not implement CloseWrite() either.
|
||||
type hijackedConn struct {
|
||||
net.Conn
|
||||
r *bufio.Reader
|
||||
}
|
||||
|
||||
func (c *hijackedConn) Read(b []byte) (int, error) {
|
||||
return c.r.Read(b)
|
||||
}
|
||||
|
||||
// hijackedConnCloseWriter is a hijackedConn which additionally implements
|
||||
// CloseWrite(). It is returned by setupHijackConn in the case that a) there
|
||||
// was already buffered data in the http layer when Hijack() was called, and b)
|
||||
// the underlying net.Conn *does* implement CloseWrite().
|
||||
type hijackedConnCloseWriter hijackedConn
|
||||
|
||||
var _ types.CloseWriter = &hijackedConnCloseWriter{}
|
||||
|
||||
func (c *hijackedConnCloseWriter) CloseWrite() error {
|
||||
conn := c.Conn.(types.CloseWriter)
|
||||
return conn.CloseWrite()
|
||||
}
|
138
vendor/github.com/docker/docker-ce/components/engine/client/image_build.go
generated
vendored
Normal file
138
vendor/github.com/docker/docker-ce/components/engine/client/image_build.go
generated
vendored
Normal file
|
@ -0,0 +1,138 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/container"
|
||||
)
|
||||
|
||||
// ImageBuild sends request to the daemon to build images.
|
||||
// The Body in the response implement an io.ReadCloser and it's up to the caller to
|
||||
// close it.
|
||||
func (cli *Client) ImageBuild(ctx context.Context, buildContext io.Reader, options types.ImageBuildOptions) (types.ImageBuildResponse, error) {
|
||||
query, err := cli.imageBuildOptionsToQuery(options)
|
||||
if err != nil {
|
||||
return types.ImageBuildResponse{}, err
|
||||
}
|
||||
|
||||
headers := http.Header(make(map[string][]string))
|
||||
buf, err := json.Marshal(options.AuthConfigs)
|
||||
if err != nil {
|
||||
return types.ImageBuildResponse{}, err
|
||||
}
|
||||
headers.Add("X-Registry-Config", base64.URLEncoding.EncodeToString(buf))
|
||||
|
||||
if options.Platform != "" {
|
||||
if err := cli.NewVersionError("1.32", "platform"); err != nil {
|
||||
return types.ImageBuildResponse{}, err
|
||||
}
|
||||
query.Set("platform", options.Platform)
|
||||
}
|
||||
headers.Set("Content-Type", "application/x-tar")
|
||||
|
||||
serverResp, err := cli.postRaw(ctx, "/build", query, buildContext, headers)
|
||||
if err != nil {
|
||||
return types.ImageBuildResponse{}, err
|
||||
}
|
||||
|
||||
osType := getDockerOS(serverResp.header.Get("Server"))
|
||||
|
||||
return types.ImageBuildResponse{
|
||||
Body: serverResp.body,
|
||||
OSType: osType,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (cli *Client) imageBuildOptionsToQuery(options types.ImageBuildOptions) (url.Values, error) {
|
||||
query := url.Values{
|
||||
"t": options.Tags,
|
||||
"securityopt": options.SecurityOpt,
|
||||
"extrahosts": options.ExtraHosts,
|
||||
}
|
||||
if options.SuppressOutput {
|
||||
query.Set("q", "1")
|
||||
}
|
||||
if options.RemoteContext != "" {
|
||||
query.Set("remote", options.RemoteContext)
|
||||
}
|
||||
if options.NoCache {
|
||||
query.Set("nocache", "1")
|
||||
}
|
||||
if options.Remove {
|
||||
query.Set("rm", "1")
|
||||
} else {
|
||||
query.Set("rm", "0")
|
||||
}
|
||||
|
||||
if options.ForceRemove {
|
||||
query.Set("forcerm", "1")
|
||||
}
|
||||
|
||||
if options.PullParent {
|
||||
query.Set("pull", "1")
|
||||
}
|
||||
|
||||
if options.Squash {
|
||||
if err := cli.NewVersionError("1.25", "squash"); err != nil {
|
||||
return query, err
|
||||
}
|
||||
query.Set("squash", "1")
|
||||
}
|
||||
|
||||
if !container.Isolation.IsDefault(options.Isolation) {
|
||||
query.Set("isolation", string(options.Isolation))
|
||||
}
|
||||
|
||||
query.Set("cpusetcpus", options.CPUSetCPUs)
|
||||
query.Set("networkmode", options.NetworkMode)
|
||||
query.Set("cpusetmems", options.CPUSetMems)
|
||||
query.Set("cpushares", strconv.FormatInt(options.CPUShares, 10))
|
||||
query.Set("cpuquota", strconv.FormatInt(options.CPUQuota, 10))
|
||||
query.Set("cpuperiod", strconv.FormatInt(options.CPUPeriod, 10))
|
||||
query.Set("memory", strconv.FormatInt(options.Memory, 10))
|
||||
query.Set("memswap", strconv.FormatInt(options.MemorySwap, 10))
|
||||
query.Set("cgroupparent", options.CgroupParent)
|
||||
query.Set("shmsize", strconv.FormatInt(options.ShmSize, 10))
|
||||
query.Set("dockerfile", options.Dockerfile)
|
||||
query.Set("target", options.Target)
|
||||
|
||||
ulimitsJSON, err := json.Marshal(options.Ulimits)
|
||||
if err != nil {
|
||||
return query, err
|
||||
}
|
||||
query.Set("ulimits", string(ulimitsJSON))
|
||||
|
||||
buildArgsJSON, err := json.Marshal(options.BuildArgs)
|
||||
if err != nil {
|
||||
return query, err
|
||||
}
|
||||
query.Set("buildargs", string(buildArgsJSON))
|
||||
|
||||
labelsJSON, err := json.Marshal(options.Labels)
|
||||
if err != nil {
|
||||
return query, err
|
||||
}
|
||||
query.Set("labels", string(labelsJSON))
|
||||
|
||||
cacheFromJSON, err := json.Marshal(options.CacheFrom)
|
||||
if err != nil {
|
||||
return query, err
|
||||
}
|
||||
query.Set("cachefrom", string(cacheFromJSON))
|
||||
if options.SessionID != "" {
|
||||
query.Set("session", options.SessionID)
|
||||
}
|
||||
if options.Platform != "" {
|
||||
query.Set("platform", strings.ToLower(options.Platform))
|
||||
}
|
||||
return query, nil
|
||||
}
|
233
vendor/github.com/docker/docker-ce/components/engine/client/image_build_test.go
generated
vendored
Normal file
233
vendor/github.com/docker/docker-ce/components/engine/client/image_build_test.go
generated
vendored
Normal file
|
@ -0,0 +1,233 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/docker/go-units"
|
||||
)
|
||||
|
||||
func TestImageBuildError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
_, err := client.ImageBuild(context.Background(), nil, types.ImageBuildOptions{})
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server Error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestImageBuild(t *testing.T) {
|
||||
v1 := "value1"
|
||||
v2 := "value2"
|
||||
emptyRegistryConfig := "bnVsbA=="
|
||||
buildCases := []struct {
|
||||
buildOptions types.ImageBuildOptions
|
||||
expectedQueryParams map[string]string
|
||||
expectedTags []string
|
||||
expectedRegistryConfig string
|
||||
}{
|
||||
{
|
||||
buildOptions: types.ImageBuildOptions{
|
||||
SuppressOutput: true,
|
||||
NoCache: true,
|
||||
Remove: true,
|
||||
ForceRemove: true,
|
||||
PullParent: true,
|
||||
},
|
||||
expectedQueryParams: map[string]string{
|
||||
"q": "1",
|
||||
"nocache": "1",
|
||||
"rm": "1",
|
||||
"forcerm": "1",
|
||||
"pull": "1",
|
||||
},
|
||||
expectedTags: []string{},
|
||||
expectedRegistryConfig: emptyRegistryConfig,
|
||||
},
|
||||
{
|
||||
buildOptions: types.ImageBuildOptions{
|
||||
SuppressOutput: false,
|
||||
NoCache: false,
|
||||
Remove: false,
|
||||
ForceRemove: false,
|
||||
PullParent: false,
|
||||
},
|
||||
expectedQueryParams: map[string]string{
|
||||
"q": "",
|
||||
"nocache": "",
|
||||
"rm": "0",
|
||||
"forcerm": "",
|
||||
"pull": "",
|
||||
},
|
||||
expectedTags: []string{},
|
||||
expectedRegistryConfig: emptyRegistryConfig,
|
||||
},
|
||||
{
|
||||
buildOptions: types.ImageBuildOptions{
|
||||
RemoteContext: "remoteContext",
|
||||
Isolation: container.Isolation("isolation"),
|
||||
CPUSetCPUs: "2",
|
||||
CPUSetMems: "12",
|
||||
CPUShares: 20,
|
||||
CPUQuota: 10,
|
||||
CPUPeriod: 30,
|
||||
Memory: 256,
|
||||
MemorySwap: 512,
|
||||
ShmSize: 10,
|
||||
CgroupParent: "cgroup_parent",
|
||||
Dockerfile: "Dockerfile",
|
||||
},
|
||||
expectedQueryParams: map[string]string{
|
||||
"remote": "remoteContext",
|
||||
"isolation": "isolation",
|
||||
"cpusetcpus": "2",
|
||||
"cpusetmems": "12",
|
||||
"cpushares": "20",
|
||||
"cpuquota": "10",
|
||||
"cpuperiod": "30",
|
||||
"memory": "256",
|
||||
"memswap": "512",
|
||||
"shmsize": "10",
|
||||
"cgroupparent": "cgroup_parent",
|
||||
"dockerfile": "Dockerfile",
|
||||
"rm": "0",
|
||||
},
|
||||
expectedTags: []string{},
|
||||
expectedRegistryConfig: emptyRegistryConfig,
|
||||
},
|
||||
{
|
||||
buildOptions: types.ImageBuildOptions{
|
||||
BuildArgs: map[string]*string{
|
||||
"ARG1": &v1,
|
||||
"ARG2": &v2,
|
||||
"ARG3": nil,
|
||||
},
|
||||
},
|
||||
expectedQueryParams: map[string]string{
|
||||
"buildargs": `{"ARG1":"value1","ARG2":"value2","ARG3":null}`,
|
||||
"rm": "0",
|
||||
},
|
||||
expectedTags: []string{},
|
||||
expectedRegistryConfig: emptyRegistryConfig,
|
||||
},
|
||||
{
|
||||
buildOptions: types.ImageBuildOptions{
|
||||
Ulimits: []*units.Ulimit{
|
||||
{
|
||||
Name: "nproc",
|
||||
Hard: 65557,
|
||||
Soft: 65557,
|
||||
},
|
||||
{
|
||||
Name: "nofile",
|
||||
Hard: 20000,
|
||||
Soft: 40000,
|
||||
},
|
||||
},
|
||||
},
|
||||
expectedQueryParams: map[string]string{
|
||||
"ulimits": `[{"Name":"nproc","Hard":65557,"Soft":65557},{"Name":"nofile","Hard":20000,"Soft":40000}]`,
|
||||
"rm": "0",
|
||||
},
|
||||
expectedTags: []string{},
|
||||
expectedRegistryConfig: emptyRegistryConfig,
|
||||
},
|
||||
{
|
||||
buildOptions: types.ImageBuildOptions{
|
||||
AuthConfigs: map[string]types.AuthConfig{
|
||||
"https://index.docker.io/v1/": {
|
||||
Auth: "dG90bwo=",
|
||||
},
|
||||
},
|
||||
},
|
||||
expectedQueryParams: map[string]string{
|
||||
"rm": "0",
|
||||
},
|
||||
expectedTags: []string{},
|
||||
expectedRegistryConfig: "eyJodHRwczovL2luZGV4LmRvY2tlci5pby92MS8iOnsiYXV0aCI6ImRHOTBid289In19",
|
||||
},
|
||||
}
|
||||
for _, buildCase := range buildCases {
|
||||
expectedURL := "/build"
|
||||
client := &Client{
|
||||
client: newMockClient(func(r *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(r.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, r.URL)
|
||||
}
|
||||
// Check request headers
|
||||
registryConfig := r.Header.Get("X-Registry-Config")
|
||||
if registryConfig != buildCase.expectedRegistryConfig {
|
||||
return nil, fmt.Errorf("X-Registry-Config header not properly set in the request. Expected '%s', got %s", buildCase.expectedRegistryConfig, registryConfig)
|
||||
}
|
||||
contentType := r.Header.Get("Content-Type")
|
||||
if contentType != "application/x-tar" {
|
||||
return nil, fmt.Errorf("Content-type header not properly set in the request. Expected 'application/x-tar', got %s", contentType)
|
||||
}
|
||||
|
||||
// Check query parameters
|
||||
query := r.URL.Query()
|
||||
for key, expected := range buildCase.expectedQueryParams {
|
||||
actual := query.Get(key)
|
||||
if actual != expected {
|
||||
return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
// Check tags
|
||||
if len(buildCase.expectedTags) > 0 {
|
||||
tags := query["t"]
|
||||
if !reflect.DeepEqual(tags, buildCase.expectedTags) {
|
||||
return nil, fmt.Errorf("t (tags) not set in URL query properly. Expected '%s', got %s", buildCase.expectedTags, tags)
|
||||
}
|
||||
}
|
||||
|
||||
headers := http.Header{}
|
||||
headers.Add("Server", "Docker/v1.23 (MyOS)")
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader([]byte("body"))),
|
||||
Header: headers,
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
buildResponse, err := client.ImageBuild(context.Background(), nil, buildCase.buildOptions)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if buildResponse.OSType != "MyOS" {
|
||||
t.Fatalf("expected OSType to be 'MyOS', got %s", buildResponse.OSType)
|
||||
}
|
||||
response, err := ioutil.ReadAll(buildResponse.Body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
buildResponse.Body.Close()
|
||||
if string(response) != "body" {
|
||||
t.Fatalf("expected Body to contain 'body' string, got %s", response)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetDockerOS(t *testing.T) {
|
||||
cases := map[string]string{
|
||||
"Docker/v1.22 (linux)": "linux",
|
||||
"Docker/v1.22 (windows)": "windows",
|
||||
"Foo/v1.22 (bar)": "",
|
||||
}
|
||||
for header, os := range cases {
|
||||
g := getDockerOS(header)
|
||||
if g != os {
|
||||
t.Fatalf("Expected %s, got %s", os, g)
|
||||
}
|
||||
}
|
||||
}
|
38
vendor/github.com/docker/docker-ce/components/engine/client/image_create.go
generated
vendored
Normal file
38
vendor/github.com/docker/docker-ce/components/engine/client/image_create.go
generated
vendored
Normal file
|
@ -0,0 +1,38 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
|
||||
"github.com/docker/distribution/reference"
|
||||
"github.com/docker/docker/api/types"
|
||||
)
|
||||
|
||||
// ImageCreate creates a new image based in the parent options.
|
||||
// It returns the JSON content in the response body.
|
||||
func (cli *Client) ImageCreate(ctx context.Context, parentReference string, options types.ImageCreateOptions) (io.ReadCloser, error) {
|
||||
ref, err := reference.ParseNormalizedNamed(parentReference)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
query := url.Values{}
|
||||
query.Set("fromImage", reference.FamiliarName(ref))
|
||||
query.Set("tag", getAPITagFromNamedRef(ref))
|
||||
if options.Platform != "" {
|
||||
query.Set("platform", strings.ToLower(options.Platform))
|
||||
}
|
||||
resp, err := cli.tryImageCreate(ctx, query, options.RegistryAuth)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp.body, nil
|
||||
}
|
||||
|
||||
func (cli *Client) tryImageCreate(ctx context.Context, query url.Values, registryAuth string) (serverResponse, error) {
|
||||
headers := map[string][]string{"X-Registry-Auth": {registryAuth}}
|
||||
return cli.post(ctx, "/images/create", query, nil, headers)
|
||||
}
|
76
vendor/github.com/docker/docker-ce/components/engine/client/image_create_test.go
generated
vendored
Normal file
76
vendor/github.com/docker/docker-ce/components/engine/client/image_create_test.go
generated
vendored
Normal file
|
@ -0,0 +1,76 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
)
|
||||
|
||||
func TestImageCreateError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
_, err := client.ImageCreate(context.Background(), "reference", types.ImageCreateOptions{})
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestImageCreate(t *testing.T) {
|
||||
expectedURL := "/images/create"
|
||||
expectedImage := "test:5000/my_image"
|
||||
expectedTag := "sha256:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
|
||||
expectedReference := fmt.Sprintf("%s@%s", expectedImage, expectedTag)
|
||||
expectedRegistryAuth := "eyJodHRwczovL2luZGV4LmRvY2tlci5pby92MS8iOnsiYXV0aCI6ImRHOTBid289IiwiZW1haWwiOiJqb2huQGRvZS5jb20ifX0="
|
||||
client := &Client{
|
||||
client: newMockClient(func(r *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(r.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, r.URL)
|
||||
}
|
||||
registryAuth := r.Header.Get("X-Registry-Auth")
|
||||
if registryAuth != expectedRegistryAuth {
|
||||
return nil, fmt.Errorf("X-Registry-Auth header not properly set in the request. Expected '%s', got %s", expectedRegistryAuth, registryAuth)
|
||||
}
|
||||
|
||||
query := r.URL.Query()
|
||||
fromImage := query.Get("fromImage")
|
||||
if fromImage != expectedImage {
|
||||
return nil, fmt.Errorf("fromImage not set in URL query properly. Expected '%s', got %s", expectedImage, fromImage)
|
||||
}
|
||||
|
||||
tag := query.Get("tag")
|
||||
if tag != expectedTag {
|
||||
return nil, fmt.Errorf("tag not set in URL query properly. Expected '%s', got %s", expectedTag, tag)
|
||||
}
|
||||
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader([]byte("body"))),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
|
||||
createResponse, err := client.ImageCreate(context.Background(), expectedReference, types.ImageCreateOptions{
|
||||
RegistryAuth: expectedRegistryAuth,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
response, err := ioutil.ReadAll(createResponse)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = createResponse.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(response) != "body" {
|
||||
t.Fatalf("expected Body to contain 'body' string, got %s", response)
|
||||
}
|
||||
}
|
22
vendor/github.com/docker/docker-ce/components/engine/client/image_history.go
generated
vendored
Normal file
22
vendor/github.com/docker/docker-ce/components/engine/client/image_history.go
generated
vendored
Normal file
|
@ -0,0 +1,22 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/url"
|
||||
|
||||
"github.com/docker/docker/api/types/image"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// ImageHistory returns the changes in an image in history format.
|
||||
func (cli *Client) ImageHistory(ctx context.Context, imageID string) ([]image.HistoryResponseItem, error) {
|
||||
var history []image.HistoryResponseItem
|
||||
serverResp, err := cli.get(ctx, "/images/"+imageID+"/history", url.Values{}, nil)
|
||||
if err != nil {
|
||||
return history, err
|
||||
}
|
||||
|
||||
err = json.NewDecoder(serverResp.body).Decode(&history)
|
||||
ensureReaderClosed(serverResp)
|
||||
return history, err
|
||||
}
|
60
vendor/github.com/docker/docker-ce/components/engine/client/image_history_test.go
generated
vendored
Normal file
60
vendor/github.com/docker/docker-ce/components/engine/client/image_history_test.go
generated
vendored
Normal file
|
@ -0,0 +1,60 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/docker/api/types/image"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestImageHistoryError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
_, err := client.ImageHistory(context.Background(), "nothing")
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestImageHistory(t *testing.T) {
|
||||
expectedURL := "/images/image_id/history"
|
||||
client := &Client{
|
||||
client: newMockClient(func(r *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(r.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, r.URL)
|
||||
}
|
||||
b, err := json.Marshal([]image.HistoryResponseItem{
|
||||
{
|
||||
ID: "image_id1",
|
||||
Tags: []string{"tag1", "tag2"},
|
||||
},
|
||||
{
|
||||
ID: "image_id2",
|
||||
Tags: []string{"tag1", "tag2"},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader(b)),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
imageHistories, err := client.ImageHistory(context.Background(), "image_id")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(imageHistories) != 2 {
|
||||
t.Fatalf("expected 2 containers, got %v", imageHistories)
|
||||
}
|
||||
}
|
41
vendor/github.com/docker/docker-ce/components/engine/client/image_import.go
generated
vendored
Normal file
41
vendor/github.com/docker/docker-ce/components/engine/client/image_import.go
generated
vendored
Normal file
|
@ -0,0 +1,41 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
|
||||
"github.com/docker/distribution/reference"
|
||||
"github.com/docker/docker/api/types"
|
||||
)
|
||||
|
||||
// ImageImport creates a new image based in the source options.
|
||||
// It returns the JSON content in the response body.
|
||||
func (cli *Client) ImageImport(ctx context.Context, source types.ImageImportSource, ref string, options types.ImageImportOptions) (io.ReadCloser, error) {
|
||||
if ref != "" {
|
||||
//Check if the given image name can be resolved
|
||||
if _, err := reference.ParseNormalizedNamed(ref); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
query := url.Values{}
|
||||
query.Set("fromSrc", source.SourceName)
|
||||
query.Set("repo", ref)
|
||||
query.Set("tag", options.Tag)
|
||||
query.Set("message", options.Message)
|
||||
if options.Platform != "" {
|
||||
query.Set("platform", strings.ToLower(options.Platform))
|
||||
}
|
||||
for _, change := range options.Changes {
|
||||
query.Add("changes", change)
|
||||
}
|
||||
|
||||
resp, err := cli.postRaw(ctx, "/images/create", query, source.Source, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp.body, nil
|
||||
}
|
81
vendor/github.com/docker/docker-ce/components/engine/client/image_import_test.go
generated
vendored
Normal file
81
vendor/github.com/docker/docker-ce/components/engine/client/image_import_test.go
generated
vendored
Normal file
|
@ -0,0 +1,81 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestImageImportError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
_, err := client.ImageImport(context.Background(), types.ImageImportSource{}, "image:tag", types.ImageImportOptions{})
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestImageImport(t *testing.T) {
|
||||
expectedURL := "/images/create"
|
||||
client := &Client{
|
||||
client: newMockClient(func(r *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(r.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, r.URL)
|
||||
}
|
||||
query := r.URL.Query()
|
||||
fromSrc := query.Get("fromSrc")
|
||||
if fromSrc != "image_source" {
|
||||
return nil, fmt.Errorf("fromSrc not set in URL query properly. Expected 'image_source', got %s", fromSrc)
|
||||
}
|
||||
repo := query.Get("repo")
|
||||
if repo != "repository_name:imported" {
|
||||
return nil, fmt.Errorf("repo not set in URL query properly. Expected 'repository_name:imported', got %s", repo)
|
||||
}
|
||||
tag := query.Get("tag")
|
||||
if tag != "imported" {
|
||||
return nil, fmt.Errorf("tag not set in URL query properly. Expected 'imported', got %s", tag)
|
||||
}
|
||||
message := query.Get("message")
|
||||
if message != "A message" {
|
||||
return nil, fmt.Errorf("message not set in URL query properly. Expected 'A message', got %s", message)
|
||||
}
|
||||
changes := query["changes"]
|
||||
expectedChanges := []string{"change1", "change2"}
|
||||
if !reflect.DeepEqual(expectedChanges, changes) {
|
||||
return nil, fmt.Errorf("changes not set in URL query properly. Expected %v, got %v", expectedChanges, changes)
|
||||
}
|
||||
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader([]byte("response"))),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
importResponse, err := client.ImageImport(context.Background(), types.ImageImportSource{
|
||||
Source: strings.NewReader("source"),
|
||||
SourceName: "image_source",
|
||||
}, "repository_name:imported", types.ImageImportOptions{
|
||||
Tag: "imported",
|
||||
Message: "A message",
|
||||
Changes: []string{"change1", "change2"},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
response, err := ioutil.ReadAll(importResponse)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
importResponse.Close()
|
||||
if string(response) != "response" {
|
||||
t.Fatalf("expected response to contain 'response', got %s", string(response))
|
||||
}
|
||||
}
|
32
vendor/github.com/docker/docker-ce/components/engine/client/image_inspect.go
generated
vendored
Normal file
32
vendor/github.com/docker/docker-ce/components/engine/client/image_inspect.go
generated
vendored
Normal file
|
@ -0,0 +1,32 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// ImageInspectWithRaw returns the image information and its raw representation.
|
||||
func (cli *Client) ImageInspectWithRaw(ctx context.Context, imageID string) (types.ImageInspect, []byte, error) {
|
||||
if imageID == "" {
|
||||
return types.ImageInspect{}, nil, objectNotFoundError{object: "image", id: imageID}
|
||||
}
|
||||
serverResp, err := cli.get(ctx, "/images/"+imageID+"/json", nil, nil)
|
||||
if err != nil {
|
||||
return types.ImageInspect{}, nil, wrapResponseError(err, serverResp, "image", imageID)
|
||||
}
|
||||
defer ensureReaderClosed(serverResp)
|
||||
|
||||
body, err := ioutil.ReadAll(serverResp.body)
|
||||
if err != nil {
|
||||
return types.ImageInspect{}, nil, err
|
||||
}
|
||||
|
||||
var response types.ImageInspect
|
||||
rdr := bytes.NewReader(body)
|
||||
err = json.NewDecoder(rdr).Decode(&response)
|
||||
return response, body, err
|
||||
}
|
84
vendor/github.com/docker/docker-ce/components/engine/client/image_inspect_test.go
generated
vendored
Normal file
84
vendor/github.com/docker/docker-ce/components/engine/client/image_inspect_test.go
generated
vendored
Normal file
|
@ -0,0 +1,84 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/pkg/errors"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestImageInspectError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
|
||||
_, _, err := client.ImageInspectWithRaw(context.Background(), "nothing")
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server Error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestImageInspectImageNotFound(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
|
||||
}
|
||||
|
||||
_, _, err := client.ImageInspectWithRaw(context.Background(), "unknown")
|
||||
if err == nil || !IsErrNotFound(err) {
|
||||
t.Fatalf("expected an imageNotFound error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestImageInspectWithEmptyID(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
return nil, errors.New("should not make request")
|
||||
}),
|
||||
}
|
||||
_, _, err := client.ImageInspectWithRaw(context.Background(), "")
|
||||
if !IsErrNotFound(err) {
|
||||
t.Fatalf("Expected NotFoundError, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestImageInspect(t *testing.T) {
|
||||
expectedURL := "/images/image_id/json"
|
||||
expectedTags := []string{"tag1", "tag2"}
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
content, err := json.Marshal(types.ImageInspect{
|
||||
ID: "image_id",
|
||||
RepoTags: expectedTags,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader(content)),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
|
||||
imageInspect, _, err := client.ImageInspectWithRaw(context.Background(), "image_id")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if imageInspect.ID != "image_id" {
|
||||
t.Fatalf("expected `image_id`, got %s", imageInspect.ID)
|
||||
}
|
||||
if !reflect.DeepEqual(imageInspect.RepoTags, expectedTags) {
|
||||
t.Fatalf("expected `%v`, got %v", expectedTags, imageInspect.RepoTags)
|
||||
}
|
||||
}
|
45
vendor/github.com/docker/docker-ce/components/engine/client/image_list.go
generated
vendored
Normal file
45
vendor/github.com/docker/docker-ce/components/engine/client/image_list.go
generated
vendored
Normal file
|
@ -0,0 +1,45 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/url"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
"github.com/docker/docker/api/types/versions"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// ImageList returns a list of images in the docker host.
|
||||
func (cli *Client) ImageList(ctx context.Context, options types.ImageListOptions) ([]types.ImageSummary, error) {
|
||||
var images []types.ImageSummary
|
||||
query := url.Values{}
|
||||
|
||||
optionFilters := options.Filters
|
||||
referenceFilters := optionFilters.Get("reference")
|
||||
if versions.LessThan(cli.version, "1.25") && len(referenceFilters) > 0 {
|
||||
query.Set("filter", referenceFilters[0])
|
||||
for _, filterValue := range referenceFilters {
|
||||
optionFilters.Del("reference", filterValue)
|
||||
}
|
||||
}
|
||||
if optionFilters.Len() > 0 {
|
||||
filterJSON, err := filters.ToParamWithVersion(cli.version, optionFilters)
|
||||
if err != nil {
|
||||
return images, err
|
||||
}
|
||||
query.Set("filters", filterJSON)
|
||||
}
|
||||
if options.All {
|
||||
query.Set("all", "1")
|
||||
}
|
||||
|
||||
serverResp, err := cli.get(ctx, "/images/json", query, nil)
|
||||
if err != nil {
|
||||
return images, err
|
||||
}
|
||||
|
||||
err = json.NewDecoder(serverResp.body).Decode(&images)
|
||||
ensureReaderClosed(serverResp)
|
||||
return images, err
|
||||
}
|
159
vendor/github.com/docker/docker-ce/components/engine/client/image_list_test.go
generated
vendored
Normal file
159
vendor/github.com/docker/docker-ce/components/engine/client/image_list_test.go
generated
vendored
Normal file
|
@ -0,0 +1,159 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestImageListError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
|
||||
_, err := client.ImageList(context.Background(), types.ImageListOptions{})
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server Error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestImageList(t *testing.T) {
|
||||
expectedURL := "/images/json"
|
||||
|
||||
noDanglingfilters := filters.NewArgs()
|
||||
noDanglingfilters.Add("dangling", "false")
|
||||
|
||||
filters := filters.NewArgs()
|
||||
filters.Add("label", "label1")
|
||||
filters.Add("label", "label2")
|
||||
filters.Add("dangling", "true")
|
||||
|
||||
listCases := []struct {
|
||||
options types.ImageListOptions
|
||||
expectedQueryParams map[string]string
|
||||
}{
|
||||
{
|
||||
options: types.ImageListOptions{},
|
||||
expectedQueryParams: map[string]string{
|
||||
"all": "",
|
||||
"filter": "",
|
||||
"filters": "",
|
||||
},
|
||||
},
|
||||
{
|
||||
options: types.ImageListOptions{
|
||||
Filters: filters,
|
||||
},
|
||||
expectedQueryParams: map[string]string{
|
||||
"all": "",
|
||||
"filter": "",
|
||||
"filters": `{"dangling":{"true":true},"label":{"label1":true,"label2":true}}`,
|
||||
},
|
||||
},
|
||||
{
|
||||
options: types.ImageListOptions{
|
||||
Filters: noDanglingfilters,
|
||||
},
|
||||
expectedQueryParams: map[string]string{
|
||||
"all": "",
|
||||
"filter": "",
|
||||
"filters": `{"dangling":{"false":true}}`,
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, listCase := range listCases {
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
query := req.URL.Query()
|
||||
for key, expected := range listCase.expectedQueryParams {
|
||||
actual := query.Get(key)
|
||||
if actual != expected {
|
||||
return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
|
||||
}
|
||||
}
|
||||
content, err := json.Marshal([]types.ImageSummary{
|
||||
{
|
||||
ID: "image_id2",
|
||||
},
|
||||
{
|
||||
ID: "image_id2",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader(content)),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
|
||||
images, err := client.ImageList(context.Background(), listCase.options)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(images) != 2 {
|
||||
t.Fatalf("expected 2 images, got %v", images)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestImageListApiBefore125(t *testing.T) {
|
||||
expectedFilter := "image:tag"
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
query := req.URL.Query()
|
||||
actualFilter := query.Get("filter")
|
||||
if actualFilter != expectedFilter {
|
||||
return nil, fmt.Errorf("filter not set in URL query properly. Expected '%s', got %s", expectedFilter, actualFilter)
|
||||
}
|
||||
actualFilters := query.Get("filters")
|
||||
if actualFilters != "" {
|
||||
return nil, fmt.Errorf("filters should have not been present, were with value: %s", actualFilters)
|
||||
}
|
||||
content, err := json.Marshal([]types.ImageSummary{
|
||||
{
|
||||
ID: "image_id2",
|
||||
},
|
||||
{
|
||||
ID: "image_id2",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader(content)),
|
||||
}, nil
|
||||
}),
|
||||
version: "1.24",
|
||||
}
|
||||
|
||||
filters := filters.NewArgs()
|
||||
filters.Add("reference", "image:tag")
|
||||
|
||||
options := types.ImageListOptions{
|
||||
Filters: filters,
|
||||
}
|
||||
|
||||
images, err := client.ImageList(context.Background(), options)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(images) != 2 {
|
||||
t.Fatalf("expected 2 images, got %v", images)
|
||||
}
|
||||
}
|
30
vendor/github.com/docker/docker-ce/components/engine/client/image_load.go
generated
vendored
Normal file
30
vendor/github.com/docker/docker-ce/components/engine/client/image_load.go
generated
vendored
Normal file
|
@ -0,0 +1,30 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/url"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
)
|
||||
|
||||
// ImageLoad loads an image in the docker host from the client host.
|
||||
// It's up to the caller to close the io.ReadCloser in the
|
||||
// ImageLoadResponse returned by this function.
|
||||
func (cli *Client) ImageLoad(ctx context.Context, input io.Reader, quiet bool) (types.ImageLoadResponse, error) {
|
||||
v := url.Values{}
|
||||
v.Set("quiet", "0")
|
||||
if quiet {
|
||||
v.Set("quiet", "1")
|
||||
}
|
||||
headers := map[string][]string{"Content-Type": {"application/x-tar"}}
|
||||
resp, err := cli.postRaw(ctx, "/images/load", v, input, headers)
|
||||
if err != nil {
|
||||
return types.ImageLoadResponse{}, err
|
||||
}
|
||||
return types.ImageLoadResponse{
|
||||
Body: resp.body,
|
||||
JSON: resp.header.Get("Content-Type") == "application/json",
|
||||
}, nil
|
||||
}
|
95
vendor/github.com/docker/docker-ce/components/engine/client/image_load_test.go
generated
vendored
Normal file
95
vendor/github.com/docker/docker-ce/components/engine/client/image_load_test.go
generated
vendored
Normal file
|
@ -0,0 +1,95 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestImageLoadError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
|
||||
_, err := client.ImageLoad(context.Background(), nil, true)
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server Error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestImageLoad(t *testing.T) {
|
||||
expectedURL := "/images/load"
|
||||
expectedInput := "inputBody"
|
||||
expectedOutput := "outputBody"
|
||||
loadCases := []struct {
|
||||
quiet bool
|
||||
responseContentType string
|
||||
expectedResponseJSON bool
|
||||
expectedQueryParams map[string]string
|
||||
}{
|
||||
{
|
||||
quiet: false,
|
||||
responseContentType: "text/plain",
|
||||
expectedResponseJSON: false,
|
||||
expectedQueryParams: map[string]string{
|
||||
"quiet": "0",
|
||||
},
|
||||
},
|
||||
{
|
||||
quiet: true,
|
||||
responseContentType: "application/json",
|
||||
expectedResponseJSON: true,
|
||||
expectedQueryParams: map[string]string{
|
||||
"quiet": "1",
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, loadCase := range loadCases {
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
contentType := req.Header.Get("Content-Type")
|
||||
if contentType != "application/x-tar" {
|
||||
return nil, fmt.Errorf("content-type not set in URL headers properly. Expected 'application/x-tar', got %s", contentType)
|
||||
}
|
||||
query := req.URL.Query()
|
||||
for key, expected := range loadCase.expectedQueryParams {
|
||||
actual := query.Get(key)
|
||||
if actual != expected {
|
||||
return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
|
||||
}
|
||||
}
|
||||
headers := http.Header{}
|
||||
headers.Add("Content-Type", loadCase.responseContentType)
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader([]byte(expectedOutput))),
|
||||
Header: headers,
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
|
||||
input := bytes.NewReader([]byte(expectedInput))
|
||||
imageLoadResponse, err := client.ImageLoad(context.Background(), input, loadCase.quiet)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if imageLoadResponse.JSON != loadCase.expectedResponseJSON {
|
||||
t.Fatalf("expected a JSON response, was not.")
|
||||
}
|
||||
body, err := ioutil.ReadAll(imageLoadResponse.Body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(body) != expectedOutput {
|
||||
t.Fatalf("expected %s, got %s", expectedOutput, string(body))
|
||||
}
|
||||
}
|
||||
}
|
36
vendor/github.com/docker/docker-ce/components/engine/client/image_prune.go
generated
vendored
Normal file
36
vendor/github.com/docker/docker-ce/components/engine/client/image_prune.go
generated
vendored
Normal file
|
@ -0,0 +1,36 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// ImagesPrune requests the daemon to delete unused data
|
||||
func (cli *Client) ImagesPrune(ctx context.Context, pruneFilters filters.Args) (types.ImagesPruneReport, error) {
|
||||
var report types.ImagesPruneReport
|
||||
|
||||
if err := cli.NewVersionError("1.25", "image prune"); err != nil {
|
||||
return report, err
|
||||
}
|
||||
|
||||
query, err := getFiltersQuery(pruneFilters)
|
||||
if err != nil {
|
||||
return report, err
|
||||
}
|
||||
|
||||
serverResp, err := cli.post(ctx, "/images/prune", query, nil, nil)
|
||||
if err != nil {
|
||||
return report, err
|
||||
}
|
||||
defer ensureReaderClosed(serverResp)
|
||||
|
||||
if err := json.NewDecoder(serverResp.body).Decode(&report); err != nil {
|
||||
return report, fmt.Errorf("Error retrieving disk usage: %v", err)
|
||||
}
|
||||
|
||||
return report, nil
|
||||
}
|
120
vendor/github.com/docker/docker-ce/components/engine/client/image_prune_test.go
generated
vendored
Normal file
120
vendor/github.com/docker/docker-ce/components/engine/client/image_prune_test.go
generated
vendored
Normal file
|
@ -0,0 +1,120 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
"github.com/gotestyourself/gotestyourself/assert"
|
||||
is "github.com/gotestyourself/gotestyourself/assert/cmp"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestImagesPruneError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
version: "1.25",
|
||||
}
|
||||
|
||||
filters := filters.NewArgs()
|
||||
|
||||
_, err := client.ImagesPrune(context.Background(), filters)
|
||||
assert.Check(t, is.Error(err, "Error response from daemon: Server error"))
|
||||
}
|
||||
|
||||
func TestImagesPrune(t *testing.T) {
|
||||
expectedURL := "/v1.25/images/prune"
|
||||
|
||||
danglingFilters := filters.NewArgs()
|
||||
danglingFilters.Add("dangling", "true")
|
||||
|
||||
noDanglingFilters := filters.NewArgs()
|
||||
noDanglingFilters.Add("dangling", "false")
|
||||
|
||||
labelFilters := filters.NewArgs()
|
||||
labelFilters.Add("dangling", "true")
|
||||
labelFilters.Add("label", "label1=foo")
|
||||
labelFilters.Add("label", "label2!=bar")
|
||||
|
||||
listCases := []struct {
|
||||
filters filters.Args
|
||||
expectedQueryParams map[string]string
|
||||
}{
|
||||
{
|
||||
filters: filters.Args{},
|
||||
expectedQueryParams: map[string]string{
|
||||
"until": "",
|
||||
"filter": "",
|
||||
"filters": "",
|
||||
},
|
||||
},
|
||||
{
|
||||
filters: danglingFilters,
|
||||
expectedQueryParams: map[string]string{
|
||||
"until": "",
|
||||
"filter": "",
|
||||
"filters": `{"dangling":{"true":true}}`,
|
||||
},
|
||||
},
|
||||
{
|
||||
filters: noDanglingFilters,
|
||||
expectedQueryParams: map[string]string{
|
||||
"until": "",
|
||||
"filter": "",
|
||||
"filters": `{"dangling":{"false":true}}`,
|
||||
},
|
||||
},
|
||||
{
|
||||
filters: labelFilters,
|
||||
expectedQueryParams: map[string]string{
|
||||
"until": "",
|
||||
"filter": "",
|
||||
"filters": `{"dangling":{"true":true},"label":{"label1=foo":true,"label2!=bar":true}}`,
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, listCase := range listCases {
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
query := req.URL.Query()
|
||||
for key, expected := range listCase.expectedQueryParams {
|
||||
actual := query.Get(key)
|
||||
assert.Check(t, is.Equal(expected, actual))
|
||||
}
|
||||
content, err := json.Marshal(types.ImagesPruneReport{
|
||||
ImagesDeleted: []types.ImageDeleteResponseItem{
|
||||
{
|
||||
Deleted: "image_id1",
|
||||
},
|
||||
{
|
||||
Deleted: "image_id2",
|
||||
},
|
||||
},
|
||||
SpaceReclaimed: 9999,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader(content)),
|
||||
}, nil
|
||||
}),
|
||||
version: "1.25",
|
||||
}
|
||||
|
||||
report, err := client.ImagesPrune(context.Background(), listCase.filters)
|
||||
assert.Check(t, err)
|
||||
assert.Check(t, is.Len(report.ImagesDeleted, 2))
|
||||
assert.Check(t, is.Equal(uint64(9999), report.SpaceReclaimed))
|
||||
}
|
||||
}
|
65
vendor/github.com/docker/docker-ce/components/engine/client/image_pull.go
generated
vendored
Normal file
65
vendor/github.com/docker/docker-ce/components/engine/client/image_pull.go
generated
vendored
Normal file
|
@ -0,0 +1,65 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
|
||||
"github.com/docker/distribution/reference"
|
||||
"github.com/docker/docker/api/types"
|
||||
)
|
||||
|
||||
// ImagePull requests the docker host to pull an image from a remote registry.
|
||||
// It executes the privileged function if the operation is unauthorized
|
||||
// and it tries one more time.
|
||||
// It's up to the caller to handle the io.ReadCloser and close it properly.
|
||||
//
|
||||
// FIXME(vdemeester): there is currently used in a few way in docker/docker
|
||||
// - if not in trusted content, ref is used to pass the whole reference, and tag is empty
|
||||
// - if in trusted content, ref is used to pass the reference name, and tag for the digest
|
||||
func (cli *Client) ImagePull(ctx context.Context, refStr string, options types.ImagePullOptions) (io.ReadCloser, error) {
|
||||
ref, err := reference.ParseNormalizedNamed(refStr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
query := url.Values{}
|
||||
query.Set("fromImage", reference.FamiliarName(ref))
|
||||
if !options.All {
|
||||
query.Set("tag", getAPITagFromNamedRef(ref))
|
||||
}
|
||||
if options.Platform != "" {
|
||||
query.Set("platform", strings.ToLower(options.Platform))
|
||||
}
|
||||
|
||||
resp, err := cli.tryImageCreate(ctx, query, options.RegistryAuth)
|
||||
if resp.statusCode == http.StatusUnauthorized && options.PrivilegeFunc != nil {
|
||||
newAuthHeader, privilegeErr := options.PrivilegeFunc()
|
||||
if privilegeErr != nil {
|
||||
return nil, privilegeErr
|
||||
}
|
||||
resp, err = cli.tryImageCreate(ctx, query, newAuthHeader)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp.body, nil
|
||||
}
|
||||
|
||||
// getAPITagFromNamedRef returns a tag from the specified reference.
|
||||
// This function is necessary as long as the docker "server" api expects
|
||||
// digests to be sent as tags and makes a distinction between the name
|
||||
// and tag/digest part of a reference.
|
||||
func getAPITagFromNamedRef(ref reference.Named) string {
|
||||
if digested, ok := ref.(reference.Digested); ok {
|
||||
return digested.Digest().String()
|
||||
}
|
||||
ref = reference.TagNameOnly(ref)
|
||||
if tagged, ok := ref.(reference.Tagged); ok {
|
||||
return tagged.Tag()
|
||||
}
|
||||
return ""
|
||||
}
|
199
vendor/github.com/docker/docker-ce/components/engine/client/image_pull_test.go
generated
vendored
Normal file
199
vendor/github.com/docker/docker-ce/components/engine/client/image_pull_test.go
generated
vendored
Normal file
|
@ -0,0 +1,199 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
)
|
||||
|
||||
func TestImagePullReferenceParseError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
return nil, nil
|
||||
}),
|
||||
}
|
||||
// An empty reference is an invalid reference
|
||||
_, err := client.ImagePull(context.Background(), "", types.ImagePullOptions{})
|
||||
if err == nil || !strings.Contains(err.Error(), "invalid reference format") {
|
||||
t.Fatalf("expected an error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestImagePullAnyError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
_, err := client.ImagePull(context.Background(), "myimage", types.ImagePullOptions{})
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server Error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestImagePullStatusUnauthorizedError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")),
|
||||
}
|
||||
_, err := client.ImagePull(context.Background(), "myimage", types.ImagePullOptions{})
|
||||
if err == nil || err.Error() != "Error response from daemon: Unauthorized error" {
|
||||
t.Fatalf("expected an Unauthorized Error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestImagePullWithUnauthorizedErrorAndPrivilegeFuncError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")),
|
||||
}
|
||||
privilegeFunc := func() (string, error) {
|
||||
return "", fmt.Errorf("Error requesting privilege")
|
||||
}
|
||||
_, err := client.ImagePull(context.Background(), "myimage", types.ImagePullOptions{
|
||||
PrivilegeFunc: privilegeFunc,
|
||||
})
|
||||
if err == nil || err.Error() != "Error requesting privilege" {
|
||||
t.Fatalf("expected an error requesting privilege, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestImagePullWithUnauthorizedErrorAndAnotherUnauthorizedError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")),
|
||||
}
|
||||
privilegeFunc := func() (string, error) {
|
||||
return "a-auth-header", nil
|
||||
}
|
||||
_, err := client.ImagePull(context.Background(), "myimage", types.ImagePullOptions{
|
||||
PrivilegeFunc: privilegeFunc,
|
||||
})
|
||||
if err == nil || err.Error() != "Error response from daemon: Unauthorized error" {
|
||||
t.Fatalf("expected an Unauthorized Error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestImagePullWithPrivilegedFuncNoError(t *testing.T) {
|
||||
expectedURL := "/images/create"
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
auth := req.Header.Get("X-Registry-Auth")
|
||||
if auth == "NotValid" {
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusUnauthorized,
|
||||
Body: ioutil.NopCloser(bytes.NewReader([]byte("Invalid credentials"))),
|
||||
}, nil
|
||||
}
|
||||
if auth != "IAmValid" {
|
||||
return nil, fmt.Errorf("Invalid auth header : expected %s, got %s", "IAmValid", auth)
|
||||
}
|
||||
query := req.URL.Query()
|
||||
fromImage := query.Get("fromImage")
|
||||
if fromImage != "myimage" {
|
||||
return nil, fmt.Errorf("fromimage not set in URL query properly. Expected '%s', got %s", "myimage", fromImage)
|
||||
}
|
||||
tag := query.Get("tag")
|
||||
if tag != "latest" {
|
||||
return nil, fmt.Errorf("tag not set in URL query properly. Expected '%s', got %s", "latest", tag)
|
||||
}
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader([]byte("hello world"))),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
privilegeFunc := func() (string, error) {
|
||||
return "IAmValid", nil
|
||||
}
|
||||
resp, err := client.ImagePull(context.Background(), "myimage", types.ImagePullOptions{
|
||||
RegistryAuth: "NotValid",
|
||||
PrivilegeFunc: privilegeFunc,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
body, err := ioutil.ReadAll(resp)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(body) != "hello world" {
|
||||
t.Fatalf("expected 'hello world', got %s", string(body))
|
||||
}
|
||||
}
|
||||
|
||||
func TestImagePullWithoutErrors(t *testing.T) {
|
||||
expectedURL := "/images/create"
|
||||
expectedOutput := "hello world"
|
||||
pullCases := []struct {
|
||||
all bool
|
||||
reference string
|
||||
expectedImage string
|
||||
expectedTag string
|
||||
}{
|
||||
{
|
||||
all: false,
|
||||
reference: "myimage",
|
||||
expectedImage: "myimage",
|
||||
expectedTag: "latest",
|
||||
},
|
||||
{
|
||||
all: false,
|
||||
reference: "myimage:tag",
|
||||
expectedImage: "myimage",
|
||||
expectedTag: "tag",
|
||||
},
|
||||
{
|
||||
all: true,
|
||||
reference: "myimage",
|
||||
expectedImage: "myimage",
|
||||
expectedTag: "",
|
||||
},
|
||||
{
|
||||
all: true,
|
||||
reference: "myimage:anything",
|
||||
expectedImage: "myimage",
|
||||
expectedTag: "",
|
||||
},
|
||||
}
|
||||
for _, pullCase := range pullCases {
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
query := req.URL.Query()
|
||||
fromImage := query.Get("fromImage")
|
||||
if fromImage != pullCase.expectedImage {
|
||||
return nil, fmt.Errorf("fromimage not set in URL query properly. Expected '%s', got %s", pullCase.expectedImage, fromImage)
|
||||
}
|
||||
tag := query.Get("tag")
|
||||
if tag != pullCase.expectedTag {
|
||||
return nil, fmt.Errorf("tag not set in URL query properly. Expected '%s', got %s", pullCase.expectedTag, tag)
|
||||
}
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader([]byte(expectedOutput))),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
resp, err := client.ImagePull(context.Background(), pullCase.reference, types.ImagePullOptions{
|
||||
All: pullCase.all,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
body, err := ioutil.ReadAll(resp)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(body) != expectedOutput {
|
||||
t.Fatalf("expected '%s', got %s", expectedOutput, string(body))
|
||||
}
|
||||
}
|
||||
}
|
56
vendor/github.com/docker/docker-ce/components/engine/client/image_push.go
generated
vendored
Normal file
56
vendor/github.com/docker/docker-ce/components/engine/client/image_push.go
generated
vendored
Normal file
|
@ -0,0 +1,56 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
|
||||
"github.com/docker/distribution/reference"
|
||||
"github.com/docker/docker/api/types"
|
||||
)
|
||||
|
||||
// ImagePush requests the docker host to push an image to a remote registry.
|
||||
// It executes the privileged function if the operation is unauthorized
|
||||
// and it tries one more time.
|
||||
// It's up to the caller to handle the io.ReadCloser and close it properly.
|
||||
func (cli *Client) ImagePush(ctx context.Context, image string, options types.ImagePushOptions) (io.ReadCloser, error) {
|
||||
ref, err := reference.ParseNormalizedNamed(image)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if _, isCanonical := ref.(reference.Canonical); isCanonical {
|
||||
return nil, errors.New("cannot push a digest reference")
|
||||
}
|
||||
|
||||
tag := ""
|
||||
name := reference.FamiliarName(ref)
|
||||
|
||||
if nameTaggedRef, isNamedTagged := ref.(reference.NamedTagged); isNamedTagged {
|
||||
tag = nameTaggedRef.Tag()
|
||||
}
|
||||
|
||||
query := url.Values{}
|
||||
query.Set("tag", tag)
|
||||
|
||||
resp, err := cli.tryImagePush(ctx, name, query, options.RegistryAuth)
|
||||
if resp.statusCode == http.StatusUnauthorized && options.PrivilegeFunc != nil {
|
||||
newAuthHeader, privilegeErr := options.PrivilegeFunc()
|
||||
if privilegeErr != nil {
|
||||
return nil, privilegeErr
|
||||
}
|
||||
resp, err = cli.tryImagePush(ctx, name, query, newAuthHeader)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp.body, nil
|
||||
}
|
||||
|
||||
func (cli *Client) tryImagePush(ctx context.Context, imageID string, query url.Values, registryAuth string) (serverResponse, error) {
|
||||
headers := map[string][]string{"X-Registry-Auth": {registryAuth}}
|
||||
return cli.post(ctx, "/images/"+imageID+"/push", query, nil, headers)
|
||||
}
|
180
vendor/github.com/docker/docker-ce/components/engine/client/image_push_test.go
generated
vendored
Normal file
180
vendor/github.com/docker/docker-ce/components/engine/client/image_push_test.go
generated
vendored
Normal file
|
@ -0,0 +1,180 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
)
|
||||
|
||||
func TestImagePushReferenceError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
return nil, nil
|
||||
}),
|
||||
}
|
||||
// An empty reference is an invalid reference
|
||||
_, err := client.ImagePush(context.Background(), "", types.ImagePushOptions{})
|
||||
if err == nil || !strings.Contains(err.Error(), "invalid reference format") {
|
||||
t.Fatalf("expected an error, got %v", err)
|
||||
}
|
||||
// An canonical reference cannot be pushed
|
||||
_, err = client.ImagePush(context.Background(), "repo@sha256:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", types.ImagePushOptions{})
|
||||
if err == nil || err.Error() != "cannot push a digest reference" {
|
||||
t.Fatalf("expected an error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestImagePushAnyError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
_, err := client.ImagePush(context.Background(), "myimage", types.ImagePushOptions{})
|
||||
if err == nil || err.Error() != "Error response from daemon: Server error" {
|
||||
t.Fatalf("expected a Server Error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestImagePushStatusUnauthorizedError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")),
|
||||
}
|
||||
_, err := client.ImagePush(context.Background(), "myimage", types.ImagePushOptions{})
|
||||
if err == nil || err.Error() != "Error response from daemon: Unauthorized error" {
|
||||
t.Fatalf("expected an Unauthorized Error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestImagePushWithUnauthorizedErrorAndPrivilegeFuncError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")),
|
||||
}
|
||||
privilegeFunc := func() (string, error) {
|
||||
return "", fmt.Errorf("Error requesting privilege")
|
||||
}
|
||||
_, err := client.ImagePush(context.Background(), "myimage", types.ImagePushOptions{
|
||||
PrivilegeFunc: privilegeFunc,
|
||||
})
|
||||
if err == nil || err.Error() != "Error requesting privilege" {
|
||||
t.Fatalf("expected an error requesting privilege, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestImagePushWithUnauthorizedErrorAndAnotherUnauthorizedError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")),
|
||||
}
|
||||
privilegeFunc := func() (string, error) {
|
||||
return "a-auth-header", nil
|
||||
}
|
||||
_, err := client.ImagePush(context.Background(), "myimage", types.ImagePushOptions{
|
||||
PrivilegeFunc: privilegeFunc,
|
||||
})
|
||||
if err == nil || err.Error() != "Error response from daemon: Unauthorized error" {
|
||||
t.Fatalf("expected an Unauthorized Error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestImagePushWithPrivilegedFuncNoError(t *testing.T) {
|
||||
expectedURL := "/images/myimage/push"
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
auth := req.Header.Get("X-Registry-Auth")
|
||||
if auth == "NotValid" {
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusUnauthorized,
|
||||
Body: ioutil.NopCloser(bytes.NewReader([]byte("Invalid credentials"))),
|
||||
}, nil
|
||||
}
|
||||
if auth != "IAmValid" {
|
||||
return nil, fmt.Errorf("Invalid auth header : expected %s, got %s", "IAmValid", auth)
|
||||
}
|
||||
query := req.URL.Query()
|
||||
tag := query.Get("tag")
|
||||
if tag != "tag" {
|
||||
return nil, fmt.Errorf("tag not set in URL query properly. Expected '%s', got %s", "tag", tag)
|
||||
}
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader([]byte("hello world"))),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
privilegeFunc := func() (string, error) {
|
||||
return "IAmValid", nil
|
||||
}
|
||||
resp, err := client.ImagePush(context.Background(), "myimage:tag", types.ImagePushOptions{
|
||||
RegistryAuth: "NotValid",
|
||||
PrivilegeFunc: privilegeFunc,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
body, err := ioutil.ReadAll(resp)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(body) != "hello world" {
|
||||
t.Fatalf("expected 'hello world', got %s", string(body))
|
||||
}
|
||||
}
|
||||
|
||||
func TestImagePushWithoutErrors(t *testing.T) {
|
||||
expectedOutput := "hello world"
|
||||
expectedURLFormat := "/images/%s/push"
|
||||
pullCases := []struct {
|
||||
reference string
|
||||
expectedImage string
|
||||
expectedTag string
|
||||
}{
|
||||
{
|
||||
reference: "myimage",
|
||||
expectedImage: "myimage",
|
||||
expectedTag: "",
|
||||
},
|
||||
{
|
||||
reference: "myimage:tag",
|
||||
expectedImage: "myimage",
|
||||
expectedTag: "tag",
|
||||
},
|
||||
}
|
||||
for _, pullCase := range pullCases {
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
expectedURL := fmt.Sprintf(expectedURLFormat, pullCase.expectedImage)
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
query := req.URL.Query()
|
||||
tag := query.Get("tag")
|
||||
if tag != pullCase.expectedTag {
|
||||
return nil, fmt.Errorf("tag not set in URL query properly. Expected '%s', got %s", pullCase.expectedTag, tag)
|
||||
}
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader([]byte(expectedOutput))),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
resp, err := client.ImagePush(context.Background(), pullCase.reference, types.ImagePushOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
body, err := ioutil.ReadAll(resp)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(body) != expectedOutput {
|
||||
t.Fatalf("expected '%s', got %s", expectedOutput, string(body))
|
||||
}
|
||||
}
|
||||
}
|
31
vendor/github.com/docker/docker-ce/components/engine/client/image_remove.go
generated
vendored
Normal file
31
vendor/github.com/docker/docker-ce/components/engine/client/image_remove.go
generated
vendored
Normal file
|
@ -0,0 +1,31 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/url"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// ImageRemove removes an image from the docker host.
|
||||
func (cli *Client) ImageRemove(ctx context.Context, imageID string, options types.ImageRemoveOptions) ([]types.ImageDeleteResponseItem, error) {
|
||||
query := url.Values{}
|
||||
|
||||
if options.Force {
|
||||
query.Set("force", "1")
|
||||
}
|
||||
if !options.PruneChildren {
|
||||
query.Set("noprune", "1")
|
||||
}
|
||||
|
||||
var dels []types.ImageDeleteResponseItem
|
||||
resp, err := cli.delete(ctx, "/images/"+imageID, query, nil)
|
||||
if err != nil {
|
||||
return dels, wrapResponseError(err, resp, "image", imageID)
|
||||
}
|
||||
|
||||
err = json.NewDecoder(resp.body).Decode(&dels)
|
||||
ensureReaderClosed(resp)
|
||||
return dels, err
|
||||
}
|
105
vendor/github.com/docker/docker-ce/components/engine/client/image_remove_test.go
generated
vendored
Normal file
105
vendor/github.com/docker/docker-ce/components/engine/client/image_remove_test.go
generated
vendored
Normal file
|
@ -0,0 +1,105 @@
|
|||
package client // import "github.com/docker/docker/client"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/gotestyourself/gotestyourself/assert"
|
||||
is "github.com/gotestyourself/gotestyourself/assert/cmp"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestImageRemoveError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
|
||||
_, err := client.ImageRemove(context.Background(), "image_id", types.ImageRemoveOptions{})
|
||||
assert.Check(t, is.Error(err, "Error response from daemon: Server error"))
|
||||
}
|
||||
|
||||
func TestImageRemoveImageNotFound(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusNotFound, "missing")),
|
||||
}
|
||||
|
||||
_, err := client.ImageRemove(context.Background(), "unknown", types.ImageRemoveOptions{})
|
||||
assert.Check(t, is.Error(err, "Error: No such image: unknown"))
|
||||
assert.Check(t, IsErrNotFound(err))
|
||||
}
|
||||
|
||||
func TestImageRemove(t *testing.T) {
|
||||
expectedURL := "/images/image_id"
|
||||
removeCases := []struct {
|
||||
force bool
|
||||
pruneChildren bool
|
||||
expectedQueryParams map[string]string
|
||||
}{
|
||||
{
|
||||
force: false,
|
||||
pruneChildren: false,
|
||||
expectedQueryParams: map[string]string{
|
||||
"force": "",
|
||||
"noprune": "1",
|
||||
},
|
||||
}, {
|
||||
force: true,
|
||||
pruneChildren: true,
|
||||
expectedQueryParams: map[string]string{
|
||||
"force": "1",
|
||||
"noprune": "",
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, removeCase := range removeCases {
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
if req.Method != "DELETE" {
|
||||
return nil, fmt.Errorf("expected DELETE method, got %s", req.Method)
|
||||
}
|
||||
query := req.URL.Query()
|
||||
for key, expected := range removeCase.expectedQueryParams {
|
||||
actual := query.Get(key)
|
||||
if actual != expected {
|
||||
return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
|
||||
}
|
||||
}
|
||||
b, err := json.Marshal([]types.ImageDeleteResponseItem{
|
||||
{
|
||||
Untagged: "image_id1",
|
||||
},
|
||||
{
|
||||
Deleted: "image_id",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(bytes.NewReader(b)),
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
imageDeletes, err := client.ImageRemove(context.Background(), "image_id", types.ImageRemoveOptions{
|
||||
Force: removeCase.force,
|
||||
PruneChildren: removeCase.pruneChildren,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(imageDeletes) != 2 {
|
||||
t.Fatalf("expected 2 deleted images, got %v", imageDeletes)
|
||||
}
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue