checkpoint_create_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package client // import "github.com/docker/docker/client"
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "io/ioutil"
  8. "net/http"
  9. "strings"
  10. "testing"
  11. "github.com/docker/docker/api/types"
  12. "github.com/docker/docker/errdefs"
  13. )
  14. func TestCheckpointCreateError(t *testing.T) {
  15. client := &Client{
  16. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  17. }
  18. err := client.CheckpointCreate(context.Background(), "nothing", types.CheckpointCreateOptions{
  19. CheckpointID: "noting",
  20. Exit: true,
  21. })
  22. if !errdefs.IsSystem(err) {
  23. t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err)
  24. }
  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 := &types.CheckpointCreateOptions{}
  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: ioutil.NopCloser(bytes.NewReader([]byte(""))),
  51. }, nil
  52. }),
  53. }
  54. err := client.CheckpointCreate(context.Background(), expectedContainerID, types.CheckpointCreateOptions{
  55. CheckpointID: expectedCheckpointID,
  56. Exit: true,
  57. })
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. }