docker_cli_search_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "testing"
  7. "gotest.tools/v3/assert"
  8. )
  9. type DockerCLISearchSuite struct {
  10. ds *DockerSuite
  11. }
  12. func (s *DockerCLISearchSuite) TearDownTest(ctx context.Context, c *testing.T) {
  13. s.ds.TearDownTest(ctx, c)
  14. }
  15. func (s *DockerCLISearchSuite) OnTimeout(c *testing.T) {
  16. s.ds.OnTimeout(c)
  17. }
  18. // search for repos named "registry" on the central registry
  19. func (s *DockerCLISearchSuite) TestSearchOnCentralRegistry(c *testing.T) {
  20. out, _ := dockerCmd(c, "search", "busybox")
  21. assert.Assert(c, strings.Contains(out, "Busybox base image."), "couldn't find any repository named (or containing) 'Busybox base image.'")
  22. }
  23. func (s *DockerCLISearchSuite) TestSearchStarsOptionWithWrongParameter(c *testing.T) {
  24. out, _, err := dockerCmdWithError("search", "--filter", "stars=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. out, _, err = dockerCmdWithError("search", "-f", "stars=a", "busybox")
  28. assert.ErrorContains(c, err, "", out)
  29. assert.Assert(c, strings.Contains(out, "invalid filter"), "couldn't find the invalid filter warning")
  30. out, _, err = dockerCmdWithError("search", "-f", "is-automated=a", "busybox")
  31. assert.ErrorContains(c, err, "", out)
  32. assert.Assert(c, strings.Contains(out, "invalid filter"), "couldn't find the invalid filter warning")
  33. out, _, err = dockerCmdWithError("search", "-f", "is-official=a", "busybox")
  34. assert.ErrorContains(c, err, "", out)
  35. assert.Assert(c, strings.Contains(out, "invalid filter"), "couldn't find the invalid filter warning")
  36. }
  37. func (s *DockerCLISearchSuite) TestSearchCmdOptions(c *testing.T) {
  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. }
  58. // search for repos which start with "ubuntu-" on the central registry
  59. func (s *DockerCLISearchSuite) TestSearchOnCentralRegistryWithDash(c *testing.T) {
  60. dockerCmd(c, "search", "ubuntu-")
  61. }
  62. // test case for #23055
  63. func (s *DockerCLISearchSuite) TestSearchWithLimit(c *testing.T) {
  64. for _, limit := range []int{10, 50, 100} {
  65. out, _, err := dockerCmdWithError("search", fmt.Sprintf("--limit=%d", limit), "docker")
  66. assert.NilError(c, err)
  67. outSlice := strings.Split(out, "\n")
  68. assert.Equal(c, len(outSlice), limit+2) // 1 header, 1 carriage return
  69. }
  70. for _, limit := range []int{-1, 101} {
  71. _, _, err := dockerCmdWithError("search", fmt.Sprintf("--limit=%d", limit), "docker")
  72. assert.ErrorContains(c, err, "")
  73. }
  74. }