checkpoint_create_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package client // import "github.com/docker/docker/client"
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "strings"
  10. "testing"
  11. "github.com/docker/docker/api/types/checkpoint"
  12. "github.com/docker/docker/errdefs"
  13. "gotest.tools/v3/assert"
  14. is "gotest.tools/v3/assert/cmp"
  15. )
  16. func TestCheckpointCreateError(t *testing.T) {
  17. client := &Client{
  18. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  19. }
  20. err := client.CheckpointCreate(context.Background(), "nothing", checkpoint.CreateOptions{
  21. CheckpointID: "noting",
  22. Exit: true,
  23. })
  24. assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
  25. }
  26. func TestCheckpointCreate(t *testing.T) {
  27. expectedContainerID := "container_id"
  28. expectedCheckpointID := "checkpoint_id"
  29. expectedURL := "/containers/container_id/checkpoints"
  30. client := &Client{
  31. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  32. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  33. return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
  34. }
  35. if req.Method != http.MethodPost {
  36. return nil, fmt.Errorf("expected POST method, got %s", req.Method)
  37. }
  38. createOptions := &checkpoint.CreateOptions{}
  39. if err := json.NewDecoder(req.Body).Decode(createOptions); err != nil {
  40. return nil, err
  41. }
  42. if createOptions.CheckpointID != expectedCheckpointID {
  43. return nil, fmt.Errorf("expected CheckpointID to be 'checkpoint_id', got %v", createOptions.CheckpointID)
  44. }
  45. if !createOptions.Exit {
  46. return nil, fmt.Errorf("expected Exit to be true")
  47. }
  48. return &http.Response{
  49. StatusCode: http.StatusOK,
  50. Body: io.NopCloser(bytes.NewReader([]byte(""))),
  51. }, nil
  52. }),
  53. }
  54. err := client.CheckpointCreate(context.Background(), expectedContainerID, checkpoint.CreateOptions{
  55. CheckpointID: expectedCheckpointID,
  56. Exit: true,
  57. })
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. }