client/container_test: Use functional option to create mock client

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
This commit is contained in:
Paweł Gronowski
2025-08-29 15:17:22 +02:00
parent c70aac772e
commit bc1d436aa9
23 changed files with 512 additions and 479 deletions

View File

@@ -17,10 +17,12 @@ import (
)
func TestContainerCommitError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
}
_, err := client.ContainerCommit(context.Background(), "nothing", container.CommitOptions{})
client, err := NewClientWithOpts(
WithMockClient(errorMock(http.StatusInternalServerError, "Server error")),
)
assert.NilError(t, err)
_, err = client.ContainerCommit(context.Background(), "nothing", container.CommitOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
_, err = client.ContainerCommit(context.Background(), "", container.CommitOptions{})
@@ -44,8 +46,8 @@ func TestContainerCommit(t *testing.T) {
)
expectedChanges := []string{"change1", "change2"}
client := &Client{
client: newMockClient(func(req *http.Request) (*http.Response, error) {
client, err := NewClientWithOpts(
WithMockClient(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)
}
@@ -89,7 +91,8 @@ func TestContainerCommit(t *testing.T) {
Body: io.NopCloser(bytes.NewReader(b)),
}, nil
}),
}
)
assert.NilError(t, err)
r, err := client.ContainerCommit(context.Background(), expectedContainerID, container.CommitOptions{
Reference: specifiedReference,

View File

@@ -20,10 +20,12 @@ import (
)
func TestContainerStatPathError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
}
_, err := client.ContainerStatPath(context.Background(), "container_id", "path")
client, err := NewClientWithOpts(
WithMockClient(errorMock(http.StatusInternalServerError, "Server error")),
)
assert.NilError(t, err)
_, err = client.ContainerStatPath(context.Background(), "container_id", "path")
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
_, err = client.ContainerStatPath(context.Background(), "", "path")
@@ -36,31 +38,35 @@ func TestContainerStatPathError(t *testing.T) {
}
func TestContainerStatPathNotFoundError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusNotFound, "Not found")),
}
_, err := client.ContainerStatPath(context.Background(), "container_id", "path")
client, err := NewClientWithOpts(
WithMockClient(errorMock(http.StatusNotFound, "Not found")),
)
assert.NilError(t, err)
_, err = client.ContainerStatPath(context.Background(), "container_id", "path")
assert.Check(t, is.ErrorType(err, cerrdefs.IsNotFound))
}
func TestContainerStatPathNoHeaderError(t *testing.T) {
client := &Client{
client: newMockClient(func(req *http.Request) (*http.Response, error) {
client, err := NewClientWithOpts(
WithMockClient(func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
}),
}
_, err := client.ContainerStatPath(context.Background(), "container_id", "path/to/file")
)
assert.NilError(t, err)
_, err = client.ContainerStatPath(context.Background(), "container_id", "path/to/file")
assert.Check(t, err != nil, "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) {
client, err := NewClientWithOpts(
WithMockClient(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)
}
@@ -88,7 +94,8 @@ func TestContainerStatPath(t *testing.T) {
},
}, nil
}),
}
)
assert.NilError(t, err)
stat, err := client.ContainerStatPath(context.Background(), "container_id", expectedPath)
assert.NilError(t, err)
assert.Check(t, is.Equal(stat.Name, "name"))
@@ -96,10 +103,12 @@ func TestContainerStatPath(t *testing.T) {
}
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("")), container.CopyToContainerOptions{})
client, err := NewClientWithOpts(
WithMockClient(errorMock(http.StatusInternalServerError, "Server error")),
)
assert.NilError(t, err)
err = client.CopyToContainer(context.Background(), "container_id", "path/to/file", bytes.NewReader([]byte("")), container.CopyToContainerOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
err = client.CopyToContainer(context.Background(), "", "path/to/file", bytes.NewReader([]byte("")), container.CopyToContainerOptions{})
@@ -112,28 +121,32 @@ func TestCopyToContainerError(t *testing.T) {
}
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("")), container.CopyToContainerOptions{})
client, err := NewClientWithOpts(
WithMockClient(errorMock(http.StatusNotFound, "Not found")),
)
assert.NilError(t, err)
err = client.CopyToContainer(context.Background(), "container_id", "path/to/file", bytes.NewReader([]byte("")), container.CopyToContainerOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsNotFound))
}
// TestCopyToContainerEmptyResponse verifies that no error is returned when a
// "204 No Content" is returned by the API.
func TestCopyToContainerEmptyResponse(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("")), container.CopyToContainerOptions{})
client, err := NewClientWithOpts(
WithMockClient(errorMock(http.StatusNoContent, "No content")),
)
assert.NilError(t, err)
err = client.CopyToContainer(context.Background(), "container_id", "path/to/file", bytes.NewReader([]byte("")), container.CopyToContainerOptions{})
assert.NilError(t, 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) {
client, err := NewClientWithOpts(
WithMockClient(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)
}
@@ -166,18 +179,22 @@ func TestCopyToContainer(t *testing.T) {
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
}),
}
err := client.CopyToContainer(context.Background(), "container_id", expectedPath, bytes.NewReader([]byte("content")), container.CopyToContainerOptions{
)
assert.NilError(t, err)
err = client.CopyToContainer(context.Background(), "container_id", expectedPath, bytes.NewReader([]byte("content")), container.CopyToContainerOptions{
AllowOverwriteDirWithFile: false,
})
assert.NilError(t, 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")
client, err := NewClientWithOpts(
WithMockClient(errorMock(http.StatusInternalServerError, "Server error")),
)
assert.NilError(t, err)
_, _, err = client.CopyFromContainer(context.Background(), "container_id", "path/to/file")
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
_, _, err = client.CopyFromContainer(context.Background(), "", "path/to/file")
@@ -190,18 +207,20 @@ func TestCopyFromContainerError(t *testing.T) {
}
func TestCopyFromContainerNotFoundError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusNotFound, "Not found")),
}
_, _, err := client.CopyFromContainer(context.Background(), "container_id", "path/to/file")
client, err := NewClientWithOpts(
WithMockClient(errorMock(http.StatusNotFound, "Not found")),
)
assert.NilError(t, err)
_, _, err = client.CopyFromContainer(context.Background(), "container_id", "path/to/file")
assert.Check(t, is.ErrorType(err, cerrdefs.IsNotFound))
}
// TestCopyFromContainerEmptyResponse verifies that no error is returned when a
// "204 No Content" is returned by the API.
func TestCopyFromContainerEmptyResponse(t *testing.T) {
client := &Client{
client: newMockClient(func(req *http.Request) (*http.Response, error) {
client, err := NewClientWithOpts(
WithMockClient(func(req *http.Request) (*http.Response, error) {
content, err := json.Marshal(container.PathStat{
Name: "path/to/file",
Mode: 0o700,
@@ -217,29 +236,33 @@ func TestCopyFromContainerEmptyResponse(t *testing.T) {
},
}, nil
}),
}
_, _, err := client.CopyFromContainer(context.Background(), "container_id", "path/to/file")
)
assert.NilError(t, err)
_, _, err = client.CopyFromContainer(context.Background(), "container_id", "path/to/file")
assert.NilError(t, err)
}
func TestCopyFromContainerNoHeaderError(t *testing.T) {
client := &Client{
client: newMockClient(func(req *http.Request) (*http.Response, error) {
client, err := NewClientWithOpts(
WithMockClient(func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
}),
}
_, _, err := client.CopyFromContainer(context.Background(), "container_id", "path/to/file")
)
assert.NilError(t, err)
_, _, err = client.CopyFromContainer(context.Background(), "container_id", "path/to/file")
assert.Check(t, err != nil, "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) {
client, err := NewClientWithOpts(
WithMockClient(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)
}
@@ -269,7 +292,8 @@ func TestCopyFromContainer(t *testing.T) {
},
}, nil
}),
}
)
assert.NilError(t, err)
r, stat, err := client.CopyFromContainer(context.Background(), "container_id", expectedPath)
assert.NilError(t, err)
assert.Check(t, is.Equal(stat.Name, "name"))

View File

@@ -17,32 +17,38 @@ import (
)
func TestContainerCreateError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
}
_, err := client.ContainerCreate(context.Background(), nil, nil, nil, nil, "nothing")
client, err := NewClientWithOpts(
WithMockClient(errorMock(http.StatusInternalServerError, "Server error")),
)
assert.NilError(t, err)
_, err = client.ContainerCreate(context.Background(), nil, nil, nil, nil, "nothing")
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
// 404 doesn't automatically means an unknown image
client = &Client{
client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
}
client, err = NewClientWithOpts(
WithMockClient(errorMock(http.StatusNotFound, "Server error")),
)
assert.NilError(t, err)
_, err = client.ContainerCreate(context.Background(), nil, nil, nil, nil, "nothing")
assert.Check(t, is.ErrorType(err, cerrdefs.IsNotFound))
}
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, nil, "unknown")
client, err := NewClientWithOpts(
WithMockClient(errorMock(http.StatusNotFound, "No such image")),
)
assert.NilError(t, err)
_, err = client.ContainerCreate(context.Background(), &container.Config{Image: "unknown_image"}, nil, nil, nil, "unknown")
assert.Check(t, is.ErrorType(err, cerrdefs.IsNotFound))
}
func TestContainerCreateWithName(t *testing.T) {
expectedURL := "/containers/create"
client := &Client{
client: newMockClient(func(req *http.Request) (*http.Response, error) {
client, err := NewClientWithOpts(
WithMockClient(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)
}
@@ -61,7 +67,8 @@ func TestContainerCreateWithName(t *testing.T) {
Body: io.NopCloser(bytes.NewReader(b)),
}, nil
}),
}
)
assert.NilError(t, err)
r, err := client.ContainerCreate(context.Background(), nil, nil, nil, nil, "container_name")
assert.NilError(t, err)
@@ -102,11 +109,13 @@ func TestContainerCreateAutoRemove(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.version, func(t *testing.T) {
client := &Client{
client: newMockClient(autoRemoveValidator(tc.expectedAutoRemove)),
version: tc.version,
}
_, err := client.ContainerCreate(context.Background(), nil, &container.HostConfig{AutoRemove: true}, nil, nil, "")
client, err := NewClientWithOpts(
WithMockClient(autoRemoveValidator(tc.expectedAutoRemove)),
WithVersion(tc.version),
)
assert.NilError(t, err)
_, err = client.ContainerCreate(context.Background(), nil, &container.HostConfig{AutoRemove: true}, nil, nil, "")
assert.NilError(t, err)
})
}
@@ -145,8 +154,8 @@ func TestContainerCreateCapabilities(t *testing.T) {
"CAP_CAPABILITY_D",
}
client := &Client{
client: newMockClient(func(req *http.Request) (*http.Response, error) {
client, err := NewClientWithOpts(
WithMockClient(func(req *http.Request) (*http.Response, error) {
var config container.CreateRequest
if err := json.NewDecoder(req.Body).Decode(&config); err != nil {
@@ -166,9 +175,10 @@ func TestContainerCreateCapabilities(t *testing.T) {
Body: io.NopCloser(bytes.NewReader(b)),
}, nil
}),
version: "1.24",
}
WithVersion("1.24"),
)
assert.NilError(t, err)
_, err := client.ContainerCreate(context.Background(), nil, &container.HostConfig{CapAdd: inputCaps, CapDrop: inputCaps}, nil, nil, "")
_, err = client.ContainerCreate(context.Background(), nil, &container.HostConfig{CapAdd: inputCaps, CapDrop: inputCaps}, nil, nil, "")
assert.NilError(t, err)
}

View File

@@ -17,10 +17,12 @@ import (
)
func TestContainerDiffError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
}
_, err := client.ContainerDiff(context.Background(), "nothing")
client, err := NewClientWithOpts(
WithMockClient(errorMock(http.StatusInternalServerError, "Server error")),
)
assert.NilError(t, err)
_, err = client.ContainerDiff(context.Background(), "nothing")
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
_, err = client.ContainerDiff(context.Background(), "")
@@ -50,8 +52,8 @@ func TestContainerDiff(t *testing.T) {
},
}
client := &Client{
client: newMockClient(func(req *http.Request) (*http.Response, error) {
client, err := NewClientWithOpts(
WithMockClient(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)
}
@@ -64,7 +66,8 @@ func TestContainerDiff(t *testing.T) {
Body: io.NopCloser(bytes.NewReader(b)),
}, nil
}),
}
)
assert.NilError(t, err)
changes, err := client.ContainerDiff(context.Background(), "container_id")
assert.NilError(t, err)

