checkpoint_list_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 TestCheckpointListError(t *testing.T) {
  14. client := &Client{
  15. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  16. }
  17. _, err := client.CheckpointList(context.Background(), "container_id", types.CheckpointListOptions{})
  18. if err == nil || err.Error() != "Error response from daemon: Server error" {
  19. t.Fatalf("expected a Server Error, got %v", err)
  20. }
  21. }
  22. func TestCheckpointList(t *testing.T) {
  23. expectedURL := "/containers/container_id/checkpoints"
  24. client := &Client{
  25. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  26. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  27. return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
  28. }
  29. content, err := json.Marshal([]types.Checkpoint{
  30. {
  31. Name: "checkpoint",
  32. },
  33. })
  34. if err != nil {
  35. return nil, err
  36. }
  37. return &http.Response{
  38. StatusCode: http.StatusOK,
  39. Body: ioutil.NopCloser(bytes.NewReader(content)),
  40. }, nil
  41. }),
  42. }
  43. checkpoints, err := client.CheckpointList(context.Background(), "container_id", types.CheckpointListOptions{})
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. if len(checkpoints) != 1 {
  48. t.Fatalf("expected 1 checkpoint, got %v", checkpoints)
  49. }
  50. }
  51. func TestCheckpointListContainerNotFound(t *testing.T) {
  52. client := &Client{
  53. client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
  54. }
  55. _, err := client.CheckpointList(context.Background(), "unknown", types.CheckpointListOptions{})
  56. if err == nil || !IsErrContainerNotFound(err) {
  57. t.Fatalf("expected a containerNotFound error, got %v", err)
  58. }
  59. }