PageRenderTime 228ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/daemon/list_test.go

https://github.com/dotcloud/docker
Go | 144 lines | 110 code | 21 blank | 13 comment | 13 complexity | 0ac02c61da769b39ed8dd2a17af1f94b MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, Apache-2.0, BSD-2-Clause, CC-BY-4.0, 0BSD, CC-BY-SA-4.0, GPL-2.0, BSD-3-Clause, MIT
  1. package daemon
  2. import (
  3. "os"
  4. "path/filepath"
  5. "testing"
  6. "github.com/docker/docker/api/types"
  7. containertypes "github.com/docker/docker/api/types/container"
  8. "github.com/docker/docker/api/types/filters"
  9. "github.com/docker/docker/container"
  10. "github.com/docker/docker/image"
  11. "github.com/google/uuid"
  12. "github.com/opencontainers/go-digest"
  13. "gotest.tools/v3/assert"
  14. is "gotest.tools/v3/assert/cmp"
  15. )
  16. var root string
  17. func TestMain(m *testing.M) {
  18. var err error
  19. root, err = os.MkdirTemp("", "docker-container-test-")
  20. if err != nil {
  21. panic(err)
  22. }
  23. defer os.RemoveAll(root)
  24. os.Exit(m.Run())
  25. }
  26. // This sets up a container with a name so that name filters
  27. // work against it. It takes in a pointer to Daemon so that
  28. // minor operations are not repeated by the caller
  29. func setupContainerWithName(t *testing.T, name string, daemon *Daemon) *container.Container {
  30. t.Helper()
  31. var (
  32. id = uuid.New().String()
  33. computedImageID = digest.FromString(id)
  34. cRoot = filepath.Join(root, id)
  35. )
  36. if err := os.MkdirAll(cRoot, 0755); err != nil {
  37. t.Fatal(err)
  38. }
  39. c := container.NewBaseContainer(id, cRoot)
  40. // these are for passing includeContainerInList
  41. if name[0] != '/' {
  42. name = "/" + name
  43. }
  44. c.Name = name
  45. c.Running = true
  46. c.HostConfig = &containertypes.HostConfig{}
  47. // these are for passing the refreshImage reducer
  48. c.ImageID = image.IDFromDigest(computedImageID)
  49. c.Config = &containertypes.Config{
  50. Image: computedImageID.String(),
  51. }
  52. // this is done here to avoid requiring these
  53. // operations n x number of containers in the
  54. // calling function
  55. daemon.containersReplica.Save(c)
  56. daemon.reserveName(id, name)
  57. return c
  58. }
  59. func containerListContainsName(containers []*types.Container, name string) bool {
  60. for _, ctr := range containers {
  61. for _, containerName := range ctr.Names {
  62. if containerName == name {
  63. return true
  64. }
  65. }
  66. }
  67. return false
  68. }
  69. func TestListInvalidFilter(t *testing.T) {
  70. db, err := container.NewViewDB()
  71. assert.Assert(t, err == nil)
  72. d := &Daemon{
  73. containersReplica: db,
  74. }
  75. f := filters.NewArgs(filters.Arg("invalid", "foo"))
  76. _, err = d.Containers(&types.ContainerListOptions{
  77. Filters: f,
  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(&types.ContainerListOptions{
  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(&types.ContainerListOptions{
  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(&types.ContainerListOptions{
  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(&types.ContainerListOptions{
  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. }