View File

@@ -17,11 +17,12 @@ import (
)
func TestContainerExecCreateError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
}
client, err := NewClientWithOpts(
WithMockClient(errorMock(http.StatusInternalServerError, "Server error")),
)
assert.NilError(t, err)
_, err := client.ContainerExecCreate(context.Background(), "container_id", container.ExecOptions{})
_, err = client.ContainerExecCreate(context.Background(), "container_id", container.ExecOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
_, err = client.ContainerExecCreate(context.Background(), "", container.ExecOptions{})
@@ -47,8 +48,8 @@ func TestContainerExecCreateConnectionError(t *testing.T) {
func TestContainerExecCreate(t *testing.T) {
expectedURL := "/containers/container_id/exec"
client := &Client{
client: newMockClient(func(req *http.Request) (*http.Response, error) {
client, err := NewClientWithOpts(
WithMockClient(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)
}
@@ -77,7 +78,8 @@ func TestContainerExecCreate(t *testing.T) {
Body: io.NopCloser(bytes.NewReader(b)),
}, nil
}),
}
)
assert.NilError(t, err)
r, err := client.ContainerExecCreate(context.Background(), "container_id", container.ExecOptions{
User: "user",
@@ -87,17 +89,19 @@ func TestContainerExecCreate(t *testing.T) {
}
func TestContainerExecStartError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
}
err := client.ContainerExecStart(context.Background(), "nothing", container.ExecStartOptions{})
client, err := NewClientWithOpts(
WithMockClient(errorMock(http.StatusInternalServerError, "Server error")),
)
assert.NilError(t, err)
err = client.ContainerExecStart(context.Background(), "nothing", container.ExecStartOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
}
func TestContainerExecStart(t *testing.T) {
expectedURL := "/exec/exec_id/start"
client := &Client{
client: newMockClient(func(req *http.Request) (*http.Response, error) {
client, err := NewClientWithOpts(
WithMockClient(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)
}
@@ -117,9 +121,10 @@ func TestContainerExecStart(t *testing.T) {
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
}),
}
)
assert.NilError(t, err)
err := client.ContainerExecStart(context.Background(), "exec_id", container.ExecStartOptions{
err = client.ContainerExecStart(context.Background(), "exec_id", container.ExecStartOptions{
Detach: true,
Tty: false,
})
@@ -127,17 +132,19 @@ func TestContainerExecStart(t *testing.T) {
}
func TestContainerExecInspectError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
}
_, err := client.ContainerExecInspect(context.Background(), "nothing")
client, err := NewClientWithOpts(
WithMockClient(errorMock(http.StatusInternalServerError, "Server error")),
)
assert.NilError(t, err)
_, err = client.ContainerExecInspect(context.Background(), "nothing")
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
}
func TestContainerExecInspect(t *testing.T) {
expectedURL := "/exec/exec_id/json"
client := &Client{
client: newMockClient(func(req *http.Request) (*http.Response, error) {
client, err := NewClientWithOpts(
WithMockClient(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)
}
@@ -153,7 +160,8 @@ func TestContainerExecInspect(t *testing.T) {
Body: io.NopCloser(bytes.NewReader(b)),
}, nil
}),
}
)
assert.NilError(t, err)
inspect, err := client.ContainerExecInspect(context.Background(), "exec_id")
assert.NilError(t, err)

View File

@@ -15,10 +15,12 @@ import (
)
func TestContainerExportError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
}
_, err := client.ContainerExport(context.Background(), "nothing")
client, err := NewClientWithOpts(
WithMockClient(errorMock(http.StatusInternalServerError, "Server error")),
)
assert.NilError(t, err)
_, err = client.ContainerExport(context.Background(), "nothing")
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
_, err = client.ContainerExport(context.Background(), "")
@@ -32,8 +34,8 @@ func TestContainerExportError(t *testing.T) {
func TestContainerExport(t *testing.T) {
expectedURL := "/containers/container_id/export"
client := &Client{
client: newMockClient(func(r *http.Request) (*http.Response, error) {
client, err := NewClientWithOpts(
WithMockClient(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)
}
@@ -43,7 +45,8 @@ func TestContainerExport(t *testing.T) {
Body: io.NopCloser(bytes.NewReader([]byte("response"))),
}, nil
}),
}
)
assert.NilError(t, err)
body, err := client.ContainerExport(context.Background(), "container_id")
assert.NilError(t, err)
defer body.Close()

View File

@@ -18,11 +18,12 @@ import (
)
func TestContainerInspectError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
}
client, err := NewClientWithOpts(
WithMockClient(errorMock(http.StatusInternalServerError, "Server error")),
)
assert.NilError(t, err)
_, err := client.ContainerInspect(context.Background(), "nothing")
_, err = client.ContainerInspect(context.Background(), "nothing")
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
_, err = client.ContainerInspect(context.Background(), "")
@@ -35,22 +36,24 @@ func TestContainerInspectError(t *testing.T) {
}
func TestContainerInspectContainerNotFound(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
}
client, err := NewClientWithOpts(
WithMockClient(errorMock(http.StatusNotFound, "Server error")),
)
assert.NilError(t, err)
_, err := client.ContainerInspect(context.Background(), "unknown")
_, err = client.ContainerInspect(context.Background(), "unknown")
assert.Check(t, is.ErrorType(err, cerrdefs.IsNotFound))
}
func TestContainerInspectWithEmptyID(t *testing.T) {
client := &Client{
client: newMockClient(func(req *http.Request) (*http.Response, error) {
client, err := NewClientWithOpts(
WithMockClient(func(req *http.Request) (*http.Response, error) {
return nil, errors.New("should not make request")
}),
}
)
assert.NilError(t, err)
_, err := client.ContainerInspect(context.Background(), "")
_, err = client.ContainerInspect(context.Background(), "")
assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument))
assert.Check(t, is.ErrorContains(err, "value is empty"))
@@ -69,8 +72,8 @@ func TestContainerInspectWithEmptyID(t *testing.T) {
func TestContainerInspect(t *testing.T) {
expectedURL := "/containers/container_id/json"
client := &Client{
client: newMockClient(func(req *http.Request) (*http.Response, error) {
client, err := NewClientWithOpts(
WithMockClient(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)
}
@@ -87,7 +90,8 @@ func TestContainerInspect(t *testing.T) {
Body: io.NopCloser(bytes.NewReader(content)),
}, nil
}),
}
)
assert.NilError(t, err)
r, err := client.ContainerInspect(context.Background(), "container_id")
assert.NilError(t, err)

View File

@@ -15,10 +15,12 @@ import (
)
func TestContainerKillError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
}
err := client.ContainerKill(context.Background(), "nothing", "SIGKILL")
client, err := NewClientWithOpts(
WithMockClient(errorMock(http.StatusInternalServerError, "Server error")),
)
assert.NilError(t, err)
err = client.ContainerKill(context.Background(), "nothing", "SIGKILL")
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
err = client.ContainerKill(context.Background(), "", "")
@@ -32,8 +34,8 @@ func TestContainerKillError(t *testing.T) {
func TestContainerKill(t *testing.T) {
expectedURL := "/containers/container_id/kill"
client := &Client{
client: newMockClient(func(req *http.Request) (*http.Response, error) {
client, err := NewClientWithOpts(
WithMockClient(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)
}
@@ -46,8 +48,9 @@ func TestContainerKill(t *testing.T) {
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
}),
}
)
assert.NilError(t, err)
err := client.ContainerKill(context.Background(), "container_id", "SIGKILL")
err = client.ContainerKill(context.Background(), "container_id", "SIGKILL")
assert.NilError(t, err)
}

View File

@@ -18,18 +18,20 @@ import (
)
func TestContainerListError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
}
_, err := client.ContainerList(context.Background(), container.ListOptions{})
client, err := NewClientWithOpts(
WithMockClient(errorMock(http.StatusInternalServerError, "Server error")),
)
assert.NilError(t, err)
_, err = client.ContainerList(context.Background(), container.ListOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
}
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) {
client, err := NewClientWithOpts(
WithMockClient(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)
}
@@ -76,7 +78,8 @@ func TestContainerList(t *testing.T) {
Body: io.NopCloser(bytes.NewReader(b)),
}, nil
}),
}
)
assert.NilError(t, err)
containers, err := client.ContainerList(context.Background(), container.ListOptions{
Size: true,

View File

@@ -20,10 +20,12 @@ import (
)
func TestContainerLogsNotFoundError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusNotFound, "Not found")),
}
_, err := client.ContainerLogs(context.Background(), "container_id", container.LogsOptions{})
client, err := NewClientWithOpts(
WithMockClient(errorMock(http.StatusNotFound, "Not found")),
)
assert.NilError(t, err)
_, err = client.ContainerLogs(context.Background(), "container_id", container.LogsOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsNotFound))
_, err = client.ContainerLogs(context.Background(), "", container.LogsOptions{})
@@ -36,10 +38,12 @@ func TestContainerLogsNotFoundError(t *testing.T) {
}
func TestContainerLogsError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
}
_, err := client.ContainerLogs(context.Background(), "container_id", container.LogsOptions{})
client, err := NewClientWithOpts(
WithMockClient(errorMock(http.StatusInternalServerError, "Server error")),
)
assert.NilError(t, err)
_, err = client.ContainerLogs(context.Background(), "container_id", container.LogsOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
_, err = client.ContainerLogs(context.Background(), "container_id", container.LogsOptions{
@@ -125,8 +129,8 @@ func TestContainerLogs(t *testing.T) {
},
}
for _, logCase := range cases {
client := &Client{
client: newMockClient(func(r *http.Request) (*http.Response, error) {
client, err := NewClientWithOpts(
WithMockClient(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)
}
@@ -143,7 +147,8 @@ func TestContainerLogs(t *testing.T) {
Body: io.NopCloser(bytes.NewReader([]byte("response"))),
}, nil
}),
}
)
assert.NilError(t, err)
body, err := client.ContainerLogs(context.Background(), "container_id", logCase.options)
if logCase.expectedError != "" {
assert.Check(t, is.Error(err, logCase.expectedError))

View File

@@ -15,17 +15,19 @@ import (
)
func TestContainerPauseError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
}
err := client.ContainerPause(context.Background(), "nothing")
client, err := NewClientWithOpts(
WithMockClient(errorMock(http.StatusInternalServerError, "Server error")),
)
assert.NilError(t, err)
err = client.ContainerPause(context.Background(), "nothing")
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
}
func TestContainerPause(t *testing.T) {
expectedURL := "/containers/container_id/pause"
client := &Client{
client: newMockClient(func(req *http.Request) (*http.Response, error) {
client, err := NewClientWithOpts(
WithMockClient(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)
}
@@ -34,7 +36,9 @@ func TestContainerPause(t *testing.T) {
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
}),
}
err := client.ContainerPause(context.Background(), "container_id")
)
assert.NilError(t, err)
err = client.ContainerPause(context.Background(), "container_id")
assert.NilError(t, err)
}

View File

@@ -18,12 +18,10 @@ import (
)
func TestContainersPruneError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
version: "1.25",
}
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")), WithVersion("1.25"))
assert.NilError(t, err)
_, err := client.ContainersPrune(context.Background(), filters.Args{})
_, err = client.ContainersPrune(context.Background(), filters.Args{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
}
@@ -83,30 +81,28 @@ func TestContainersPrune(t *testing.T) {
},
}
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(container.PruneReport{
ContainersDeleted: []string{"container_id1", "container_id2"},
SpaceReclaimed: 9999,
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(content)),
}, nil
}),
version: "1.25",
}
client, err := NewClientWithOpts(WithMockClient(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(container.PruneReport{
ContainersDeleted: []string{"container_id1", "container_id2"},
SpaceReclaimed: 9999,
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(content)),
}, nil
}), WithVersion("1.25"))
assert.NilError(t, err)
report, err := client.ContainersPrune(context.Background(), listCase.filters)
assert.NilError(t, err)

