list_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package daemon
  2. import (
  3. "context"
  4. "os"
  5. "path/filepath"
  6. "testing"
  7. "github.com/docker/docker/api/types"
  8. containertypes "github.com/docker/docker/api/types/container"
  9. "github.com/docker/docker/api/types/filters"
  10. "github.com/docker/docker/container"
  11. "github.com/docker/docker/image"
  12. "github.com/google/uuid"
  13. "github.com/opencontainers/go-digest"
  14. "gotest.tools/v3/assert"
  15. is "gotest.tools/v3/assert/cmp"
  16. )
  17. var root string
  18. func TestMain(m *testing.M) {
  19. var err error
  20. root, err = os.MkdirTemp("", "docker-container-test-")
  21. if err != nil {
  22. panic(err)
  23. }
  24. defer os.RemoveAll(root)
  25. os.Exit(m.Run())
  26. }
  27. // This sets up a container with a name so that name filters
  28. // work against it. It takes in a pointer to Daemon so that
  29. // minor operations are not repeated by the caller
  30. func setupContainerWithName(t *testing.T, name string, daemon *Daemon) *container.Container {
  31. t.Helper()
  32. var (
  33. id = uuid.New().String()
  34. computedImageID = image.ID(digest.FromString(id))
  35. cRoot = filepath.Join(root, id)
  36. )
  37. if err := os.MkdirAll(cRoot, 0o755); err != nil {
  38. t.Fatal(err)
  39. }
  40. c := container.NewBaseContainer(id, cRoot)
  41. // these are for passing includeContainerInList
  42. if name[0] != '/' {
  43. name = "/" + name
  44. }
  45. c.Name = name
  46. c.Running = true
  47. c.HostConfig = &containertypes.HostConfig{}
  48. // these are for passing the refreshImage reducer
  49. c.ImageID = computedImageID
  50. c.Config = &containertypes.Config{
  51. Image: computedImageID.String(),
  52. }
  53. // this is done here to avoid requiring these
  54. // operations n x number of containers in the
  55. // calling function
  56. daemon.containersReplica.Save(c)
  57. daemon.reserveName(id, name)
  58. return c
  59. }
  60. func containerListContainsName(containers []*types.Container, name string) bool {
  61. for _, ctr := range containers {
  62. for _, containerName := range ctr.Names {
  63. if containerName == name {
  64. return true
  65. }
  66. }
  67. }
  68. return false
  69. }
  70. func TestListInvalidFilter(t *testing.T) {
  71. db, err := container.NewViewDB()
  72. assert.Assert(t, err == nil)
  73. d := &Daemon{
  74. containersReplica: db,
  75. }
  76. _, err = d.Containers(context.Background(), &containertypes.ListOptions{
  77. Filters: filters.NewArgs(filters.Arg("invalid", "foo")),
  78. })
  79. assert.Assert(t, is.Error(err, "invalid filter 'invalid'"))
  80. }
  81. func TestNameFilter(t *testing.T) {
  82. db, err := container.NewViewDB()
  83. assert.Assert(t, err == nil)
  84. d := &Daemon{
  85. containersReplica: db,
  86. }
  87. var (
  88. one = setupContainerWithName(t, "a1", d)
  89. two = setupContainerWithName(t, "a2", d)
  90. three = setupContainerWithName(t, "b1", d)
  91. )
  92. // moby/moby #37453 - ^ regex not working due to prefix slash
  93. // not being stripped
  94. containerList, err := d.Containers(context.Background(), &containertypes.ListOptions{
  95. Filters: filters.NewArgs(filters.Arg("name", "^a")),
  96. })
  97. assert.NilError(t, err)
  98. assert.Assert(t, is.Len(containerList, 2))
  99. assert.Assert(t, containerListContainsName(containerList, one.Name))
  100. assert.Assert(t, containerListContainsName(containerList, two.Name))
  101. // Same as above but with slash prefix should produce the same result
  102. containerListWithPrefix, err := d.Containers(context.Background(), &containertypes.ListOptions{
  103. Filters: filters.NewArgs(filters.Arg("name", "^/a")),
  104. })
  105. assert.NilError(t, err)
  106. assert.Assert(t, is.Len(containerListWithPrefix, 2))
  107. assert.Assert(t, containerListContainsName(containerListWithPrefix, one.Name))
  108. assert.Assert(t, containerListContainsName(containerListWithPrefix, two.Name))
  109. // Same as above but make sure it works for exact names
  110. containerList, err = d.Containers(context.Background(), &containertypes.ListOptions{
  111. Filters: filters.NewArgs(filters.Arg("name", "b1")),
  112. })
  113. assert.NilError(t, err)
  114. assert.Assert(t, is.Len(containerList, 1))
  115. assert.Assert(t, containerListContainsName(containerList, three.Name))
  116. // Same as above but with slash prefix should produce the same result
  117. containerListWithPrefix, err = d.Containers(context.Background(), &containertypes.ListOptions{
  118. Filters: filters.NewArgs(filters.Arg("name", "/b1")),
  119. })
  120. assert.NilError(t, err)
  121. assert.Assert(t, is.Len(containerListWithPrefix, 1))
  122. assert.Assert(t, containerListContainsName(containerListWithPrefix, three.Name))
  123. }