docker_cli_search_test.go 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. }
  28. func (s *DockerSuite) TestSearchCmdOptions(c *check.C) {
  29. testRequires(c, Network, DaemonIsLinux)
  30. outSearchCmd, _ := dockerCmd(c, "search", "busybox")
  31. assert.Assert(c, strings.Count(outSearchCmd, "\n") > 3, outSearchCmd)
  32. outSearchCmdautomated, _ := dockerCmd(c, "search", "--filter", "is-automated=true", "busybox") //The busybox is a busybox base image, not an AUTOMATED image.
  33. outSearchCmdautomatedSlice := strings.Split(outSearchCmdautomated, "\n")
  34. for i := range outSearchCmdautomatedSlice {
  35. assert.Assert(c, !strings.HasPrefix(outSearchCmdautomatedSlice[i], "busybox "), "The busybox is not an AUTOMATED image: %s", outSearchCmdautomated)
  36. }
  37. outSearchCmdNotOfficial, _ := dockerCmd(c, "search", "--filter", "is-official=false", "busybox") //The busybox is a busybox base image, official image.
  38. outSearchCmdNotOfficialSlice := strings.Split(outSearchCmdNotOfficial, "\n")
  39. for i := range outSearchCmdNotOfficialSlice {
  40. assert.Assert(c, !strings.HasPrefix(outSearchCmdNotOfficialSlice[i], "busybox "), "The busybox is not an OFFICIAL image: %s", outSearchCmdNotOfficial)
  41. }
  42. outSearchCmdOfficial, _ := dockerCmd(c, "search", "--filter", "is-official=true", "busybox") //The busybox is a busybox base image, official image.
  43. outSearchCmdOfficialSlice := strings.Split(outSearchCmdOfficial, "\n")
  44. assert.Equal(c, len(outSearchCmdOfficialSlice), 3) // 1 header, 1 line, 1 carriage return
  45. assert.Assert(c, strings.HasPrefix(outSearchCmdOfficialSlice[1], "busybox "), "The busybox is an OFFICIAL image: %s", outSearchCmdOfficial)
  46. outSearchCmdStars, _ := dockerCmd(c, "search", "--filter", "stars=10", "busybox")
  47. 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)
  48. dockerCmd(c, "search", "--filter", "is-automated=true", "--filter", "stars=2", "--no-trunc=true", "busybox")
  49. }
  50. // search for repos which start with "ubuntu-" on the central registry
  51. func (s *DockerSuite) TestSearchOnCentralRegistryWithDash(c *check.C) {
  52. testRequires(c, Network, DaemonIsLinux)
  53. dockerCmd(c, "search", "ubuntu-")
  54. }
  55. // test case for #23055
  56. func (s *DockerSuite) TestSearchWithLimit(c *check.C) {
  57. testRequires(c, Network, DaemonIsLinux)
  58. for _, limit := range []int{10, 50, 100} {
  59. out, _, err := dockerCmdWithError("search", fmt.Sprintf("--limit=%d", limit), "docker")
  60. assert.NilError(c, err)
  61. outSlice := strings.Split(out, "\n")
  62. assert.Equal(c, len(outSlice), limit+2) // 1 header, 1 carriage return
  63. }
  64. for _, limit := range []int{-1, 0, 101} {
  65. _, _, err := dockerCmdWithError("search", fmt.Sprintf("--limit=%d", limit), "docker")
  66. assert.ErrorContains(c, err, "")
  67. }
  68. }