github.com/moby/docker@v26.1.3+incompatible/integration-cli/docker_cli_search_test.go (about)

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