client: WithMockClient: match version behavior of actual client

The WithMockClient option was explicitly resetting the client's API
version (see [1]), which differs from the regular client, which is
initialized with the current API version used by the client (see [2]).

This patch:

- reduces the `WithMockClient` to only set the custom HTTP client, leaving
  other fields un-touched.
- adds a test utility and updates tests to handle the API-version prefix
- removes redundant uses of `WithVersion()` in tests; for most test-cases
  it was used to make sure a current API version is used that supports the
  feature being tested, but there was no test to verify the behavior for
  lower API versions, so we may as well test against "latest".

[1]: 5a582729d8/client/client_mock_test.go (L22-L36)
[2]: 5a582729d8/client/client.go (L167-L190)

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-09-17 23:12:40 +02:00
parent 64419958cc
commit 839c2709af
93 changed files with 389 additions and 626 deletions

View File

@@ -8,7 +8,6 @@ import (
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -39,18 +38,16 @@ func TestCheckpointCreateError(t *testing.T) {
}
func TestCheckpointCreate(t *testing.T) {
expectedContainerID := "container_id"
expectedCheckpointID := "checkpoint_id"
expectedURL := "/containers/container_id/checkpoints"
const (
expectedContainerID = "container_id"
expectedCheckpointID = "checkpoint_id"
expectedURL = "/containers/container_id/checkpoints"
)
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)
}
if req.Method != http.MethodPost {
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
createOptions := &CheckpointCreateOptions{}

View File

@@ -3,10 +3,8 @@ package client
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -36,15 +34,12 @@ func TestCheckpointDeleteError(t *testing.T) {
}
func TestCheckpointDelete(t *testing.T) {
expectedURL := "/containers/container_id/checkpoints/checkpoint_id"
const expectedURL = "/containers/container_id/checkpoints/checkpoint_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)
}
if req.Method != http.MethodDelete {
return nil, fmt.Errorf("expected DELETE method, got %s", req.Method)
if err := assertRequest(req, http.MethodDelete, expectedURL); err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,

View File

@@ -4,10 +4,8 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -27,12 +25,12 @@ func TestCheckpointListError(t *testing.T) {
}
func TestCheckpointList(t *testing.T) {
expectedURL := "/containers/container_id/checkpoints"
const expectedURL = "/containers/container_id/checkpoints"
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)
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
content, err := json.Marshal([]checkpoint.Summary{
{

View File

@@ -3,12 +3,33 @@ package client
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"github.com/moby/moby/api/types/common"
)
// defaultAPIPath is the API path prefix for the default API version used.
const defaultAPIPath = "/v" + MaxAPIVersion
// assertRequest checks for the request method and path. If the expected
// path does not contain a version prefix, it is prefixed with the current API
// version.
func assertRequest(req *http.Request, expMethod string, expectedPath string) error {
if !strings.HasPrefix(expectedPath, "/v1.") {
expectedPath = defaultAPIPath + expectedPath
}
if !strings.HasPrefix(req.URL.Path, expectedPath) {
return fmt.Errorf("expected URL '%s', got '%s'", expectedPath, req.URL.Path)
}
if req.Method != expMethod {
return fmt.Errorf("expected %s method, got %s", expMethod, req.Method)
}
return nil
}
func transportEnsureBody(f transportFunc) transportFunc {
return func(req *http.Request) (*http.Response, error) {
resp, err := f(req)
@@ -21,18 +42,9 @@ func transportEnsureBody(f transportFunc) transportFunc {
// WithMockClient is a test helper that allows you to inject a mock client for testing.
func WithMockClient(doer func(*http.Request) (*http.Response, error)) Opt {
return func(c *clientConfig) error {
c.client = &http.Client{
Transport: transportEnsureBody(transportFunc(doer)),
}
if !c.manualOverride {
c.version = ""
}
c.host = ""
c.proto = ""
c.addr = ""
return nil
}
return WithHTTPClient(&http.Client{
Transport: transportEnsureBody(transportFunc(doer)),
})
}
func errorMock(statusCode int, message string) func(req *http.Request) (*http.Response, error) {

View File

@@ -4,10 +4,8 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -18,7 +16,6 @@ import (
func TestConfigCreateError(t *testing.T) {
client, err := NewClientWithOpts(
WithVersion("1.30"),
WithMockClient(errorMock(http.StatusInternalServerError, "Server error")),
)
assert.NilError(t, err)
@@ -28,15 +25,11 @@ func TestConfigCreateError(t *testing.T) {
}
func TestConfigCreate(t *testing.T) {
expectedURL := "/v1.30/configs/create"
const expectedURL = "/configs/create"
client, err := NewClientWithOpts(
WithVersion("1.30"),
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)
}
if req.Method != http.MethodPost {
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
b, err := json.Marshal(swarm.ConfigCreateResponse{
ID: "test_config",

View File

@@ -5,10 +5,8 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -45,7 +43,6 @@ func TestConfigInspectWithEmptyID(t *testing.T) {
func TestConfigInspectError(t *testing.T) {
client, err := NewClientWithOpts(
WithVersion("1.30"),
WithMockClient(errorMock(http.StatusInternalServerError, "Server error")),
)
assert.NilError(t, err)
@@ -56,7 +53,6 @@ func TestConfigInspectError(t *testing.T) {
func TestConfigInspectConfigNotFound(t *testing.T) {
client, err := NewClientWithOpts(
WithVersion("1.30"),
WithMockClient(errorMock(http.StatusNotFound, "Server error")),
)
assert.NilError(t, err)
@@ -66,12 +62,11 @@ func TestConfigInspectConfigNotFound(t *testing.T) {
}
func TestConfigInspect(t *testing.T) {
expectedURL := "/v1.30/configs/config_id"
const expectedURL = "/configs/config_id"
client, err := NewClientWithOpts(
WithVersion("1.30"),
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)
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
content, err := json.Marshal(swarm.Config{
ID: "config_id",

View File

@@ -7,7 +7,6 @@ import (
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -19,7 +18,6 @@ import (
func TestConfigListError(t *testing.T) {
client, err := NewClientWithOpts(
WithVersion("1.30"),
WithMockClient(errorMock(http.StatusInternalServerError, "Server error")),
)
assert.NilError(t, err)
@@ -29,7 +27,7 @@ func TestConfigListError(t *testing.T) {
}
func TestConfigList(t *testing.T) {
expectedURL := "/v1.30/configs"
const expectedURL = "/configs"
listCases := []struct {
options ConfigListOptions
@@ -55,10 +53,9 @@ func TestConfigList(t *testing.T) {
}
for _, listCase := range listCases {
client, err := NewClientWithOpts(
WithVersion("1.30"),
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)
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
query := req.URL.Query()
for key, expected := range listCase.expectedQueryParams {

View File

@@ -3,10 +3,8 @@ package client
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -16,7 +14,6 @@ import (
func TestConfigRemoveError(t *testing.T) {
client, err := NewClientWithOpts(
WithVersion("1.30"),
WithMockClient(errorMock(http.StatusInternalServerError, "Server error")),
)
assert.NilError(t, err)
@@ -34,16 +31,12 @@ func TestConfigRemoveError(t *testing.T) {
}
func TestConfigRemove(t *testing.T) {
expectedURL := "/v1.30/configs/config_id"
const expectedURL = "/configs/config_id"
client, err := NewClientWithOpts(
WithVersion("1.30"),
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)
}
if req.Method != http.MethodDelete {
return nil, fmt.Errorf("expected DELETE method, got %s", req.Method)
if err := assertRequest(req, http.MethodDelete, expectedURL); err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,

View File

@@ -3,10 +3,8 @@ package client
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -17,7 +15,6 @@ import (
func TestConfigUpdateError(t *testing.T) {
client, err := NewClientWithOpts(
WithVersion("1.30"),
WithMockClient(errorMock(http.StatusInternalServerError, "Server error")),
)
assert.NilError(t, err)
@@ -35,16 +32,12 @@ func TestConfigUpdateError(t *testing.T) {
}
func TestConfigUpdate(t *testing.T) {
expectedURL := "/v1.30/configs/config_id/update"
const expectedURL = "/configs/config_id/update"
client, err := NewClientWithOpts(
WithVersion("1.30"),
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)
}
if req.Method != http.MethodPost {
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,

View File

@@ -7,7 +7,6 @@ import (
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -48,8 +47,8 @@ func TestContainerCommit(t *testing.T) {
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)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
query := req.URL.Query()
containerID := query.Get("container")

View File

@@ -10,7 +10,6 @@ import (
"io"
"net/http"
"os"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -63,15 +62,14 @@ func TestContainerStatPathNoHeaderError(t *testing.T) {
}
func TestContainerStatPath(t *testing.T) {
expectedURL := "/containers/container_id/archive"
expectedPath := "path/to/file"
const (
expectedURL = "/containers/container_id/archive"
expectedPath = "path/to/file"
)
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)
}
if req.Method != http.MethodHead {
return nil, fmt.Errorf("expected HEAD method, got %s", req.Method)
if err := assertRequest(req, http.MethodHead, expectedURL); err != nil {
return nil, err
}
query := req.URL.Query()
path := query.Get("path")
@@ -143,15 +141,14 @@ func TestCopyToContainerEmptyResponse(t *testing.T) {
}
func TestCopyToContainer(t *testing.T) {
expectedURL := "/containers/container_id/archive"
expectedPath := "path/to/file"
const (
expectedURL = "/containers/container_id/archive"
expectedPath = "path/to/file"
)
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)
}
if req.Method != http.MethodPut {
return nil, fmt.Errorf("expected PUT method, got %s", req.Method)
if err := assertRequest(req, http.MethodPut, expectedURL); err != nil {
return nil, err
}
query := req.URL.Query()
path := query.Get("path")
@@ -259,15 +256,14 @@ func TestCopyFromContainerNoHeaderError(t *testing.T) {
}
func TestCopyFromContainer(t *testing.T) {
expectedURL := "/containers/container_id/archive"
expectedPath := "path/to/file"
const (
expectedURL = "/containers/container_id/archive"
expectedPath = "path/to/file"
)
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)
}
if req.Method != http.MethodGet {
return nil, fmt.Errorf("expected GET method, got %s", req.Method)
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
query := req.URL.Query()
path := query.Get("path")

View File

@@ -7,7 +7,6 @@ import (
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -50,11 +49,11 @@ func TestContainerCreateImageNotFound(t *testing.T) {
}
func TestContainerCreateWithName(t *testing.T) {
expectedURL := "/containers/create"
const expectedURL = "/containers/create"
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)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
name := req.URL.Query().Get("name")
if name != "container_name" {
@@ -179,7 +178,6 @@ func TestContainerCreateCapabilities(t *testing.T) {
Body: io.NopCloser(bytes.NewReader(b)),
}, nil
}),
WithVersion("1.24"),
)
assert.NilError(t, err)

View File

@@ -4,10 +4,8 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -54,8 +52,8 @@ func TestContainerDiff(t *testing.T) {
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)
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
b, err := json.Marshal(expected)
if err != nil {

View File

@@ -7,7 +7,6 @@ import (
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -47,14 +46,11 @@ func TestContainerExecCreateConnectionError(t *testing.T) {
}
func TestContainerExecCreate(t *testing.T) {
expectedURL := "/containers/container_id/exec"
const expectedURL = "/containers/container_id/exec"
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)
}
if req.Method != http.MethodPost {
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
// FIXME validate the content is the given ExecConfig ?
if err := req.ParseForm(); err != nil {
@@ -99,11 +95,11 @@ func TestContainerExecStartError(t *testing.T) {
}
func TestContainerExecStart(t *testing.T) {
expectedURL := "/exec/exec_id/start"
const expectedURL = "/exec/exec_id/start"
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)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
if err := req.ParseForm(); err != nil {
return nil, err
@@ -142,11 +138,11 @@ func TestContainerExecInspectError(t *testing.T) {
}
func TestContainerExecInspect(t *testing.T) {
expectedURL := "/exec/exec_id/json"
const expectedURL = "/exec/exec_id/json"
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)
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
b, err := json.Marshal(container.ExecInspectResponse{
ID: "exec_id",

View File

@@ -3,10 +3,8 @@ package client
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -33,13 +31,12 @@ func TestContainerExportError(t *testing.T) {
}
func TestContainerExport(t *testing.T) {
expectedURL := "/containers/container_id/export"
const expectedURL = "/containers/container_id/export"
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)
WithMockClient(func(req *http.Request) (*http.Response, error) {
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte("response"))),

View File

@@ -5,10 +5,8 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -71,11 +69,11 @@ func TestContainerInspectWithEmptyID(t *testing.T) {
}
func TestContainerInspect(t *testing.T) {
expectedURL := "/containers/container_id/json"
const expectedURL = "/containers/container_id/json"
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)
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
content, err := json.Marshal(container.InspectResponse{
ID: "container_id",

View File

@@ -6,7 +6,6 @@ import (
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -33,11 +32,11 @@ func TestContainerKillError(t *testing.T) {
}
func TestContainerKill(t *testing.T) {
expectedURL := "/containers/container_id/kill"
const expectedURL = "/containers/container_id/kill"
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)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
signal := req.URL.Query().Get("signal")
if signal != "SIGKILL" {

View File

@@ -7,7 +7,6 @@ import (
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -28,12 +27,14 @@ func TestContainerListError(t *testing.T) {
}
func TestContainerList(t *testing.T) {
expectedURL := "/containers/json"
expectedFilters := `{"before":{"container":true},"label":{"label1":true,"label2":true}}`
const (
expectedURL = "/containers/json"
expectedFilters = `{"before":{"container":true},"label":{"label1":true,"label2":true}}`
)
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)
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
query := req.URL.Query()
all := query.Get("all")

View File

@@ -9,7 +9,6 @@ import (
"log"
"net/http"
"os"
"strings"
"testing"
"time"
@@ -56,7 +55,7 @@ func TestContainerLogsError(t *testing.T) {
}
func TestContainerLogs(t *testing.T) {
expectedURL := "/containers/container_id/logs"
const expectedURL = "/containers/container_id/logs"
cases := []struct {
options ContainerLogsOptions
expectedQueryParams map[string]string
@@ -129,12 +128,12 @@ func TestContainerLogs(t *testing.T) {
}
for _, logCase := range cases {
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)
WithMockClient(func(req *http.Request) (*http.Response, error) {
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
// Check query parameters
query := r.URL.Query()
query := req.URL.Query()
for key, expected := range logCase.expectedQueryParams {
actual := query.Get(key)
if actual != expected {

View File

@@ -3,10 +3,8 @@ package client
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -25,11 +23,11 @@ func TestContainerPauseError(t *testing.T) {
}
func TestContainerPause(t *testing.T) {
expectedURL := "/containers/container_id/pause"
const expectedURL = "/containers/container_id/pause"
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)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,

View File

@@ -4,10 +4,8 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -18,7 +16,7 @@ import (
)
func TestContainersPruneError(t *testing.T) {
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")), WithVersion("1.25"))
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
assert.NilError(t, err)
_, err = client.ContainersPrune(context.Background(), filters.Args{})
@@ -26,7 +24,7 @@ func TestContainersPruneError(t *testing.T) {
}
func TestContainersPrune(t *testing.T) {
expectedURL := "/v1.25/containers/prune"
const expectedURL = "/containers/prune"
listCases := []struct {
filters filters.Args
@@ -82,8 +80,8 @@ func TestContainersPrune(t *testing.T) {
}
for _, listCase := range listCases {
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)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
query := req.URL.Query()
for key, expected := range listCase.expectedQueryParams {
@@ -101,7 +99,7 @@ func TestContainersPrune(t *testing.T) {
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)

View File

@@ -6,7 +6,6 @@ import (
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -38,10 +37,10 @@ func TestContainerRemoveNotFoundError(t *testing.T) {
}
func TestContainerRemove(t *testing.T) {
expectedURL := "/containers/container_id"
const expectedURL = "/containers/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)
if err := assertRequest(req, http.MethodDelete, expectedURL); err != nil {
return nil, err
}
query := req.URL.Query()
volume := query.Get("v")

View File

@@ -6,7 +6,6 @@ import (
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -30,10 +29,10 @@ func TestContainerRenameError(t *testing.T) {
}
func TestContainerRename(t *testing.T) {
expectedURL := "/containers/container_id/rename"
const expectedURL = "/containers/container_id/rename"
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)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
name := req.URL.Query().Get("name")
if name != "newName" {

View File

@@ -122,7 +122,7 @@ func TestContainerExecResize(t *testing.T) {
func resizeTransport(t *testing.T, expectedURL, expectedHeight, expectedWidth string) func(req *http.Request) (*http.Response, error) {
return func(req *http.Request) (*http.Response, error) {
assert.Check(t, is.Equal(req.URL.Path, expectedURL))
assert.Check(t, assertRequest(req, http.MethodPost, expectedURL))
query := req.URL.Query()
assert.Check(t, is.Equal(query.Get("h"), expectedHeight))

View File

@@ -6,7 +6,6 @@ import (
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -42,10 +41,10 @@ func TestContainerRestartConnectionError(t *testing.T) {
}
func TestContainerRestart(t *testing.T) {
const expectedURL = "/v1.42/containers/container_id/restart"
const expectedURL = "/containers/container_id/restart"
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)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
s := req.URL.Query().Get("signal")
if s != "SIGKILL" {
@@ -59,7 +58,7 @@ func TestContainerRestart(t *testing.T) {
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", ContainerStopOptions{

View File

@@ -7,7 +7,6 @@ import (
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -31,10 +30,10 @@ func TestContainerStartError(t *testing.T) {
}
func TestContainerStart(t *testing.T) {
expectedURL := "/containers/container_id/start"
const expectedURL = "/containers/container_id/start"
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)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
// we're not expecting any payload, but if one is supplied, check it is valid.
if req.Header.Get("Content-Type") == "application/json" {

View File

@@ -6,7 +6,6 @@ import (
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -30,8 +29,8 @@ func TestContainerStatsError(t *testing.T) {
}
func TestContainerStats(t *testing.T) {
expectedURL := "/containers/container_id/stats"
cases := []struct {
const expectedURL = "/containers/container_id/stats"
tests := []struct {
stream bool
expectedStream string
}{
@@ -43,16 +42,16 @@ func TestContainerStats(t *testing.T) {
expectedStream: "1",
},
}
for _, c := range cases {
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)
for _, tc := range tests {
client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
query := r.URL.Query()
query := req.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)
if stream != tc.expectedStream {
return nil, fmt.Errorf("stream not set in URL query properly. Expected '%s', got %s", tc.expectedStream, stream)
}
return &http.Response{
@@ -61,7 +60,7 @@ func TestContainerStats(t *testing.T) {
}, nil
}))
assert.NilError(t, err)
resp, err := client.ContainerStats(context.Background(), "container_id", c.stream)
resp, err := client.ContainerStats(context.Background(), "container_id", tc.stream)
assert.NilError(t, err)
t.Cleanup(func() {
_ = resp.Body.Close()

View File

@@ -6,7 +6,6 @@ import (
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -42,10 +41,10 @@ func TestContainerStopConnectionError(t *testing.T) {
}
func TestContainerStop(t *testing.T) {
const expectedURL = "/v1.42/containers/container_id/stop"
const expectedURL = "/containers/container_id/stop"
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)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
s := req.URL.Query().Get("signal")
if s != "SIGKILL" {
@@ -59,7 +58,7 @@ func TestContainerStop(t *testing.T) {
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", ContainerStopOptions{

View File

@@ -7,7 +7,6 @@ import (
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -32,7 +31,7 @@ func TestContainerTopError(t *testing.T) {
}
func TestContainerTop(t *testing.T) {
expectedURL := "/containers/container_id/top"
const expectedURL = "/containers/container_id/top"
expectedProcesses := [][]string{
{"p1", "p2"},
{"p3"},
@@ -40,8 +39,8 @@ func TestContainerTop(t *testing.T) {
expectedTitles := []string{"title1", "title2"}
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)
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
query := req.URL.Query()
args := query.Get("ps_args")

View File

@@ -3,10 +3,8 @@ package client
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -30,10 +28,10 @@ func TestContainerUnpauseError(t *testing.T) {
}
func TestContainerUnpause(t *testing.T) {
expectedURL := "/containers/container_id/unpause"
const expectedURL = "/containers/container_id/unpause"
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)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,

View File

@@ -4,10 +4,8 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -32,11 +30,11 @@ func TestContainerUpdateError(t *testing.T) {
}
func TestContainerUpdate(t *testing.T) {
expectedURL := "/containers/container_id/update"
const expectedURL = "/containers/container_id/update"
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)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
b, err := json.Marshal(container.UpdateResponse{})

View File

@@ -5,7 +5,6 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
@@ -51,10 +50,10 @@ func TestContainerWaitConnectionError(t *testing.T) {
}
func TestContainerWait(t *testing.T) {
expectedURL := "/containers/container_id/wait"
const expectedURL = "/containers/container_id/wait"
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)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
b, err := json.Marshal(container.WaitResponse{
StatusCode: 15,
@@ -79,40 +78,43 @@ 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"
const (
expectedURL = "/containers/container_id/wait"
expErr = "copying response body from Docker: unexpected EOF"
)
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)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader(msg)),
Body: io.NopCloser(strings.NewReader(expErr)),
}, nil
}), WithVersion("1.30"))
}))
assert.NilError(t, err)
resultC, errC := client.ContainerWait(context.Background(), "container_id", "")
select {
case err := <-errC:
assert.Check(t, is.ErrorContains(err, msg))
assert.Check(t, is.ErrorContains(err, expErr))
case result := <-resultC:
t.Fatalf("Unexpected result: %v", result)
}
}
func TestContainerWaitProxyInterruptLong(t *testing.T) {
expectedURL := "/v1.30/containers/container_id/wait"
const expectedURL = "/containers/container_id/wait"
msg := strings.Repeat("x", containerWaitErrorMsgLimit*5)
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)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
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", "")
@@ -145,7 +147,7 @@ func TestContainerWaitErrorHandling(t *testing.T) {
StatusCode: http.StatusOK,
Body: io.NopCloser(test.rdr),
}, nil
}), WithVersion("1.30"))
}))
assert.NilError(t, err)
resultC, errC := client.ContainerWait(ctx, "container_id", "")
select {

View File

@@ -7,7 +7,6 @@ import (
"io"
"net/http"
"reflect"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -154,24 +153,24 @@ func TestImageBuild(t *testing.T) {
expectedRegistryConfig: "eyJodHRwczovL2luZGV4LmRvY2tlci5pby92MS8iOnsiYXV0aCI6ImRHOTBid289In19",
},
}
const expectedURL = "/build"
for _, buildCase := range buildCases {
expectedURL := "/build"
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)
client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
// Check request headers
registryConfig := r.Header.Get("X-Registry-Config")
registryConfig := req.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")
contentType := req.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()
query := req.URL.Query()
for key, expected := range buildCase.expectedQueryParams {
actual := query.Get(key)
if actual != expected {

View File

@@ -6,7 +6,6 @@ import (
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -31,16 +30,16 @@ func TestImageCreate(t *testing.T) {
expectedRegistryAuth = "eyJodHRwczovL2luZGV4LmRvY2tlci5pby92MS8iOnsiYXV0aCI6ImRHOTBid289IiwiZW1haWwiOiJqb2huQGRvZS5jb20ifX0="
)
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)
client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
registryAuth := r.Header.Get(registry.AuthHeader)
registryAuth := req.Header.Get(registry.AuthHeader)
if registryAuth != expectedRegistryAuth {
return nil, fmt.Errorf("%s header not properly set in the request. Expected '%s', got %s", registry.AuthHeader, expectedRegistryAuth, registryAuth)
}
query := r.URL.Query()
query := req.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)

View File

@@ -27,9 +27,9 @@ func TestImageHistory(t *testing.T) {
historyResponse = `[{"Comment":"","Created":0,"CreatedBy":"","Id":"image_id1","Size":0,"Tags":["tag1","tag2"]},{"Comment":"","Created":0,"CreatedBy":"","Id":"image_id2","Size":0,"Tags":["tag1","tag2"]}]`
expectedPlatform = `{"architecture":"arm64","os":"linux","variant":"v8"}`
)
client, err := NewClientWithOpts(WithMockClient(func(r *http.Request) (*http.Response, error) {
assert.Check(t, is.Equal(r.URL.Path, expectedURL))
assert.Check(t, is.Equal(r.URL.Query().Get("platform"), expectedPlatform))
client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
assert.Check(t, assertRequest(req, http.MethodGet, expectedURL))
assert.Check(t, is.Equal(req.URL.Query().Get("platform"), expectedPlatform))
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader(historyResponse)),

View File

@@ -68,7 +68,7 @@ func TestImageImport(t *testing.T) {
for _, tc := range tests {
t.Run(tc.doc, func(t *testing.T) {
client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
assert.Check(t, is.Equal(req.URL.Path, expectedURL))
assert.Check(t, assertRequest(req, http.MethodPost, expectedURL))
query := req.URL.Query()
assert.Check(t, is.DeepEqual(query, tc.expectedQueryParams))
return &http.Response{

View File

@@ -8,7 +8,6 @@ import (
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -44,11 +43,11 @@ func TestImageInspectWithEmptyID(t *testing.T) {
}
func TestImageInspect(t *testing.T) {
expectedURL := "/images/image_id/json"
const expectedURL = "/images/image_id/json"
expectedTags := []string{"tag1", "tag2"}
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)
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
content, err := json.Marshal(image.InspectResponse{
ID: "image_id",
@@ -71,7 +70,7 @@ func TestImageInspect(t *testing.T) {
}
func TestImageInspectWithPlatform(t *testing.T) {
expectedURL := "/images/image_id/json"
const expectedURL = "/images/image_id/json"
requestedPlatform := &ocispec.Platform{
OS: "linux",
Architecture: "arm64",
@@ -81,8 +80,8 @@ func TestImageInspectWithPlatform(t *testing.T) {
assert.NilError(t, 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)
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
// Check if platform parameter is passed correctly

View File

@@ -80,8 +80,8 @@ func TestImageList(t *testing.T) {
}
for _, listCase := range listCases {
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)
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
query := req.URL.Query()
for key, expected := range listCase.expectedQueryParams {

View File

@@ -82,7 +82,7 @@ func TestImageLoad(t *testing.T) {
for _, tc := range tests {
t.Run(tc.doc, func(t *testing.T) {
client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
assert.Check(t, is.Equal(req.URL.Path, expectedURL))
assert.Check(t, assertRequest(req, http.MethodPost, expectedURL))
assert.Check(t, is.Equal(req.Header.Get("Content-Type"), expectedContentType))
assert.Check(t, is.DeepEqual(req.URL.Query(), tc.expectedQueryParams))
return &http.Response{

View File

@@ -4,10 +4,8 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -19,7 +17,7 @@ import (
)
func TestImagesPruneError(t *testing.T) {
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")), WithVersion("1.25"))
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
assert.NilError(t, err)
_, err = client.ImagesPrune(context.Background(), filters.NewArgs())
@@ -27,7 +25,7 @@ func TestImagesPruneError(t *testing.T) {
}
func TestImagesPrune(t *testing.T) {
const expectedURL = "/v1.25/images/prune"
const expectedURL = "/images/prune"
listCases := []struct {
filters filters.Args
@@ -72,8 +70,8 @@ func TestImagesPrune(t *testing.T) {
}
for _, listCase := range listCases {
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)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
query := req.URL.Query()
for key, expected := range listCase.expectedQueryParams {
@@ -98,7 +96,7 @@ func TestImagesPrune(t *testing.T) {
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(content)),
}, nil
}), WithVersion("1.25"))
}))
assert.NilError(t, err)
report, err := client.ImagesPrune(context.Background(), listCase.filters)

View File

@@ -7,7 +7,6 @@ import (
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -65,8 +64,8 @@ func TestImagePullWithPrivilegedFuncNoError(t *testing.T) {
const invalidAuth = "NotValid"
const validAuth = "IAmValid"
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)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
auth := req.Header.Get(registry.AuthHeader)
if auth == invalidAuth {
@@ -167,8 +166,8 @@ func TestImagePullWithoutErrors(t *testing.T) {
for _, pullCase := range pullCases {
t.Run(pullCase.reference, func(t *testing.T) {
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)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
query := req.URL.Query()
fromImage := query.Get("fromImage")

View File

@@ -7,7 +7,6 @@ import (
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -72,8 +71,8 @@ func TestImagePushWithPrivilegedFuncNoError(t *testing.T) {
const invalidAuth = "NotValid"
const validAuth = "IAmValid"
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)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
auth := req.Header.Get(registry.AuthHeader)
if auth == invalidAuth {
@@ -171,8 +170,8 @@ func TestImagePushWithoutErrors(t *testing.T) {
t.Run(fmt.Sprintf("%s,all-tags=%t", tc.reference, tc.all), func(t *testing.T) {
client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
expectedURL := fmt.Sprintf(expectedURLFormat, tc.expectedImage)
if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
query := req.URL.Query()
tag := query.Get("tag")

View File

@@ -7,7 +7,6 @@ import (
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -35,7 +34,7 @@ func TestImageRemoveImageNotFound(t *testing.T) {
}
func TestImageRemove(t *testing.T) {
expectedURL := "/images/image_id"
const expectedURL = "/images/image_id"
removeCases := []struct {
force bool
pruneChildren bool
@@ -70,11 +69,8 @@ func TestImageRemove(t *testing.T) {
}
for _, removeCase := range removeCases {
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)
}
if req.Method != http.MethodDelete {
return nil, fmt.Errorf("expected DELETE method, got %s", req.Method)
if err := assertRequest(req, http.MethodDelete, expectedURL); err != nil {
return nil, err
}
query := req.URL.Query()
for key, expected := range removeCase.expectedQueryParams {

View File

@@ -65,7 +65,7 @@ func TestImageSave(t *testing.T) {
for _, tc := range tests {
t.Run(tc.doc, func(t *testing.T) {
client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
assert.Check(t, is.Equal(req.URL.Path, expectedURL))
assert.Check(t, assertRequest(req, http.MethodGet, expectedURL))
assert.Check(t, is.DeepEqual(req.URL.Query(), tc.expectedQueryParams))
return &http.Response{
StatusCode: http.StatusOK,

View File

@@ -8,7 +8,6 @@ import (
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -57,10 +56,10 @@ func TestImageSearchWithUnauthorizedErrorAndAnotherUnauthorizedError(t *testing.
}
func TestImageSearchWithPrivilegedFuncNoError(t *testing.T) {
expectedURL := "/images/search"
const expectedURL = "/images/search"
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)
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
auth := req.Header.Get(registry.AuthHeader)
if auth == "NotValid" {
@@ -107,8 +106,8 @@ func TestImageSearchWithoutErrors(t *testing.T) {
const expectedFilters = `{"is-automated":{"true":true},"stars":{"3":true}}`
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)
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
query := req.URL.Query()
term := query.Get("term")

View File

@@ -7,7 +7,6 @@ import (
"io"
"math/rand"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -154,11 +153,8 @@ func TestImageTag(t *testing.T) {
}
for _, tagCase := range tagCases {
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)
}
if req.Method != http.MethodPost {
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
query := req.URL.Query()
for key, expected := range tagCase.expectedQueryParams {

View File

@@ -7,7 +7,6 @@ import (
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -34,15 +33,11 @@ func TestNetworkConnectError(t *testing.T) {
}
func TestNetworkConnectEmptyNilEndpointSettings(t *testing.T) {
expectedURL := "/networks/network_id/connect"
const expectedURL = "/networks/network_id/connect"
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)
}
if req.Method != http.MethodPost {
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
var connect NetworkConnectOptions
@@ -70,15 +65,11 @@ func TestNetworkConnectEmptyNilEndpointSettings(t *testing.T) {
}
func TestNetworkConnect(t *testing.T) {
expectedURL := "/networks/network_id/connect"
const expectedURL = "/networks/network_id/connect"
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)
}
if req.Method != http.MethodPost {
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
var connect NetworkConnectOptions

View File

@@ -4,10 +4,8 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -37,15 +35,11 @@ func TestNetworkCreateConnectionError(t *testing.T) {
}
func TestNetworkCreate(t *testing.T) {
expectedURL := "/networks/create"
const expectedURL = "/networks/create"
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)
}
if req.Method != http.MethodPost {
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
content, err := json.Marshal(network.CreateResponse{

View File

@@ -7,7 +7,6 @@ import (
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -33,15 +32,11 @@ func TestNetworkDisconnectError(t *testing.T) {
}
func TestNetworkDisconnect(t *testing.T) {
expectedURL := "/networks/network_id/disconnect"
const expectedURL = "/networks/network_id/disconnect"
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)
}
if req.Method != http.MethodPost {
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
var disconnect NetworkDisconnectOptions

View File

@@ -4,7 +4,6 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"net/http"
"strings"
@@ -17,22 +16,22 @@ import (
)
func TestNetworkInspect(t *testing.T) {
const expectedURL = "/networks/network_id"
client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
if req.Method != http.MethodGet {
return nil, errors.New("expected GET method, got " + req.Method)
}
if req.URL.Path == "/networks/" {
if req.URL.Path == defaultAPIPath+"/networks/" {
return errorMock(http.StatusInternalServerError, "client should not make a request for empty IDs")(req)
}
if strings.HasPrefix(req.URL.Path, "/networks/unknown") {
if strings.HasPrefix(req.URL.Path, defaultAPIPath+"/networks/unknown") {
return errorMock(http.StatusNotFound, "Error: No such network: unknown")(req)
}
if strings.HasPrefix(req.URL.Path, "/networks/test-500-response") {
if strings.HasPrefix(req.URL.Path, defaultAPIPath+"/networks/test-500-response") {
return errorMock(http.StatusInternalServerError, "Server error")(req)
}
// other test-cases all use "network_id"
if !strings.HasPrefix(req.URL.Path, "/networks/network_id") {
return nil, errors.New("expected URL '/networks/network_id', got " + req.URL.Path)
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
if strings.Contains(req.URL.RawQuery, "scope=global") {
return errorMock(http.StatusNotFound, "Error: No such network: network_id")(req)

View File

@@ -7,7 +7,6 @@ import (
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -61,11 +60,8 @@ func TestNetworkList(t *testing.T) {
for _, listCase := range listCases {
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)
}
if req.Method != http.MethodGet {
return nil, fmt.Errorf("expected GET method, got %s", req.Method)
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
query := req.URL.Query()
actualFilters := query.Get("filters")

View File

@@ -4,10 +4,8 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -20,7 +18,6 @@ import (
func TestNetworksPruneError(t *testing.T) {
client, err := NewClientWithOpts(
WithMockClient(errorMock(http.StatusInternalServerError, "Server error")),
WithVersion("1.25"),
)
assert.NilError(t, err)
@@ -29,7 +26,7 @@ func TestNetworksPruneError(t *testing.T) {
}
func TestNetworksPrune(t *testing.T) {
const expectedURL = "/v1.25/networks/prune"
const expectedURL = "/networks/prune"
listCases := []struct {
filters filters.Args
@@ -75,8 +72,8 @@ func TestNetworksPrune(t *testing.T) {
for _, listCase := range listCases {
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)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
query := req.URL.Query()
for key, expected := range listCase.expectedQueryParams {
@@ -94,7 +91,6 @@ func TestNetworksPrune(t *testing.T) {
Body: io.NopCloser(bytes.NewReader(content)),
}, nil
}),
WithVersion("1.25"),
)
assert.NilError(t, err)

View File

@@ -3,10 +3,8 @@ package client
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -31,14 +29,11 @@ func TestNetworkRemoveError(t *testing.T) {
}
func TestNetworkRemove(t *testing.T) {
expectedURL := "/networks/network_id"
const expectedURL = "/networks/network_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)
}
if req.Method != http.MethodDelete {
return nil, fmt.Errorf("expected DELETE method, got %s", req.Method)
if err := assertRequest(req, http.MethodDelete, expectedURL); err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,

View File

@@ -5,10 +5,8 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -48,10 +46,10 @@ func TestNodeInspectWithEmptyID(t *testing.T) {
}
func TestNodeInspect(t *testing.T) {
expectedURL := "/nodes/node_id"
const expectedURL = "/nodes/node_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)
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
content, err := json.Marshal(swarm.Node{
ID: "node_id",

View File

@@ -7,7 +7,6 @@ import (
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -52,8 +51,8 @@ func TestNodeList(t *testing.T) {
}
for _, listCase := range listCases {
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)
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
query := req.URL.Query()
for key, expected := range listCase.expectedQueryParams {

View File

@@ -6,7 +6,6 @@ import (
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -31,7 +30,7 @@ func TestNodeRemoveError(t *testing.T) {
}
func TestNodeRemove(t *testing.T) {
expectedURL := "/nodes/node_id"
const expectedURL = "/nodes/node_id"
removeCases := []struct {
force bool
@@ -48,11 +47,8 @@ func TestNodeRemove(t *testing.T) {
for _, removeCase := range removeCases {
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)
}
if req.Method != http.MethodDelete {
return nil, fmt.Errorf("expected DELETE method, got %s", req.Method)
if err := assertRequest(req, http.MethodDelete, expectedURL); err != nil {
return nil, err
}
force := req.URL.Query().Get("force")
if force != removeCase.expectedForce {

View File

@@ -3,10 +3,8 @@ package client
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -32,14 +30,11 @@ func TestNodeUpdateError(t *testing.T) {
}
func TestNodeUpdate(t *testing.T) {
expectedURL := "/nodes/node_id/update"
const expectedURL = "/nodes/node_id/update"
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)
}
if req.Method != http.MethodPost {
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,

View File

@@ -3,10 +3,8 @@ package client
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -31,14 +29,11 @@ func TestPluginDisableError(t *testing.T) {
}
func TestPluginDisable(t *testing.T) {
expectedURL := "/plugins/plugin_name/disable"
const expectedURL = "/plugins/plugin_name/disable"
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)
}
if req.Method != http.MethodPost {
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,

View File

@@ -3,10 +3,8 @@ package client
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -31,14 +29,11 @@ func TestPluginEnableError(t *testing.T) {
}
func TestPluginEnable(t *testing.T) {
expectedURL := "/plugins/plugin_name/enable"
const expectedURL = "/plugins/plugin_name/enable"
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)
}
if req.Method != http.MethodPost {
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,

View File

@@ -5,10 +5,8 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -40,10 +38,10 @@ func TestPluginInspectWithEmptyID(t *testing.T) {
}
func TestPluginInspect(t *testing.T) {
expectedURL := "/plugins/plugin_name"
const expectedURL = "/plugins/plugin_name"
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)
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
content, err := json.Marshal(plugin.Plugin{
ID: "plugin_id",

View File

@@ -7,7 +7,6 @@ import (
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -63,8 +62,8 @@ func TestPluginList(t *testing.T) {
for _, listCase := range listCases {
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)
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
query := req.URL.Query()
for key, expected := range listCase.expectedQueryParams {

View File

@@ -6,7 +6,6 @@ import (
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -32,14 +31,11 @@ func TestPluginPushError(t *testing.T) {
}
func TestPluginPush(t *testing.T) {
expectedURL := "/plugins/plugin_name"
const expectedURL = "/plugins/plugin_name"
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)
}
if req.Method != http.MethodPost {
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
auth := req.Header.Get(registry.AuthHeader)
if auth != "authtoken" {

View File

@@ -3,10 +3,8 @@ package client
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -31,14 +29,11 @@ func TestPluginRemoveError(t *testing.T) {
}
func TestPluginRemove(t *testing.T) {
expectedURL := "/plugins/plugin_name"
const expectedURL = "/plugins/plugin_name"
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)
}
if req.Method != http.MethodDelete {
return nil, fmt.Errorf("expected DELETE method, got %s", req.Method)
if err := assertRequest(req, http.MethodDelete, expectedURL); err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,

View File

@@ -3,10 +3,8 @@ package client
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -31,14 +29,11 @@ func TestPluginSetError(t *testing.T) {
}
func TestPluginSet(t *testing.T) {
expectedURL := "/plugins/plugin_name/set"
const expectedURL = "/plugins/plugin_name/set"
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)
}
if req.Method != http.MethodPost {
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,

View File

@@ -52,8 +52,8 @@ func TestSetHostHeader(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.host, func(t *testing.T) {
client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, testEndpoint) {
return nil, fmt.Errorf("expected URL %q, got %q", testEndpoint, req.URL)
if err := assertRequest(req, http.MethodGet, testEndpoint); err != nil {
return nil, err
}
if req.Host != tc.expectedHost {
return nil, fmt.Errorf("wxpected host %q, got %q", tc.expectedHost, req.Host)

View File

@@ -4,10 +4,8 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -17,20 +15,17 @@ import (
)
func TestSecretCreateError(t *testing.T) {
client, err := NewClientWithOpts(WithVersion("1.25"), WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
assert.NilError(t, err)
_, err = client.SecretCreate(context.Background(), swarm.SecretSpec{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
}
func TestSecretCreate(t *testing.T) {
expectedURL := "/v1.25/secrets/create"
client, err := NewClientWithOpts(WithVersion("1.25"), 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)
}
if req.Method != http.MethodPost {
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
const expectedURL = "/secrets/create"
client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
b, err := json.Marshal(swarm.SecretCreateResponse{
ID: "test_secret",

View File

@@ -5,10 +5,8 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -18,7 +16,7 @@ import (
)
func TestSecretInspectError(t *testing.T) {
client, err := NewClientWithOpts(WithVersion("1.25"), WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
assert.NilError(t, err)
_, _, err = client.SecretInspectWithRaw(context.Background(), "nothing")
@@ -26,7 +24,7 @@ func TestSecretInspectError(t *testing.T) {
}
func TestSecretInspectSecretNotFound(t *testing.T) {
client, err := NewClientWithOpts(WithVersion("1.25"), WithMockClient(errorMock(http.StatusNotFound, "Server error")))
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusNotFound, "Server error")))
assert.NilError(t, err)
_, _, err = client.SecretInspectWithRaw(context.Background(), "unknown")
@@ -48,10 +46,10 @@ func TestSecretInspectWithEmptyID(t *testing.T) {
}
func TestSecretInspect(t *testing.T) {
expectedURL := "/v1.25/secrets/secret_id"
client, err := NewClientWithOpts(WithVersion("1.25"), 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)
const expectedURL = "/secrets/secret_id"
client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
content, err := json.Marshal(swarm.Secret{
ID: "secret_id",

View File

@@ -7,7 +7,6 @@ import (
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -18,7 +17,7 @@ import (
)
func TestSecretListError(t *testing.T) {
client, err := NewClientWithOpts(WithVersion("1.25"), WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
assert.NilError(t, err)
_, err = client.SecretList(context.Background(), SecretListOptions{})
@@ -26,7 +25,7 @@ func TestSecretListError(t *testing.T) {
}
func TestSecretList(t *testing.T) {
const expectedURL = "/v1.25/secrets"
const expectedURL = "/secrets"
listCases := []struct {
options SecretListOptions
@@ -51,9 +50,9 @@ func TestSecretList(t *testing.T) {
},
}
for _, listCase := range listCases {
client, err := NewClientWithOpts(WithVersion("1.25"), 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)
client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
query := req.URL.Query()
for key, expected := range listCase.expectedQueryParams {

View File

@@ -3,10 +3,8 @@ package client
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -15,7 +13,7 @@ import (
)
func TestSecretRemoveError(t *testing.T) {
client, err := NewClientWithOpts(WithVersion("1.25"), WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
assert.NilError(t, err)
err = client.SecretRemove(context.Background(), "secret_id")
@@ -31,14 +29,11 @@ func TestSecretRemoveError(t *testing.T) {
}
func TestSecretRemove(t *testing.T) {
expectedURL := "/v1.25/secrets/secret_id"
const expectedURL = "/secrets/secret_id"
client, err := NewClientWithOpts(WithVersion("1.25"), 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)
}
if req.Method != http.MethodDelete {
return nil, fmt.Errorf("expected DELETE method, got %s", req.Method)
client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
if err := assertRequest(req, http.MethodDelete, expectedURL); err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,

View File

@@ -3,10 +3,8 @@ package client
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -16,7 +14,7 @@ import (
)
func TestSecretUpdateError(t *testing.T) {
client, err := NewClientWithOpts(WithVersion("1.25"), WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
assert.NilError(t, err)
err = client.SecretUpdate(context.Background(), "secret_id", swarm.Version{}, swarm.SecretSpec{})
@@ -32,14 +30,11 @@ func TestSecretUpdateError(t *testing.T) {
}
func TestSecretUpdate(t *testing.T) {
expectedURL := "/v1.25/secrets/secret_id/update"
const expectedURL = "/secrets/secret_id/update"
client, err := NewClientWithOpts(WithVersion("1.25"), 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)
}
if req.Method != http.MethodPost {
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,

View File

@@ -40,13 +40,10 @@ func TestServiceCreateConnectionError(t *testing.T) {
}
func TestServiceCreate(t *testing.T) {
expectedURL := "/services/create"
const expectedURL = "/services/create"
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)
}
if req.Method != http.MethodPost {
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
b, err := json.Marshal(swarm.ServiceCreateResponse{
ID: "service_id",
@@ -67,8 +64,8 @@ func TestServiceCreate(t *testing.T) {
}
func TestServiceCreateCompatiblePlatforms(t *testing.T) {
client, err := NewClientWithOpts(WithVersion("1.30"), WithMockClient(func(req *http.Request) (*http.Response, error) {
if strings.HasPrefix(req.URL.Path, "/v1.30/services/create") {
client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
if strings.HasPrefix(req.URL.Path, defaultAPIPath+"/services/create") {
var serviceSpec swarm.ServiceSpec
// check if the /distribution endpoint returned correct output
@@ -91,7 +88,7 @@ func TestServiceCreateCompatiblePlatforms(t *testing.T) {
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(b)),
}, nil
} else if strings.HasPrefix(req.URL.Path, "/v1.30/distribution/") {
} else if strings.HasPrefix(req.URL.Path, defaultAPIPath+"/distribution/") {
b, err := json.Marshal(registrytypes.DistributionInspect{
Descriptor: ocispec.Descriptor{
Digest: "sha256:c0537ff6a5218ef531ece93d4984efc99bbf3f7497c0a7726c88e2bb7584dc96",
@@ -145,8 +142,8 @@ func TestServiceCreateDigestPinning(t *testing.T) {
{"cannotresolve", "cannotresolve:latest"},
}
client, err := NewClientWithOpts(WithVersion("1.30"), WithMockClient(func(req *http.Request) (*http.Response, error) {
if strings.HasPrefix(req.URL.Path, "/v1.30/services/create") {
client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
if strings.HasPrefix(req.URL.Path, defaultAPIPath+"/services/create") {
// reset and set image received by the service create endpoint
serviceCreateImage = ""
var service swarm.ServiceSpec
@@ -165,10 +162,10 @@ func TestServiceCreateDigestPinning(t *testing.T) {
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(b)),
}, nil
} else if strings.HasPrefix(req.URL.Path, "/v1.30/distribution/cannotresolve") {
} else if strings.HasPrefix(req.URL.Path, defaultAPIPath+"/distribution/cannotresolve") {
// unresolvable image
return nil, errors.New("cannot resolve image")
} else if strings.HasPrefix(req.URL.Path, "/v1.30/distribution/") {
} else if strings.HasPrefix(req.URL.Path, defaultAPIPath+"/distribution/") {
// resolvable images
b, err := json.Marshal(registrytypes.DistributionInspect{
Descriptor: ocispec.Descriptor{

View File

@@ -5,10 +5,8 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -48,10 +46,10 @@ func TestServiceInspectWithEmptyID(t *testing.T) {
}
func TestServiceInspect(t *testing.T) {
expectedURL := "/services/service_id"
const expectedURL = "/services/service_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)
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
content, err := json.Marshal(swarm.Service{
ID: "service_id",

View File

@@ -7,7 +7,6 @@ import (
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -52,8 +51,8 @@ func TestServiceList(t *testing.T) {
}
for _, listCase := range listCases {
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)
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
query := req.URL.Query()
for key, expected := range listCase.expectedQueryParams {

View File

@@ -9,7 +9,6 @@ import (
"log"
"net/http"
"os"
"strings"
"testing"
"time"
@@ -39,7 +38,7 @@ func TestServiceLogsError(t *testing.T) {
}
func TestServiceLogs(t *testing.T) {
expectedURL := "/services/service_id/logs"
const expectedURL = "/services/service_id/logs"
cases := []struct {
options ContainerLogsOptions
expectedQueryParams map[string]string
@@ -94,12 +93,12 @@ func TestServiceLogs(t *testing.T) {
},
}
for _, logCase := range cases {
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)
client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
// Check query parameters
query := r.URL.Query()
query := req.URL.Query()
for key, expected := range logCase.expectedQueryParams {
actual := query.Get(key)
if actual != expected {

View File

@@ -3,10 +3,8 @@ package client
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -40,14 +38,11 @@ func TestServiceRemoveNotFoundError(t *testing.T) {
}
func TestServiceRemove(t *testing.T) {
expectedURL := "/services/service_id"
const expectedURL = "/services/service_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)
}
if req.Method != http.MethodDelete {
return nil, fmt.Errorf("expected DELETE method, got %s", req.Method)
if err := assertRequest(req, http.MethodDelete, expectedURL); err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,

View File

@@ -6,7 +6,6 @@ import (
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -44,7 +43,7 @@ func TestServiceUpdateConnectionError(t *testing.T) {
}
func TestServiceUpdate(t *testing.T) {
expectedURL := "/services/service_id/update"
const expectedURL = "/services/service_id/update"
updateCases := []struct {
swarmVersion swarm.Version
@@ -69,11 +68,8 @@ func TestServiceUpdate(t *testing.T) {
for _, updateCase := range updateCases {
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)
}
if req.Method != http.MethodPost {
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
version := req.URL.Query().Get("version")
if version != updateCase.expectedVersion {

View File

@@ -4,10 +4,8 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -25,15 +23,14 @@ func TestSwarmGetUnlockKeyError(t *testing.T) {
}
func TestSwarmGetUnlockKey(t *testing.T) {
expectedURL := "/swarm/unlockkey"
unlockKey := "SWMKEY-1-y6guTZNTwpQeTL5RhUfOsdBdXoQjiB2GADHSRJvbXeE"
const (
expectedURL = "/swarm/unlockkey"
unlockKey = "SWMKEY-1-y6guTZNTwpQeTL5RhUfOsdBdXoQjiB2GADHSRJvbXeE"
)
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)
}
if req.Method != http.MethodGet {
return nil, fmt.Errorf("expected GET method, got %s", req.Method)
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
key := swarm.UnlockKeyResponse{

View File

@@ -3,10 +3,8 @@ package client
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -24,14 +22,11 @@ func TestSwarmInitError(t *testing.T) {
}
func TestSwarmInit(t *testing.T) {
expectedURL := "/swarm/init"
const expectedURL = "/swarm/init"
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)
}
if req.Method != http.MethodPost {
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,

View File

@@ -4,10 +4,8 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -25,10 +23,10 @@ func TestSwarmInspectError(t *testing.T) {
}
func TestSwarmInspect(t *testing.T) {
expectedURL := "/swarm"
const expectedURL = "/swarm"
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)
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
content, err := json.Marshal(swarm.Swarm{
ClusterInfo: swarm.ClusterInfo{

View File

@@ -3,10 +3,8 @@ package client
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -24,14 +22,11 @@ func TestSwarmJoinError(t *testing.T) {
}
func TestSwarmJoin(t *testing.T) {
expectedURL := "/swarm/join"
const expectedURL = "/swarm/join"
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)
}
if req.Method != http.MethodPost {
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,

View File

@@ -6,7 +6,6 @@ import (
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -23,7 +22,7 @@ func TestSwarmLeaveError(t *testing.T) {
}
func TestSwarmLeave(t *testing.T) {
expectedURL := "/swarm/leave"
const expectedURL = "/swarm/leave"
leaveCases := []struct {
force bool
@@ -40,11 +39,8 @@ func TestSwarmLeave(t *testing.T) {
for _, leaveCase := range leaveCases {
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)
}
if req.Method != http.MethodPost {
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
force := req.URL.Query().Get("force")
if force != leaveCase.expectedForce {

View File

@@ -3,10 +3,8 @@ package client
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -24,14 +22,11 @@ func TestSwarmUnlockError(t *testing.T) {
}
func TestSwarmUnlock(t *testing.T) {
expectedURL := "/swarm/unlock"
const expectedURL = "/swarm/unlock"
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)
}
if req.Method != http.MethodPost {
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,

View File

@@ -3,10 +3,8 @@ package client
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -24,14 +22,11 @@ func TestSwarmUpdateError(t *testing.T) {
}
func TestSwarmUpdate(t *testing.T) {
expectedURL := "/swarm/update"
const expectedURL = "/swarm/update"
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)
}
if req.Method != http.MethodPost {
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,

View File

@@ -4,10 +4,8 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -24,10 +22,10 @@ func TestDiskUsageError(t *testing.T) {
}
func TestDiskUsage(t *testing.T) {
expectedURL := "/system/df"
const expectedURL = "/system/df"
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)
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
du := system.DiskUsage{

View File

@@ -8,7 +8,6 @@ import (
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -109,8 +108,8 @@ func TestEvents(t *testing.T) {
for _, eventsCase := range eventsCases {
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)
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
query := req.URL.Query()

View File

@@ -4,10 +4,8 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -36,10 +34,10 @@ func TestInfoInvalidResponseJSONError(t *testing.T) {
}
func TestInfo(t *testing.T) {
expectedURL := "/info"
const expectedURL = "/info"
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)
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
info := &system.Info{
ID: "daemonID",
@@ -65,10 +63,10 @@ func TestInfo(t *testing.T) {
}
func TestInfoWithDiscoveredDevices(t *testing.T) {
expectedURL := "/info"
const expectedURL = "/info"
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)
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
info := &system.Info{
ID: "daemonID",

View File

@@ -5,10 +5,8 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -40,10 +38,10 @@ func TestTaskInspectWithEmptyID(t *testing.T) {
}
func TestTaskInspect(t *testing.T) {
expectedURL := "/tasks/task_id"
const expectedURL = "/tasks/task_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)
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
content, err := json.Marshal(swarm.Task{
ID: "task_id",

View File

@@ -7,7 +7,6 @@ import (
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -52,8 +51,8 @@ func TestTaskList(t *testing.T) {
}
for _, listCase := range listCases {
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)
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
query := req.URL.Query()
for key, expected := range listCase.expectedQueryParams {

View File

@@ -4,10 +4,8 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -25,15 +23,11 @@ func TestVolumeCreateError(t *testing.T) {
}
func TestVolumeCreate(t *testing.T) {
expectedURL := "/volumes/create"
const expectedURL = "/volumes/create"
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)
}
if req.Method != http.MethodPost {
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
content, err := json.Marshal(volume.Volume{

View File

@@ -5,10 +5,8 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -48,7 +46,7 @@ func TestVolumeInspectWithEmptyID(t *testing.T) {
}
func TestVolumeInspect(t *testing.T) {
expectedURL := "/volumes/volume_id"
const expectedURL = "/volumes/volume_id"
expected := volume.Volume{
Name: "name",
Driver: "driver",
@@ -56,11 +54,8 @@ func TestVolumeInspect(t *testing.T) {
}
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)
}
if req.Method != http.MethodGet {
return nil, fmt.Errorf("expected GET method, got %s", req.Method)
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
content, err := json.Marshal(expected)
if err != nil {

View File

@@ -7,7 +7,6 @@ import (
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -52,8 +51,8 @@ func TestVolumeList(t *testing.T) {
for _, listCase := range listCases {
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)
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
query := req.URL.Query()
actualFilters := query.Get("filters")

View File

@@ -3,10 +3,8 @@ package client
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -43,14 +41,11 @@ func TestVolumeRemoveConnectionError(t *testing.T) {
}
func TestVolumeRemove(t *testing.T) {
expectedURL := "/volumes/volume_id"
const expectedURL = "/volumes/volume_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)
}
if req.Method != http.MethodDelete {
return nil, fmt.Errorf("expected DELETE method, got %s", req.Method)
if err := assertRequest(req, http.MethodDelete, expectedURL); err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,

View File

@@ -33,15 +33,14 @@ func TestVolumeUpdateError(t *testing.T) {
}
func TestVolumeUpdate(t *testing.T) {
expectedURL := "/volumes/test1"
expectedVersion := "version=10"
const (
expectedURL = "/volumes/test1"
expectedVersion = "version=10"
)
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)
}
if req.Method != http.MethodPut {
return nil, fmt.Errorf("expected PUT method, got %s", req.Method)
if err := assertRequest(req, http.MethodPut, expectedURL); err != nil {
return nil, err
}
if !strings.Contains(req.URL.RawQuery, expectedVersion) {
return nil, fmt.Errorf("expected query to contain '%s', got '%s'", expectedVersion, req.URL.RawQuery)