client: simplify test with mock-responses

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-10-23 14:20:03 +02:00
parent 1d8c8e192f
commit 47fd987af2
93 changed files with 228 additions and 1087 deletions

View File

@@ -1,12 +1,10 @@
package client
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"testing"
@@ -62,11 +60,7 @@ func TestCheckpointCreate(t *testing.T) {
if !createOptions.Exit {
return nil, errors.New("expected Exit to be true")
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
return mockJSONResponse(http.StatusOK, nil, "")(req)
}),
)
assert.NilError(t, err)

View File

@@ -1,9 +1,7 @@
package client
import (
"bytes"
"context"
"io"
"net/http"
"testing"
@@ -41,10 +39,7 @@ func TestCheckpointDelete(t *testing.T) {
if err := assertRequest(req, http.MethodDelete, expectedURL); err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
return mockResponse(http.StatusOK, nil, "")(req)
}),
)
assert.NilError(t, err)

View File

@@ -1,10 +1,7 @@
package client
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"testing"
@@ -32,18 +29,9 @@ func TestCheckpointList(t *testing.T) {
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
content, err := json.Marshal([]checkpoint.Summary{
{
Name: "checkpoint",
},
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(content)),
}, nil
return mockJSONResponse(http.StatusOK, nil, []checkpoint.Summary{
{Name: "checkpoint"},
})(req)
}),
)
assert.NilError(t, err)

View File

@@ -1,10 +1,7 @@
package client
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"testing"
@@ -31,16 +28,9 @@ func TestConfigCreate(t *testing.T) {
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
b, err := json.Marshal(swarm.ConfigCreateResponse{
return mockJSONResponse(http.StatusCreated, nil, swarm.ConfigCreateResponse{
ID: "test_config",
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusCreated,
Body: io.NopCloser(bytes.NewReader(b)),
}, nil
})(req)
}),
)
assert.NilError(t, err)

View File

@@ -1,11 +1,8 @@
package client
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"net/http"
"testing"
@@ -68,16 +65,9 @@ func TestConfigInspect(t *testing.T) {
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
content, err := json.Marshal(swarm.Config{
return mockJSONResponse(http.StatusOK, nil, swarm.Config{
ID: "config_id",
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(content)),
}, nil
})(req)
}),
)
assert.NilError(t, err)

View File

@@ -1,11 +1,8 @@
package client
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"testing"
@@ -62,21 +59,10 @@ func TestConfigList(t *testing.T) {
return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
}
}
content, err := json.Marshal([]swarm.Config{
{
ID: "config_id1",
},
{
ID: "config_id2",
},
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(content)),
}, nil
return mockJSONResponse(http.StatusOK, nil, []swarm.Config{
{ID: "config_id1"},
{ID: "config_id2"},
})(req)
}),
)
assert.NilError(t, err)

View File

@@ -1,9 +1,7 @@
package client
import (
"bytes"
"context"
"io"
"net/http"
"testing"
@@ -38,10 +36,7 @@ func TestConfigRemove(t *testing.T) {
if err := assertRequest(req, http.MethodDelete, expectedURL); err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte("body"))),
}, nil
return mockJSONResponse(http.StatusOK, nil, "")(req)
}),
)
assert.NilError(t, err)

View File

@@ -1,9 +1,7 @@
package client
import (
"bytes"
"context"
"io"
"net/http"
"testing"
@@ -38,10 +36,7 @@ func TestConfigUpdate(t *testing.T) {
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte("body"))),
}, nil
return mockJSONResponse(http.StatusOK, nil, "")(req)
}),
)
assert.NilError(t, err)

View File

@@ -1,11 +1,8 @@
package client
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"testing"
@@ -79,16 +76,9 @@ func TestContainerCommit(t *testing.T) {
if len(changes) != len(expectedChanges) {
return nil, fmt.Errorf("expected container changes size to be '%d', got %d", len(expectedChanges), len(changes))
}
b, err := json.Marshal(container.CommitResponse{
return mockJSONResponse(http.StatusOK, nil, container.CommitResponse{
ID: "new_container_id",
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(b)),
}, nil
})(req)
}),
)
assert.NilError(t, err)

View File

@@ -48,12 +48,7 @@ func TestContainerStatPathNotFoundError(t *testing.T) {
func TestContainerStatPathNoHeaderError(t *testing.T) {
client, err := NewClientWithOpts(
WithMockClient(func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
}),
WithMockClient(mockResponse(http.StatusOK, nil, "")),
)
assert.NilError(t, err)
@@ -84,13 +79,10 @@ func TestContainerStatPath(t *testing.T) {
return nil, err
}
base64PathStat := base64.StdEncoding.EncodeToString(content)
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(""))),
Header: http.Header{
"X-Docker-Container-Path-Stat": []string{base64PathStat},
},
}, nil
hdr := http.Header{
"X-Docker-Container-Path-Stat": []string{base64PathStat},
}
return mockResponse(http.StatusOK, hdr, "")(req)
}),
)
assert.NilError(t, err)
@@ -171,10 +163,7 @@ func TestCopyToContainer(t *testing.T) {
return nil, fmt.Errorf("expected content to be 'content', got %s", string(content))
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
return mockResponse(http.StatusOK, nil, "")(req)
}),
)
assert.NilError(t, err)
@@ -226,12 +215,10 @@ func TestCopyFromContainerEmptyResponse(t *testing.T) {
return nil, err
}
base64PathStat := base64.StdEncoding.EncodeToString(content)
return &http.Response{
StatusCode: http.StatusNoContent,
Header: http.Header{
"X-Docker-Container-Path-Stat": []string{base64PathStat},
},
}, nil
hdr := http.Header{
"X-Docker-Container-Path-Stat": []string{base64PathStat},
}
return mockResponse(http.StatusNoContent, hdr, "")(req)
}),
)
assert.NilError(t, err)
@@ -242,12 +229,7 @@ func TestCopyFromContainerEmptyResponse(t *testing.T) {
func TestCopyFromContainerNoHeaderError(t *testing.T) {
client, err := NewClientWithOpts(
WithMockClient(func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
}),
WithMockClient(mockResponse(http.StatusOK, nil, "")),
)
assert.NilError(t, err)
@@ -279,14 +261,10 @@ func TestCopyFromContainer(t *testing.T) {
return nil, err
}
base64PathStat := base64.StdEncoding.EncodeToString(headercontent)
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte("content"))),
Header: http.Header{
"X-Docker-Container-Path-Stat": []string{base64PathStat},
},
}, nil
hdr := http.Header{
"X-Docker-Container-Path-Stat": []string{base64PathStat},
}
return mockResponse(http.StatusOK, hdr, "content")(req)
}),
)
assert.NilError(t, err)

View File