View File

@@ -16,10 +16,9 @@ import (
)
func TestContainerRemoveError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
}
err := client.ContainerRemove(context.Background(), "container_id", container.RemoveOptions{})
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
assert.NilError(t, err)
err = client.ContainerRemove(context.Background(), "container_id", container.RemoveOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
err = client.ContainerRemove(context.Background(), "", container.RemoveOptions{})
@@ -32,42 +31,40 @@ func TestContainerRemoveError(t *testing.T) {
}
func TestContainerRemoveNotFoundError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusNotFound, "no such container: container_id")),
}
err := client.ContainerRemove(context.Background(), "container_id", container.RemoveOptions{})
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusNotFound, "no such container: container_id")))
assert.NilError(t, err)
err = client.ContainerRemove(context.Background(), "container_id", container.RemoveOptions{})
assert.Check(t, is.ErrorContains(err, "no such container: container_id"))
assert.Check(t, is.ErrorType(err, cerrdefs.IsNotFound))
}
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: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
}),
}
client, err := NewClientWithOpts(WithMockClient(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: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
}))
assert.NilError(t, err)
err := client.ContainerRemove(context.Background(), "container_id", container.RemoveOptions{
err = client.ContainerRemove(context.Background(), "container_id", container.RemoveOptions{
RemoveVolumes: true,
Force: true,
})

View File

@@ -15,10 +15,9 @@ import (
)
func TestContainerRenameError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
}
err := client.ContainerRename(context.Background(), "nothing", "newNothing")
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
assert.NilError(t, err)
err = client.ContainerRename(context.Background(), "nothing", "newNothing")
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
err = client.ContainerRename(context.Background(), "", "newNothing")
@@ -32,22 +31,21 @@ func TestContainerRenameError(t *testing.T) {
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: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
}),
}
client, err := NewClientWithOpts(WithMockClient(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: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
}))
assert.NilError(t, err)
err := client.ContainerRename(context.Background(), "container_id", "newName")
err = client.ContainerRename(context.Background(), "container_id", "newName")
assert.NilError(t, err)
}

