File: docker_cli_search_test.go

package info (click to toggle)
docker.io 26.1.5%2Bdfsg1-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 68,576 kB
  • sloc: sh: 5,748; makefile: 912; ansic: 664; asm: 228; python: 162
file content (95 lines) | stat: -rw-r--r-- 4,399 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main

import (
	"context"
	"fmt"
	"strings"
	"testing"

	"github.com/docker/docker/integration-cli/cli"
	"gotest.tools/v3/assert"
	is "gotest.tools/v3/assert/cmp"
)

type DockerCLISearchSuite struct {
	ds *DockerSuite
}

func (s *DockerCLISearchSuite) TearDownTest(ctx context.Context, c *testing.T) {
	s.ds.TearDownTest(ctx, c)
}

func (s *DockerCLISearchSuite) OnTimeout(c *testing.T) {
	s.ds.OnTimeout(c)
}

// search for repos named  "registry" on the central registry
func (s *DockerCLISearchSuite) TestSearchOnCentralRegistry(c *testing.T) {
	out := cli.DockerCmd(c, "search", "busybox").Stdout()
	assert.Assert(c, strings.Contains(out, "Busybox base image."), "couldn't find any repository named (or containing) 'Busybox base image.'")
}

func (s *DockerCLISearchSuite) TestSearchStarsOptionWithWrongParameter(c *testing.T) {
	out, _, err := dockerCmdWithError("search", "--filter", "stars=a", "busybox")
	assert.ErrorContains(c, err, "", out)
	assert.Assert(c, strings.Contains(out, "invalid filter"), "couldn't find the invalid filter warning")

	out, _, err = dockerCmdWithError("search", "-f", "stars=a", "busybox")
	assert.ErrorContains(c, err, "", out)
	assert.Assert(c, strings.Contains(out, "invalid filter"), "couldn't find the invalid filter warning")

	out, _, err = dockerCmdWithError("search", "-f", "is-automated=a", "busybox")
	assert.ErrorContains(c, err, "", out)
	assert.Assert(c, strings.Contains(out, "invalid filter"), "couldn't find the invalid filter warning")

	out, _, err = dockerCmdWithError("search", "-f", "is-official=a", "busybox")
	assert.ErrorContains(c, err, "", out)
	assert.Assert(c, strings.Contains(out, "invalid filter"), "couldn't find the invalid filter warning")
}

func (s *DockerCLISearchSuite) TestSearchCmdOptions(c *testing.T) {
	outSearchCmd := cli.DockerCmd(c, "search", "busybox").Combined()
	assert.Assert(c, strings.Count(outSearchCmd, "\n") > 3, outSearchCmd)

	outSearchCmdautomated := cli.DockerCmd(c, "search", "--filter", "is-automated=true", "busybox").Combined() // The busybox is a busybox base image, not an AUTOMATED image.
	outSearchCmdautomatedSlice := strings.Split(outSearchCmdautomated, "\n")

	// is-automated=true should produce no results (only a header)
	assert.Check(c, is.Len(outSearchCmdautomatedSlice, 2))

	outSearchCmdNotOfficial := cli.DockerCmd(c, "search", "--filter", "is-official=false", "busybox").Combined() // The busybox is a busybox base image, official image.
	outSearchCmdNotOfficialSlice := strings.Split(outSearchCmdNotOfficial, "\n")
	for i := range outSearchCmdNotOfficialSlice {
		assert.Assert(c, !strings.HasPrefix(outSearchCmdNotOfficialSlice[i], "busybox "), "The busybox is not an OFFICIAL image: %s", outSearchCmdNotOfficial)
	}

	outSearchCmdOfficial := cli.DockerCmd(c, "search", "--filter", "is-official=true", "busybox").Combined() // The busybox is a busybox base image, official image.
	outSearchCmdOfficialSlice := strings.Split(outSearchCmdOfficial, "\n")
	assert.Equal(c, len(outSearchCmdOfficialSlice), 3) // 1 header, 1 line, 1 carriage return
	assert.Assert(c, strings.HasPrefix(outSearchCmdOfficialSlice[1], "busybox "), "The busybox is an OFFICIAL image: %s", outSearchCmdOfficial)

	outSearchCmdStars := cli.DockerCmd(c, "search", "--filter", "stars=10", "busybox").Combined()
	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)

	cli.DockerCmd(c, "search", "--filter", "is-automated=true", "--filter", "stars=2", "--no-trunc=true", "busybox")
}

// search for repos which start with "ubuntu-" on the central registry
func (s *DockerCLISearchSuite) TestSearchOnCentralRegistryWithDash(c *testing.T) {
	cli.DockerCmd(c, "search", "ubuntu-")
}

// test case for #23055
func (s *DockerCLISearchSuite) TestSearchWithLimit(c *testing.T) {
	for _, limit := range []int{10, 50, 100} {
		out, _, err := dockerCmdWithError("search", fmt.Sprintf("--limit=%d", limit), "docker")
		assert.NilError(c, err)
		outSlice := strings.Split(out, "\n")
		assert.Equal(c, len(outSlice), limit+2) // 1 header, 1 carriage return
	}

	for _, limit := range []int{-1, 101} {
		_, _, err := dockerCmdWithError("search", fmt.Sprintf("--limit=%d", limit), "docker")
		assert.ErrorContains(c, err, "")
	}
}