@@ -1,12 +1,10 @@
package client
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"testing"
@@ -60,16 +58,9 @@ func TestContainerCreateWithName(t *testing.T) {
if name != "container_name" {
return nil, fmt.Errorf("container name not set in URL query properly. Expected `container_name`, got %s", name)
}
b, err := json.Marshal(container.CreateResponse{
return mockJSONResponse(http.StatusOK, nil, container.CreateResponse{
ID: "container_id",
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(b)),
}, nil
})(req)
}),
)
assert.NilError(t, err)
@@ -89,16 +80,9 @@ func TestContainerCreateAutoRemove(t *testing.T) {
if !config.HostConfig.AutoRemove {
return nil, errors.New("expected AutoRemove to be enabled")
}
b, err := json.Marshal(container.CreateResponse{
return mockJSONResponse(http.StatusOK, nil, container.CreateResponse{
ID: "container_id",
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(b)),
}, nil
})(req)
}),
)
assert.NilError(t, err)
@@ -151,16 +135,9 @@ func TestContainerCreateCapabilities(t *testing.T) {
assert.Check(t, is.DeepEqual(config.HostConfig.CapAdd, expectedCaps))
assert.Check(t, is.DeepEqual(config.HostConfig.CapDrop, expectedCaps))
b, err := json.Marshal(container.CreateResponse{
return mockJSONResponse(http.StatusOK, nil, container.CreateResponse{
ID: "container_id",
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(b)),
}, nil
})(req)
}),
)
assert.NilError(t, err)

View File

@@ -1,10 +1,7 @@
package client
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"testing"
@@ -55,14 +52,7 @@ func TestContainerDiff(t *testing.T) {
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
b, err := json.Marshal(expected)
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(b)),
}, nil
return mockJSONResponse(http.StatusOK, nil, expected)(req)
}),
)
assert.NilError(t, err)

View File

@@ -1,11 +1,9 @@
package client
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"testing"
@@ -63,16 +61,9 @@ func TestExecCreate(t *testing.T) {
if execConfig.User != "user" {
return nil, fmt.Errorf("expected an execConfig with User == 'user', got %v", execConfig)
}
b, err := json.Marshal(container.ExecCreateResponse{
return mockJSONResponse(http.StatusOK, nil, container.ExecCreateResponse{
ID: "exec_id",
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(b)),
}, nil
})(req)
}),
)
assert.NilError(t, err)
@@ -111,11 +102,7 @@ func TestExecStart(t *testing.T) {
if request.Tty || !request.Detach {
return nil, fmt.Errorf("expected ExecStartOptions{Detach:true,Tty:false}, got %v", request)
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
return mockResponse(http.StatusOK, nil, "")(req)
}),
)
assert.NilError(t, err)
@@ -144,17 +131,10 @@ func TestExecInspect(t *testing.T) {
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
b, err := json.Marshal(container.ExecInspectResponse{
return mockJSONResponse(http.StatusOK, nil, container.ExecInspectResponse{
ID: "exec_id",
ContainerID: "container_id",
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(b)),
}, nil
})(req)
}),
)
assert.NilError(t, err)

View File

@@ -1,7 +1,6 @@
package client
import (
"bytes"
"context"
"io"
"net/http"
@@ -37,10 +36,7 @@ func TestContainerExport(t *testing.T) {
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"))),
}, nil
return mockResponse(http.StatusOK, nil, "response")(req)
}),
)
assert.NilError(t, err)

View File

@@ -1,11 +1,8 @@
package client
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"net/http"
"testing"
@@ -75,18 +72,11 @@ func TestContainerInspect(t *testing.T) {
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
content, err := json.Marshal(container.InspectResponse{
return mockJSONResponse(http.StatusOK, nil, container.InspectResponse{
ID: "container_id",
Image: "image",
Name: "name",
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(content)),
}, nil
})(req)
}),
)
assert.NilError(t, err)

View File

@@ -1,10 +1,8 @@
package client
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"testing"
@@ -42,10 +40,7 @@ func TestContainerKill(t *testing.T) {
if signal != "SIGKILL" {
return nil, fmt.Errorf("signal not set in URL query properly. Expected 'SIGKILL', got %s", signal)
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
return mockResponse(http.StatusOK, nil, "")(req)
}),
)
assert.NilError(t, err)

View File

@@ -1,11 +1,8 @@
package client
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"testing"
@@ -60,23 +57,10 @@ func TestContainerList(t *testing.T) {
if fltrs != expectedFilters {
return nil, fmt.Errorf("expected filters incoherent '%v' with actual filters %v", expectedFilters, fltrs)
}
b, err := json.Marshal([]container.Summary{
{
ID: "container_id1",
},
{
ID: "container_id2",
},
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(b)),
}, nil
return mockJSONResponse(http.StatusOK, nil, []container.Summary{
{ID: "container_id1"},
{ID: "container_id2"},
})(req)
}),
)
assert.NilError(t, err)

View File

@@ -1,7 +1,6 @@
package client
import (
"bytes"
"context"
"errors"
"fmt"
@@ -140,10 +139,7 @@ func TestContainerLogs(t *testing.T) {
return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
}
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte("response"))),
}, nil
return mockResponse(http.StatusOK, nil, "response")(req)
}),
)
assert.NilError(t, err)

View File

@@ -1,9 +1,7 @@
package client
import (
"bytes"
"context"
"io"
"net/http"
"testing"
@@ -29,10 +27,7 @@ func TestContainerPause(t *testing.T) {
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
return mockResponse(http.StatusOK, nil, "")(req)
}),
)
assert.NilError(t, err)

View File

@@ -1,10 +1,7 @@
package client
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"testing"
@@ -85,17 +82,10 @@ func TestContainersPrune(t *testing.T) {
actual := query.Get(key)
assert.Check(t, is.Equal(expected, actual))
}
content, err := json.Marshal(container.PruneReport{
return mockJSONResponse(http.StatusOK, nil, container.PruneReport{
ContainersDeleted: []string{"container_id1", "container_id2"},
SpaceReclaimed: 9999,
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(content)),
}, nil
})(req)
}))
assert.NilError(t, err)

View File

@@ -1,10 +1,8 @@
package client
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"testing"
@@ -55,10 +53,7 @@ func TestContainerRemove(t *testing.T) {
if link != "" {
return nil, fmt.Errorf("link should have not be present in query, go %s", link)
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
return mockResponse(http.StatusOK, nil, "")(req)
}))
assert.NilError(t, err)

View File

@@ -1,10 +1,8 @@
package client
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"testing"
@@ -38,10 +36,7 @@ func TestContainerRename(t *testing.T) {
if name != "newName" {
return nil, fmt.Errorf("name not set in URL query properly. Expected 'newName', got %s", name)
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
return mockResponse(http.StatusOK, nil, "")(req)
}))
assert.NilError(t, err)

View File

@@ -1,9 +1,7 @@
package client
import (
"bytes"
"context"
"io"
"math"
"net/http"
"testing"
@@ -127,9 +125,6 @@ func resizeTransport(t *testing.T, expectedURL, expectedHeight, expectedWidth st
query := req.URL.Query()
assert.Check(t, is.Equal(query.Get("h"), expectedHeight))
assert.Check(t, is.Equal(query.Get("w"), expectedWidth))
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
return mockResponse(http.StatusOK, nil, "")(req)
}
}

View File

@@ -1,10 +1,8 @@
package client
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"testing"
@@ -54,10 +52,7 @@ func TestContainerRestart(t *testing.T) {
if t != "100" {
return nil, fmt.Errorf("t (timeout) not set in URL query properly. Expected '100', got %s", t)
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
return mockResponse(http.StatusOK, nil, "")(req)
}))
assert.NilError(t, err)
timeout := 100

