File: search_test.go

package info (click to toggle)
podman 5.7.0%2Bds2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 23,824 kB
  • sloc: sh: 4,700; python: 2,798; perl: 1,885; ansic: 1,484; makefile: 977; ruby: 42; csh: 8
file content (323 lines) | stat: -rw-r--r-- 13,375 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
//go:build linux || freebsd

package integration

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
	"os"
	"strconv"
	"text/template"

	"github.com/containers/podman/v5/pkg/domain/entities"
	. "github.com/containers/podman/v5/test/utils"
	. "github.com/onsi/ginkgo/v2"
	. "github.com/onsi/gomega"
)

type endpoint struct {
	Host string
	Port string
}

func (e *endpoint) Address() string {
	return fmt.Sprintf("%s:%s", e.Host, e.Port)
}

var _ = Describe("Podman search", func() {

	const regFileContents = `
[registries.search]
registries = ['{{.Host}}:{{.Port}}']

[registries.insecure]
registries = ['{{.Host}}:{{.Port}}']`
	registryFileTmpl := template.Must(template.New("registryFile").Parse(regFileContents))

	const badRegFileContents = `
[registries.search]
registries = ['{{.Host}}:{{.Port}}']
# empty
[registries.insecure]
registries = []`
	registryFileBadTmpl := template.Must(template.New("registryFileBad").Parse(badRegFileContents))

	mockFakeRegistryServerAsContainer := func(name string) endpoint {
		if podmanTest.Host.Arch == "ppc64le" {
			Skip("No registry image for ppc64le")
		}
		port := GetPort()
		fakereg := podmanTest.Podman([]string{"run", "-d", "--name", name,
			"-p", fmt.Sprintf("%d:5000", port),
			REGISTRY_IMAGE, "/entrypoint.sh", "/etc/docker/registry/config.yml"})
		fakereg.WaitWithDefaultTimeout()
		Expect(fakereg).Should(ExitCleanly())

		if !WaitContainerReady(podmanTest, name, "listening on", 20, 1) {
			Fail("Cannot start docker registry on port %s", port)
		}
		ep := endpoint{Port: strconv.Itoa(port), Host: "localhost"}
		return ep
	}

	pushAlpineImageIntoMockRegistry := func(ep endpoint) string {
		err = podmanTest.RestoreArtifact(ALPINE)
		Expect(err).ToNot(HaveOccurred())
		image := fmt.Sprintf("%s/my-alpine", ep.Address())
		podmanTest.PodmanExitCleanly("push", "-q", "--tls-verify=false", "--remove-signatures", ALPINE, image)
		return image
	}

	Context("podman search with mock registry", func() {
		var registryAddress string
		var srv *http.Server
		var serverErr chan error

		BeforeEach(func() {
			registryAddress, srv, serverErr = CreateMockRegistryServer()
		})

		AfterEach(func() {
			CloseMockRegistryServer(srv, serverErr)
		})

		It("podman search", func() {
			search := podmanTest.PodmanExitCleanly("search", "--tls-verify=false", registryAddress+"/alpine")
			Expect(len(search.OutputToStringArray())).To(BeNumerically(">", 1))
			Expect(search.OutputToString()).To(ContainSubstring("alpine"))
		})

		It("podman search single registry flag", func() {
			search := podmanTest.PodmanExitCleanly("search", "--tls-verify=false", registryAddress+"/skopeo/stable:latest")
			Expect(search.OutputToString()).To(ContainSubstring(registryAddress + "/skopeo/stable"))
		})

		It("podman search image with description", func() {
			search := podmanTest.PodmanExitCleanly("search", "--tls-verify=false", registryAddress+"/podman/stable")
			output := string(search.Out.Contents())
			Expect(output).To(MatchRegexp(`(?m)NAME\s+DESCRIPTION$`))
			Expect(output).To(MatchRegexp(`(?m)/podman/stable\s+.*Podman Image`))
		})

		It("podman search image with --compatible", func() {
			search := podmanTest.PodmanExitCleanly("search", "--compatible", "--tls-verify=false", registryAddress+"/podman/stable")
			output := string(search.Out.Contents())
			Expect(output).To(MatchRegexp(`(?m)NAME\s+DESCRIPTION\s+STARS\s+OFFICIAL\s+AUTOMATED$`))
		})

		It("podman search format flag", func() {
			search := podmanTest.PodmanExitCleanly("search", "--format", "table {{.Index}} {{.Name}}", "--tls-verify=false", registryAddress+"/testdigest_v2s2")
			Expect(len(search.OutputToStringArray())).To(BeNumerically(">", 1))
			Expect(search.OutputToString()).To(ContainSubstring(registryAddress + "/libpod/testdigest_v2s2"))
		})

		It("podman search format json", func() {
			search := podmanTest.PodmanExitCleanly("search", "--format", "json", "--tls-verify=false", registryAddress+"/testdigest_v2s1")
			Expect(search.OutputToString()).To(BeValidJSON())
			Expect(search.OutputToString()).To(ContainSubstring(registryAddress + "/libpod/testdigest_v2s1"))
			Expect(search.OutputToString()).To(ContainSubstring("Test image used by buildah regression tests"))

			// Test for https://github.com/containers/podman/issues/11894
			contents := make([]entities.ImageSearchReport, 0)
			err := json.Unmarshal(search.Out.Contents(), &contents)
			Expect(err).ToNot(HaveOccurred())
			Expect(contents).ToNot(BeEmpty(), "No results from image search")
			for _, element := range contents {
				Expect(element.Description).ToNot(HaveSuffix("..."))
			}
		})

		It("podman search format json list tags", func() {
			search := podmanTest.PodmanExitCleanly("search", "--list-tags", "--format", "json", "--tls-verify=false", registryAddress+"/libpod/alpine:latest")
			Expect(search.OutputToString()).To(BeValidJSON())
			Expect(search.OutputToString()).To(ContainSubstring(registryAddress + "/libpod/alpine"))
			Expect(search.OutputToString()).To(ContainSubstring("3.10.2"))
			Expect(search.OutputToString()).To(ContainSubstring("3.2"))
		})

		// Test for https://github.com/containers/podman/issues/11894
		It("podman search no-trunc=false flag", func() {
			search := podmanTest.PodmanExitCleanly("search", "--no-trunc=false", "--tls-verify=false", registryAddress+"/alpine", "--format={{.Description}}")

			for _, line := range search.OutputToStringArray() {
				if len(line) > 44 {
					Expect(line).To(HaveSuffix("..."), line+" should have been truncated")
				}
			}
		})

		It("podman search limit flag", func() {
			search := podmanTest.PodmanExitCleanly("search", "--tls-verify=false", registryAddress+"/alpine")
			Expect(len(search.OutputToStringArray())).To(BeNumerically(">", 10))

			search = podmanTest.PodmanExitCleanly("search", "--limit", "3", "--tls-verify=false", registryAddress+"/alpine")
			search.WaitWithDefaultTimeout()
			Expect(search).Should(ExitCleanly())
			Expect(search.OutputToStringArray()).To(HaveLen(4))

			search = podmanTest.PodmanExitCleanly("search", "--limit", "10", "--tls-verify=false", registryAddress+"/alpine")
			Expect(search.OutputToStringArray()).To(HaveLen(11))
		})

		It("podman search with filter stars", func() {
			search := podmanTest.PodmanExitCleanly("search", "--filter", "stars=10", "--format", "{{.Stars}}", "--tls-verify=false", registryAddress+"/alpine")
			output := search.OutputToStringArray()
			for i := range output {
				Expect(strconv.Atoi(output[i])).To(BeNumerically(">=", 10))
			}
		})

		It("podman search with filter is-official", func() {
			search := podmanTest.PodmanExitCleanly("search", "--filter", "is-official", "--format", "{{.Official}}", "--tls-verify=false", registryAddress+"/alpine")
			output := search.OutputToStringArray()
			for i := range output {
				Expect(output[i]).To(Equal("[OK]"))
			}
		})

		It("podman search with filter is-automated", func() {
			search := podmanTest.PodmanExitCleanly("search", "--filter", "is-automated=false", "--format", "{{.Automated}}", "--tls-verify=false", registryAddress+"/alpine")
			output := search.OutputToStringArray()
			for i := range output {
				Expect(output[i]).To(Equal(""))
			}
		})

		It("podman search format list tags with custom", func() {
			search := podmanTest.PodmanExitCleanly("search", "--list-tags", "--format", "{{.Name}}", "--limit", "1", "--tls-verify=false", registryAddress+"/libpod/alpine")
			Expect(search.OutputToString()).To(Equal(registryAddress + "/libpod/alpine"))
		})

		It("podman search with wildcards", func() {
			search := podmanTest.PodmanExitCleanly("search", "--tls-verify=false", registryAddress+"/*alpine*")
			Expect(len(search.OutputToStringArray())).To(BeNumerically(">", 1))
			Expect(search.OutputToString()).To(ContainSubstring("alpine"))
		})

		It("podman search repository tags", func() {
			search := podmanTest.PodmanExitCleanly("search", "--list-tags", "--limit", "30", "--tls-verify=false", registryAddress+"/podman/stable")
			Expect(search.OutputToStringArray()).To(HaveLen(31))

			search = podmanTest.PodmanExitCleanly("search", "--list-tags", "--tls-verify=false", registryAddress+"/podman/stable")
			Expect(len(search.OutputToStringArray())).To(BeNumerically(">", 2))

			search = podmanTest.Podman([]string{"search", "--filter=is-official", "--list-tags", "--tls-verify=false", registryAddress + "/podman/stable"})
			search.WaitWithDefaultTimeout()
			Expect(search).To(ExitWithError(125, "filters are not applicable to list tags result"))

			// With trailing slash
			search = podmanTest.Podman([]string{"search", "--list-tags", "--tls-verify=false", registryAddress + "/podman/"})
			search.WaitWithDefaultTimeout()
			Expect(search).To(ExitWithError(125, `reference "podman/" must be a docker reference`))
			Expect(search.OutputToStringArray()).To(BeEmpty())

			// No trailing slash
			search = podmanTest.Podman([]string{"search", "--list-tags", "--tls-verify=false", registryAddress + "/podman"})
			search.WaitWithDefaultTimeout()
			Expect(search).To(ExitWithError(125, "getting repository tags: fetching tags list: StatusCode: 404"))
			Expect(search.OutputToStringArray()).To(BeEmpty())
		})

		It("podman search with limit over 100", func() {
			search := podmanTest.PodmanExitCleanly("search", "--limit", "100", "--tls-verify=false", registryAddress+"/podman")
			Expect(len(search.OutputToStringArray())).To(BeNumerically("<=", 101))
		})

	})

	Context("podman search with container-based registries", func() {
		var ep endpoint
		var image string
		var registryName string
		var port int64

		setupRegistryConfig := func(ep endpoint, registryName string, template *template.Template) {
			var buffer bytes.Buffer
			err := template.Execute(&buffer, ep)
			Expect(err).ToNot(HaveOccurred())
			podmanTest.setRegistriesConfigEnv(buffer.Bytes())
			err = os.WriteFile(fmt.Sprintf("%s/%s.conf", tempdir, registryName), buffer.Bytes(), 0o644)
			Expect(err).ToNot(HaveOccurred())
		}

		BeforeEach(func() {
			registryName = fmt.Sprintf("registry%d", GinkgoRandomSeed())
			ep = mockFakeRegistryServerAsContainer(registryName)
			image = pushAlpineImageIntoMockRegistry(ep)

			port, err = strconv.ParseInt(ep.Port, 10, 64)
			Expect(err).ToNot(HaveOccurred())
		})

		AfterEach(func() {
			resetRegistriesConfigEnv()
			podmanTest.PodmanExitCleanly("rm", "-f", registryName)
		})

		It("podman search attempts HTTP if tls-verify flag is set false", func() {
			// if this test succeeded, there will be no output (there is no entry named fake/image:andtag in an empty registry)
			// and the exit code will be 0
			search := podmanTest.PodmanExitCleanly("search", fmt.Sprintf("%s/fake/image:andtag", ep.Address()), "--tls-verify=false")
			Expect(search.OutputToString()).Should(BeEmpty())
		})

		It("podman search in local registry", func() {
			search := podmanTest.PodmanExitCleanly("search", image, "--tls-verify=false")
			Expect(search.OutputToString()).ShouldNot(BeEmpty())

			// podman search v2 registry with empty query
			searchEmpty := podmanTest.PodmanExitCleanly("search", fmt.Sprintf("%s/", ep.Address()), "--tls-verify=false")
			Expect(searchEmpty.OutputToStringArray()).ToNot(BeEmpty())
			Expect(search.OutputToString()).To(ContainSubstring("my-alpine"))
		})

		It("podman search attempts HTTP if registry is in registries.insecure and force secure is false", func() {
			// registries.conf set up
			setupRegistryConfig(ep, registryName, registryFileTmpl)
			if IsRemote() {
				podmanTest.RestartRemoteService()
				defer podmanTest.RestartRemoteService()
			}

			search := podmanTest.PodmanExitCleanly("search", image)
			Expect(search.OutputToString()).To(ContainSubstring("my-alpine"))
		})

		It("podman search doesn't attempt HTTP if force secure is true", func() {
			setupRegistryConfig(ep, registryName, registryFileTmpl)
			if IsRemote() {
				podmanTest.RestartRemoteService()
				defer podmanTest.RestartRemoteService()
			}

			search := podmanTest.Podman([]string{"search", image, "--tls-verify=true"})
			search.WaitWithDefaultTimeout()

			Expect(search).Should(ExitWithError(125, fmt.Sprintf(`couldn't search registry "localhost:%d": pinging container registry localhost:%d: Get "https://localhost:%d/v2/": http: server gave HTTP response to HTTPS client`, port, port, port)))
			Expect(search.OutputToString()).Should(BeEmpty())
		})

		It("podman search doesn't attempt HTTP if registry is not listed as insecure", func() {
			setupRegistryConfig(ep, registryName, registryFileBadTmpl)
			if IsRemote() {
				podmanTest.RestartRemoteService()
				defer podmanTest.RestartRemoteService()
			}

			search := podmanTest.Podman([]string{"search", image})
			search.WaitWithDefaultTimeout()

			Expect(search).Should(ExitWithError(125, fmt.Sprintf(`couldn't search registry "localhost:%d": pinging container registry localhost:%d: Get "https://localhost:%d/v2/": http: server gave HTTP response to HTTPS client`, port, port, port)))
			Expect(search.OutputToString()).Should(BeEmpty())
		})
	})

	// search should fail with nonexistent authfile
	It("podman search fail with nonexistent --authfile", func() {
		search := podmanTest.Podman([]string{"search", "--authfile", "/tmp/nonexistent", ALPINE})
		search.WaitWithDefaultTimeout()
		Expect(search).To(ExitWithError(125, "credential file is not accessible: faccessat /tmp/nonexistent: no such file or directory"))
	})
})