container_list_test.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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"
  12. "github.com/docker/docker/api/types/container"
  13. "github.com/docker/docker/api/types/filters"
  14. "github.com/docker/docker/errdefs"
  15. "gotest.tools/v3/assert"
  16. is "gotest.tools/v3/assert/cmp"
  17. )
  18. func TestContainerListError(t *testing.T) {
  19. client := &Client{
  20. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  21. }
  22. _, err := client.ContainerList(context.Background(), container.ListOptions{})
  23. assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
  24. }
  25. func TestContainerList(t *testing.T) {
  26. expectedURL := "/containers/json"
  27. expectedFilters := `{"before":{"container":true},"label":{"label1":true,"label2":true}}`
  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. query := req.URL.Query()
  34. all := query.Get("all")
  35. if all != "1" {
  36. return nil, fmt.Errorf("all not set in URL query properly. Expected '1', got %s", all)
  37. }
  38. limit := query.Get("limit")
  39. if limit != "" {
  40. return nil, fmt.Errorf("limit should have not be present in query, got %s", limit)
  41. }
  42. since := query.Get("since")
  43. if since != "container" {
  44. return nil, fmt.Errorf("since not set in URL query properly. Expected 'container', got %s", since)
  45. }
  46. before := query.Get("before")
  47. if before != "" {
  48. return nil, fmt.Errorf("before should have not be present in query, got %s", before)
  49. }
  50. size := query.Get("size")
  51. if size != "1" {
  52. return nil, fmt.Errorf("size not set in URL query properly. Expected '1', got %s", size)
  53. }
  54. fltrs := query.Get("filters")
  55. if fltrs != expectedFilters {
  56. return nil, fmt.Errorf("expected filters incoherent '%v' with actual filters %v", expectedFilters, fltrs)
  57. }
  58. b, err := json.Marshal([]types.Container{
  59. {
  60. ID: "container_id1",
  61. },
  62. {
  63. ID: "container_id2",
  64. },
  65. })
  66. if err != nil {
  67. return nil, err
  68. }
  69. return &http.Response{
  70. StatusCode: http.StatusOK,
  71. Body: io.NopCloser(bytes.NewReader(b)),
  72. }, nil
  73. }),
  74. }
  75. containers, err := client.ContainerList(context.Background(), container.ListOptions{
  76. Size: true,
  77. All: true,
  78. Since: "container",
  79. Filters: filters.NewArgs(
  80. filters.Arg("label", "label1"),
  81. filters.Arg("label", "label2"),
  82. filters.Arg("before", "container"),
  83. ),
  84. })
  85. if err != nil {
  86. t.Fatal(err)
  87. }
  88. if len(containers) != 2 {
  89. t.Fatalf("expected 2 containers, got %v", containers)
  90. }
  91. }