View File

@@ -1,11 +1,9 @@
package client
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"testing"
@@ -47,11 +45,7 @@ func TestContainerStart(t *testing.T) {
if checkpoint != "checkpoint_id" {
return nil, fmt.Errorf("checkpoint not set in URL query properly. Expected 'checkpoint_id', got %s", checkpoint)
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
return mockResponse(http.StatusOK, nil, "")(req)
}))
assert.NilError(t, err)

View File

@@ -1,7 +1,6 @@
package client
import (
"bytes"
"context"
"fmt"
"io"
@@ -53,11 +52,7 @@ func TestContainerStats(t *testing.T) {
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{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte("response"))),
}, nil
return mockResponse(http.StatusOK, nil, "response")(req)
}))
assert.NilError(t, err)
resp, err := client.ContainerStats(context.Background(), "container_id", tc.stream)

View File

@@ -1,10 +1,8 @@
package client
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"testing"
@@ -54,10 +52,7 @@ func TestContainerStop(t *testing.T) {
if t != "100" {
return nil, fmt.Errorf("t (timeout) not set in URL query properly. Expected '100', got %s", t)
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
return mockResponse(http.StatusOK, nil, "")(req)
}))
assert.NilError(t, err)
timeout := 100

View File

@@ -1,11 +1,8 @@
package client
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"testing"
@@ -47,22 +44,13 @@ func TestContainerTop(t *testing.T) {
if args != "arg1 arg2" {
return nil, fmt.Errorf("args not set in URL query properly. Expected 'arg1 arg2', got %v", args)
}
b, err := json.Marshal(container.TopResponse{
return mockJSONResponse(http.StatusOK, nil, container.TopResponse{
Processes: [][]string{
{"p1", "p2"},
{"p3"},
},
Titles: []string{"title1", "title2"},
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(b)),
}, nil
})(req)
}))
assert.NilError(t, err)

View File

@@ -1,9 +1,7 @@
package client
import (
"bytes"
"context"
"io"
"net/http"
"testing"
@@ -33,10 +31,7 @@ func TestContainerUnpause(t *testing.T) {
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
return mockResponse(http.StatusOK, nil, "")(req)
}))
assert.NilError(t, err)
err = client.ContainerUnpause(context.Background(), "container_id")

View File

@@ -1,10 +1,7 @@
package client
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"testing"
@@ -36,16 +33,7 @@ func TestContainerUpdate(t *testing.T) {
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
b, err := json.Marshal(container.UpdateResponse{})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(b)),
}, nil
return mockJSONResponse(http.StatusOK, nil, container.UpdateResponse{})(req)
}))
assert.NilError(t, err)

View File

@@ -1,9 +1,7 @@
package client
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"log"
@@ -55,16 +53,9 @@ func TestContainerWait(t *testing.T) {
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
b, err := json.Marshal(container.WaitResponse{
return mockJSONResponse(http.StatusOK, nil, container.WaitResponse{
StatusCode: 15,
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(b)),
}, nil
})(req)
}))
assert.NilError(t, err)
@@ -87,10 +78,7 @@ func TestContainerWaitProxyInterrupt(t *testing.T) {
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader(expErr)),
}, nil
return mockResponse(http.StatusOK, nil, expErr)(req)
}))
assert.NilError(t, err)
@@ -110,10 +98,7 @@ func TestContainerWaitProxyInterruptLong(t *testing.T) {
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
return mockResponse(http.StatusOK, nil, msg)(req)
}))
assert.NilError(t, err)

View File

@@ -1,7 +1,6 @@
package client
import (
"bytes"
"context"
"fmt"
"io"
@@ -186,10 +185,7 @@ func TestImageBuild(t *testing.T) {
}
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte("body"))),
}, nil
return mockResponse(http.StatusOK, nil, "body")(req)
}))
assert.NilError(t, err)
buildResponse, err := client.ImageBuild(context.Background(), nil, buildCase.buildOptions)

View File

@@ -1,7 +1,6 @@
package client
import (
"bytes"
"context"
"fmt"
"io"
@@ -50,10 +49,7 @@ func TestImageCreate(t *testing.T) {
return nil, fmt.Errorf("tag not set in URL query properly. Expected '%s', got %s", expectedTag, tag)
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte("body"))),
}, nil
return mockResponse(http.StatusOK, nil, "body")(req)
}))
assert.NilError(t, err)

View File

@@ -2,9 +2,7 @@ package client
import (
"context"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -30,10 +28,7 @@ func TestImageHistory(t *testing.T) {
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)),
}, nil
return mockResponse(http.StatusOK, nil, historyResponse)(req)
}))
assert.NilError(t, err)
expected := ImageHistoryResult{

View File

@@ -1,7 +1,6 @@
package client
import (
"bytes"
"context"
"io"
"net/http"
@@ -71,10 +70,7 @@ func TestImageImport(t *testing.T) {
assert.Check(t, assertRequest(req, http.MethodPost, expectedURL))
query := req.URL.Query()
assert.Check(t, is.DeepEqual(query, tc.expectedQueryParams))
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(expectedOutput))),
}, nil
return mockResponse(http.StatusOK, nil, expectedOutput)(req)
}))
assert.NilError(t, err)
result, err := client.ImageImport(context.Background(), ImageImportSource{

View File

@@ -1,12 +1,9 @@
package client
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"testing"
@@ -49,17 +46,10 @@ func TestImageInspect(t *testing.T) {
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
content, err := json.Marshal(image.InspectResponse{
return mockJSONResponse(http.StatusOK, nil, image.InspectResponse{
ID: "image_id",
RepoTags: expectedTags,
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(content)),
}, nil
})(req)
}))
assert.NilError(t, err)
@@ -90,18 +80,11 @@ func TestImageInspectWithPlatform(t *testing.T) {
return nil, fmt.Errorf("Expected platform '%s', got '%s'", expectedPlatform, platform)
}
content, err := json.Marshal(image.InspectResponse{
return mockJSONResponse(http.StatusOK, nil, image.InspectResponse{
ID: "image_id",
Architecture: "arm64",
Os: "linux",
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(content)),
}, nil
})(req)
}))
assert.NilError(t, err)

View File

@@ -1,14 +1,10 @@
package client
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
@@ -88,21 +84,10 @@ func TestImageList(t *testing.T) {
return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
}
}
content, err := json.Marshal([]image.Summary{
{
ID: "image_id2",
},
{
ID: "image_id2",
},
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(content)),
}, nil
return mockJSONResponse(http.StatusOK, nil, []image.Summary{
{ID: "image_id2"},
{ID: "image_id2"},
})(req)
}))
assert.NilError(t, err)
@@ -131,10 +116,7 @@ func TestImageListWithSharedSize(t *testing.T) {
var query url.Values
client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
query = req.URL.Query()
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader("[]")),
}, nil
return mockResponse(http.StatusOK, nil, "[]")(req)
}), WithVersion(tc.version))
assert.NilError(t, err)
_, err = client.ImageList(context.Background(), tc.options)

