GitLabgitlab-org
cli
main
1package list
3import (
4 "encoding/base64"
5 "encoding/json"
6 "os"
7 "path/filepath"
8 "strconv"
9 "testing"
10 "time"
12 "github.com/MakeNowJust/heredoc/v2"
13 "github.com/stretchr/testify/assert"
14 "github.com/stretchr/testify/require"
15 "github.com/zalando/go-keyring"
17 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
18 gitlab_testing "gitlab.com/gitlab-org/api/client-go/v3/testing"
20 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
21)
23// helper to write a filesystem cached token under <cacheDir>/gitlab/<base64(url)>-<agentID>
24func writeFSToken(t *testing.T, gitlabURL string, agentID int64, pat *gitlab.PersonalAccessToken) {
25 t.Helper()
27 // Use os.UserCacheDir to get the actual cache directory that the code will use
28 // This ensures compatibility across Windows, Linux, and macOS
29 actualCacheDir, err := os.UserCacheDir()
30 require.NoError(t, err)
32 enc := base64.StdEncoding.EncodeToString([]byte(gitlabURL))
33 fname := enc + "-" + strconv.FormatInt(agentID, 10)
34 dir := filepath.Join(actualCacheDir, "gitlab")
35 require.NoError(t, os.MkdirAll(dir, 0o700))
36 fp := filepath.Join(dir, fname)
37 data, err := json.Marshal(pat)
38 require.NoError(t, err)
39 require.NoError(t, os.WriteFile(fp, data, 0o600))
40}
42// setUserCacheDir points Go's UserCacheDir to tempDir for this process (Windows, Unix, and macOS)
43func setUserCacheDir(t *testing.T, tempDir string) {
44 t.Helper()
45 t.Setenv("LocalAppData", tempDir) // Windows
46 t.Setenv("LOCALAPPDATA", tempDir) // Windows
47 t.Setenv("XDG_CACHE_HOME", tempDir) // Unix (Linux, BSD, etc.)
48 t.Setenv("HOME", tempDir) // macOS (uses $HOME/Library/Caches)
49}
51func TestList_NoTokens_Default(t *testing.T) {
52 keyring.MockInit()
53 tc := gitlab_testing.NewTestClient(t, gitlab.WithBaseURL("https://gitlab.example.com"))
54 exec := cmdtest.SetupCmdForTest(t, NewCmd, false, cmdtest.WithGitLabClient(tc.Client))
56 out, err := exec("--filesystem --keyring=false")
57 require.NoError(t, err)
58 assert.Equal(t, heredoc.Doc(`
59 No cached tokens found.
60 `), out.String())
61 assert.Empty(t, out.Stderr())
62}
64func TestList_FilesystemTokens_ShowsTable(t *testing.T) {
65 keyring.MockInit()
66 tc := gitlab_testing.NewTestClient(t, gitlab.WithBaseURL("https://gitlab.example.com"))
67 exec := cmdtest.SetupCmdForTest(t, NewCmd, false, cmdtest.WithGitLabClient(tc.Client))
69 // create a temp cache directory
70 cacheDir := t.TempDir()
71 setUserCacheDir(t, cacheDir)
73 expires := new(gitlab.ISOTime(time.Now().Add(1 * time.Hour)))
74 pat := &gitlab.PersonalAccessToken{Name: "tok1", ExpiresAt: expires}
75 writeFSToken(t, tc.Client.BaseURL().String(), 7, pat)
77 out, err := exec("--filesystem --keyring=false")
78 require.NoError(t, err)
79 // token table should contain agent id and token name
80 assert.Contains(t, out.String(), "Agent ID")
81 assert.Contains(t, out.String(), "7")
82 assert.Contains(t, out.String(), "tok1")
83 assert.Empty(t, out.Stderr())
84}
86func TestList_FilterAgents(t *testing.T) {
87 keyring.MockInit()
88 tc := gitlab_testing.NewTestClient(t, gitlab.WithBaseURL("https://gitlab.example.com"))
89 exec := cmdtest.SetupCmdForTest(t, NewCmd, false, cmdtest.WithGitLabClient(tc.Client))
90 cacheDir := t.TempDir()
91 setUserCacheDir(t, cacheDir)
93 pat := &gitlab.PersonalAccessToken{Name: "tokA"}
94 writeFSToken(t, tc.Client.BaseURL().String(), 10, pat)
95 writeFSToken(t, tc.Client.BaseURL().String(), 11, pat)
97 out, err := exec("--filesystem --keyring=false --agent 10")
98 require.NoError(t, err)
99 assert.Contains(t, out.String(), "10")
100 assert.NotContains(t, out.String(), "11")
101}
Diagnostics11 git calls · 819 ms
CallStatusSizeTime0819 ms
discover200185 B149 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 KiB · hit420 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=238 git.response.bytes=59849 git.response.cache=HIT git.response.duration_ms=271 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 · hit45 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=32926 git.response.cache=HIT git.response.duration_ms=45 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
fetchtree:02001.1 KiB · hit5 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=1112 git.response.cache=HIT git.response.duration_ms=5 git.fetch.filter=tree:0 git.fetch.wants=fed5065f0120a857fec6fa567dcdacfd34c3187b 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=1112 git.pkt.payload.max=1085 git.sideband.band1.bytes=1085 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=1085 git.pack.trailer=792a8e6885c3db50fcc6be7a28de09363c6fb5e4Open pack map
fetchtree:0200890 B · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=1 git.response.bytes=890 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=11daffb8433226c87be08ba8cfc35154e4957d77 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=890 git.pkt.payload.max=863 git.sideband.band1.bytes=863 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=863 git.pack.trailer=b909e7b7573f11b0bb74a32781c8c5e4e19277f4Open 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=fb03183b7909876778f740d196d7516811003517Open pack map
fetchtree:0200200 B · hit4 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=200 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=9988d47506dcbe3004b8b72cbdb9d66d6d75cda4 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=200 git.pkt.payload.max=173 git.sideband.band1.bytes=173 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=173 git.pack.trailer=948ee807a1ee817af67f2df94421a202e82325f9Open pack map
fetchtree:0200405 B · hit5 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=405 git.response.cache=HIT git.response.duration_ms=5 git.fetch.filter=tree:0 git.fetch.wants=d271680e941992259de0ff84d4a04bcbba55f531 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=405 git.pkt.payload.max=378 git.sideband.band1.bytes=378 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=378 git.pack.trailer=adfacba5ff1f4fcf28dcaac12e2874f6d48a7934Open pack map
fetchtree:0200175 B · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=1 git.response.bytes=175 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=b2f8b8171cc11242e253b76b9b852197f2ebc178 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=175 git.pkt.payload.max=148 git.sideband.band1.bytes=148 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=148 git.pack.trailer=e894e38a7c0abf202dc4cff099138af128997f53Open pack map
fetchtree:0200166 B · hit7 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=5 git.response.bytes=166 git.response.cache=HIT git.response.duration_ms=7 git.fetch.filter=tree:0 git.fetch.wants=92a9b75f424e03656d9e79de23b380ee6f603a90 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=166 git.pkt.payload.max=139 git.sideband.band1.bytes=139 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=139 git.pack.trailer=dbfeb102fb320f5a06d4db129610e55c3aee3b59Open pack map
fetch2001.3 KiB316 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=316 git.response.bytes=1289 git.response.cache=MISS git.response.duration_ms=316 git.fetch.wants=13239dbe0bbc28ae4d8a81d0dfa963703b882901 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=1289 git.pkt.payload.max=1262 git.sideband.band1.bytes=1262 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
- a5eb149e9e29cf75
- Request
- 3a61d006…
- 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