Files
moby/client/network_connect_test.go
Sebastiaan van Stijn 839c2709af 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>
2025-09-18 11:37:56 +02:00

104 lines
3.1 KiB
Go

package client
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"testing"
cerrdefs "github.com/containerd/errdefs"
"github.com/moby/moby/api/types/network"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
func TestNetworkConnectError(t *testing.T) {
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
assert.NilError(t, err)
err = client.NetworkConnect(context.Background(), "network_id", "container_id", nil)
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
// Empty network ID or container ID
err = client.NetworkConnect(context.Background(), "", "container_id", nil)
assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument))
assert.Check(t, is.ErrorContains(err, "value is empty"))
err = client.NetworkConnect(context.Background(), "network_id", "", nil)
assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument))
assert.Check(t, is.ErrorContains(err, "value is empty"))
}
func TestNetworkConnectEmptyNilEndpointSettings(t *testing.T) {
const expectedURL = "/networks/network_id/connect"
client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
var connect NetworkConnectOptions
if err := json.NewDecoder(req.Body).Decode(&connect); err != nil {
return nil, err
}
if connect.Container != "container_id" {
return nil, fmt.Errorf("expected 'container_id', got %s", connect.Container)
}
if connect.EndpointConfig != nil {
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
}))
assert.NilError(t, err)
err = client.NetworkConnect(context.Background(), "network_id", "container_id", nil)
assert.NilError(t, err)
}
func TestNetworkConnect(t *testing.T) {
const expectedURL = "/networks/network_id/connect"
client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
return nil, err
}
var connect NetworkConnectOptions
if err := json.NewDecoder(req.Body).Decode(&connect); err != nil {
return nil, err
}
if connect.Container != "container_id" {
return nil, fmt.Errorf("expected 'container_id', got %s", connect.Container)
}
if connect.EndpointConfig == nil {
return nil, fmt.Errorf("expected connect.EndpointConfig to be not nil, got %v", connect.EndpointConfig)
}
if connect.EndpointConfig.NetworkID != "NetworkID" {
return nil, fmt.Errorf("expected 'NetworkID', got %s", connect.EndpointConfig.NetworkID)
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
}))
assert.NilError(t, err)
err = client.NetworkConnect(context.Background(), "network_id", "container_id", &network.EndpointSettings{
NetworkID: "NetworkID",
})
assert.NilError(t, err)
}