GitLabgitlab-org
cli
main
1//go:build !integration
3package list
5import (
6 "regexp"
7 "strings"
8 "testing"
9 "time"
11 "github.com/MakeNowJust/heredoc/v2"
12 "github.com/stretchr/testify/assert"
13 "github.com/stretchr/testify/require"
14 "go.uber.org/mock/gomock"
16 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
17 gitlabtesting "gitlab.com/gitlab-org/api/client-go/v3/testing"
19 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
20)
22func TestCiList(t *testing.T) {
23 t.Parallel()
25 // GIVEN
26 testClient := gitlabtesting.NewTestClient(t)
28 createdAt1, _ := time.Parse(time.RFC3339, "2020-12-01T01:15:50.559Z")
29 updatedAt1, _ := time.Parse(time.RFC3339, "2020-12-01T01:36:41.737Z")
30 createdAt2, _ := time.Parse(time.RFC3339, "2020-11-30T18:20:47.571Z")
31 updatedAt2, _ := time.Parse(time.RFC3339, "2020-11-30T18:39:40.092Z")
33 testClient.MockPipelines.EXPECT().
34 ListProjectPipelines("OWNER/REPO", gomock.Any()).
35 Return([]*gitlab.PipelineInfo{
36 {
37 ID: 1,
38 IID: 3,
39 ProjectID: 5,
40 SHA: "c366255c71600e17519e802850ddcf7105d3cf66",
41 Ref: "refs/merge-requests/1107/merge",
42 Status: "success",
43 Source: "merge_request_event",
44 CreatedAt: &createdAt1,
45 UpdatedAt: &updatedAt1,
46 WebURL: "https://gitlab.com/OWNER/REPO/-/pipelines/710046436",
47 },
48 {
49 ID: 2,
50 IID: 4,
51 ProjectID: 5,
52 SHA: "c9a7c0d9351cd1e71d1c2ad8277f3bc7e3c47d1f",
53 Ref: "main",
54 Status: "success",
55 Source: "push",
56 CreatedAt: &createdAt2,
57 UpdatedAt: &updatedAt2,
58 WebURL: "https://gitlab.com/OWNER/REPO/-/pipelines/709793838",
59 },
60 }, nil, nil)
62 exec := cmdtest.SetupCmdForTest(
63 t,
64 NewCmdList,
65 false,
66 cmdtest.WithGitLabClient(testClient.Client),
67 )
69 // WHEN
70 output, err := exec("")
72 // THEN
73 require.NoError(t, err)
75 out := output.String()
76 timeRE := regexp.MustCompile(`\d+ years`)
77 out = timeRE.ReplaceAllString(out, "X years")
79 assert.Equal(t, heredoc.Doc(`
80 Showing 2 pipelines on OWNER/REPO. (Page 1)
82 State IID Ref Created
83 (success) • #1 (#3) refs/merge-requests/1107/merge (about X years ago)
84 (success) • #2 (#4) main (about X years ago)
86 `), out)
87 assert.Empty(t, output.Stderr())
88}
90func TestCiListHeaderTotal(t *testing.T) {
91 t.Parallel()
93 pipes := []*gitlab.PipelineInfo{
94 {ID: 1, IID: 3, Status: "success", Ref: "main"},
95 }
97 tests := []struct {
98 name string
99 cli string
100 resp *gitlab.Response
101 wantHeader string
102 }{
103 {
104 name: "shows the total from the API",
105 cli: "--per-page 20 --page 17",
106 resp: &gitlab.Response{CurrentPage: 17, TotalPages: 17, TotalItems: 326},
107 wantHeader: "Showing 1 of 326 pipelines on OWNER/REPO. (Page 17)\n",
108 },
109 {
110 name: "status filter shows the total from the API",
111 cli: "--status failed",
112 resp: &gitlab.Response{CurrentPage: 1, NextPage: 2, TotalPages: 4, TotalItems: 61},
113 wantHeader: "Showing 1 of 61 failed pipelines on OWNER/REPO. (Page 1)\n",
114 },
115 {
116 name: "without a total from the API",
117 cli: "",
118 resp: &gitlab.Response{CurrentPage: 1, NextPage: 2},
119 wantHeader: "Showing 1 pipeline on OWNER/REPO. (Page 1)\n",
120 },
121 }
123 for _, tt := range tests {
124 t.Run(tt.name, func(t *testing.T) {
125 t.Parallel()
127 testClient := gitlabtesting.NewTestClient(t)
128 testClient.MockPipelines.EXPECT().
129 ListProjectPipelines("OWNER/REPO", gomock.Any()).
130 Return(pipes, tt.resp, nil)
132 exec := cmdtest.SetupCmdForTest(
133 t,
134 NewCmdList,
135 false,
136 cmdtest.WithGitLabClient(testClient.Client),
137 )
139 output, err := exec(tt.cli)
140 require.NoError(t, err)
142 assert.True(t, strings.HasPrefix(output.String(), tt.wantHeader), "got %q", output.String())
143 assert.Empty(t, output.Stderr())
144 })
145 }
146}
148func TestCiListJSON(t *testing.T) {
149 t.Parallel()
151 // GIVEN
152 testClient := gitlabtesting.NewTestClient(t)
154 createdAt1, _ := time.Parse(time.RFC3339, "2024-02-11T18:55:08.793Z")
155 updatedAt1, _ := time.Parse(time.RFC3339, "2024-02-11T18:56:07.777Z")
156 createdAt2, _ := time.Parse(time.RFC3339, "2024-02-10T18:55:16.722Z")
157 updatedAt2, _ := time.Parse(time.RFC3339, "2024-02-10T18:56:13.972Z")
159 testClient.MockPipelines.EXPECT().
160 ListProjectPipelines("OWNER/REPO", gomock.Any()).
161 Return([]*gitlab.PipelineInfo{
162 {
163 ID: 1172622998,
164 IID: 338,
165 ProjectID: 37777023,
166 Status: "success",
167 Source: "schedule",
168 Name: "foo",
169 Ref: "#test#",
170 SHA: "3c890c11d784329052aa4ff63526dde2fa65b320",
171 WebURL: "https://gitlab.com/jay_mccure/test2target/-/pipelines/1172622998",
172 UpdatedAt: &updatedAt1,
173 CreatedAt: &createdAt1,
174 },
175 {
176 ID: 1172086480,
177 IID: 337,
178 ProjectID: 37777023,
179 Status: "success",
180 Source: "schedule",
181 Name: "bar",
182 Ref: "#test#",
183 SHA: "3c890c11d784329052aa4ff63526dde2fa65b320",
184 WebURL: "https://gitlab.com/jay_mccure/test2target/-/pipelines/1172086480",
185 UpdatedAt: &updatedAt2,
186 CreatedAt: &createdAt2,
187 },
188 }, nil, nil)
190 exec := cmdtest.SetupCmdForTest(
191 t,
192 NewCmdList,
193 false,
194 cmdtest.WithGitLabClient(testClient.Client),
195 )
197 // WHEN
198 output, err := exec("-F json")
200 // THEN
201 require.NoError(t, err)
203 expectedOut := `[
204 {
205 "id": 1172622998,
206 "iid": 338,
207 "project_id": 37777023,
208 "status": "success",
209 "source": "schedule",
210 "name": "foo",
211 "ref": "#test#",
212 "sha": "3c890c11d784329052aa4ff63526dde2fa65b320",
213 "web_url": "https://gitlab.com/jay_mccure/test2target/-/pipelines/1172622998",
214 "updated_at": "2024-02-11T18:56:07.777Z",
215 "created_at": "2024-02-11T18:55:08.793Z"
216 },
217 {
218 "id": 1172086480,
219 "iid": 337,
220 "project_id": 37777023,
221 "status": "success",
222 "source": "schedule",
223 "name": "bar",
224 "ref": "#test#",
225 "sha": "3c890c11d784329052aa4ff63526dde2fa65b320",
226 "web_url": "https://gitlab.com/jay_mccure/test2target/-/pipelines/1172086480",
227 "updated_at": "2024-02-10T18:56:13.972Z",
228 "created_at": "2024-02-10T18:55:16.722Z"
229 }
230]`
232 assert.JSONEq(t, expectedOut, output.String())
233 assert.Empty(t, output.Stderr())
234}
Diagnostics8 git calls · 231 ms
CallStatusSizeTime0231 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=44Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · 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=4273 git.response.cache=HIT git.response.duration_ms=3 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=fb03183b7909876778f740d196d7516811003517Open pack map
fetchtree:0200635 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=635 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=517b073a0d5c32bc4e93f86de74b31d60ff8cf03 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=635 git.pkt.payload.max=608 git.sideband.band1.bytes=608 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=608 git.pack.trailer=178abb9456d874b7fd38fa21ad5cf9ca3ebcccaaOpen pack map
fetchtree:0200168 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=168 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=6ca5b26387fd434500ad395c6d60920a43ede2d5 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=168 git.pkt.payload.max=141 git.sideband.band1.bytes=141 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=141 git.pack.trailer=571be34d9227ee765b60b0ef0698b1edad444b18Open pack map
fetch2001.8 KiB216 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=216 git.response.bytes=1850 git.response.cache=MISS git.response.duration_ms=216 git.fetch.wants=067d0a234e48dbfb45000b4ef74764496d7e8a15 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=1850 git.pkt.payload.max=1823 git.sideband.band1.bytes=1823 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
- a5e2e17299d14af4
- Request
- 3a3016e2…
- 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