github.com/adityamillind98/moby@v23.0.0-rc.4+incompatible/integration-cli/docker_cli_search_test.go (about)

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