container_list_test.go 2.6 KB

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