View File

@@ -85,11 +85,9 @@ func TestImageLoad(t *testing.T) {
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{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(expectedOutput))),
Header: http.Header{"Content-Type": []string{tc.responseContentType}},
}, nil
hdr := http.Header{"Content-Type": []string{tc.responseContentType}}
return mockResponse(http.StatusOK, hdr, expectedOutput)(req)
}))
assert.NilError(t, err)

View File

@@ -1,10 +1,7 @@
package client
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"testing"
@@ -75,24 +72,14 @@ func TestImagesPrune(t *testing.T) {
actual := query.Get(key)
assert.Check(t, is.Equal(expected, actual))
}
content, err := json.Marshal(image.PruneReport{
return mockJSONResponse(http.StatusOK, nil, image.PruneReport{
ImagesDeleted: []image.DeleteResponse{
{
Deleted: "image_id1",
},
{
Deleted: "image_id2",
},
{Deleted: "image_id1"},
{Deleted: "image_id2"},
},
SpaceReclaimed: 9999,
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(content)),
}, nil
})(req)
}))
assert.NilError(t, err)

View File

@@ -1,7 +1,6 @@
package client
import (
"bytes"
"context"
"errors"
"fmt"
@@ -72,10 +71,7 @@ func TestImagePullWithPrivilegedFuncNoError(t *testing.T) {
}
auth := req.Header.Get(registry.AuthHeader)
if auth == invalidAuth {
return &http.Response{
StatusCode: http.StatusUnauthorized,
Body: io.NopCloser(bytes.NewReader([]byte("Invalid credentials"))),
}, nil
return mockResponse(http.StatusUnauthorized, nil, "Invalid credentials")(req)
}
if auth != validAuth {
return nil, fmt.Errorf("invalid auth header: expected %s, got %s", "IAmValid", auth)
@@ -89,10 +85,7 @@ func TestImagePullWithPrivilegedFuncNoError(t *testing.T) {
if tag != "latest" {
return nil, fmt.Errorf("tag not set in URL query properly. Expected '%s', got %s", "latest", tag)
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte("hello world"))),
}, nil
return mockResponse(http.StatusOK, nil, "hello world")(req)
}))
assert.NilError(t, err)
resp, err := client.ImagePull(context.Background(), "myimage", ImagePullOptions{
@@ -181,10 +174,7 @@ func TestImagePullWithoutErrors(t *testing.T) {
if tag != pullCase.expectedTag {
return nil, fmt.Errorf("tag not set in URL query properly. Expected '%s', got %s", pullCase.expectedTag, tag)
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(expectedOutput))),
}, nil
return mockResponse(http.StatusOK, nil, expectedOutput)(req)
}))
assert.NilError(t, err)
resp, err := client.ImagePull(context.Background(), pullCase.reference, ImagePullOptions{

View File

@@ -1,7 +1,6 @@
package client
import (
"bytes"
"context"
"errors"
"fmt"
@@ -76,10 +75,7 @@ func TestImagePushWithPrivilegedFuncNoError(t *testing.T) {
}
auth := req.Header.Get(registry.AuthHeader)
if auth == invalidAuth {
return &http.Response{
StatusCode: http.StatusUnauthorized,
Body: io.NopCloser(bytes.NewReader([]byte("Invalid credentials"))),
}, nil
return mockResponse(http.StatusUnauthorized, nil, "Invalid credentials")(req)
}
if auth != validAuth {
return nil, fmt.Errorf("invalid auth header: expected %s, got %s", "IAmValid", auth)
@@ -89,10 +85,7 @@ func TestImagePushWithPrivilegedFuncNoError(t *testing.T) {
if tag != "tag" {
return nil, fmt.Errorf("tag not set in URL query properly. Expected '%s', got %s", "tag", tag)
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte("hello world"))),
}, nil
return mockResponse(http.StatusOK, nil, "hello world")(req)
}))
assert.NilError(t, err)
resp, err := client.ImagePush(context.Background(), "myname/myimage:tag", ImagePushOptions{
@@ -178,10 +171,7 @@ func TestImagePushWithoutErrors(t *testing.T) {
if tag != tc.expectedTag {
return nil, fmt.Errorf("tag not set in URL query properly. Expected '%s', got %s", tc.expectedTag, tag)
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(expectedOutput))),
}, nil
return mockResponse(http.StatusOK, nil, expectedOutput)(req)
}))
assert.NilError(t, err)
resp, err := client.ImagePush(context.Background(), tc.reference, ImagePushOptions{

View File

@@ -1,11 +1,8 @@
package client
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"testing"
@@ -79,22 +76,10 @@ func TestImageRemove(t *testing.T) {
return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
}
}
b, err := json.Marshal([]image.DeleteResponse{
{
Untagged: "image_id1",
},
{
Deleted: "image_id",
},
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(b)),
}, nil
return mockJSONResponse(http.StatusOK, nil, []image.DeleteResponse{
{Untagged: "image_id1"},
{Deleted: "image_id"},
})(req)
}))
assert.NilError(t, err)

View File

@@ -1,7 +1,6 @@
package client
import (
"bytes"
"context"
"io"
"net/http"
@@ -67,10 +66,7 @@ func TestImageSave(t *testing.T) {
client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
assert.Check(t, assertRequest(req, http.MethodGet, expectedURL))
assert.Check(t, is.DeepEqual(req.URL.Query(), tc.expectedQueryParams))
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(expectedOutput))),
}, nil
return mockResponse(http.StatusOK, nil, expectedOutput)(req)
}))
assert.NilError(t, err)
resp, err := client.ImageSave(context.Background(), []string{"image_id1", "image_id2"}, tc.options...)

View File

@@ -1,12 +1,9 @@
package client
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"testing"
@@ -62,10 +59,7 @@ func TestImageSearchWithPrivilegedFuncNoError(t *testing.T) {
}
auth := req.Header.Get(registry.AuthHeader)
if auth == "NotValid" {
return &http.Response{
StatusCode: http.StatusUnauthorized,
Body: io.NopCloser(bytes.NewReader([]byte("Invalid credentials"))),
}, nil
return mockResponse(http.StatusUnauthorized, nil, "Invalid credentials")(req)
}
if auth != "IAmValid" {
return nil, fmt.Errorf("invalid auth header: expected 'IAmValid', got %s", auth)
@@ -75,18 +69,9 @@ func TestImageSearchWithPrivilegedFuncNoError(t *testing.T) {
if term != "some-image" {
return nil, fmt.Errorf("term not set in URL query properly. Expected 'some-image', got %s", term)
}
content, err := json.Marshal([]registry.SearchResult{
{
Name: "anything",
},
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(content)),
}, nil
return mockJSONResponse(http.StatusOK, nil, []registry.SearchResult{
{Name: "anything"},
})(req)
}))
assert.NilError(t, err)
privilegeFunc := func(_ context.Context) (string, error) {
@@ -117,18 +102,9 @@ func TestImageSearchWithoutErrors(t *testing.T) {
if fltrs != expectedFilters {
return nil, fmt.Errorf("filters not set in URL query properly. Expected '%s', got %s", expectedFilters, fltrs)
}
content, err := json.Marshal([]registry.SearchResult{
{
Name: "anything",
},
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(content)),
}, nil
return mockJSONResponse(http.StatusOK, nil, []registry.SearchResult{
{Name: "anything"},
})(req)
}))
assert.NilError(t, err)
results, err := client.ImageSearch(context.Background(), "some-image", ImageSearchOptions{

View File

@@ -1,10 +1,8 @@
package client
import (
"bytes"
"context"
"fmt"
"io"
"math/rand"
"net/http"
"testing"
@@ -88,7 +86,7 @@ func generateRandomAlphaOnlyString(n int) string {
}
func TestImageTagHexSource(t *testing.T) {
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusOK, "OK")))
client, err := NewClientWithOpts(WithMockClient(mockResponse(http.StatusOK, nil, "OK")))
assert.NilError(t, err)
_, err = client.ImageTag(context.Background(), ImageTagOptions{Source: "0d409d33b27e47423b049f7f863faa08655a8c901749c2b25b93ca67d01a470d", Target: "repo:tag"})
@@ -163,10 +161,7 @@ func TestImageTag(t *testing.T) {
return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
}
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
return mockResponse(http.StatusOK, nil, "")(req)
}))
assert.NilError(t, err)
_, err = client.ImageTag(context.Background(), ImageTagOptions{Source: "image_id", Target: tagCase.reference})

