checkpoint_create_test.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 err == nil || err.Error() != "Error response from daemon: Server error" {
  23. t.Fatalf("expected a Server Error, got %v", err)
  24. }
  25. if !errdefs.IsSystem(err) {
  26. t.Fatalf("expected a Server Error, got %T", err)
  27. }
  28. }
  29. func TestCheckpointCreate(t *testing.T) {
  30. expectedContainerID := "container_id"
  31. expectedCheckpointID := "checkpoint_id"
  32. expectedURL := "/containers/container_id/checkpoints"
  33. client := &Client{
  34. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  35. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  36. return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
  37. }
  38. if req.Method != "POST" {
  39. return nil, fmt.Errorf("expected POST method, got %s", req.Method)
  40. }
  41. createOptions := &types.CheckpointCreateOptions{}
  42. if err := json.NewDecoder(req.Body).Decode(createOptions); err != nil {
  43. return nil, err
  44. }
  45. if createOptions.CheckpointID != expectedCheckpointID {
  46. return nil, fmt.Errorf("expected CheckpointID to be 'checkpoint_id', got %v", createOptions.CheckpointID)
  47. }
  48. if !createOptions.Exit {
  49. return nil, fmt.Errorf("expected Exit to be true")
  50. }
  51. return &http.Response{
  52. StatusCode: http.StatusOK,
  53. Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
  54. }, nil
  55. }),
  56. }
  57. err := client.CheckpointCreate(context.Background(), expectedContainerID, types.CheckpointCreateOptions{
  58. CheckpointID: expectedCheckpointID,
  59. Exit: true,
  60. })
  61. if err != nil {
  62. t.Fatal(err)
  63. }
  64. }