mirror of
https://github.com/moby/moby.git
synced 2026-01-11 18:51:37 +00:00
client/network_test: Use functional option to create mock client
Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
This commit is contained in:
@@ -16,11 +16,10 @@ import (
|
||||
)
|
||||
|
||||
func TestNetworkDisconnectError(t *testing.T) {
|
||||
client := &Client{
|
||||
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
||||
}
|
||||
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
|
||||
assert.NilError(t, err)
|
||||
|
||||
err := client.NetworkDisconnect(context.Background(), "network_id", "container_id", false)
|
||||
err = client.NetworkDisconnect(context.Background(), "network_id", "container_id", false)
|
||||
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
|
||||
|
||||
// Empty network ID or container ID
|
||||
@@ -36,36 +35,35 @@ func TestNetworkDisconnectError(t *testing.T) {
|
||||
func TestNetworkDisconnect(t *testing.T) {
|
||||
expectedURL := "/networks/network_id/disconnect"
|
||||
|
||||
client := &Client{
|
||||
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
|
||||
if req.Method != http.MethodPost {
|
||||
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
|
||||
}
|
||||
if req.Method != http.MethodPost {
|
||||
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
|
||||
}
|
||||
|
||||
var disconnect NetworkDisconnectOptions
|
||||
if err := json.NewDecoder(req.Body).Decode(&disconnect); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var disconnect NetworkDisconnectOptions
|
||||
if err := json.NewDecoder(req.Body).Decode(&disconnect); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if disconnect.Container != "container_id" {
|
||||
return nil, fmt.Errorf("expected 'container_id', got %s", disconnect.Container)
|
||||
}
|
||||
if disconnect.Container != "container_id" {
|
||||
return nil, fmt.Errorf("expected 'container_id', got %s", disconnect.Container)
|
||||
}
|
||||
|
||||
if !disconnect.Force {
|
||||
return nil, fmt.Errorf("expected Force to be true, got %v", disconnect.Force)
|
||||
}
|
||||
if !disconnect.Force {
|
||||
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 &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: io.NopCloser(bytes.NewReader([]byte(""))),
|
||||
}, nil
|
||||
}))
|
||||
assert.NilError(t, err)
|
||||
|
||||
err := client.NetworkDisconnect(context.Background(), "network_id", "container_id", true)
|
||||
err = client.NetworkDisconnect(context.Background(), "network_id", "container_id", true)
|
||||
assert.NilError(t, err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user