View File

@@ -1,11 +1,9 @@
package client
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"testing"
@@ -53,10 +51,7 @@ func TestNetworkConnectEmptyNilEndpointSettings(t *testing.T) {
return nil, fmt.Errorf("expected connect.EndpointConfig to be nil, got %v", connect.EndpointConfig)
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
return mockResponse(http.StatusOK, nil, "")(req)
}))
assert.NilError(t, err)
@@ -89,10 +84,7 @@ func TestNetworkConnect(t *testing.T) {
return nil, fmt.Errorf("expected 'NetworkID', got %s", connect.EndpointConfig.NetworkID)
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
return mockResponse(http.StatusOK, nil, "")(req)
}))
assert.NilError(t, err)

View File

@@ -1,10 +1,7 @@
package client
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"testing"
@@ -41,18 +38,10 @@ func TestNetworkCreate(t *testing.T) {
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
content, err := json.Marshal(network.CreateResponse{
return mockJSONResponse(http.StatusOK, nil, network.CreateResponse{
ID: "network_id",
Warning: "warning",
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(content)),
}, nil
})(req)
}))
assert.NilError(t, err)

View File

@@ -1,11 +1,9 @@
package client
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"testing"
@@ -52,10 +50,7 @@ func TestNetworkDisconnect(t *testing.T) {
return nil, fmt.Errorf("expected Force to be true, got %v", disconnect.Force)
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
return mockResponse(http.StatusOK, nil, "")(req)
}))
assert.NilError(t, err)

View File

@@ -1,10 +1,7 @@
package client
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"strings"
"testing"
@@ -36,31 +33,20 @@ func TestNetworkInspect(t *testing.T) {
if strings.Contains(req.URL.RawQuery, "scope=global") {
return errorMock(http.StatusNotFound, "Error: No such network: network_id")(req)
}
var (
content []byte
err error
)
var resp network.Inspect
if strings.Contains(req.URL.RawQuery, "verbose=true") {
s := map[string]network.ServiceInfo{
"web": {},
}
content, err = json.Marshal(network.Inspect{
Network: network.Network{Name: "mynetwork"},
Services: s,
})
} else {
content, err = json.Marshal(network.Inspect{
resp = network.Inspect{
Network: network.Network{Name: "mynetwork"},
})
Services: map[string]network.ServiceInfo{
"web": {},
},
}
} else {
resp = network.Inspect{
Network: network.Network{Name: "mynetwork"},
}
}
if err != nil {
return nil, err
}
return &http.Response{
Header: http.Header{"Content-Type": []string{"application/json"}},
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(content)),
}, nil
return mockJSONResponse(http.StatusOK, nil, resp)(req)
}))
assert.NilError(t, err)

View File

@@ -1,11 +1,8 @@
package client
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"testing"
@@ -66,21 +63,14 @@ func TestNetworkList(t *testing.T) {
if actualFilters != listCase.expectedFilters {
return nil, fmt.Errorf("filters not set in URL query properly. Expected '%s', got %s", listCase.expectedFilters, actualFilters)
}
content, err := json.Marshal([]network.Summary{
return mockJSONResponse(http.StatusOK, nil, []network.Summary{
{
Network: network.Network{
Name: "network",
Driver: "bridge",
},
},
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(content)),
}, nil
})(req)
}))
assert.NilError(t, err)

View File

@@ -1,10 +1,7 @@
package client
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"testing"
@@ -78,16 +75,9 @@ func TestNetworksPrune(t *testing.T) {
actual := query.Get(key)
assert.Check(t, is.Equal(expected, actual))
}
content, err := json.Marshal(network.PruneReport{
return mockJSONResponse(http.StatusOK, nil, network.PruneReport{
NetworksDeleted: []string{"network_id1", "network_id2"},
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(content)),
}, nil
})(req)
}),
)
assert.NilError(t, err)

View File

@@ -1,9 +1,7 @@
package client
import (
"bytes"
"context"
"io"
"net/http"
"testing"
@@ -35,10 +33,7 @@ func TestNetworkRemove(t *testing.T) {
if err := assertRequest(req, http.MethodDelete, expectedURL); err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte("body"))),
}, nil
return mockResponse(http.StatusOK, nil, "body")(req)
}))
assert.NilError(t, err)

View File

@@ -1,11 +1,8 @@
package client
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"net/http"
"testing"
@@ -51,16 +48,9 @@ func TestNodeInspect(t *testing.T) {
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
content, err := json.Marshal(swarm.Node{
return mockJSONResponse(http.StatusOK, nil, swarm.Node{
ID: "node_id",
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(content)),
}, nil
})(req)
}))
assert.NilError(t, err)

View File

@@ -1,11 +1,8 @@
package client
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"testing"
@@ -58,21 +55,10 @@ func TestNodeList(t *testing.T) {
return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
}
}
content, err := json.Marshal([]swarm.Node{
{
ID: "node_id1",
},
{
ID: "node_id2",
},
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(content)),
}, nil
return mockJSONResponse(http.StatusOK, nil, []swarm.Node{
{ID: "node_id1"},
{ID: "node_id2"},
})(req)
}))
assert.NilError(t, err)

View File

@@ -1,10 +1,8 @@
package client
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"testing"
@@ -55,10 +53,7 @@ func TestNodeRemove(t *testing.T) {
return nil, fmt.Errorf("force not set in URL query properly. expected '%s', got %s", removeCase.expectedForce, force)
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte("body"))),
}, nil
return mockResponse(http.StatusOK, nil, "body")(req)
}))
assert.NilError(t, err)

View File

@@ -1,9 +1,7 @@
package client
import (
"bytes"
"context"
"io"
"net/http"
"testing"
@@ -45,10 +43,7 @@ func TestNodeUpdate(t *testing.T) {
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte("body"))),
}, nil
return mockResponse(http.StatusOK, nil, "body")(req)
}))
assert.NilError(t, err)

