github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/engine/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  // search for repos named  "registry" on the central registry
    12  func (s *DockerSuite) TestSearchOnCentralRegistry(c *testing.T) {
    13  	out, _ := dockerCmd(c, "search", "busybox")
    14  	assert.Assert(c, strings.Contains(out, "Busybox base image."), "couldn't find any repository named (or containing) 'Busybox base image.'")
    15  }
    16  
    17  func (s *DockerSuite) TestSearchStarsOptionWithWrongParameter(c *testing.T) {
    18  	out, _, err := dockerCmdWithError("search", "--filter", "stars=a", "busybox")
    19  	assert.ErrorContains(c, err, "", out)
    20  	assert.Assert(c, strings.Contains(out, "Invalid filter"), "couldn't find the invalid filter warning")
    21  
    22  	out, _, err = dockerCmdWithError("search", "-f", "stars=a", "busybox")
    23  	assert.ErrorContains(c, err, "", out)
    24  	assert.Assert(c, strings.Contains(out, "Invalid filter"), "couldn't find the invalid filter warning")
    25  
    26  	out, _, err = dockerCmdWithError("search", "-f", "is-automated=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  
    30  	out, _, err = dockerCmdWithError("search", "-f", "is-official=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  
    35  func (s *DockerSuite) TestSearchCmdOptions(c *testing.T) {
    36  	outSearchCmd, _ := dockerCmd(c, "search", "busybox")
    37  	assert.Assert(c, strings.Count(outSearchCmd, "\n") > 3, outSearchCmd)
    38  
    39  	outSearchCmdautomated, _ := dockerCmd(c, "search", "--filter", "is-automated=true", "busybox") // The busybox is a busybox base image, not an AUTOMATED image.
    40  	outSearchCmdautomatedSlice := strings.Split(outSearchCmdautomated, "\n")
    41  	for i := range outSearchCmdautomatedSlice {
    42  		assert.Assert(c, !strings.HasPrefix(outSearchCmdautomatedSlice[i], "busybox "), "The busybox is not an AUTOMATED image: %s", outSearchCmdautomated)
    43  	}
    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  
    51  	outSearchCmdOfficial, _ := dockerCmd(c, "search", "--filter", "is-official=true", "busybox") // 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  
    56  	outSearchCmdStars, _ := dockerCmd(c, "search", "--filter", "stars=10", "busybox")
    57  	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)
    58  
    59  	dockerCmd(c, "search", "--filter", "is-automated=true", "--filter", "stars=2", "--no-trunc=true", "busybox")
    60  }
    61  
    62  // search for repos which start with "ubuntu-" on the central registry
    63  func (s *DockerSuite) TestSearchOnCentralRegistryWithDash(c *testing.T) {
    64  	dockerCmd(c, "search", "ubuntu-")
    65  }
    66  
    67  // test case for #23055
    68  func (s *DockerSuite) TestSearchWithLimit(c *testing.T) {
    69  	for _, limit := range []int{10, 50, 100} {
    70  		out, _, err := dockerCmdWithError("search", fmt.Sprintf("--limit=%d", limit), "docker")
    71  		assert.NilError(c, err)
    72  		outSlice := strings.Split(out, "\n")
    73  		assert.Equal(c, len(outSlice), limit+2) // 1 header, 1 carriage return
    74  	}
    75  
    76  	for _, limit := range []int{-1, 0, 101} {
    77  		_, _, err := dockerCmdWithError("search", fmt.Sprintf("--limit=%d", limit), "docker")
    78  		assert.ErrorContains(c, err, "")
    79  	}
    80  }