checkpoint_list_test.go 1.9 KB

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