Files
moby/client/checkpoint_create_test.go
Sebastiaan van Stijn a8afc2c6fb api/types/checkpoint: move checkpoint options to client
Move the option-types to the client and in some cases create a
copy for the backend. These types are used to construct query-
args, and not marshaled to JSON, and can be replaced with functional
options in the client.

The CreateOptions type was used both as options-struct for the client,
and as struct to marshal/unmarshal the request. For this type, a copy
is created in the Client and a new `checkpoint.CreateRequest` is added
in the API.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-09-05 10:20:06 +02:00

83 lines
2.3 KiB
Go

package client
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
func TestCheckpointCreateError(t *testing.T) {
client, err := NewClientWithOpts(
WithMockClient(errorMock(http.StatusInternalServerError, "Server error")),
)
assert.NilError(t, err)
err = client.CheckpointCreate(context.Background(), "nothing", CheckpointCreateOptions{
CheckpointID: "noting",
Exit: true,
})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
err = client.CheckpointCreate(context.Background(), "", CheckpointCreateOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument))
assert.Check(t, is.ErrorContains(err, "value is empty"))
err = client.CheckpointCreate(context.Background(), " ", CheckpointCreateOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument))
assert.Check(t, is.ErrorContains(err, "value is empty"))
}
func TestCheckpointCreate(t *testing.T) {
expectedContainerID := "container_id"
expectedCheckpointID := "checkpoint_id"
expectedURL := "/containers/container_id/checkpoints"
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)
}
createOptions := &CheckpointCreateOptions{}
if err := json.NewDecoder(req.Body).Decode(createOptions); err != nil {
return nil, err
}
if createOptions.CheckpointID != expectedCheckpointID {
return nil, fmt.Errorf("expected CheckpointID to be 'checkpoint_id', got %v", createOptions.CheckpointID)
}
if !createOptions.Exit {
return nil, errors.New("expected Exit to be true")
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
}),
)
assert.NilError(t, err)
err = client.CheckpointCreate(context.Background(), expectedContainerID, CheckpointCreateOptions{
CheckpointID: expectedCheckpointID,
Exit: true,
})
assert.NilError(t, err)
}