GitLabgitlab-org

cli

main
ply10 git calls249 ms99 KiB8 cachedprotocol v2
198 lines4.9 KiBRaw
1//go:build !integration
3package list
5import (
6 "errors"
7 "testing"
8 "time"
10 "github.com/MakeNowJust/heredoc/v2"
11 "github.com/stretchr/testify/assert"
12 "github.com/stretchr/testify/require"
13 "go.uber.org/mock/gomock"
15 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
16 gitlabtesting "gitlab.com/gitlab-org/api/client-go/v3/testing"
18 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
21func TestList_InvalidAgentID(t *testing.T) {
22 t.Parallel()
24 // GIVEN
25 exec := cmdtest.SetupCmdForTest(t, NewCmd, false)
27 // WHEN
28 _, err := exec("this-is-not-an-agent-id")
30 // THEN
31 assert.EqualError(t, err, `agent ID must be a valid integer, got "this-is-not-an-agent-id"`)
34func TestList_FailToRetrieveTokens(t *testing.T) {
35 t.Parallel()
37 // GIVEN
38 tc := gitlabtesting.NewTestClient(t)
39 exec := cmdtest.SetupCmdForTest(
40 t,
41 NewCmd,
42 false,
43 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
44 cmdtest.WithGitLabClient(tc.Client),
45 )
47 // setup mock expectations
48 tc.MockClusterAgents.EXPECT().
49 ListAgentTokens("OWNER/REPO", int64(1), nil, gomock.Any()).
50 Return(nil, nil, errors.New("dummy API failure"))
52 // WHEN
53 _, err := exec("1")
55 // THEN
56 assert.EqualError(t, err, "unable to retrieve agent tokens: dummy API failure")
59func TestList_SingleToken(t *testing.T) {
60 t.Parallel()
62 // GIVEN
63 tc := gitlabtesting.NewTestClient(t)
64 exec := cmdtest.SetupCmdForTest(
65 t,
66 NewCmd,
67 false,
68 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
69 cmdtest.WithGitLabClient(tc.Client),
70 )
72 // setup mock expectations
73 gomock.InOrder(
74 tc.MockClusterAgents.EXPECT().
75 ListAgentTokens("OWNER/REPO", int64(1), nil, gomock.Any()).
76 Return([]*gitlab.AgentToken{
77 {
78 ID: 42,
79 Name: "any-name",
80 Description: "any-description",
81 AgentID: 1,
82 Status: "active",
83 CreatedAt: new(time.Time{}),
84 CreatedByUserID: 100,
85 // LastUsedAt: &time.Time{},
86 },
87 }, nil, nil),
88 tc.MockUsers.EXPECT().
89 GetUser(int64(100), gomock.Any(), gomock.Any()).
90 Return(&gitlab.User{Username: "any-username"}, nil, nil),
91 )
93 // WHEN
94 out, err := exec("1")
95 require.NoError(t, err)
97 expectedOutput := heredoc.Docf(`
98 ID%[1]sName%[1]sStatus%[1]sCreated At%[1]sCreated By%[1]sLast Used At%[1]sDescription
99 42%[1]sany-name%[1]sactive%[1]s0001-01-01T00:00:00Z%[1]sany-username%[1]snever%[1]sany-description
100 `, "\t")
102 // THEN
103 assert.Equal(t, expectedOutput, out.OutBuf.String())
106func TestList_MultipleToken(t *testing.T) {
107 t.Parallel()
109 // GIVEN
110 tc := gitlabtesting.NewTestClient(t)
111 exec := cmdtest.SetupCmdForTest(
112 t,
113 NewCmd,
114 false,
115 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
116 cmdtest.WithGitLabClient(tc.Client),
117 )
119 // setup mock expectations
120 gomock.InOrder(
121 tc.MockClusterAgents.EXPECT().
122 ListAgentTokens("OWNER/REPO", int64(1), nil, gomock.Any()).
123 Return([]*gitlab.AgentToken{
124 {
125 ID: 42,
126 Name: "any-name",
127 Description: "any-description",
128 AgentID: 1,
129 Status: "active",
130 CreatedAt: new(time.Time{}),
131 CreatedByUserID: 100,
132 // LastUsedAt: &time.Time{},
133 },
134 {
135 ID: 84,
136 Name: "another-name",
137 Description: "another-description",
138 AgentID: 1,
139 Status: "revoked",
140 CreatedAt: new(time.Time{}),
141 CreatedByUserID: 100,
142 // LastUsedAt: &time.Time{},
143 },
144 }, nil, nil),
145 tc.MockUsers.EXPECT().
146 GetUser(int64(100), gomock.Any(), gomock.Any()).
147 Return(&gitlab.User{Username: "any-username"}, nil, nil),
148 )
150 // WHEN
151 out, err := exec("1")
152 require.NoError(t, err)
154 expectedOutput := heredoc.Docf(`
155 ID%[1]sName%[1]sStatus%[1]sCreated At%[1]sCreated By%[1]sLast Used At%[1]sDescription
156 42%[1]sany-name%[1]sactive%[1]s0001-01-01T00:00:00Z%[1]sany-username%[1]snever%[1]sany-description
157 84%[1]sanother-name%[1]srevoked%[1]s0001-01-01T00:00:00Z%[1]sany-username%[1]snever%[1]sanother-description
158 `, "\t")
160 // THEN
161 assert.Equal(t, expectedOutput, out.OutBuf.String())
164func TestAgentTokenList_JSON(t *testing.T) {
165 t.Parallel()
167 tc := gitlabtesting.NewTestClient(t)
168 exec := cmdtest.SetupCmdForTest(
169 t,
170 NewCmd,
171 false,
172 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
173 cmdtest.WithGitLabClient(tc.Client),
174 )
176 tc.MockClusterAgents.EXPECT().
177 ListAgentTokens("OWNER/REPO", int64(1), nil, gomock.Any()).
178 Return([]*gitlab.AgentToken{
179 {
180 ID: 42,
181 Name: "any-name",
182 Description: "any-description",
183 AgentID: 1,
184 Status: "active",
185 CreatedAt: new(time.Time{}),
186 CreatedByUserID: 100,
187 },
188 }, nil, nil)
190 out, err := exec("1 --output json")
191 require.NoError(t, err)
193 assert.Contains(t, out.String(), `"id":42`)
194 assert.Contains(t, out.String(), `"name":"any-name"`)
195 assert.Contains(t, out.String(), `"status":"active"`)
196 assert.Contains(t, out.String(), `"description":"any-description"`)
197 assert.Empty(t, out.Stderr())
Diagnostics10 git calls · 249 ms
CallStatusSizeTime0249 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 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=3
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
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=2
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 · hit1 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=1857
git.response.cache=HIT
git.response.duration_ms=1
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:0200200 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=200
git.response.cache=HIT
git.response.duration_ms=3
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=948ee807a1ee817af67f2df94421a202e82325f9
Open pack map
fetchtree:0200405 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=405
git.response.cache=HIT
git.response.duration_ms=3
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=adfacba5ff1f4fcf28dcaac12e2874f6d48a7934
Open pack map
fetchtree:0200167 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=167
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=7dfd1c9bf93d8973834f70c611fab6bb6c6258a5
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=167
git.pkt.payload.max=140
git.sideband.band1.bytes=140
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=140
git.pack.trailer=8b63cee433b4118712f1ed3590553e906f29e021
Open pack map
fetchtree:0200134 B · 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=134
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=e459914cb4e2e9e3447771739e12c8d7de47d97b
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=134
git.pkt.payload.max=107
git.sideband.band1.bytes=107
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=107
git.pack.trailer=77dbe9638eb886ae0e4dccdf90191fdcc46fe838
Open pack map
fetch2001.2 KiB232 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=232
git.response.bytes=1195
git.response.cache=MISS
git.response.duration_ms=232
git.fetch.wants=90e18f55b873e0f4a6b553276ea14812e43162bc
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=1195
git.pkt.payload.max=1168
git.sideband.band1.bytes=1168
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
a5eb14bedf67cf75
Request
4ab3dcdc…
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