2018-02-05 21:05:59 +00:00
|
|
|
package client // import "github.com/docker/docker/client"
|
2016-09-06 18:46:37 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-04-19 22:30:59 +00:00
|
|
|
"context"
|
2016-09-06 18:46:37 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-08-24 10:10:50 +00:00
|
|
|
"io"
|
2016-09-06 18:46:37 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2023-08-26 10:37:41 +00:00
|
|
|
"github.com/docker/docker/api/types/checkpoint"
|
2018-12-31 17:22:43 +00:00
|
|
|
"github.com/docker/docker/errdefs"
|
2023-05-10 11:17:40 +00:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2016-09-06 18:46:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCheckpointCreateError(t *testing.T) {
|
|
|
|
client := &Client{
|
2016-09-09 03:44:25 +00:00
|
|
|
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
2023-08-26 10:37:41 +00:00
|
|
|
err := client.CheckpointCreate(context.Background(), "nothing", checkpoint.CreateOptions{
|
2016-09-06 18:46:37 +00:00
|
|
|
CheckpointID: "noting",
|
|
|
|
Exit: true,
|
|
|
|
})
|
|
|
|
|
2023-05-10 11:17:40 +00:00
|
|
|
assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckpointCreate(t *testing.T) {
|
|
|
|
expectedContainerID := "container_id"
|
|
|
|
expectedCheckpointID := "checkpoint_id"
|
|
|
|
expectedURL := "/containers/container_id/checkpoints"
|
|
|
|
|
|
|
|
client := &Client{
|
2016-09-09 03:44:25 +00:00
|
|
|
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
2016-09-06 18:46:37 +00:00
|
|
|
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
|
|
|
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
|
|
|
}
|
|
|
|
|
2019-10-12 18:41:14 +00:00
|
|
|
if req.Method != http.MethodPost {
|
2016-09-06 18:46:37 +00:00
|
|
|
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
|
|
|
|
}
|
|
|
|
|
2023-08-26 10:37:41 +00:00
|
|
|
createOptions := &checkpoint.CreateOptions{}
|
2016-09-06 18:46:37 +00:00
|
|
|
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, fmt.Errorf("expected Exit to be true")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
2021-08-24 10:10:50 +00:00
|
|
|
Body: io.NopCloser(bytes.NewReader([]byte(""))),
|
2016-09-06 18:46:37 +00:00
|
|
|
}, nil
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
2023-08-26 10:37:41 +00:00
|
|
|
err := client.CheckpointCreate(context.Background(), expectedContainerID, checkpoint.CreateOptions{
|
2016-09-06 18:46:37 +00:00
|
|
|
CheckpointID: expectedCheckpointID,
|
|
|
|
Exit: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|