View File

@@ -3,7 +3,6 @@ package client
import (
"errors"
"fmt"
"io"
"net/http"
"slices"
"strings"
@@ -20,15 +19,14 @@ import (
func TestPingFail(t *testing.T) {
var withHeader bool
client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
resp := &http.Response{StatusCode: http.StatusInternalServerError}
var hdr http.Header
if withHeader {
resp.Header = http.Header{}
resp.Header.Set("Api-Version", "awesome")
resp.Header.Set("Docker-Experimental", "true")
resp.Header.Set("Swarm", "inactive")
hdr = http.Header{}
hdr.Set("Api-Version", "awesome")
hdr.Set("Docker-Experimental", "true")
hdr.Set("Swarm", "inactive")
}
resp.Body = io.NopCloser(strings.NewReader("some error with the server"))
return resp, nil
return mockResponse(http.StatusInternalServerError, hdr, "some error with the server")(req)
}))
assert.NilError(t, err)
@@ -67,13 +65,11 @@ func TestPingWithError(t *testing.T) {
// details on success.
func TestPingSuccess(t *testing.T) {
client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
resp := &http.Response{StatusCode: http.StatusOK}
resp.Header = http.Header{}
resp.Header.Set("Api-Version", "awesome")
resp.Header.Set("Docker-Experimental", "true")
resp.Header.Set("Swarm", "active/manager")
resp.Body = io.NopCloser(strings.NewReader("OK"))
return resp, nil
hdr := http.Header{}
hdr.Set("Api-Version", "awesome")
hdr.Set("Docker-Experimental", "true")
hdr.Set("Swarm", "active/manager")
return mockResponse(http.StatusOK, hdr, "OK")(req)
}))
assert.NilError(t, err)
ping, err := client.Ping(t.Context(), PingOptions{})

View File

@@ -1,9 +1,7 @@
package client
import (
"bytes"
"context"
"io"
"net/http"
"testing"
@@ -35,10 +33,7 @@ func TestPluginDisable(t *testing.T) {
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
return mockResponse(http.StatusOK, nil, "")(req)
}))
assert.NilError(t, err)

View File

@@ -1,9 +1,7 @@
package client
import (
"bytes"
"context"
"io"
"net/http"
"testing"
@@ -35,10 +33,7 @@ func TestPluginEnable(t *testing.T) {
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
return mockResponse(http.StatusOK, nil, "")(req)
}))
assert.NilError(t, err)

View File

@@ -1,10 +1,7 @@
package client
import (
"bytes"
"encoding/json"
"errors"
"io"
"net/http"
"testing"
@@ -42,16 +39,9 @@ func TestPluginInspect(t *testing.T) {
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
content, err := json.Marshal(plugin.Plugin{
return mockJSONResponse(http.StatusOK, nil, plugin.Plugin{
ID: "plugin_id",
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(content)),
}, nil
})(req)
}))
assert.NilError(t, err)

View File

@@ -1,11 +1,8 @@
package client
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"testing"
@@ -67,21 +64,10 @@ func TestPluginList(t *testing.T) {
return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
}
}
content, err := json.Marshal([]*plugin.Plugin{
{
ID: "plugin_id1",
},
{
ID: "plugin_id2",
},
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(content)),
}, nil
return mockJSONResponse(http.StatusOK, nil, []*plugin.Plugin{
{ID: "plugin_id1"},
{ID: "plugin_id2"},
})(req)
}))
assert.NilError(t, err)

View File

@@ -1,10 +1,8 @@
package client
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"testing"
@@ -41,10 +39,7 @@ func TestPluginPush(t *testing.T) {
if auth != "authtoken" {
return nil, fmt.Errorf("invalid auth header: expected 'authtoken', got %s", auth)
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
return mockResponse(http.StatusOK, nil, "")(req)
}))
assert.NilError(t, err)

View File

@@ -1,9 +1,7 @@
package client
import (
"bytes"
"context"
"io"
"net/http"
"testing"
@@ -35,10 +33,7 @@ func TestPluginRemove(t *testing.T) {
if err := assertRequest(req, http.MethodDelete, expectedURL); err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
return mockResponse(http.StatusOK, nil, "")(req)
}))
assert.NilError(t, err)

View File

@@ -1,9 +1,7 @@
package client
import (
"bytes"
"context"
"io"
"net/http"
"testing"
@@ -35,10 +33,7 @@ func TestPluginSet(t *testing.T) {
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
return mockResponse(http.StatusOK, nil, "")(req)
}))
assert.NilError(t, err)

View File

@@ -1,7 +1,6 @@
package client
import (
"bytes"
"context"
"encoding/json"
"fmt"
@@ -61,10 +60,7 @@ func TestSetHostHeader(t *testing.T) {
if req.URL.Host != tc.expectedURLHost {
return nil, fmt.Errorf("expected URL host %q, got %q", tc.expectedURLHost, req.URL.Host)
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
return mockResponse(http.StatusOK, nil, "")(req)
}), WithHost(tc.host))
assert.NilError(t, err)
@@ -200,11 +196,7 @@ func TestResponseErrors(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) {
return &http.Response{
StatusCode: http.StatusBadRequest,
Header: http.Header{"Content-Type": []string{tc.contentType}},
Body: io.NopCloser(bytes.NewReader([]byte(tc.response))),
}, nil
return mockResponse(http.StatusBadRequest, http.Header{"Content-Type": []string{tc.contentType}}, tc.response)(req)
}))
if tc.apiVersion != "" {
client, err = NewClientWithOpts(WithHTTPClient(client.client), WithVersion(tc.apiVersion))

View File

@@ -1,10 +1,7 @@
package client
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"testing"
@@ -27,16 +24,9 @@ func TestSecretCreate(t *testing.T) {
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
b, err := json.Marshal(swarm.SecretCreateResponse{
return mockJSONResponse(http.StatusCreated, nil, swarm.SecretCreateResponse{
ID: "test_secret",
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusCreated,
Body: io.NopCloser(bytes.NewReader(b)),
}, nil
})(req)
}))
assert.NilError(t, err)

View File

@@ -1,11 +1,8 @@
package client
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"net/http"
"testing"
@@ -51,16 +48,9 @@ func TestSecretInspect(t *testing.T) {
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
content, err := json.Marshal(swarm.Secret{
return mockJSONResponse(http.StatusOK, nil, swarm.Secret{
ID: "secret_id",
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(content)),
}, nil
})(req)
}))
assert.NilError(t, err)

View File

@@ -1,11 +1,8 @@
package client
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"testing"
@@ -57,21 +54,11 @@ func TestSecretList(t *testing.T) {
return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
}
}
content, err := json.Marshal([]swarm.Secret{
{
ID: "secret_id1",
},
{
ID: "secret_id2",
},
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(content)),
}, nil
return mockJSONResponse(http.StatusOK, nil, []swarm.Secret{
{ID: "secret_id1"},
{ID: "secret_id2"},
})(req)
}))
assert.NilError(t, err)

View File

