docker_cli_search_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/go-check/check"
  6. "gotest.tools/assert"
  7. )
  8. // search for repos named "registry" on the central registry
  9. func (s *DockerSuite) TestSearchOnCentralRegistry(c *check.C) {
  10. testRequires(c, Network, DaemonIsLinux)
  11. out, _ := dockerCmd(c, "search", "busybox")
  12. assert.Assert(c, strings.Contains(out, "Busybox base image."), "couldn't find any repository named (or containing) 'Busybox base image.'")
  13. }
  14. func (s *DockerSuite) TestSearchStarsOptionWithWrongParameter(c *check.C) {
  15. out, _, err := dockerCmdWithError("search", "--filter", "stars=a", "busybox")
  16. assert.ErrorContains(c, err, "", out)
  17. assert.Assert(c, strings.Contains(out, "Invalid filter"), "couldn't find the invalid filter warning")
  18. out, _, err = dockerCmdWithError("search", "-f", "stars=a", "busybox")
  19. assert.ErrorContains(c, err, "", out)
  20. assert.Assert(c, strings.Contains(out, "Invalid filter"), "couldn't find the invalid filter warning")
  21. out, _, err = dockerCmdWithError("search", "-f", "is-automated=a", "busybox")
  22. assert.ErrorContains(c, err, "", out)
  23. assert.Assert(c, strings.Contains(out, "Invalid filter"), "couldn't find the invalid filter warning")
  24. out, _, err = dockerCmdWithError("search", "-f", "is-official=a", "busybox")
  25. assert.ErrorContains(c, err, "", out)
  26. assert.Assert(c, strings.Contains(out, "Invalid filter"), "couldn't find the invalid filter warning")
  27. // -s --stars deprecated since Docker 1.13
  28. out, _, err = dockerCmdWithError("search", "--stars=a", "busybox")
  29. assert.ErrorContains(c, err, "", out)
  30. assert.Assert(c, strings.Contains(out, "invalid syntax"), "couldn't find the invalid value warning")
  31. // -s --stars deprecated since Docker 1.13
  32. out, _, err = dockerCmdWithError("search", "-s=-1", "busybox")
  33. assert.ErrorContains(c, err, "", out)
  34. assert.Assert(c, strings.Contains(out, "invalid syntax"), "couldn't find the invalid value warning")
  35. }
  36. func (s *DockerSuite) TestSearchCmdOptions(c *check.C) {
  37. testRequires(c, Network, DaemonIsLinux)
  38. outSearchCmd, _ := dockerCmd(c, "search", "busybox")
  39. assert.Assert(c, strings.Count(outSearchCmd, "\n") > 3, outSearchCmd)
  40. outSearchCmdautomated, _ := dockerCmd(c, "search", "--filter", "is-automated=true", "busybox") //The busybox is a busybox base image, not an AUTOMATED image.
  41. outSearchCmdautomatedSlice := strings.Split(outSearchCmdautomated, "\n")
  42. for i := range outSearchCmdautomatedSlice {
  43. assert.Assert(c, !strings.HasPrefix(outSearchCmdautomatedSlice[i], "busybox "), "The busybox is not an AUTOMATED image: %s", outSearchCmdautomated)
  44. }
  45. outSearchCmdNotOfficial, _ := dockerCmd(c, "search", "--filter", "is-official=false", "busybox") //The busybox is a busybox base image, official image.
  46. outSearchCmdNotOfficialSlice := strings.Split(outSearchCmdNotOfficial, "\n")
  47. for i := range outSearchCmdNotOfficialSlice {
  48. assert.Assert(c, !strings.HasPrefix(outSearchCmdNotOfficialSlice[i], "busybox "), "The busybox is not an OFFICIAL image: %s", outSearchCmdNotOfficial)
  49. }
  50. outSearchCmdOfficial, _ := dockerCmd(c, "search", "--filter", "is-official=true", "busybox") //The busybox is a busybox base image, official image.
  51. outSearchCmdOfficialSlice := strings.Split(outSearchCmdOfficial, "\n")
  52. assert.Equal(c, len(outSearchCmdOfficialSlice), 3) // 1 header, 1 line, 1 carriage return
  53. assert.Assert(c, strings.HasPrefix(outSearchCmdOfficialSlice[1], "busybox "), "The busybox is an OFFICIAL image: %s", outSearchCmdOfficial)
  54. outSearchCmdStars, _ := dockerCmd(c, "search", "--filter", "stars=10", "busybox")
  55. assert.Assert(c, strings.Count(outSearchCmdStars, "\n") <= strings.Count(outSearchCmd, "\n"), "Number of images with 10+ stars should be less than that of all images:\noutSearchCmdStars: %s\noutSearch: %s\n", outSearchCmdStars, outSearchCmd)
  56. dockerCmd(c, "search", "--filter", "is-automated=true", "--filter", "stars=2", "--no-trunc=true", "busybox")
  57. // --automated deprecated since Docker 1.13
  58. outSearchCmdautomated1, _ := dockerCmd(c, "search", "--automated=true", "busybox") //The busybox is a busybox base image, not an AUTOMATED image.
  59. outSearchCmdautomatedSlice1 := strings.Split(outSearchCmdautomated1, "\n")
  60. for i := range outSearchCmdautomatedSlice1 {
  61. assert.Assert(c, !strings.HasPrefix(outSearchCmdautomatedSlice1[i], "busybox "), "The busybox is not an AUTOMATED image: %s", outSearchCmdautomated)
  62. }
  63. // -s --stars deprecated since Docker 1.13
  64. outSearchCmdStars1, _ := dockerCmd(c, "search", "--stars=2", "busybox")
  65. assert.Assert(c, strings.Count(outSearchCmdStars1, "[OK]") <= strings.Count(outSearchCmd, "[OK]"), "The quantity of images with stars should be less than that of all images: %s", outSearchCmdStars1)
  66. // -s --stars deprecated since Docker 1.13
  67. dockerCmd(c, "search", "--stars=2", "--automated=true", "--no-trunc=true", "busybox")
  68. }
  69. // search for repos which start with "ubuntu-" on the central registry
  70. func (s *DockerSuite) TestSearchOnCentralRegistryWithDash(c *check.C) {
  71. testRequires(c, Network, DaemonIsLinux)
  72. dockerCmd(c, "search", "ubuntu-")
  73. }
  74. // test case for #23055
  75. func (s *DockerSuite) TestSearchWithLimit(c *check.C) {
  76. testRequires(c, Network, DaemonIsLinux)
  77. limit := 10
  78. out, _, err := dockerCmdWithError("search", fmt.Sprintf("--limit=%d", limit), "docker")
  79. assert.NilError(c, err)
  80. outSlice := strings.Split(out, "\n")
  81. assert.Equal(c, len(outSlice), limit+2) // 1 header, 1 carriage return
  82. limit = 50
  83. out, _, err = dockerCmdWithError("search", fmt.Sprintf("--limit=%d", limit), "docker")
  84. assert.NilError(c, err)
  85. outSlice = strings.Split(out, "\n")
  86. assert.Equal(c, len(outSlice), limit+2) // 1 header, 1 carriage return
  87. limit = 100
  88. out, _, err = dockerCmdWithError("search", fmt.Sprintf("--limit=%d", limit), "docker")
  89. assert.NilError(c, err)
  90. outSlice = strings.Split(out, "\n")
  91. assert.Equal(c, len(outSlice), limit+2) // 1 header, 1 carriage return
  92. limit = 0
  93. _, _, err = dockerCmdWithError("search", fmt.Sprintf("--limit=%d", limit), "docker")
  94. assert.ErrorContains(c, err, "")
  95. limit = 200
  96. _, _, err = dockerCmdWithError("search", fmt.Sprintf("--limit=%d", limit), "docker")
  97. assert.ErrorContains(c, err, "")
  98. }