2018-02-05 21:05:59 +00:00
|
|
|
package client // import "github.com/docker/docker/client"
|
2016-09-06 18:46:37 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-04-19 22:30:59 +00:00
|
|
|
"context"
|
2016-09-06 18:46:37 +00:00
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-08-24 10:10:50 +00:00
|
|
|
"io"
|
2016-09-06 18:46:37 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2018-12-31 17:22:43 +00:00
|
|
|
"github.com/docker/docker/errdefs"
|
2023-05-10 11:17:40 +00:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2016-09-06 18:46:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestContainerStatPathError(t *testing.T) {
|
|
|
|
client := &Client{
|
2016-09-09 03:44:25 +00:00
|
|
|
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
_, err := client.ContainerStatPath(context.Background(), "container_id", "path")
|
2023-05-10 11:17:40 +00:00
|
|
|
assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
|
2018-01-11 12:15:46 +00:00
|
|
|
func TestContainerStatPathNotFoundError(t *testing.T) {
|
|
|
|
client := &Client{
|
|
|
|
client: newMockClient(errorMock(http.StatusNotFound, "Not found")),
|
|
|
|
}
|
|
|
|
_, err := client.ContainerStatPath(context.Background(), "container_id", "path")
|
2023-05-10 11:17:40 +00:00
|
|
|
assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
|
2018-01-11 12:15:46 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 18:46:37 +00:00
|
|
|
func TestContainerStatPathNoHeaderError(t *testing.T) {
|
|
|
|
client := &Client{
|
2016-09-09 03:44:25 +00:00
|
|
|
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
2016-09-06 18:46:37 +00:00
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
2021-08-24 10:10:50 +00:00
|
|
|
Body: io.NopCloser(bytes.NewReader([]byte(""))),
|
2016-09-06 18:46:37 +00:00
|
|
|
}, 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{
|
2016-09-09 03:44:25 +00:00
|
|
|
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
2016-09-06 18:46:37 +00:00
|
|
|
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
|
|
|
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
|
|
|
}
|
2019-10-12 18:41:14 +00:00
|
|
|
if req.Method != http.MethodHead {
|
2016-09-06 18:46:37 +00:00
|
|
|
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,
|
2021-08-24 10:10:50 +00:00
|
|
|
Body: io.NopCloser(bytes.NewReader([]byte(""))),
|
2016-09-06 18:46:37 +00:00
|
|
|
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" {
|
2016-12-01 19:32:04 +00:00
|
|
|
t.Fatalf("expected container path stat name to be 'name', got '%s'", stat.Name)
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
if stat.Mode != 0700 {
|
2016-12-01 19:32:04 +00:00
|
|
|
t.Fatalf("expected container path stat mode to be 0700, got '%v'", stat.Mode)
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCopyToContainerError(t *testing.T) {
|
|
|
|
client := &Client{
|
2016-09-09 03:44:25 +00:00
|
|
|
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
err := client.CopyToContainer(context.Background(), "container_id", "path/to/file", bytes.NewReader([]byte("")), types.CopyToContainerOptions{})
|
2023-05-10 11:17:40 +00:00
|
|
|
assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
|
2018-01-11 12:15:46 +00:00
|
|
|
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{})
|
2023-05-10 11:17:40 +00:00
|
|
|
assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
|
2018-01-11 12:15:46 +00:00
|
|
|
}
|
|
|
|
|
2022-03-20 16:13:04 +00:00
|
|
|
// TestCopyToContainerEmptyResponse verifies that no error is returned when a
|
|
|
|
// "204 No Content" is returned by the API.
|
|
|
|
func TestCopyToContainerEmptyResponse(t *testing.T) {
|
2016-09-06 18:46:37 +00:00
|
|
|
client := &Client{
|
2016-09-09 03:44:25 +00:00
|
|
|
client: newMockClient(errorMock(http.StatusNoContent, "No content")),
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
err := client.CopyToContainer(context.Background(), "container_id", "path/to/file", bytes.NewReader([]byte("")), types.CopyToContainerOptions{})
|
2022-03-20 16:13:04 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCopyToContainer(t *testing.T) {
|
|
|
|
expectedURL := "/containers/container_id/archive"
|
|
|
|
expectedPath := "path/to/file"
|
|
|
|
client := &Client{
|
2016-09-09 03:44:25 +00:00
|
|
|
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
2016-09-06 18:46:37 +00:00
|
|
|
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
|
|
|
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
|
|
|
}
|
2019-10-12 18:41:14 +00:00
|
|
|
if req.Method != http.MethodPut {
|
2016-09-06 18:46:37 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-08-24 10:10:50 +00:00
|
|
|
content, err := io.ReadAll(req.Body)
|
2016-09-06 18:46:37 +00:00
|
|
|
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,
|
2021-08-24 10:10:50 +00:00
|
|
|
Body: io.NopCloser(bytes.NewReader([]byte(""))),
|
2016-09-06 18:46:37 +00:00
|
|
|
}, 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{
|
2016-09-09 03:44:25 +00:00
|
|
|
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
_, _, err := client.CopyFromContainer(context.Background(), "container_id", "path/to/file")
|
2023-05-10 11:17:40 +00:00
|
|
|
assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
|
2018-01-11 12:15:46 +00:00
|
|
|
func TestCopyFromContainerNotFoundError(t *testing.T) {
|
|
|
|
client := &Client{
|
|
|
|
client: newMockClient(errorMock(http.StatusNotFound, "Not found")),
|
|
|
|
}
|
|
|
|
_, _, err := client.CopyFromContainer(context.Background(), "container_id", "path/to/file")
|
2023-05-10 11:17:40 +00:00
|
|
|
assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
|
2018-01-11 12:15:46 +00:00
|
|
|
}
|
|
|
|
|
2022-03-20 16:13:04 +00:00
|
|
|
// TestCopyFromContainerEmptyResponse verifies that no error is returned when a
|
|
|
|
// "204 No Content" is returned by the API.
|
|
|
|
func TestCopyFromContainerEmptyResponse(t *testing.T) {
|
2016-09-06 18:46:37 +00:00
|
|
|
client := &Client{
|
2022-03-20 16:13:04 +00:00
|
|
|
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
|
|
|
content, err := json.Marshal(types.ContainerPathStat{
|
|
|
|
Name: "path/to/file",
|
|
|
|
Mode: 0700,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
base64PathStat := base64.StdEncoding.EncodeToString(content)
|
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusNoContent,
|
|
|
|
Header: http.Header{
|
|
|
|
"X-Docker-Container-Path-Stat": []string{base64PathStat},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}),
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
_, _, err := client.CopyFromContainer(context.Background(), "container_id", "path/to/file")
|
2022-03-20 16:13:04 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCopyFromContainerNoHeaderError(t *testing.T) {
|
|
|
|
client := &Client{
|
2016-09-09 03:44:25 +00:00
|
|
|
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
2016-09-06 18:46:37 +00:00
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
2021-08-24 10:10:50 +00:00
|
|
|
Body: io.NopCloser(bytes.NewReader([]byte(""))),
|
2016-09-06 18:46:37 +00:00
|
|
|
}, 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{
|
2016-09-09 03:44:25 +00:00
|
|
|
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
2016-09-06 18:46:37 +00:00
|
|
|
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
|
|
|
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
|
|
|
}
|
2019-10-12 18:41:14 +00:00
|
|
|
if req.Method != http.MethodGet {
|
2016-11-17 07:50:38 +00:00
|
|
|
return nil, fmt.Errorf("expected GET method, got %s", req.Method)
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
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,
|
2021-08-24 10:10:50 +00:00
|
|
|
Body: io.NopCloser(bytes.NewReader([]byte("content"))),
|
2016-09-06 18:46:37 +00:00
|
|
|
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" {
|
2016-12-01 19:32:04 +00:00
|
|
|
t.Fatalf("expected container path stat name to be 'name', got '%s'", stat.Name)
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
if stat.Mode != 0700 {
|
2016-12-01 19:32:04 +00:00
|
|
|
t.Fatalf("expected container path stat mode to be 0700, got '%v'", stat.Mode)
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
2021-08-24 10:10:50 +00:00
|
|
|
content, err := io.ReadAll(r)
|
2016-09-06 18:46:37 +00:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|