GitLabgitlab-org

cli

main
ply8 git calls211 ms100 KiB6 cachedprotocol v2
149 lines5.6 KiBRaw
1//go:build !integration
3package docker
5import (
6 "encoding/json"
7 "net/http"
8 "net/http/httptest"
9 "net/url"
10 "path/filepath"
11 "testing"
12 "time"
14 "github.com/stretchr/testify/assert"
15 "github.com/stretchr/testify/require"
16 "github.com/zalando/go-keyring"
18 "gitlab.com/gitlab-org/cli/internal/config"
21const (
22 refreshTestHost = "gitlab.example.com"
23 refreshTestRegistry = "registry.gitlab.example.com"
26// rewriteTransport sends every request to base instead of its real host, so the
27// OAuth token endpoint derived from the config hostname hits an httptest server.
28type rewriteTransport struct {
29 base *url.URL
32func (t rewriteTransport) RoundTrip(req *http.Request) (*http.Response, error) {
33 r := req.Clone(req.Context())
34 r.URL.Scheme = t.base.Scheme
35 r.URL.Host = t.base.Host
36 return http.DefaultTransport.RoundTrip(r)
39// fileModeOAuthConfig writes a file-backed (use_keyring unset) OAuth config for
40// refreshTestHost and returns it re-read from disk, so the config has a backing
41// directory and Reload() returns a distinct object — as in real usage. The
42// directory is returned so the test can inspect what was persisted.
43func fileModeOAuthConfig(t *testing.T, accessToken, refreshToken string, expiry time.Time) (config.Config, string) {
44 t.Helper()
46 keyring.MockInit()
47 t.Cleanup(keyring.MockInit)
49 dir := t.TempDir()
50 seed := config.NewBlankConfigInDir(dir)
51 require.NoError(t, seed.Set(refreshTestHost, "is_oauth2", "true"))
52 require.NoError(t, seed.Set(refreshTestHost, "client_id", "abc"))
53 require.NoError(t, seed.Set(refreshTestHost, "user", "user1"))
54 require.NoError(t, seed.Set(refreshTestHost, "container_registry_domains", refreshTestRegistry))
55 require.NoError(t, seed.Set(refreshTestHost, "token", accessToken))
56 require.NoError(t, seed.Set(refreshTestHost, "oauth2_refresh_token", refreshToken))
57 require.NoError(t, seed.Set(refreshTestHost, "oauth2_expiry_date", expiry.Format(time.RFC3339)))
58 require.NoError(t, seed.Write())
60 cfg, err := config.ParseConfig(filepath.Join(dir, "config.yml"))
61 require.NoError(t, err)
62 return cfg, dir
65// The credential helper forces a refresh via the token source, then reads the
66// access token back out of its own config. The refreshed token must be the one
67// handed to Docker; returning the pre-refresh token hands Docker an already
68// expired credential.
69func TestHelper_GetReturnsRefreshedTokenForFileBackedHost(t *testing.T) {
70 t.Setenv("GITLAB_TOKEN", "")
72 cfg, dir := fileModeOAuthConfig(t, "access-old", "refresh-old", time.Now().Add(-time.Hour))
74 var refreshed bool
75 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
76 refreshed = true
77 // assert, not require: this runs in the server's handler goroutine.
78 assert.NoError(t, r.ParseForm())
79 assert.Equal(t, "refresh_token", r.Form.Get("grant_type"))
80 assert.Equal(t, "refresh-old", r.Form.Get("refresh_token"))
82 w.Header().Set("Content-Type", "application/json")
83 assert.NoError(t, json.NewEncoder(w).Encode(map[string]any{
84 "access_token": "access-new",
85 "refresh_token": "refresh-new",
86 "token_type": "bearer",
87 "expires_in": 7200,
88 }))
89 }))
90 defer srv.Close()
92 base, err := url.Parse(srv.URL)
93 require.NoError(t, err)
95 helper := Helper{
96 cfg: cfg,
97 client: &http.Client{Transport: rewriteTransport{base: base}},
98 }
100 gotUser, gotPassword, err := helper.Get(refreshTestRegistry)
101 require.NoError(t, err)
102 require.True(t, refreshed, "expected the expired token to be refreshed")
104 assert.Equal(t, "user1", gotUser)
105 assert.Equal(t, "access-new", gotPassword,
106 "Docker must receive the refreshed access token, not the expired one")
108 // Sanity check: the refresh does reach disk. Only the in-memory copy that
109 // helper.Get() reads back from lags behind.
110 onDisk, err := config.ParseConfig(filepath.Join(dir, "config.yml"))
111 require.NoError(t, err)
112 diskToken, _, err := onDisk.GetWithSource(refreshTestHost, "token", false)
113 require.NoError(t, err)
114 assert.Equal(t, "access-new", diskToken, "disk should hold the refreshed token")
117// When the token is still valid no refresh happens, and if the config cannot be
118// re-read (for example it lives in an XDG_CONFIG_DIRS entry with no user-level
119// file, so no refresh ever created one) the helper must fall back to the
120// in-memory copy rather than hard-failing. Modeled with a dir-backed config that
121// was never written to disk, so Reload() cannot find config.yml.
122func TestHelper_GetFallsBackToInMemoryConfigWhenReloadCannotFindFile(t *testing.T) {
123 t.Setenv("GITLAB_TOKEN", "")
124 keyring.MockInit()
125 t.Cleanup(keyring.MockInit)
127 cfg := config.NewBlankConfigInDir(t.TempDir())
128 require.NoError(t, cfg.Set(refreshTestHost, "is_oauth2", "true"))
129 require.NoError(t, cfg.Set(refreshTestHost, "client_id", "abc"))
130 require.NoError(t, cfg.Set(refreshTestHost, "user", "user1"))
131 require.NoError(t, cfg.Set(refreshTestHost, "container_registry_domains", refreshTestRegistry))
132 require.NoError(t, cfg.Set(refreshTestHost, "token", "valid-token"))
133 require.NoError(t, cfg.Set(refreshTestHost, "oauth2_refresh_token", "refresh"))
134 require.NoError(t, cfg.Set(refreshTestHost, "oauth2_expiry_date", time.Now().Add(time.Hour).Format(time.RFC3339)))
136 // The token is valid, so no token-endpoint request should be made.
137 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
138 t.Errorf("token endpoint should not be called; the token is valid")
139 w.WriteHeader(http.StatusBadRequest)
140 }))
141 defer srv.Close()
143 helper := Helper{cfg: cfg, client: srv.Client()}
145 gotUser, gotPassword, err := helper.Get(refreshTestRegistry)
146 require.NoError(t, err)
147 assert.Equal(t, "user1", gotUser)
148 assert.Equal(t, "valid-token", gotPassword)
Diagnostics8 git calls · 211 ms
CallStatusSizeTime0211 ms
discover200185 B4 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=4
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 · hit10 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=4
git.response.bytes=59849
git.response.cache=HIT
git.response.duration_ms=10
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
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit2 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=2
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: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=2
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
fetchtree:02001.8 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=1857
git.response.cache=HIT
git.response.duration_ms=2
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 · 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=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=756e6cebb5c15b5cd4bd8e9270eb683dbb54a67c
Open pack map
fetch2002.1 KiB191 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=191
git.response.bytes=2103
git.response.cache=MISS
git.response.duration_ms=191
git.fetch.wants=f85d9963292dbf4c12fc84cfd669a0c0e78e0324
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=2103
git.pkt.payload.max=2076
git.sideband.band1.bytes=2076
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
a5e2c2973c4aaedd
Request
6e2e28d8…
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