@@ -1,9 +1,7 @@
package client
import (
"bytes"
"context"
"io"
"net/http"
"testing"
@@ -35,10 +33,7 @@ func TestSecretRemove(t *testing.T) {
if err := assertRequest(req, http.MethodDelete, expectedURL); err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte("body"))),
}, nil
return mockResponse(http.StatusOK, nil, "body")(req)
}))
assert.NilError(t, err)

View File

@@ -1,9 +1,7 @@
package client
import (
"bytes"
"context"
"io"
"net/http"
"testing"
@@ -35,10 +33,7 @@ func TestSecretUpdate(t *testing.T) {
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte("body"))),
}, nil
return mockResponse(http.StatusOK, nil, "body")(req)
}))
assert.NilError(t, err)

View File

@@ -1,11 +1,9 @@
package client
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"testing"
@@ -44,16 +42,9 @@ func TestServiceCreate(t *testing.T) {
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
b, err := json.Marshal(swarm.ServiceCreateResponse{
return mockJSONResponse(http.StatusOK, nil, swarm.ServiceCreateResponse{
ID: "service_id",
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(b)),
}, nil
})(req)
}))
assert.NilError(t, err)
@@ -77,18 +68,11 @@ func TestServiceCreateCompatiblePlatforms(t *testing.T) {
assert.Check(t, is.Len(serviceSpec.TaskTemplate.Placement.Platforms, 1))
p := serviceSpec.TaskTemplate.Placement.Platforms[0]
b, err := json.Marshal(swarm.ServiceCreateResponse{
return mockJSONResponse(http.StatusOK, nil, swarm.ServiceCreateResponse{
ID: "service_" + p.OS + "_" + p.Architecture,
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(b)),
}, nil
})(req)
} else if strings.HasPrefix(req.URL.Path, defaultAPIPath+"/distribution/") {
b, err := json.Marshal(registrytypes.DistributionInspect{
return mockJSONResponse(http.StatusOK, nil, registrytypes.DistributionInspect{
Descriptor: ocispec.Descriptor{
Digest: "sha256:c0537ff6a5218ef531ece93d4984efc99bbf3f7497c0a7726c88e2bb7584dc96",
},
@@ -98,14 +82,7 @@ func TestServiceCreateCompatiblePlatforms(t *testing.T) {
OS: "linux",
},
},
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(b)),
}, nil
})(req)
} else {
return nil, fmt.Errorf("unexpected URL '%s'", req.URL.Path)
}
@@ -156,33 +133,19 @@ func TestServiceCreateDigestPinning(t *testing.T) {
}
serviceCreateImage = service.TaskTemplate.ContainerSpec.Image
b, err := json.Marshal(swarm.ServiceCreateResponse{
return mockJSONResponse(http.StatusOK, nil, swarm.ServiceCreateResponse{
ID: "service_id",
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(b)),
}, nil
})(req)
} 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, defaultAPIPath+"/distribution/") {
// resolvable images
b, err := json.Marshal(registrytypes.DistributionInspect{
return mockJSONResponse(http.StatusOK, nil, registrytypes.DistributionInspect{
Descriptor: ocispec.Descriptor{
Digest: digest.Digest(dgst),
},
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(b)),
}, nil
})(req)
}
return nil, fmt.Errorf("unexpected URL '%s'", req.URL.Path)
}))

View File

@@ -1,11 +1,8 @@
package client
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"net/http"
"testing"
@@ -51,16 +48,9 @@ func TestServiceInspect(t *testing.T) {
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
content, err := json.Marshal(swarm.Service{
return mockJSONResponse(http.StatusOK, nil, swarm.Service{
ID: "service_id",
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(content)),
}, nil
})(req)
}))
assert.NilError(t, err)

View File

@@ -1,11 +1,8 @@
package client
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"testing"
@@ -57,21 +54,11 @@ func TestServiceList(t *testing.T) {
return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
}
}
content, err := json.Marshal([]swarm.Service{
{
ID: "service_id1",
},
{
ID: "service_id2",
},
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(content)),
}, nil
return mockJSONResponse(http.StatusOK, nil, []swarm.Service{
{ID: "service_id1"},
{ID: "service_id2"},
})(req)
}))
assert.NilError(t, err)

View File

@@ -1,7 +1,6 @@
package client
import (
"bytes"
"context"
"errors"
"fmt"
@@ -105,10 +104,7 @@ func TestServiceLogs(t *testing.T) {
return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
}
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte("response"))),
}, nil
return mockResponse(http.StatusOK, nil, "response")(req)
}))
assert.NilError(t, err)
body, err := client.ServiceLogs(context.Background(), "service_id", logCase.options)

View File

@@ -1,9 +1,7 @@
package client
import (
"bytes"
"context"
"io"
"net/http"
"testing"
@@ -44,10 +42,7 @@ func TestServiceRemove(t *testing.T) {
if err := assertRequest(req, http.MethodDelete, expectedURL); err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte("body"))),
}, nil
return mockResponse(http.StatusOK, nil, "body")(req)
}))
assert.NilError(t, err)

View File

@@ -1,9 +1,7 @@
package client
import (
"bytes"
"fmt"
"io"
"net/http"
"testing"
@@ -74,10 +72,7 @@ func TestServiceUpdate(t *testing.T) {
if version != updateCase.expectedVersion {
return nil, fmt.Errorf("version not set in URL query properly, expected '%s', got %s", updateCase.expectedVersion, version)
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte("{}"))),
}, nil
return mockResponse(http.StatusOK, nil, "{}")(req)
}))
assert.NilError(t, err)

View File

@@ -1,10 +1,7 @@
package client
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"testing"
@@ -32,20 +29,9 @@ func TestSwarmGetUnlockKey(t *testing.T) {
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
key := swarm.UnlockKeyResponse{
return mockJSONResponse(http.StatusOK, nil, swarm.UnlockKeyResponse{
UnlockKey: unlockKey,
}
b, err := json.Marshal(key)
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(b)),
}, nil
})(req)
}))
assert.NilError(t, err)

View File

@@ -1,9 +1,7 @@
package client
import (
"bytes"
"context"
"io"
"net/http"
"testing"
@@ -27,10 +25,7 @@ func TestSwarmInit(t *testing.T) {
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(`"body"`))),
}, nil
return mockJSONResponse(http.StatusOK, nil, "node-id")(req)
}))
assert.NilError(t, err)
@@ -38,5 +33,5 @@ func TestSwarmInit(t *testing.T) {
ListenAddr: "0.0.0.0:2377",
})
assert.NilError(t, err)
assert.Check(t, is.Equal(result.NodeID, "body"))
assert.Check(t, is.Equal(result.NodeID, "node-id"))
}

View File

@@ -1,9 +1,6 @@
package client
import (
"bytes"
"encoding/json"
"io"
"net/http"
"testing"
@@ -27,18 +24,11 @@ func TestSwarmInspect(t *testing.T) {
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
content, err := json.Marshal(swarm.Swarm{
return mockJSONResponse(http.StatusOK, nil, swarm.Swarm{
ClusterInfo: swarm.ClusterInfo{
ID: "swarm_id",
},
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(content)),
}, nil
})(req)
}))
assert.NilError(t, err)

View File

