list_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package daemon
  2. import (
  3. "io/ioutil"
  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/opencontainers/go-digest"
  13. "github.com/pborman/uuid"
  14. "gotest.tools/assert"
  15. is "gotest.tools/assert/cmp"
  16. )
  17. var root string
  18. func TestMain(m *testing.M) {
  19. var err error
  20. root, err = ioutil.TempDir("", "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()
  34. computedImageID = digest.FromString(id)
  35. cRoot = filepath.Join(root, id)
  36. )
  37. if err := os.MkdirAll(cRoot, 0755); 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 = image.IDFromDigest(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 _, container := range containers {
  62. for _, containerName := range container.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. f := filters.NewArgs(filters.Arg("invalid", "foo"))
  77. _, err = d.Containers(&types.ContainerListOptions{
  78. Filters: f,
  79. })
  80. assert.Assert(t, is.Error(err, "Invalid filter 'invalid'"))
  81. }
  82. func TestNameFilter(t *testing.T) {
  83. db, err := container.NewViewDB()
  84. assert.Assert(t, err == nil)
  85. d := &Daemon{
  86. containersReplica: db,
  87. }
  88. var (
  89. one = setupContainerWithName(t, "a1", d)
  90. two = setupContainerWithName(t, "a2", d)
  91. three = setupContainerWithName(t, "b1", d)
  92. )
  93. // moby/moby #37453 - ^ regex not working due to prefix slash
  94. // not being stripped
  95. containerList, err := d.Containers(&types.ContainerListOptions{
  96. Filters: filters.NewArgs(filters.Arg("name", "^a")),
  97. })
  98. assert.NilError(t, err)
  99. assert.Assert(t, is.Len(containerList, 2))
  100. assert.Assert(t, containerListContainsName(containerList, one.Name))
  101. assert.Assert(t, containerListContainsName(containerList, two.Name))
  102. // Same as above but with slash prefix should produce the same result
  103. containerListWithPrefix, err := d.Containers(&types.ContainerListOptions{
  104. Filters: filters.NewArgs(filters.Arg("name", "^/a")),
  105. })
  106. assert.NilError(t, err)
  107. assert.Assert(t, is.Len(containerListWithPrefix, 2))
  108. assert.Assert(t, containerListContainsName(containerListWithPrefix, one.Name))
  109. assert.Assert(t, containerListContainsName(containerListWithPrefix, two.Name))
  110. // Same as above but make sure it works for exact names
  111. containerList, err = d.Containers(&types.ContainerListOptions{
  112. Filters: filters.NewArgs(filters.Arg("name", "b1")),
  113. })
  114. assert.NilError(t, err)
  115. assert.Assert(t, is.Len(containerList, 1))
  116. assert.Assert(t, containerListContainsName(containerList, three.Name))
  117. // Same as above but with slash prefix should produce the same result
  118. containerListWithPrefix, err = d.Containers(&types.ContainerListOptions{
  119. Filters: filters.NewArgs(filters.Arg("name", "/b1")),
  120. })
  121. assert.NilError(t, err)
  122. assert.Assert(t, is.Len(containerListWithPrefix, 1))
  123. assert.Assert(t, containerListContainsName(containerListWithPrefix, three.Name))
  124. }