View File

@@ -14,10 +14,9 @@ import (
)
func TestContainerResizeError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
}
err := client.ContainerResize(context.Background(), "container_id", ContainerResizeOptions{})
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
assert.NilError(t, err)
err = client.ContainerResize(context.Background(), "container_id", ContainerResizeOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
err = client.ContainerResize(context.Background(), "", ContainerResizeOptions{})
@@ -30,10 +29,9 @@ func TestContainerResizeError(t *testing.T) {
}
func TestContainerExecResizeError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
}
err := client.ContainerExecResize(context.Background(), "exec_id", ContainerResizeOptions{})
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
assert.NilError(t, err)
err = client.ContainerExecResize(context.Background(), "exec_id", ContainerResizeOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
}
@@ -72,10 +70,9 @@ func TestContainerResize(t *testing.T) {
}
for _, tc := range tests {
t.Run(tc.doc, func(t *testing.T) {
client := &Client{
client: newMockClient(resizeTransport(t, expectedURL, tc.expectedHeight, tc.expectedWidth)),
}
err := client.ContainerResize(context.Background(), "container_id", tc.opts)
client, err := NewClientWithOpts(WithMockClient(resizeTransport(t, expectedURL, tc.expectedHeight, tc.expectedWidth)))
assert.NilError(t, err)
err = client.ContainerResize(context.Background(), "container_id", tc.opts)
assert.NilError(t, err)
})
}
@@ -115,10 +112,9 @@ func TestContainerExecResize(t *testing.T) {
}
for _, tc := range tests {
t.Run(tc.doc, func(t *testing.T) {
client := &Client{
client: newMockClient(resizeTransport(t, expectedURL, tc.expectedHeight, tc.expectedWidth)),
}
err := client.ContainerExecResize(context.Background(), "exec_id", tc.opts)
client, err := NewClientWithOpts(WithMockClient(resizeTransport(t, expectedURL, tc.expectedHeight, tc.expectedWidth)))
assert.NilError(t, err)
err = client.ContainerExecResize(context.Background(), "exec_id", tc.opts)
assert.NilError(t, err)
})
}

