docker_cli_search_test.go 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. )
  10. type DockerCLISearchSuite struct {
  11. ds *DockerSuite
  12. }
  13. func (s *DockerCLISearchSuite) TearDownTest(ctx context.Context, c *testing.T) {
  14. s.ds.TearDownTest(ctx, c)
  15. }
  16. func (s *DockerCLISearchSuite) OnTimeout(c *testing.T) {
  17. s.ds.OnTimeout(c)
  18. }
  19. // search for repos named "registry" on the central registry
  20. func (s *DockerCLISearchSuite) TestSearchOnCentralRegistry(c *testing.T) {
  21. out := cli.DockerCmd(c, "search", "busybox").Stdout()
  22. assert.Assert(c, strings.Contains(out, "Busybox base image."), "couldn't find any repository named (or containing) 'Busybox base image.'")
  23. }
  24. func (s *DockerCLISearchSuite) TestSearchStarsOptionWithWrongParameter(c *testing.T) {
  25. out, _, err := dockerCmdWithError("search", "--filter", "stars=a", "busybox")
  26. assert.ErrorContains(c, err, "", out)
  27. assert.Assert(c, strings.Contains(out, "invalid filter"), "couldn't find the invalid filter warning")
  28. out, _, err = dockerCmdWithError("search", "-f", "stars=a", "busybox")
  29. assert.ErrorContains(c, err, "", out)
  30. assert.Assert(c, strings.Contains(out, "invalid filter"), "couldn't find the invalid filter warning")
  31. out, _, err = dockerCmdWithError("search", "-f", "is-automated=a", "busybox")
  32. assert.ErrorContains(c, err, "", out)
  33. assert.Assert(c, strings.Contains(out, "invalid filter"), "couldn't find the invalid filter warning")
  34. out, _, err = dockerCmdWithError("search", "-f", "is-official=a", "busybox")
  35. assert.ErrorContains(c, err, "", out)
  36. assert.Assert(c, strings.Contains(out, "invalid filter"), "couldn't find the invalid filter warning")
  37. }
  38. func (s *DockerCLISearchSuite) TestSearchCmdOptions(c *testing.T) {
  39. outSearchCmd := cli.DockerCmd(c, "search", "busybox").Combined()
  40. assert.Assert(c, strings.Count(outSearchCmd, "\n") > 3, outSearchCmd)
  41. outSearchCmdautomated := cli.DockerCmd(c, "search", "--filter", "is-automated=true", "busybox").Combined() // The busybox is a busybox base image, not an AUTOMATED image.
  42. outSearchCmdautomatedSlice := strings.Split(outSearchCmdautomated, "\n")
  43. for i := range outSearchCmdautomatedSlice {
  44. assert.Assert(c, !strings.HasPrefix(outSearchCmdautomatedSlice[i], "busybox "), "The busybox is not an AUTOMATED image: %s", outSearchCmdautomated)
  45. }
  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. }