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