docker_cli_search_test.go 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. "testing"
  6. "gotest.tools/v3/assert"
  7. )
  8. type DockerCLISearchSuite struct {
  9. ds *DockerSuite
  10. }
  11. func (s *DockerCLISearchSuite) TearDownTest(c *testing.T) {
  12. s.ds.TearDownTest(c)
  13. }
  14. func (s *DockerCLISearchSuite) OnTimeout(c *testing.T) {
  15. s.ds.OnTimeout(c)
  16. }
  17. // search for repos named "registry" on the central registry
  18. func (s *DockerCLISearchSuite) TestSearchOnCentralRegistry(c *testing.T) {
  19. out, _ := dockerCmd(c, "search", "busybox")
  20. assert.Assert(c, strings.Contains(out, "Busybox base image."), "couldn't find any repository named (or containing) 'Busybox base image.'")
  21. }
  22. func (s *DockerCLISearchSuite) TestSearchStarsOptionWithWrongParameter(c *testing.T) {
  23. out, _, err := dockerCmdWithError("search", "--filter", "stars=a", "busybox")
  24. assert.ErrorContains(c, err, "", out)
  25. assert.Assert(c, strings.Contains(out, "invalid filter"), "couldn't find the invalid filter warning")
  26. out, _, err = dockerCmdWithError("search", "-f", "stars=a", "busybox")
  27. assert.ErrorContains(c, err, "", out)
  28. assert.Assert(c, strings.Contains(out, "invalid filter"), "couldn't find the invalid filter warning")
  29. out, _, err = dockerCmdWithError("search", "-f", "is-automated=a", "busybox")
  30. assert.ErrorContains(c, err, "", out)
  31. assert.Assert(c, strings.Contains(out, "invalid filter"), "couldn't find the invalid filter warning")
  32. out, _, err = dockerCmdWithError("search", "-f", "is-official=a", "busybox")
  33. assert.ErrorContains(c, err, "", out)
  34. assert.Assert(c, strings.Contains(out, "invalid filter"), "couldn't find the invalid filter warning")
  35. }
  36. func (s *DockerCLISearchSuite) TestSearchCmdOptions(c *testing.T) {
  37. outSearchCmd, _ := dockerCmd(c, "search", "busybox")
  38. assert.Assert(c, strings.Count(outSearchCmd, "\n") > 3, outSearchCmd)
  39. outSearchCmdautomated, _ := dockerCmd(c, "search", "--filter", "is-automated=true", "busybox") // The busybox is a busybox base image, not an AUTOMATED image.
  40. outSearchCmdautomatedSlice := strings.Split(outSearchCmdautomated, "\n")
  41. for i := range outSearchCmdautomatedSlice {
  42. assert.Assert(c, !strings.HasPrefix(outSearchCmdautomatedSlice[i], "busybox "), "The busybox is not an AUTOMATED image: %s", outSearchCmdautomated)
  43. }
  44. outSearchCmdNotOfficial, _ := dockerCmd(c, "search", "--filter", "is-official=false", "busybox") // The busybox is a busybox base image, official image.
  45. outSearchCmdNotOfficialSlice := strings.Split(outSearchCmdNotOfficial, "\n")
  46. for i := range outSearchCmdNotOfficialSlice {
  47. assert.Assert(c, !strings.HasPrefix(outSearchCmdNotOfficialSlice[i], "busybox "), "The busybox is not an OFFICIAL image: %s", outSearchCmdNotOfficial)
  48. }
  49. outSearchCmdOfficial, _ := dockerCmd(c, "search", "--filter", "is-official=true", "busybox") // The busybox is a busybox base image, official image.
  50. outSearchCmdOfficialSlice := strings.Split(outSearchCmdOfficial, "\n")
  51. assert.Equal(c, len(outSearchCmdOfficialSlice), 3) // 1 header, 1 line, 1 carriage return
  52. assert.Assert(c, strings.HasPrefix(outSearchCmdOfficialSlice[1], "busybox "), "The busybox is an OFFICIAL image: %s", outSearchCmdOfficial)
  53. outSearchCmdStars, _ := dockerCmd(c, "search", "--filter", "stars=10", "busybox")
  54. 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)
  55. dockerCmd(c, "search", "--filter", "is-automated=true", "--filter", "stars=2", "--no-trunc=true", "busybox")
  56. }
  57. // search for repos which start with "ubuntu-" on the central registry
  58. func (s *DockerCLISearchSuite) TestSearchOnCentralRegistryWithDash(c *testing.T) {
  59. dockerCmd(c, "search", "ubuntu-")
  60. }
  61. // test case for #23055
  62. func (s *DockerCLISearchSuite) TestSearchWithLimit(c *testing.T) {
  63. for _, limit := range []int{10, 50, 100} {
  64. out, _, err := dockerCmdWithError("search", fmt.Sprintf("--limit=%d", limit), "docker")
  65. assert.NilError(c, err)
  66. outSlice := strings.Split(out, "\n")
  67. assert.Equal(c, len(outSlice), limit+2) // 1 header, 1 carriage return
  68. }
  69. for _, limit := range []int{-1, 101} {
  70. _, _, err := dockerCmdWithError("search", fmt.Sprintf("--limit=%d", limit), "docker")
  71. assert.ErrorContains(c, err, "")
  72. }
  73. }