View File

@@ -16,10 +16,9 @@ import (
)
func TestContainerRestartError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
}
err := client.ContainerRestart(context.Background(), "nothing", container.StopOptions{})
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
assert.NilError(t, err)
err = client.ContainerRestart(context.Background(), "nothing", container.StopOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
err = client.ContainerRestart(context.Background(), "", container.StopOptions{})
@@ -45,28 +44,26 @@ func TestContainerRestartConnectionError(t *testing.T) {
func TestContainerRestart(t *testing.T) {
const expectedURL = "/v1.42/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)
}
s := req.URL.Query().Get("signal")
if s != "SIGKILL" {
return nil, fmt.Errorf("signal not set in URL query. Expected 'SIGKILL', got '%s'", s)
}
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: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
}),
version: "1.42",
}
client, err := NewClientWithOpts(WithMockClient(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)
}
s := req.URL.Query().Get("signal")
if s != "SIGKILL" {
return nil, fmt.Errorf("signal not set in URL query. Expected 'SIGKILL', got '%s'", s)
}
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: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
}), WithVersion("1.42"))
assert.NilError(t, err)
timeout := 100
err := client.ContainerRestart(context.Background(), "container_id", container.StopOptions{
err = client.ContainerRestart(context.Background(), "container_id", container.StopOptions{
Signal: "SIGKILL",
Timeout: &timeout,
})

View File

@@ -17,10 +17,9 @@ import (
)
func TestContainerStartError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
}
err := client.ContainerStart(context.Background(), "nothing", container.StartOptions{})
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
assert.NilError(t, err)
err = client.ContainerStart(context.Background(), "nothing", container.StartOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
err = client.ContainerStart(context.Background(), "", container.StartOptions{})
@@ -34,31 +33,30 @@ func TestContainerStartError(t *testing.T) {
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 any
if err := json.NewDecoder(req.Body).Decode(&startConfig); err != nil {
return nil, fmt.Errorf("Unable to parse json: %s", err)
}
client, err := NewClientWithOpts(WithMockClient(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 any
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)
}
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: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
}),
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
}))
assert.NilError(t, err)
err := client.ContainerStart(context.Background(), "container_id", container.StartOptions{CheckpointID: "checkpoint_id"})
err = client.ContainerStart(context.Background(), "container_id", container.StartOptions{CheckpointID: "checkpoint_id"})
assert.NilError(t, err)
}

