GitLabgitlab-org

cli

main
ply8 git calls228 ms99 KiB6 cachedprotocol v2
258 lines7.3 KiBRaw
1//go:build !integration
3package list
5import (
6 "strings"
7 "testing"
9 "github.com/MakeNowJust/heredoc/v2"
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
12 "go.uber.org/mock/gomock"
14 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
15 gitlabtesting "gitlab.com/gitlab-org/api/client-go/v3/testing"
17 "gitlab.com/gitlab-org/cli/internal/api"
18 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
21func TestRunnerList_Project(t *testing.T) {
22 t.Setenv("NO_COLOR", "true")
24 tc := gitlabtesting.NewTestClient(t)
25 exec := cmdtest.SetupCmdForTest(
26 t,
27 NewCmd,
28 false,
29 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "", api.WithGitLabClient(tc.Client))),
30 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
31 )
33 tc.MockRunners.EXPECT().
34 ListProjectRunners("OWNER/REPO", &gitlab.ListProjectRunnersOptions{
35 ListOptions: gitlab.ListOptions{Page: 1, PerPage: 30},
36 }, gomock.Any()).
37 Return([]*gitlab.Runner{
38 {ID: 1, Description: "runner-1", Status: "online", RunnerType: "project_type", Paused: false},
39 {ID: 2, Description: "runner-2", Status: "offline", RunnerType: "project_type", Paused: true},
40 }, nil, nil)
42 out, err := exec("")
43 require.NoError(t, err)
45 assert.Equal(t, heredoc.Doc(`
46 Showing 2 runners on OWNER/REPO. (Page 1)
48 ID Description Status Paused
49 1 runner-1 online false
50 2 runner-2 offline true
52 `), out.String())
53 assert.Empty(t, out.Stderr())
56func TestRunnerList_Group(t *testing.T) {
57 t.Setenv("NO_COLOR", "true")
59 tc := gitlabtesting.NewTestClient(t)
60 exec := cmdtest.SetupCmdForTest(
61 t,
62 NewCmd,
63 false,
64 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "", api.WithGitLabClient(tc.Client))),
65 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
66 )
68 tc.MockRunners.EXPECT().
69 ListGroupsRunners("mygroup", &gitlab.ListGroupsRunnersOptions{
70 ListOptions: gitlab.ListOptions{Page: 1, PerPage: 30},
71 }, gomock.Any()).
72 Return([]*gitlab.Runner{
73 {ID: 10, Description: "group-runner", Status: "online", RunnerType: "group_type", Paused: false},
74 }, nil, nil)
76 out, err := exec("--group mygroup")
77 require.NoError(t, err)
79 assert.Equal(t, heredoc.Doc(`
80 Showing 1 runner on mygroup. (Page 1)
82 ID Description Status Paused
83 10 group-runner online false
85 `), out.String())
86 assert.Empty(t, out.Stderr())
89func TestRunnerList_Instance(t *testing.T) {
90 t.Setenv("NO_COLOR", "true")
92 tc := gitlabtesting.NewTestClient(t)
93 exec := cmdtest.SetupCmdForTest(
94 t,
95 NewCmd,
96 false,
97 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "", api.WithGitLabClient(tc.Client))),
98 )
100 tc.MockRunners.EXPECT().
101 ListRunners(&gitlab.ListRunnersOptions{
102 ListOptions: gitlab.ListOptions{Page: 1, PerPage: 30},
103 }, gomock.Any()).
104 Return([]*gitlab.Runner{
105 {ID: 100, Description: "instance-runner", Status: "online", RunnerType: "instance_type", Paused: false},
106 }, nil, nil)
108 out, err := exec("--instance")
109 require.NoError(t, err)
111 assert.Equal(t, heredoc.Doc(`
112 Showing 1 runner on instance. (Page 1)
114 ID Description Status Paused
115 100 instance-runner online false
117 `), out.String())
118 assert.Empty(t, out.Stderr())
121func TestRunnerList_Pagination(t *testing.T) {
122 t.Setenv("NO_COLOR", "true")
124 tc := gitlabtesting.NewTestClient(t)
125 exec := cmdtest.SetupCmdForTest(
126 t,
127 NewCmd,
128 false,
129 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "", api.WithGitLabClient(tc.Client))),
130 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
131 )
133 tc.MockRunners.EXPECT().
134 ListProjectRunners("OWNER/REPO", &gitlab.ListProjectRunnersOptions{
135 ListOptions: gitlab.ListOptions{Page: 2, PerPage: 5},
136 }, gomock.Any()).
137 Return([]*gitlab.Runner{
138 {ID: 3, Description: "runner-p3", Status: "online", RunnerType: "project_type", Paused: false},
139 }, nil, nil)
141 out, err := exec("--page 2 --per-page 5")
142 require.NoError(t, err)
144 assert.Equal(t, heredoc.Doc(`
145 Showing 1 runner on OWNER/REPO. (Page 2)
147 ID Description Status Paused
148 3 runner-p3 online false
150 `), out.String())
151 assert.Empty(t, out.Stderr())
154func TestRunnerList_HeaderTotal(t *testing.T) {
155 t.Parallel()
157 runners := []*gitlab.Runner{
158 {ID: 1, Description: "runner-1", Status: "online", RunnerType: "project_type", Paused: false},
159 }
160 withTotal := &gitlab.Response{CurrentPage: 2, TotalPages: 3, TotalItems: 11}
162 tests := []struct {
163 name string
164 cli string
165 expect func(tc *gitlabtesting.TestClient, resp *gitlab.Response)
166 resp *gitlab.Response
167 wantHeader string
168 }{
169 {
170 name: "project shows the total from the API",
171 cli: "--page 2 --per-page 5",
172 expect: func(tc *gitlabtesting.TestClient, resp *gitlab.Response) {
173 tc.MockRunners.EXPECT().ListProjectRunners("OWNER/REPO", gomock.Any(), gomock.Any()).Return(runners, resp, nil)
174 },
175 resp: withTotal,
176 wantHeader: "Showing 1 of 11 runners on OWNER/REPO. (Page 2)\n",
177 },
178 {
179 name: "group shows the total from the API",
180 cli: "--group mygroup --page 2 --per-page 5",
181 expect: func(tc *gitlabtesting.TestClient, resp *gitlab.Response) {
182 tc.MockRunners.EXPECT().ListGroupsRunners("mygroup", gomock.Any(), gomock.Any()).Return(runners, resp, nil)
183 },
184 resp: withTotal,
185 wantHeader: "Showing 1 of 11 runners on mygroup. (Page 2)\n",
186 },
187 {
188 name: "instance shows the total from the API",
189 cli: "--instance --page 2 --per-page 5",
190 expect: func(tc *gitlabtesting.TestClient, resp *gitlab.Response) {
191 tc.MockRunners.EXPECT().ListRunners(gomock.Any(), gomock.Any()).Return(runners, resp, nil)
192 },
193 resp: withTotal,
194 wantHeader: "Showing 1 of 11 runners on instance. (Page 2)\n",
195 },
196 {
197 name: "without a total from the API",
198 expect: func(tc *gitlabtesting.TestClient, resp *gitlab.Response) {
199 tc.MockRunners.EXPECT().ListProjectRunners("OWNER/REPO", gomock.Any(), gomock.Any()).Return(runners, resp, nil)
200 },
201 resp: &gitlab.Response{CurrentPage: 1, NextPage: 2},
202 wantHeader: "Showing 1 runner on OWNER/REPO. (Page 1)\n",
203 },
204 }
206 for _, tt := range tests {
207 t.Run(tt.name, func(t *testing.T) {
208 t.Parallel()
210 tc := gitlabtesting.NewTestClient(t)
211 tt.expect(tc, tt.resp)
213 exec := cmdtest.SetupCmdForTest(
214 t,
215 NewCmd,
216 false,
217 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "", api.WithGitLabClient(tc.Client))),
218 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
219 )
221 out, err := exec(tt.cli)
222 require.NoError(t, err)
224 assert.True(t, strings.HasPrefix(out.String(), tt.wantHeader), "got %q", out.String())
225 assert.Empty(t, out.Stderr())
226 })
227 }
230func TestRunnerList_JSON(t *testing.T) {
231 t.Parallel()
233 tc := gitlabtesting.NewTestClient(t)
234 exec := cmdtest.SetupCmdForTest(
235 t,
236 NewCmd,
237 false,
238 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "", api.WithGitLabClient(tc.Client))),
239 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
240 )
242 tc.MockRunners.EXPECT().
243 ListProjectRunners("OWNER/REPO", &gitlab.ListProjectRunnersOptions{
244 ListOptions: gitlab.ListOptions{Page: 1, PerPage: 30},
245 }, gomock.Any()).
246 Return([]*gitlab.Runner{
247 {ID: 1, Description: "runner-1", Status: "online", RunnerType: "project_type", Paused: false},
248 }, nil, nil)
250 out, err := exec("--output json")
251 require.NoError(t, err)
253 // Compact JSON (no space after colons) from PrintJSON
254 assert.Contains(t, out.String(), `"id":1`)
255 assert.Contains(t, out.String(), `"description":"runner-1"`)
256 assert.Contains(t, out.String(), `"status":"online"`)
257 assert.Empty(t, out.Stderr())
Diagnostics8 git calls · 228 ms
CallStatusSizeTime0228 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 · hit9 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=9
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 · 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=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:0200314 B · 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=314
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=9182edcf49c51a2d4d26f5f18ad76a93a377699f
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=314
git.pkt.payload.max=287
git.sideband.band1.bytes=287
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=287
git.pack.trailer=d25ce511fa222aedcda012c18364dcb1f012062b
Open pack map
fetchtree:0200135 B · 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=135
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=4c29033a778ddfc5e7c3564b6cd14f3c734a6f68
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=135
git.pkt.payload.max=108
git.sideband.band1.bytes=108
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=108
git.pack.trailer=4804568d56624ee73c06fd30ea4875d8099250bb
Open pack map
fetch2001.5 KiB208 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=208
git.response.bytes=1503
git.response.cache=MISS
git.response.duration_ms=208
git.fetch.wants=a5b1a7c2c28e689c9bae53f2ef93db64294a58f7
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=1503
git.pkt.payload.max=1476
git.sideband.band1.bytes=1476
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
a5e2bb546849cfc3
Request
861853d4…
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