GitLabgitlab-org

cli

main
ply8 git calls262 ms99 KiB5 cachedprotocol v2
220 lines6.8 KiBRaw
1//go:build !integration
3package docker
5import (
6 "errors"
7 "net/http"
8 "testing"
9 "time"
11 "github.com/golang-jwt/jwt/v5"
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 "gitlab.com/gitlab-org/cli/internal/testing/tokenexchangetest"
20func TestHelper_findArtifactRegistryHostname(t *testing.T) {
21 t.Run("resolves the matching host", func(t *testing.T) {
22 cfg := config.NewFromString(`
23---
24hosts:
25 gitlab.example.com:
26 artifact_registry_domains: registry.gitlab.example.com
27`)
28 helper := Helper{cfg: cfg}
30 hostname, err := helper.findArtifactRegistryHostname("registry.gitlab.example.com")
31 require.NoError(t, err)
32 assert.Equal(t, "gitlab.example.com", hostname)
33 })
35 t.Run("picks the right host among several", func(t *testing.T) {
36 cfg := config.NewFromString(`
37---
38hosts:
39 gitlab.example.com:
40 artifact_registry_domains: registry.gitlab.example.com
41 gdk.example.com:
42 artifact_registry_domains: registry.gdk.example.com
43`)
44 helper := Helper{cfg: cfg}
46 hostname, err := helper.findArtifactRegistryHostname("registry.gdk.example.com")
47 require.NoError(t, err)
48 assert.Equal(t, "gdk.example.com", hostname)
49 })
51 t.Run("returns the fall-through sentinel when unconfigured", func(t *testing.T) {
52 cfg := config.NewFromString(`
53---
54hosts:
55 gitlab.example.com:
56 token: token1
57`)
58 helper := Helper{cfg: cfg}
60 _, err := helper.findArtifactRegistryHostname("registry.gitlab.example.com")
61 require.ErrorIs(t, err, errNotArtifactRegistry)
62 })
64 t.Run("does not match on container_registry_domains", func(t *testing.T) {
65 cfg := config.NewFromString(`
66---
67hosts:
68 gitlab.example.com:
69 container_registry_domains: registry.gitlab.example.com
70`)
71 helper := Helper{cfg: cfg}
73 _, err := helper.findArtifactRegistryHostname("registry.gitlab.example.com")
74 require.ErrorIs(t, err, errNotArtifactRegistry)
75 })
77 t.Run("propagates a real read failure", func(t *testing.T) {
78 cfg := failingConfig{
79 Config: config.NewFromString(`
80---
81hosts:
82 gitlab.example.com:
83 token: token1
84`),
85 failKey: "artifact_registry_domains",
86 err: errors.New("keyring is locked"),
87 }
88 helper := Helper{cfg: cfg}
90 _, err := helper.findArtifactRegistryHostname("registry.gitlab.example.com")
91 require.ErrorContains(t, err, "keyring is locked")
92 require.NotErrorIs(t, err, errNotArtifactRegistry)
93 })
95 t.Run("a broken host does not hide a matching host", func(t *testing.T) {
96 cfg := failingConfig{
97 Config: config.NewFromString(`
98---
99hosts:
100 broken.example.com:
101 token: token1
102 gdk.example.com:
103 artifact_registry_domains: registry.gdk.example.com
104`),
105 failHost: "broken.example.com",
106 failKey: "artifact_registry_domains",
107 err: errors.New("keyring is locked"),
108 }
109 ios, _, _, _ := cmdtest.TestIOStreams()
110 helper := Helper{cfg: cfg, io: ios}
112 hostname, err := helper.findArtifactRegistryHostname("registry.gdk.example.com")
113 require.NoError(t, err)
114 assert.Equal(t, "gdk.example.com", hostname)
115 })
118func TestHelper_getArtifactRegistryToken(t *testing.T) {
119 t.Run("no host configured", func(t *testing.T) {
120 cfg := config.NewFromString(`
121---
122hosts:
123 gitlab.example.com:
124 token: token1
125`)
126 helper := Helper{cfg: cfg}
128 _, _, err := helper.getArtifactRegistryToken(t.Context(), "registry.gitlab.example.com")
129 require.ErrorIs(t, err, errNotArtifactRegistry)
130 })
132 t.Run("a server error is not mistaken for the fall-through sentinel", func(t *testing.T) {
133 srv, _ := tokenexchangetest.NewTestServer(t, func(w http.ResponseWriter, r *http.Request) {
134 w.Header().Set("Content-Type", "application/json")
135 w.WriteHeader(http.StatusForbidden)
136 _, _ = w.Write([]byte(`{"message":"artifact registry is disabled for this project"}`))
137 })
138 cfg := config.NewFromString(`
139---
140hosts:
141 gitlab.example.com:
142 token: token1
143 artifact_registry_domains: registry.gitlab.example.com
144 api_host: ` + apiHost(t, srv) + `
145 api_protocol: http
146`)
147 helper := Helper{cfg: cfg}
149 _, _, err := helper.getArtifactRegistryToken(t.Context(), "registry.gitlab.example.com")
150 require.Error(t, err)
151 require.NotErrorIs(t, err, errNotArtifactRegistry)
152 assert.Contains(t, err.Error(), "artifact registry is disabled for this project")
153 })
155 t.Run("success returns the token convention Docker expects", func(t *testing.T) {
156 wantToken := tokenexchangetest.MakeJWT(t, jwt.RegisteredClaims{
157 Issuer: "https://gitlab.example.com",
158 Subject: "gid://gitlab/User/1",
159 ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Hour)),
160 })
161 var gotBody tokenexchangetest.WireRequest
162 srv, count := tokenexchangetest.NewTokenExchangeServer(t, wantToken, &gotBody)
163 cfg := config.NewFromString(`
164---
165hosts:
166 gitlab.example.com:
167 token: token1
168 artifact_registry_domains: registry.gitlab.example.com
169 api_host: ` + apiHost(t, srv) + `
170 api_protocol: http
171`)
172 helper := Helper{cfg: cfg}
174 gotUser, gotToken, err := helper.getArtifactRegistryToken(t.Context(), "registry.gitlab.example.com")
175 require.NoError(t, err)
176 assert.Equal(t, "__token__", gotUser)
177 assert.Equal(t, wantToken, gotToken)
178 assert.EqualValues(t, 1, count.Load())
179 // Pins artifactregistry.DockerHelperDuration's value at 1 hour (in
180 // seconds, the wire format): without this, the request body is never
181 // inspected, so a change to the duration this helper requests would go
182 // unnoticed. Hardcoded rather than derived from the constant itself,
183 // so the assertion cannot silently follow a future change to it.
184 assert.Equal(t, int(time.Hour.Seconds()), gotBody.ExpiresIn)
185 })
188// TestHelper_getArtifactRegistryToken_IgnoresEnvironmentToken pins the same
189// rule the container-registry path already relies on: a Docker credential
190// helper runs as a subprocess that inherits the user's shell environment, so
191// a stray GITLAB_TOKEN must not decide which identity mints the token.
192func TestHelper_getArtifactRegistryToken_IgnoresEnvironmentToken(t *testing.T) {
193 t.Setenv("GITLAB_TOKEN", "env-token")
195 srv, _ := tokenexchangetest.NewTestServer(t, func(w http.ResponseWriter, r *http.Request) {
196 assert.Equal(t, "config-token", r.Header.Get("Private-Token"))
198 wantToken := tokenexchangetest.MakeJWT(t, jwt.RegisteredClaims{
199 Issuer: "https://gitlab.example.com",
200 Subject: "gid://gitlab/User/1",
201 ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Hour)),
202 })
203 w.Header().Set("Content-Type", "application/json")
204 w.WriteHeader(http.StatusOK)
205 _, _ = w.Write([]byte(`{"token":"` + wantToken + `"}`))
206 })
207 cfg := config.NewFromString(`
208---
209hosts:
210 gitlab.example.com:
211 token: config-token
212 artifact_registry_domains: registry.gitlab.example.com
213 api_host: ` + apiHost(t, srv) + `
214 api_protocol: http
215`)
216 helper := Helper{cfg: cfg}
218 _, _, err := helper.getArtifactRegistryToken(t.Context(), "registry.gitlab.example.com")
219 require.NoError(t, err)
Diagnostics8 git calls · 262 ms
CallStatusSizeTime0262 ms
discover200185 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
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 KiB3 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=UPDATING
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 · 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=32926
git.response.cache=HIT
git.response.duration_ms=3
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=44
Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=4
git.response.bytes=4273
git.response.cache=HIT
git.response.duration_ms=4
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=fb03183b7909876778f740d196d7516811003517
Open 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=22a484e5ecad6d6e62b224e8a7f90cf5ff085938
Open pack map
fetchtree:0200352 B · hit16 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=16
git.response.bytes=352
git.response.cache=HIT
git.response.duration_ms=16
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=756e6cebb5c15b5cd4bd8e9270eb683dbb54a67c
Open pack map
fetch2001.7 KiB233 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=233
git.response.bytes=1717
git.response.cache=MISS
git.response.duration_ms=233
git.fetch.wants=0ddeb039496d258295ed6df38e8f174489d68a2a
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=1717
git.pkt.payload.max=1690
git.sideband.band1.bytes=1690
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
a5e2d5c5ca9d689d
Request
d934c58d…
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