View File

@@ -15,10 +15,9 @@ import (
)
func TestContainerStatsError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
}
_, err := client.ContainerStats(context.Background(), "nothing", false)
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
assert.NilError(t, err)
_, err = client.ContainerStats(context.Background(), "nothing", false)
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
_, err = client.ContainerStats(context.Background(), "", false)
@@ -45,24 +44,23 @@ func TestContainerStats(t *testing.T) {
},
}
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)
}
client, err := NewClientWithOpts(WithMockClient(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)
}
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: io.NopCloser(bytes.NewReader([]byte("response"))),
}, nil
}),
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte("response"))),
}, nil
}))
assert.NilError(t, err)
resp, err := client.ContainerStats(context.Background(), "container_id", c.stream)
assert.NilError(t, err)
t.Cleanup(func() {

View File

@@ -16,10 +16,9 @@ import (
)
func TestContainerStopError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
}
err := client.ContainerStop(context.Background(), "container_id", container.StopOptions{})
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
assert.NilError(t, err)
err = client.ContainerStop(context.Background(), "container_id", container.StopOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
err = client.ContainerStop(context.Background(), "", container.StopOptions{})
@@ -45,28 +44,26 @@ func TestContainerStopConnectionError(t *testing.T) {
func TestContainerStop(t *testing.T) {
const expectedURL = "/v1.42/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)
}
s := req.URL.Query().Get("signal")
if s != "SIGKILL" {
return nil, fmt.Errorf("signal not set in URL query. Expected 'SIGKILL', got '%s'", s)
}
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: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
}),
version: "1.42",
}
client, err := NewClientWithOpts(WithMockClient(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)
}
s := req.URL.Query().Get("signal")
if s != "SIGKILL" {
return nil, fmt.Errorf("signal not set in URL query. Expected 'SIGKILL', got '%s'", s)
}
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: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
}), WithVersion("1.42"))
assert.NilError(t, err)
timeout := 100
err := client.ContainerStop(context.Background(), "container_id", container.StopOptions{
err = client.ContainerStop(context.Background(), "container_id", container.StopOptions{
Signal: "SIGKILL",
Timeout: &timeout,
})

View File

@@ -17,10 +17,9 @@ import (
)
func TestContainerTopError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
}
_, err := client.ContainerTop(context.Background(), "nothing", []string{})
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
assert.NilError(t, err)
_, err = client.ContainerTop(context.Background(), "nothing", []string{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
_, err = client.ContainerTop(context.Background(), "", []string{})
@@ -40,34 +39,33 @@ func TestContainerTop(t *testing.T) {
}
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)
}
client, err := NewClientWithOpts(WithMockClient(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.TopResponse{
Processes: [][]string{
{"p1", "p2"},
{"p3"},
},
Titles: []string{"title1", "title2"},
})
if err != nil {
return nil, err
}
b, err := json.Marshal(container.TopResponse{
Processes: [][]string{
{"p1", "p2"},
{"p3"},
},
Titles: []string{"title1", "title2"},
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(b)),
}, nil
}),
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(b)),
}, nil
}))
assert.NilError(t, err)
processList, err := client.ContainerTop(context.Background(), "container_id", []string{"arg1", "arg2"})
assert.NilError(t, err)

