mirror of
https://github.com/moby/moby.git
synced 2026-01-11 18:51:37 +00:00
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>
105 lines
3.8 KiB
Go
105 lines
3.8 KiB
Go
package client
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
|
|
cerrdefs "github.com/containerd/errdefs"
|
|
"github.com/moby/moby/api/types/network"
|
|
"gotest.tools/v3/assert"
|
|
is "gotest.tools/v3/assert/cmp"
|
|
)
|
|
|
|
func TestNetworkInspect(t *testing.T) {
|
|
const expectedURL = "/networks/network_id"
|
|
|
|
client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
|
|
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, defaultAPIPath+"/networks/unknown") {
|
|
return errorMock(http.StatusNotFound, "Error: No such network: unknown")(req)
|
|
}
|
|
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 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)
|
|
}
|
|
var (
|
|
content []byte
|
|
err error
|
|
)
|
|
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{
|
|
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
|
|
}))
|
|
assert.NilError(t, err)
|
|
|
|
t.Run("empty ID", func(t *testing.T) {
|
|
// verify that the client does not create a request if the network-ID/name is empty.
|
|
_, err := client.NetworkInspect(context.Background(), "", NetworkInspectOptions{})
|
|
assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument))
|
|
assert.Check(t, is.ErrorContains(err, "value is empty"))
|
|
|
|
_, err = client.NetworkInspect(context.Background(), " ", NetworkInspectOptions{})
|
|
assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument))
|
|
assert.Check(t, is.ErrorContains(err, "value is empty"))
|
|
})
|
|
t.Run("no options", func(t *testing.T) {
|
|
r, err := client.NetworkInspect(context.Background(), "network_id", NetworkInspectOptions{})
|
|
assert.NilError(t, err)
|
|
assert.Check(t, is.Equal(r.Name, "mynetwork"))
|
|
})
|
|
t.Run("verbose", func(t *testing.T) {
|
|
r, err := client.NetworkInspect(context.Background(), "network_id", NetworkInspectOptions{Verbose: true})
|
|
assert.NilError(t, err)
|
|
assert.Check(t, is.Equal(r.Name, "mynetwork"))
|
|
_, ok := r.Services["web"]
|
|
assert.Check(t, ok, "expected service `web` missing in the verbose output")
|
|
})
|
|
t.Run("global scope", func(t *testing.T) {
|
|
_, err := client.NetworkInspect(context.Background(), "network_id", NetworkInspectOptions{Scope: "global"})
|
|
assert.Check(t, is.ErrorContains(err, "Error: No such network: network_id"))
|
|
assert.Check(t, is.ErrorType(err, cerrdefs.IsNotFound))
|
|
})
|
|
t.Run("unknown network", func(t *testing.T) {
|
|
_, err := client.NetworkInspect(context.Background(), "unknown", NetworkInspectOptions{})
|
|
assert.Check(t, is.ErrorContains(err, "Error: No such network: unknown"))
|
|
assert.Check(t, is.ErrorType(err, cerrdefs.IsNotFound))
|
|
})
|
|
t.Run("server error", func(t *testing.T) {
|
|
// Just testing that an internal server error is converted correctly by the client
|
|
_, err := client.NetworkInspect(context.Background(), "test-500-response", NetworkInspectOptions{})
|
|
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
|
|
})
|
|
}
|