@@ -1,9 +1,7 @@
package client
import (
"bytes"
"context"
"io"
"net/http"
"testing"
@@ -27,10 +25,7 @@ func TestSwarmJoin(t *testing.T) {
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
return mockResponse(http.StatusOK, nil, "")(req)
}))
assert.NilError(t, err)

View File

@@ -1,10 +1,8 @@
package client
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"testing"
@@ -46,10 +44,7 @@ func TestSwarmLeave(t *testing.T) {
if force != leaveCase.expectedForce {
return nil, fmt.Errorf("force not set in URL query properly. expected '%s', got %s", leaveCase.expectedForce, force)
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
return mockResponse(http.StatusOK, nil, "")(req)
}))
assert.NilError(t, err)

View File

@@ -1,9 +1,7 @@
package client
import (
"bytes"
"context"
"io"
"net/http"
"testing"
@@ -27,10 +25,7 @@ func TestSwarmUnlock(t *testing.T) {
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
return mockResponse(http.StatusOK, nil, "")(req)
}))
assert.NilError(t, err)

View File

@@ -1,8 +1,6 @@
package client
import (
"bytes"
"io"
"net/http"
"testing"
@@ -26,10 +24,7 @@ func TestSwarmUpdate(t *testing.T) {
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
return mockResponse(http.StatusOK, nil, "")(req)
}))
assert.NilError(t, err)

View File

@@ -1,10 +1,7 @@
package client
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"testing"
@@ -28,22 +25,12 @@ func TestDiskUsage(t *testing.T) {
return nil, err
}
du := system.DiskUsage{
return mockJSONResponse(http.StatusOK, nil, system.DiskUsage{
LayersSize: int64(100),
Images: nil,
Containers: nil,
Volumes: nil,
}
b, err := json.Marshal(du)
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(b)),
}, nil
})(req)
}))
assert.NilError(t, err)
_, err = client.DiskUsage(context.Background(), DiskUsageOptions{})

View File

@@ -1,10 +1,7 @@
package client
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"testing"
@@ -22,12 +19,7 @@ func TestInfoServerError(t *testing.T) {
}
func TestInfoInvalidResponseJSONError(t *testing.T) {
client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte("invalid json"))),
}, nil
}))
client, err := NewClientWithOpts(WithMockClient(mockResponse(http.StatusOK, nil, "invalid json")))
assert.NilError(t, err)
_, err = client.Info(context.Background())
assert.Check(t, is.ErrorContains(err, "invalid character"))
@@ -39,19 +31,10 @@ func TestInfo(t *testing.T) {
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
info := &system.Info{
return mockJSONResponse(http.StatusOK, nil, system.Info{
ID: "daemonID",
Containers: 3,
}
b, err := json.Marshal(info)
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(b)),
}, nil
})(req)
}))
assert.NilError(t, err)
@@ -68,7 +51,7 @@ func TestInfoWithDiscoveredDevices(t *testing.T) {
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
info := &system.Info{
return mockJSONResponse(http.StatusOK, nil, system.Info{
ID: "daemonID",
Containers: 3,
DiscoveredDevices: []system.DeviceInfo{
@@ -81,16 +64,7 @@ func TestInfoWithDiscoveredDevices(t *testing.T) {
ID: "vendor.com/gpu=1",
},
},
}
b, err := json.Marshal(info)
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(b)),
}, nil
})(req)
}))
assert.NilError(t, err)

View File

@@ -1,11 +1,8 @@
package client
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"net/http"
"testing"
@@ -43,16 +40,9 @@ func TestTaskInspect(t *testing.T) {
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
content, err := json.Marshal(swarm.Task{
return mockJSONResponse(http.StatusOK, nil, swarm.Task{
ID: "task_id",
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(content)),
}, nil
})(req)
}))
assert.NilError(t, err)

View File

@@ -1,11 +1,8 @@
package client
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"testing"
@@ -57,21 +54,10 @@ func TestTaskList(t *testing.T) {
return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
}
}
content, err := json.Marshal([]swarm.Task{
{
ID: "task_id1",
},
{
ID: "task_id2",
},
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(content)),
}, nil
return mockJSONResponse(http.StatusOK, nil, []swarm.Task{
{ID: "task_id1"},
{ID: "task_id2"},
})(req)
}))
assert.NilError(t, err)

View File

@@ -1,10 +1,7 @@
package client
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"testing"
@@ -29,19 +26,11 @@ func TestVolumeCreate(t *testing.T) {
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
content, err := json.Marshal(volume.Volume{
return mockJSONResponse(http.StatusOK, nil, volume.Volume{
Name: "volume",
Driver: "local",
Mountpoint: "mountpoint",
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(content)),
}, nil
})(req)
}))
assert.NilError(t, err)

View File

@@ -1,10 +1,7 @@
package client
import (
"bytes"
"encoding/json"
"errors"
"io"
"net/http"
"testing"
@@ -56,14 +53,7 @@ func TestVolumeInspect(t *testing.T) {
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
content, err := json.Marshal(expected)
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(content)),
}, nil
return mockJSONResponse(http.StatusOK, nil, expected)(req)
}))
assert.NilError(t, err)

View File

@@ -1,11 +1,8 @@
package client
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"testing"
@@ -54,21 +51,14 @@ func TestVolumeList(t *testing.T) {
if actualFilters != listCase.expectedFilters {
return nil, fmt.Errorf("filters not set in URL query properly. Expected '%s', got %s", listCase.expectedFilters, actualFilters)
}
content, err := json.Marshal(volume.ListResponse{
return mockJSONResponse(http.StatusOK, nil, volume.ListResponse{
Volumes: []*volume.Volume{
{
Name: "volume",
Driver: "local",
},
},
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(content)),
}, nil
})(req)
}))
assert.NilError(t, err)

View File

@@ -1,10 +1,7 @@
package client
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"testing"
@@ -75,17 +72,10 @@ func TestVolumePrune(t *testing.T) {
if actualFilters != tc.expectedFilters {
return nil, fmt.Errorf("filters not set in URL query properly. Expected '%s', got %s", tc.expectedFilters, actualFilters)
}
content, err := json.Marshal(volume.PruneReport{
return mockJSONResponse(http.StatusOK, nil, volume.PruneReport{
VolumesDeleted: []string{"volume"},
SpaceReclaimed: 12345,
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(content)),
}, nil
})(req)
}))
assert.NilError(t, err)

View File

@@ -1,10 +1,8 @@
package client
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"testing"
@@ -52,10 +50,7 @@ func TestVolumeRemove(t *testing.T) {
return nil, fmt.Errorf("expected force=1, got %s", v)
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte("body"))),
}, nil
return mockResponse(http.StatusOK, nil, "body")(req)
}))
assert.NilError(t, err)

View File

@@ -1,10 +1,8 @@
package client
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"strings"
"testing"
@@ -44,10 +42,7 @@ func TestVolumeUpdate(t *testing.T) {
if !strings.Contains(req.URL.RawQuery, expectedVersion) {
return nil, fmt.Errorf("expected query to contain '%s', got '%s'", expectedVersion, req.URL.RawQuery)
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte("body"))),
}, nil
return mockResponse(http.StatusOK, nil, "body")(req)
}))
assert.NilError(t, err)