View File

@@ -15,10 +15,9 @@ import (
)
func TestContainerUnpauseError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
}
err := client.ContainerUnpause(context.Background(), "nothing")
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
assert.NilError(t, err)
err = client.ContainerUnpause(context.Background(), "nothing")
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
err = client.ContainerUnpause(context.Background(), "")
@@ -32,17 +31,16 @@ func TestContainerUnpauseError(t *testing.T) {
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: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
}),
}
err := client.ContainerUnpause(context.Background(), "container_id")
client, err := NewClientWithOpts(WithMockClient(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: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
}))
assert.NilError(t, err)
err = client.ContainerUnpause(context.Background(), "container_id")
assert.NilError(t, err)
}

View File

@@ -17,10 +17,9 @@ import (
)
func TestContainerUpdateError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
}
_, err := client.ContainerUpdate(context.Background(), "nothing", container.UpdateConfig{})
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
assert.NilError(t, err)
_, err = client.ContainerUpdate(context.Background(), "nothing", container.UpdateConfig{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
_, err = client.ContainerUpdate(context.Background(), "", container.UpdateConfig{})
@@ -35,25 +34,24 @@ func TestContainerUpdateError(t *testing.T) {
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)
}
client, err := NewClientWithOpts(WithMockClient(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.UpdateResponse{})
if err != nil {
return nil, err
}
b, err := json.Marshal(container.UpdateResponse{})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(b)),
}, nil
}),
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(b)),
}, nil
}))
assert.NilError(t, err)
_, err := client.ContainerUpdate(context.Background(), "container_id", container.UpdateConfig{
_, err = client.ContainerUpdate(context.Background(), "container_id", container.UpdateConfig{
Resources: container.Resources{
CPUPeriod: 1,
},

View File

@@ -22,9 +22,8 @@ import (
)
func TestContainerWaitError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
}
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
assert.NilError(t, err)
resultC, errC := client.ContainerWait(context.Background(), "nothing", "")
select {
case result := <-resultC:
@@ -53,23 +52,22 @@ func TestContainerWaitConnectionError(t *testing.T) {
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.WaitResponse{
StatusCode: 15,
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(b)),
}, nil
}),
}
client, err := NewClientWithOpts(WithMockClient(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.WaitResponse{
StatusCode: 15,
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(b)),
}, nil
}))
assert.NilError(t, err)
resultC, errC := client.ContainerWait(context.Background(), "container_id", "")
select {
@@ -83,18 +81,16 @@ func TestContainerWait(t *testing.T) {
func TestContainerWaitProxyInterrupt(t *testing.T) {
expectedURL := "/v1.30/containers/container_id/wait"
msg := "copying response body from Docker: unexpected EOF"
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)
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader(msg)),
}, nil
}),
}
client, err := NewClientWithOpts(WithMockClient(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: io.NopCloser(strings.NewReader(msg)),
}, nil
}), WithVersion("1.30"))
assert.NilError(t, err)
resultC, errC := client.ContainerWait(context.Background(), "container_id", "")
select {
@@ -108,18 +104,16 @@ func TestContainerWaitProxyInterrupt(t *testing.T) {
func TestContainerWaitProxyInterruptLong(t *testing.T) {
expectedURL := "/v1.30/containers/container_id/wait"
msg := strings.Repeat("x", containerWaitErrorMsgLimit*5)
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)
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader(msg)),
}, nil
}),
}
client, err := NewClientWithOpts(WithMockClient(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: io.NopCloser(strings.NewReader(msg)),
}, nil
}), WithVersion("1.30"))
assert.NilError(t, err)
resultC, errC := client.ContainerWait(context.Background(), "container_id", "")
select {
@@ -146,15 +140,13 @@ func TestContainerWaitErrorHandling(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
client := &Client{
version: "1.30",
client: newMockClient(func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(test.rdr),
}, nil
}),
}
client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(test.rdr),
}, nil
}), WithVersion("1.30"))
assert.NilError(t, err)
resultC, errC := client.ContainerWait(ctx, "container_id", "")
select {
case err := <-errC: