checkpoint_create_test.go 1.9 KB

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