GitLabgitlab-org

cli

main
ply8 git calls247 ms99 KiB6 cachedprotocol v2
287 lines7.9 KiBRaw
1//go:build !integration
3package list
5import (
6 "net/http"
7 "strings"
8 "testing"
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 TestIterationList(t *testing.T) {
22 t.Parallel()
24 testClient := gitlabtesting.NewTestClient(t)
26 testClient.MockProjectIterations.EXPECT().
27 ListProjectIterations("OWNER/REPO", gomock.Any()).
28 Return([]*gitlab.ProjectIteration{
29 {
30 ID: 53,
31 IID: 13,
32 GroupID: 5,
33 Title: "Iteration II",
34 Description: "Ipsum Lorem ipsum",
35 State: 2,
36 WebURL: "http://gitlab.example.com/groups/my-group/-/iterations/13",
37 },
38 }, nil, nil)
40 apiClient, err := api.NewClient(
41 func(*http.Client) (gitlab.AuthSource, error) {
42 return gitlab.AccessTokenAuthSource{Token: "test-token"}, nil
43 },
44 api.WithGitLabClient(testClient.Client),
45 )
46 require.NoError(t, err)
48 exec := cmdtest.SetupCmdForTest(t, NewCmdList, true,
49 cmdtest.WithApiClient(apiClient),
50 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
51 )
53 output, err := exec("")
54 require.NoError(t, err)
56 assert.Equal(t, "Showing 1 iteration on OWNER/REPO. (Page 1)\n\n Iteration II -> Ipsum Lorem ipsum (http://gitlab.example.com/groups/my-group/-/iterations/13)\n \n", output.String())
57 assert.Empty(t, output.Stderr())
60func TestIterationListJSON(t *testing.T) {
61 t.Parallel()
63 testClient := gitlabtesting.NewTestClient(t)
65 testClient.MockProjectIterations.EXPECT().
66 ListProjectIterations("OWNER/REPO", gomock.Any()).
67 Return([]*gitlab.ProjectIteration{
68 {
69 ID: 53,
70 IID: 13,
71 GroupID: 5,
72 Title: "Iteration II",
73 Description: "Ipsum Lorem ipsum",
74 State: 2,
75 WebURL: "https://gitlab.com/api/v4/projects/OWNER%2FREPO/iterations?include_ancestors=true&page=1&per_page=30",
76 },
77 }, nil, nil)
79 apiClient, err := api.NewClient(
80 func(*http.Client) (gitlab.AuthSource, error) {
81 return gitlab.AccessTokenAuthSource{Token: "test-token"}, nil
82 },
83 api.WithGitLabClient(testClient.Client),
84 )
85 require.NoError(t, err)
87 exec := cmdtest.SetupCmdForTest(t, NewCmdList, true,
88 cmdtest.WithApiClient(apiClient),
89 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
90 )
92 output, err := exec("-F json")
93 require.NoError(t, err)
95 expectedBody := `[
96 {
97 "id": 53,
98 "iid": 13,
99 "group_id": 5,
100 "title": "Iteration II",
101 "description": "Ipsum Lorem ipsum",
102 "state": 2,
103 "created_at": null,
104 "updated_at": null,
105 "due_date": null,
106 "start_date": null,
107 "sequence": 0,
108 "web_url": "https://gitlab.com/api/v4/projects/OWNER%2FREPO/iterations?include_ancestors=true&page=1&per_page=30"
109 }
110]`
112 assert.JSONEq(t, expectedBody, output.String())
113 assert.Empty(t, output.Stderr())
116func TestIterationListGroup(t *testing.T) {
117 t.Parallel()
119 testClient := gitlabtesting.NewTestClient(t)
121 testClient.MockGroupIterations.EXPECT().
122 ListGroupIterations("my-group", gomock.Any()).
123 Return([]*gitlab.GroupIteration{
124 {
125 ID: 53,
126 IID: 13,
127 Title: "Group Iteration",
128 Description: "Group iteration description",
129 State: 1,
130 WebURL: "http://gitlab.example.com/groups/my-group/-/iterations/13",
131 },
132 }, nil, nil)
134 apiClient, err := api.NewClient(
135 func(*http.Client) (gitlab.AuthSource, error) {
136 return gitlab.AccessTokenAuthSource{Token: "test-token"}, nil
137 },
138 api.WithGitLabClient(testClient.Client),
139 )
140 require.NoError(t, err)
142 exec := cmdtest.SetupCmdForTest(t, NewCmdList, true,
143 cmdtest.WithApiClient(apiClient),
144 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
145 )
147 output, err := exec("-g my-group")
148 require.NoError(t, err)
150 assert.Equal(t, "Showing 1 iteration on my-group. (Page 1)\n\n Group Iteration -> Group iteration description (http://gitlab.example.com/groups/my-group/-/iterations/13)\n \n", output.String())
151 assert.Empty(t, output.Stderr())
154func TestIterationListEmpty(t *testing.T) {
155 t.Parallel()
157 testClient := gitlabtesting.NewTestClient(t)
159 testClient.MockProjectIterations.EXPECT().
160 ListProjectIterations("OWNER/REPO", gomock.Any()).
161 Return([]*gitlab.ProjectIteration{}, nil, nil)
163 apiClient, err := api.NewClient(
164 func(*http.Client) (gitlab.AuthSource, error) {
165 return gitlab.AccessTokenAuthSource{Token: "test-token"}, nil
166 },
167 api.WithGitLabClient(testClient.Client),
168 )
169 require.NoError(t, err)
171 exec := cmdtest.SetupCmdForTest(t, NewCmdList, true,
172 cmdtest.WithApiClient(apiClient),
173 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
174 )
176 output, err := exec("")
177 require.NoError(t, err)
179 assert.Equal(t, "No iterations available on OWNER/REPO.\n\n", output.String())
180 assert.Empty(t, output.Stderr())
183func TestIterationListGroupEmpty(t *testing.T) {
184 t.Parallel()
186 testClient := gitlabtesting.NewTestClient(t)
188 testClient.MockGroupIterations.EXPECT().
189 ListGroupIterations("my-group", gomock.Any()).
190 Return([]*gitlab.GroupIteration{}, nil, nil)
192 apiClient, err := api.NewClient(
193 func(*http.Client) (gitlab.AuthSource, error) {
194 return gitlab.AccessTokenAuthSource{Token: "test-token"}, nil
195 },
196 api.WithGitLabClient(testClient.Client),
197 )
198 require.NoError(t, err)
200 exec := cmdtest.SetupCmdForTest(t, NewCmdList, true,
201 cmdtest.WithApiClient(apiClient),
202 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
203 )
205 output, err := exec("-g my-group")
206 require.NoError(t, err)
208 assert.Equal(t, "No iterations available on my-group.\n\n", output.String())
209 assert.Empty(t, output.Stderr())
212func TestIterationListHeaderTotal(t *testing.T) {
213 t.Parallel()
215 projectIterations := []*gitlab.ProjectIteration{
216 {Title: "Iteration II", WebURL: "http://gitlab.example.com/groups/my-group/-/iterations/13"},
217 }
218 groupIterations := []*gitlab.GroupIteration{
219 {Title: "Group Iteration", WebURL: "http://gitlab.example.com/groups/my-group/-/iterations/13"},
220 }
222 tests := []struct {
223 name string
224 cli string
225 setupMock func(tc *gitlabtesting.TestClient)
226 wantHeader string
227 }{
228 {
229 name: "project shows the total from the API",
230 cli: "",
231 setupMock: func(tc *gitlabtesting.TestClient) {
232 tc.MockProjectIterations.EXPECT().
233 ListProjectIterations("OWNER/REPO", gomock.Any()).
234 Return(projectIterations, &gitlab.Response{CurrentPage: 1, TotalPages: 25, TotalItems: 25}, nil)
235 },
236 wantHeader: "Showing 1 of 25 iterations on OWNER/REPO. (Page 1)\n",
237 },
238 {
239 name: "group shows the total from the API",
240 cli: "-g my-group --per-page 1 --page 25",
241 setupMock: func(tc *gitlabtesting.TestClient) {
242 tc.MockGroupIterations.EXPECT().
243 ListGroupIterations("my-group", gomock.Any()).
244 Return(groupIterations, &gitlab.Response{CurrentPage: 25, TotalPages: 25, TotalItems: 25}, nil)
245 },
246 wantHeader: "Showing 1 of 25 iterations on my-group. (Page 25)\n",
247 },
248 {
249 name: "project without a total from the API",
250 cli: "",
251 setupMock: func(tc *gitlabtesting.TestClient) {
252 tc.MockProjectIterations.EXPECT().
253 ListProjectIterations("OWNER/REPO", gomock.Any()).
254 Return(projectIterations, &gitlab.Response{CurrentPage: 1, NextPage: 2}, nil)
255 },
256 wantHeader: "Showing 1 iteration on OWNER/REPO. (Page 1)\n",
257 },
258 }
260 for _, tt := range tests {
261 t.Run(tt.name, func(t *testing.T) {
262 t.Parallel()
264 testClient := gitlabtesting.NewTestClient(t)
265 tt.setupMock(testClient)
267 apiClient, err := api.NewClient(
268 func(*http.Client) (gitlab.AuthSource, error) {
269 return gitlab.AccessTokenAuthSource{Token: "test-token"}, nil
270 },
271 api.WithGitLabClient(testClient.Client),
272 )
273 require.NoError(t, err)
275 exec := cmdtest.SetupCmdForTest(t, NewCmdList, true,
276 cmdtest.WithApiClient(apiClient),
277 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
278 )
280 output, err := exec(tt.cli)
281 require.NoError(t, err)
283 assert.True(t, strings.HasPrefix(output.String(), tt.wantHeader), "got %q", output.String())
284 assert.Empty(t, output.Stderr())
285 })
286 }
Diagnostics8 git calls · 247 ms
CallStatusSizeTime0247 ms
discover200185 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
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 · hit2 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=2
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 · 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
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:02001.8 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=1857
git.response.cache=HIT
git.response.duration_ms=4
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:0200173 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=173
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=8ac918baeed3139875cbcdc137f38cfb62fb2dd1
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=173
git.pkt.payload.max=146
git.sideband.band1.bytes=146
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=146
git.pack.trailer=9ba4921db5e89ed276b38a8c60b7a7cb31d1e799
Open pack map
fetchtree:0200144 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=144
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=2027645b4787a18b14dbda23b058e4fa9995a3d6
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=144
git.pkt.payload.max=117
git.sideband.band1.bytes=117
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=117
git.pack.trailer=aae2511ccb89aa136f2c1ef74b4cb9f466c4efbb
Open pack map
fetch2001.5 KiB234 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=234
git.response.bytes=1562
git.response.cache=MISS
git.response.duration_ms=234
git.fetch.wants=e6cfe19380f216581754a7675fea2aaec150a362
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=1562
git.pkt.payload.max=1535
git.sideband.band1.bytes=1535
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
a5e2f8208ac13a24
Request
0932e599…
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