mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
client: use subtests for TestNetworkInspect
Unify the NetworkInspect tests to remove some boilerplating Before this change: go test -v -run TestNetworkInspect ./client/ === RUN TestNetworkInspectError --- PASS: TestNetworkInspectError (0.00s) === RUN TestNetworkInspectNotFoundError --- PASS: TestNetworkInspectNotFoundError (0.00s) === RUN TestNetworkInspectWithEmptyID --- PASS: TestNetworkInspectWithEmptyID (0.00s) === RUN TestNetworkInspect --- PASS: TestNetworkInspect (0.00s) PASS ok github.com/docker/docker/client 0.010s With this change: go test -v -run TestNetworkInspect ./client/ === RUN TestNetworkInspect === RUN TestNetworkInspect/empty_ID === RUN TestNetworkInspect/no_options === RUN TestNetworkInspect/verbose === RUN TestNetworkInspect/global_scope === RUN TestNetworkInspect/unknown_network === RUN TestNetworkInspect/server_error --- PASS: TestNetworkInspect (0.00s) --- PASS: TestNetworkInspect/empty_ID (0.00s) --- PASS: TestNetworkInspect/no_options (0.00s) --- PASS: TestNetworkInspect/verbose (0.00s) --- PASS: TestNetworkInspect/global_scope (0.00s) --- PASS: TestNetworkInspect/unknown_network (0.00s) --- PASS: TestNetworkInspect/server_error (0.00s) PASS ok github.com/docker/docker/client 0.012s Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
085c6a98d5
commit
4045c4ceaf
1 changed files with 53 additions and 70 deletions
|
@ -4,7 +4,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -13,66 +13,36 @@ import (
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
"github.com/docker/docker/api/types/network"
|
"github.com/docker/docker/api/types/network"
|
||||||
"github.com/docker/docker/errdefs"
|
"github.com/docker/docker/errdefs"
|
||||||
"github.com/pkg/errors"
|
|
||||||
"gotest.tools/v3/assert"
|
"gotest.tools/v3/assert"
|
||||||
is "gotest.tools/v3/assert/cmp"
|
is "gotest.tools/v3/assert/cmp"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNetworkInspectError(t *testing.T) {
|
|
||||||
client := &Client{
|
|
||||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := client.NetworkInspect(context.Background(), "nothing", types.NetworkInspectOptions{})
|
|
||||||
if !errdefs.IsSystem(err) {
|
|
||||||
t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestNetworkInspectNotFoundError(t *testing.T) {
|
|
||||||
client := &Client{
|
|
||||||
client: newMockClient(errorMock(http.StatusNotFound, "missing")),
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := client.NetworkInspect(context.Background(), "unknown", types.NetworkInspectOptions{})
|
|
||||||
assert.Check(t, is.Error(err, "Error: No such network: unknown"))
|
|
||||||
assert.Check(t, IsErrNotFound(err))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestNetworkInspectWithEmptyID(t *testing.T) {
|
|
||||||
client := &Client{
|
|
||||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
|
||||||
return nil, errors.New("should not make request")
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
_, _, err := client.NetworkInspectWithRaw(context.Background(), "", types.NetworkInspectOptions{})
|
|
||||||
if !IsErrNotFound(err) {
|
|
||||||
t.Fatalf("Expected NotFoundError, got %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestNetworkInspect(t *testing.T) {
|
func TestNetworkInspect(t *testing.T) {
|
||||||
expectedURL := "/networks/network_id"
|
|
||||||
client := &Client{
|
client := &Client{
|
||||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
|
||||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
|
||||||
}
|
|
||||||
if req.Method != http.MethodGet {
|
if req.Method != http.MethodGet {
|
||||||
return nil, fmt.Errorf("expected GET method, got %s", req.Method)
|
return nil, errors.New("expected GET method, got " + req.Method)
|
||||||
|
}
|
||||||
|
if req.URL.Path == "/networks/" {
|
||||||
|
return errorMock(http.StatusInternalServerError, "client should not make a request for empty IDs")(req)
|
||||||
|
}
|
||||||
|
if strings.HasPrefix(req.URL.Path, "/networks/unknown") {
|
||||||
|
return errorMock(http.StatusNotFound, "Error: No such network: unknown")(req)
|
||||||
|
}
|
||||||
|
if strings.HasPrefix(req.URL.Path, "/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 strings.Contains(req.URL.RawQuery, "scope=global") {
|
||||||
|
return errorMock(http.StatusNotFound, "Error: No such network: unknown")(req)
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
content []byte
|
content []byte
|
||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
if strings.Contains(req.URL.RawQuery, "scope=global") {
|
|
||||||
return &http.Response{
|
|
||||||
StatusCode: http.StatusNotFound,
|
|
||||||
Body: io.NopCloser(bytes.NewReader(content)),
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if strings.Contains(req.URL.RawQuery, "verbose=true") {
|
if strings.Contains(req.URL.RawQuery, "verbose=true") {
|
||||||
s := map[string]network.ServiceInfo{
|
s := map[string]network.ServiceInfo{
|
||||||
"web": {},
|
"web": {},
|
||||||
|
@ -90,32 +60,45 @@ func TestNetworkInspect(t *testing.T) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &http.Response{
|
return &http.Response{
|
||||||
|
Header: http.Header{"Content-Type": []string{"application/json"}},
|
||||||
StatusCode: http.StatusOK,
|
StatusCode: http.StatusOK,
|
||||||
Body: io.NopCloser(bytes.NewReader(content)),
|
Body: io.NopCloser(bytes.NewReader(content)),
|
||||||
}, nil
|
}, nil
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
|
|
||||||
r, err := client.NetworkInspect(context.Background(), "network_id", types.NetworkInspectOptions{})
|
t.Run("empty ID", func(t *testing.T) {
|
||||||
if err != nil {
|
// verify that the client does not create a request if the network-ID/name is empty.
|
||||||
t.Fatal(err)
|
_, err := client.NetworkInspect(context.Background(), "", types.NetworkInspectOptions{})
|
||||||
}
|
assert.Check(t, IsErrNotFound(err))
|
||||||
if r.Name != "mynetwork" {
|
})
|
||||||
t.Fatalf("expected `mynetwork`, got %s", r.Name)
|
t.Run("no options", func(t *testing.T) {
|
||||||
}
|
r, err := client.NetworkInspect(context.Background(), "network_id", types.NetworkInspectOptions{})
|
||||||
|
assert.NilError(t, err)
|
||||||
r, err = client.NetworkInspect(context.Background(), "network_id", types.NetworkInspectOptions{Verbose: true})
|
assert.Equal(t, r.Name, "mynetwork")
|
||||||
if err != nil {
|
})
|
||||||
t.Fatal(err)
|
t.Run("verbose", func(t *testing.T) {
|
||||||
}
|
r, err := client.NetworkInspect(context.Background(), "network_id", types.NetworkInspectOptions{Verbose: true})
|
||||||
if r.Name != "mynetwork" {
|
assert.NilError(t, err)
|
||||||
t.Fatalf("expected `mynetwork`, got %s", r.Name)
|
assert.Equal(t, r.Name, "mynetwork")
|
||||||
}
|
_, ok := r.Services["web"]
|
||||||
_, ok := r.Services["web"]
|
if !ok {
|
||||||
if !ok {
|
t.Fatalf("expected service `web` missing in the verbose output")
|
||||||
t.Fatalf("expected service `web` missing in the verbose output")
|
}
|
||||||
}
|
})
|
||||||
|
t.Run("global scope", func(t *testing.T) {
|
||||||
_, err = client.NetworkInspect(context.Background(), "network_id", types.NetworkInspectOptions{Scope: "global"})
|
_, err := client.NetworkInspect(context.Background(), "network_id", types.NetworkInspectOptions{Scope: "global"})
|
||||||
assert.Check(t, is.Error(err, "Error: No such network: network_id"))
|
assert.Check(t, is.Error(err, "Error: No such network: network_id"))
|
||||||
|
assert.Check(t, IsErrNotFound(err))
|
||||||
|
})
|
||||||
|
t.Run("unknown network", func(t *testing.T) {
|
||||||
|
_, err := client.NetworkInspect(context.Background(), "unknown", types.NetworkInspectOptions{})
|
||||||
|
assert.Check(t, is.Error(err, "Error: No such network: unknown"))
|
||||||
|
assert.Check(t, IsErrNotFound(err))
|
||||||
|
})
|
||||||
|
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", types.NetworkInspectOptions{})
|
||||||
|
assert.Check(t, errdefs.IsSystem(err))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue