Files
moby/client/container_resize_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

136 lines
4.0 KiB
Go

package client
import (
"bytes"
"context"
"io"
"math"
"net/http"
"testing"
cerrdefs "github.com/containerd/errdefs"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
func TestContainerResizeError(t *testing.T) {
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
assert.NilError(t, err)
err = client.ContainerResize(context.Background(), "container_id", ContainerResizeOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
err = client.ContainerResize(context.Background(), "", ContainerResizeOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument))
assert.Check(t, is.ErrorContains(err, "value is empty"))
err = client.ContainerResize(context.Background(), " ", ContainerResizeOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument))
assert.Check(t, is.ErrorContains(err, "value is empty"))
}
func TestContainerExecResizeError(t *testing.T) {
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
assert.NilError(t, err)
err = client.ContainerExecResize(context.Background(), "exec_id", ContainerResizeOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
}
func TestContainerResize(t *testing.T) {
const expectedURL = "/containers/container_id/resize"
tests := []struct {
doc string
opts ContainerResizeOptions
expectedHeight, expectedWidth string
}{
{
doc: "zero width height", // valid, but not very useful
opts: ContainerResizeOptions{},
expectedWidth: "0",
expectedHeight: "0",
},
{
doc: "valid resize",
opts: ContainerResizeOptions{
Height: 500,
Width: 600,
},
expectedHeight: "500",
expectedWidth: "600",
},
{
doc: "larger than maxint64",
opts: ContainerResizeOptions{
Height: math.MaxInt64 + 1,
Width: math.MaxInt64 + 2,
},
expectedHeight: "9223372036854775808",
expectedWidth: "9223372036854775809",
},
}
for _, tc := range tests {
t.Run(tc.doc, func(t *testing.T) {
client, err := NewClientWithOpts(WithMockClient(resizeTransport(t, expectedURL, tc.expectedHeight, tc.expectedWidth)))
assert.NilError(t, err)
err = client.ContainerResize(context.Background(), "container_id", tc.opts)
assert.NilError(t, err)
})
}
}
func TestContainerExecResize(t *testing.T) {
const expectedURL = "/exec/exec_id/resize"
tests := []struct {
doc string
opts ContainerResizeOptions
expectedHeight, expectedWidth string
}{
{
doc: "zero width height", // valid, but not very useful
opts: ContainerResizeOptions{},
expectedWidth: "0",
expectedHeight: "0",
},
{
doc: "valid resize",
opts: ContainerResizeOptions{
Height: 500,
Width: 600,
},
expectedHeight: "500",
expectedWidth: "600",
},
{
doc: "larger than maxint64",
opts: ContainerResizeOptions{
Height: math.MaxInt64 + 1,
Width: math.MaxInt64 + 2,
},
expectedHeight: "9223372036854775808",
expectedWidth: "9223372036854775809",
},
}
for _, tc := range tests {
t.Run(tc.doc, func(t *testing.T) {
client, err := NewClientWithOpts(WithMockClient(resizeTransport(t, expectedURL, tc.expectedHeight, tc.expectedWidth)))
assert.NilError(t, err)
err = client.ContainerExecResize(context.Background(), "exec_id", tc.opts)
assert.NilError(t, err)
})
}
}
func resizeTransport(t *testing.T, expectedURL, expectedHeight, expectedWidth string) func(req *http.Request) (*http.Response, error) {
return func(req *http.Request) (*http.Response, error) {
assert.Check(t, assertRequest(req, http.MethodPost, expectedURL))
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
}
}