checkpoint_list_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package client // import "github.com/docker/docker/client"
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "strings"
  10. "testing"
  11. "github.com/docker/docker/api/types/checkpoint"
  12. "github.com/docker/docker/errdefs"
  13. "gotest.tools/v3/assert"
  14. is "gotest.tools/v3/assert/cmp"
  15. )
  16. func TestCheckpointListError(t *testing.T) {
  17. client := &Client{
  18. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  19. }
  20. _, err := client.CheckpointList(context.Background(), "container_id", checkpoint.ListOptions{})
  21. assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
  22. }
  23. func TestCheckpointList(t *testing.T) {
  24. expectedURL := "/containers/container_id/checkpoints"
  25. client := &Client{
  26. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  27. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  28. return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
  29. }
  30. content, err := json.Marshal([]checkpoint.Summary{
  31. {
  32. Name: "checkpoint",
  33. },
  34. })
  35. if err != nil {
  36. return nil, err
  37. }
  38. return &http.Response{
  39. StatusCode: http.StatusOK,
  40. Body: io.NopCloser(bytes.NewReader(content)),
  41. }, nil
  42. }),
  43. }
  44. checkpoints, err := client.CheckpointList(context.Background(), "container_id", checkpoint.ListOptions{})
  45. if err != nil {
  46. t.Fatal(err)
  47. }
  48. if len(checkpoints) != 1 {
  49. t.Fatalf("expected 1 checkpoint, got %v", checkpoints)
  50. }
  51. }
  52. func TestCheckpointListContainerNotFound(t *testing.T) {
  53. client := &Client{
  54. client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
  55. }
  56. _, err := client.CheckpointList(context.Background(), "unknown", checkpoint.ListOptions{})
  57. assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
  58. }