GitLabgitlab-org
cli
main
1//go:build !integration
3package docker
5import (
6 "errors"
7 "os"
8 "path/filepath"
9 "runtime"
10 "testing"
12 "github.com/stretchr/testify/assert"
13 "github.com/stretchr/testify/require"
15 "gitlab.com/gitlab-org/cli/internal/config"
16 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
17)
19// failingConfig wraps a config.Config and makes reads of failKey return err,
20// standing in for the failures GetWithSource surfaces in production: a
21// structurally broken host entry, or a keyring that is locked or denies
22// access. When failHost is set, only that host's reads fail.
23type failingConfig struct {
24 config.Config
25 failHost string
26 failKey string
27 err error
28}
30func (c failingConfig) GetWithSource(hostname, key string, searchENVVars bool) (string, string, error) {
31 if key == c.failKey && (c.failHost == "" || hostname == c.failHost) {
32 return "", "", c.err
33 }
34 return c.Config.GetWithSource(hostname, key, searchENVVars)
35}
37// sandboxDocker points PATH at a directory holding a fake glab binary, and HOME
38// and $DOCKER_CONFIG at scratch directories, so the shim install and the
39// config.json write both land inside the test's sandbox. It returns the
40// $DOCKER_CONFIG directory.
41func sandboxDocker(t *testing.T) string {
42 t.Helper()
44 binDir := t.TempDir()
45 name := "glab"
46 if runtime.GOOS == "windows" {
47 name = "glab.exe"
48 }
49 require.NoError(t, os.WriteFile(filepath.Join(binDir, name), []byte("#!/bin/sh\n"), 0o755))
51 home := t.TempDir()
52 dockerDir := t.TempDir()
53 t.Setenv("PATH", binDir)
54 t.Setenv("HOME", home)
55 t.Setenv("USERPROFILE", home)
56 t.Setenv("DOCKER_CONFIG", dockerDir)
58 return dockerDir
59}
61// sandboxPackagedGlab is sandboxDocker for a glab the user cannot write next
62// to, as the .deb and .rpm packages install it: PATH holds only the
63// root-owned directory glab sits in, plus the user's own ~/.local/bin. It
64// returns the $DOCKER_CONFIG directory, the directory glab sits in, and that
65// ~/.local/bin path.
66func sandboxPackagedGlab(t *testing.T) (string, string, string) {
67 t.Helper()
69 if runtime.GOOS == "windows" {
70 t.Skip("POSIX directory modes not enforced on Windows")
71 }
72 if os.Geteuid() == 0 {
73 t.Skip("root bypasses the directory mode this test relies on")
74 }
76 glabDir := t.TempDir()
77 require.NoError(t, os.WriteFile(filepath.Join(glabDir, "glab"), []byte("#!/bin/sh\n"), 0o755))
78 require.NoError(t, os.Chmod(glabDir, 0o555))
79 t.Cleanup(func() { _ = os.Chmod(glabDir, 0o755) })
81 home := t.TempDir()
82 localBin := filepath.Join(home, ".local", "bin")
83 require.NoError(t, os.MkdirAll(localBin, 0o755))
85 dockerDir := t.TempDir()
86 t.Setenv("PATH", glabDir+string(os.PathListSeparator)+localBin)
87 t.Setenv("HOME", home)
88 t.Setenv("USERPROFILE", home)
89 t.Setenv("DOCKER_CONFIG", dockerDir)
91 return dockerDir, glabDir, localBin
92}
94func TestConfigureDocker_RegistersConfiguredDomains(t *testing.T) {
95 dockerDir := sandboxDocker(t)
97 cfg := config.NewFromString(`
98---
99hosts:
100 gitlab.example.com:
101 token: token1
102 container_registry_domains: registry.gitlab.example.com
103`)
104 ios, _, out, _ := cmdtest.TestIOStreams()
106 require.NoError(t, configureDocker(ios, cfg))
107 assert.Contains(t, out.String(), "registry.gitlab.example.com")
109 data, err := os.ReadFile(filepath.Join(dockerDir, "config.json"))
110 require.NoError(t, err)
111 assert.Contains(t, string(data), "registry.gitlab.example.com")
112}
114// TestConfigureDocker_WarnsWhenTheShimIsNotOnPath covers the last-resort
115// install: with nothing on PATH writable the shim goes to ~/.local/bin, where
116// Docker cannot resolve it, so the command has to say so rather than report a
117// success that does not work.
118func TestConfigureDocker_WarnsWhenTheShimIsNotOnPath(t *testing.T) {
119 _, glabDir, localBin := sandboxPackagedGlab(t)
120 // Drop ~/.local/bin back off PATH, leaving only the unwritable directory
121 // glab sits in.
122 t.Setenv("PATH", glabDir)
124 cfg := config.NewFromString(`
125---
126hosts:
127 gitlab.example.com:
128 token: token1
129 container_registry_domains: registry.gitlab.example.com
130`)
131 ios, _, _, errOut := cmdtest.TestIOStreams()
133 require.NoError(t, configureDocker(ios, cfg))
134 assert.FileExists(t, filepath.Join(localBin, "docker-credential-glab"))
135 assert.Contains(t, errOut.String(), localBin)
136 assert.Contains(t, errOut.String(), "not on your PATH")
137}
139// TestConfigureDocker_ReportsUnreadableDomains covers the message a user gets
140// when their container_registry_domains cannot be read. Telling them to go
141// configure a domain would misdirect: the domain is configured, it just could
142// not be read.
143func TestConfigureDocker_ReportsUnreadableDomains(t *testing.T) {
144 sandboxDocker(t)
146 cfg := failingConfig{
147 Config: config.NewFromString(`
148---
149hosts:
150 gitlab.example.com:
151 token: token1
152 container_registry_domains: registry.gitlab.example.com
153`),
154 failKey: "container_registry_domains",
155 err: errors.New("keyring is locked"),
156 }
157 ios, _, _, errOut := cmdtest.TestIOStreams()
159 err := configureDocker(ios, cfg)
160 require.ErrorContains(t, err, "keyring is locked")
161 assert.NotContains(t, err.Error(), "no hosts were configured")
162 assert.Contains(t, errOut.String(), "Skipped gitlab.example.com")
163}
165// TestConfigureDocker_KeepsGoingPastAnUnreadableHost pins that one broken host
166// does not stop the others from being configured, even though the overall
167// command still reports the read failure via a non-zero exit.
168func TestConfigureDocker_KeepsGoingPastAnUnreadableHost(t *testing.T) {
169 sandboxDocker(t)
171 cfg := failingConfig{
172 Config: config.NewFromString(`
173---
174hosts:
175 broken.example.com:
176 token: token1
177 gdk.example.com:
178 token: token2
179 container_registry_domains: registry.gdk.example.com
180`),
181 failHost: "broken.example.com",
182 failKey: "container_registry_domains",
183 err: errors.New("keyring is locked"),
184 }
185 ios, _, out, errOut := cmdtest.TestIOStreams()
187 err := configureDocker(ios, cfg)
188 require.ErrorContains(t, err, "keyring is locked")
189 assert.Contains(t, errOut.String(), "Skipped broken.example.com")
190 assert.Contains(t, out.String(), "registry.gdk.example.com")
191}
193// TestConfigureDocker_IgnoresTrailingCommaInDomains reproduces a trailing
194// comma in container_registry_domains: parseDomains must drop the empty
195// segment it produces, or it lands as an empty-string key in credHelpers.
196func TestConfigureDocker_IgnoresTrailingCommaInDomains(t *testing.T) {
197 dockerDir := sandboxDocker(t)
199 cfg := config.NewFromString(`
200---
201hosts:
202 gitlab.example.com:
203 token: token1
204 container_registry_domains: registry.gitlab.example.com,
205`)
206 ios, _, out, _ := cmdtest.TestIOStreams()
208 require.NoError(t, configureDocker(ios, cfg))
209 assert.Contains(t, out.String(), "registry.gitlab.example.com")
211 data, err := os.ReadFile(filepath.Join(dockerDir, "config.json"))
212 require.NoError(t, err)
213 assert.NotContains(t, string(data), `"":`, "a trailing comma must not write an empty-string domain")
214}
216func TestConfigureDocker_NoDomainsConfigured(t *testing.T) {
217 sandboxDocker(t)
219 cfg := config.NewFromString(`
220---
221hosts:
222 gitlab.example.com:
223 token: token1
224`)
225 ios, _, _, _ := cmdtest.TestIOStreams()
227 err := configureDocker(ios, cfg)
228 require.ErrorContains(t, err, "no container registry domains are configured")
229 assert.ErrorContains(t, err, "glab config set container_registry_domains")
230}
232// TestConfigureDocker_RefusesToReplaceAnotherHelper checks that the guard in
233// dockercredhelper.Register surfaces through the command rather than silently
234// taking the registry away from the tool that owns it.
235func TestConfigureDocker_RefusesToReplaceAnotherHelper(t *testing.T) {
236 dockerDir := sandboxDocker(t)
237 require.NoError(t, os.WriteFile(filepath.Join(dockerDir, "config.json"),
238 []byte(`{"credHelpers":{"registry.gitlab.example.com":"ecr-login"}}`), 0o600))
240 cfg := config.NewFromString(`
241---
242hosts:
243 gitlab.example.com:
244 token: token1
245 container_registry_domains: registry.gitlab.example.com
246`)
247 ios, _, _, _ := cmdtest.TestIOStreams()
249 err := configureDocker(ios, cfg)
250 require.ErrorContains(t, err, "ecr-login")
252 data, readErr := os.ReadFile(filepath.Join(dockerDir, "config.json"))
253 require.NoError(t, readErr)
254 assert.Contains(t, string(data), "ecr-login", "the existing helper must survive")
255}
257func TestConfigureDocker_WarnsWhenShadowingADockerLogin(t *testing.T) {
258 dockerDir := sandboxDocker(t)
259 require.NoError(t, os.WriteFile(filepath.Join(dockerDir, "config.json"),
260 []byte(`{"auths":{"registry.gitlab.example.com":{"auth":"dXNlcjpwYXNz"}}}`), 0o600))
262 cfg := config.NewFromString(`
263---
264hosts:
265 gitlab.example.com:
266 token: token1
267 container_registry_domains: registry.gitlab.example.com
268`)
269 ios, _, _, errOut := cmdtest.TestIOStreams()
271 require.NoError(t, configureDocker(ios, cfg))
272 assert.Contains(t, errOut.String(), "no longer used")
273 assert.Contains(t, errOut.String(), "docker logout registry.gitlab.example.com")
274}
Diagnostics8 git calls · 291 ms
CallStatusSizeTime0291 ms
discover200185 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=185 git.server.agent=git/2.56.g1891de4-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=185 git.pkt.payload.max=35
ls-refs20058 KiB · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=59849 git.response.cache=HIT git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=731 git.pkt.data=730 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=59849 git.pkt.payload.max=143 git.ls_refs.count=730 git.ls_refs.head=refs/heads/main
fetchtree:0 · deepen 1020032 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=32926 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=22 git.pkt.data=20 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=32926 git.pkt.payload.max=32046 git.sideband.band1.bytes=32046 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=80 git.pack.bytes=32046 git.pack.ofs_delta=36 git.pack.ref_delta=0 git.pack.chain.max=2 git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91 git.pack.full=44Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=4273 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=combine:blob:none+tree:2 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=1 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=4273 git.pkt.payload.max=4173 git.sideband.band1.bytes=4173 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=11 git.pack.bytes=4173 git.fetch.wide.objects=11 git.fetch.wide.extra_bytes=3930 git.fetch.wide.blobs=0
fetchtree:02001.8 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=1857 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=dce174ff8c632a19d17eea99c8a9b7c37fb2b592 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1857 git.pkt.payload.max=1830 git.sideband.band1.bytes=1830 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=1830 git.pack.trailer=fb03183b7909876778f740d196d7516811003517Open pack map
fetchtree:0200358 B · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=358 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=5d783fac547a70b19ca138c47d067b44febb52da git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=358 git.pkt.payload.max=331 git.sideband.band1.bytes=331 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=331 git.pack.trailer=22a484e5ecad6d6e62b224e8a7f90cf5ff085938Open pack map
fetchtree:0200352 B · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=352 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=078b54e79fa23822972abf99b4c28a7b8a3e2806 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=352 git.pkt.payload.max=325 git.sideband.band1.bytes=325 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=325 git.pack.trailer=756e6cebb5c15b5cd4bd8e9270eb683dbb54a67cOpen pack map
fetch2002.4 KiB276 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=276 git.response.bytes=2441 git.response.cache=MISS git.response.duration_ms=276 git.fetch.wants=e1f63a69a3a6624ef8ff3803dd5d8c92c62d4e50 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=2441 git.pkt.payload.max=2414 git.sideband.band1.bytes=2414 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://gitlab.com/gitlab-org/cli
- Ref
- refs/heads/main
- Commit
- 0dd7574dce9e385c5c30540e6c15657adc6bdce9
- cf-ray
- a5e2572f5e1a1ae0
- Request
- eed5be41…
- Protocol
- v2 · sha1
- Agent
- git/2.56.g1891de4-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format