docker_cli_search_test.go 4.3 KB

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