GitLabgitlab-org

cli

main
ply8 git calls222 ms99 KiB5 cachedprotocol v2
250 lines7.1 KiBRaw
1//go:build !integration
3package retry
5import (
6 "fmt"
7 "net/http"
8 "testing"
9 "time"
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 TestCiRetry(t *testing.T) {
22 t.Parallel()
24 createdAt, _ := time.Parse(time.RFC3339, "2022-12-01T05:13:13.703Z")
26 // Response indicating last page
27 lastPageResponse := &gitlab.Response{
28 Response: &http.Response{StatusCode: http.StatusOK},
29 NextPage: 0,
30 }
32 type testCase struct {
33 name string
34 args string
35 expectedError string
36 expectedStderr string
37 expectedOut string
38 setupMock func(tc *gitlabtesting.TestClient)
39 }
41 tests := []testCase{
42 {
43 name: "when retry with job-id",
44 args: "1122",
45 expectedOut: "Retried job (ID: 1123), status: pending, ref: branch-name, weburl: https://gitlab.com/OWNER/REPO/-/jobs/1123\n",
46 setupMock: func(tc *gitlabtesting.TestClient) {
47 tc.MockJobs.EXPECT().
48 RetryJob("OWNER/REPO", int64(1122), gomock.Any()).
49 Return(&gitlab.Job{
50 ID: 1123,
51 Status: "pending",
52 Stage: "build",
53 Name: "build-job",
54 Ref: "branch-name",
55 Tag: false,
56 AllowFailure: false,
57 CreatedAt: &createdAt,
58 WebURL: "https://gitlab.com/OWNER/REPO/-/jobs/1123",
59 }, nil, nil)
60 },
61 },
62 {
63 name: "when retry with job-id throws error",
64 args: "1122",
65 expectedError: "403 Forbidden",
66 expectedOut: "",
67 setupMock: func(tc *gitlabtesting.TestClient) {
68 forbiddenResponse := &gitlab.Response{Response: &http.Response{StatusCode: http.StatusForbidden}}
69 tc.MockJobs.EXPECT().
70 RetryJob("OWNER/REPO", int64(1122), gomock.Any()).
71 Return(nil, forbiddenResponse, fmt.Errorf("403 Forbidden"))
72 },
73 },
74 {
75 name: "when retry with job-name",
76 args: "lint -b main -p 123",
77 expectedOut: "Retried job (ID: 1123), status: pending, ref: branch-name, weburl: https://gitlab.com/OWNER/REPO/-/jobs/1123\n",
78 setupMock: func(tc *gitlabtesting.TestClient) {
79 tc.MockJobs.EXPECT().
80 ListPipelineJobs("OWNER/REPO", int64(123), gomock.Any(), gomock.Any()).
81 Return([]*gitlab.Job{
82 {
83 ID: 1122,
84 Name: "lint",
85 Status: "failed",
86 },
87 {
88 ID: 1124,
89 Name: "publish",
90 Status: "failed",
91 },
92 }, lastPageResponse, nil)
94 tc.MockJobs.EXPECT().
95 RetryJob("OWNER/REPO", int64(1122), gomock.Any()).
96 Return(&gitlab.Job{
97 ID: 1123,
98 Status: "pending",
99 Stage: "build",
100 Name: "build-job",
101 Ref: "branch-name",
102 Tag: false,
103 AllowFailure: false,
104 CreatedAt: &createdAt,
105 WebURL: "https://gitlab.com/OWNER/REPO/-/jobs/1123",
106 }, nil, nil)
107 },
108 },
109 {
110 name: "when retry with job-name throws error",
111 args: "lint -b main -p 123",
112 expectedError: "list pipeline jobs: 403 Forbidden",
113 expectedStderr: "invalid job ID: lint\n",
114 setupMock: func(tc *gitlabtesting.TestClient) {
115 forbiddenResponse := &gitlab.Response{Response: &http.Response{StatusCode: http.StatusForbidden}}
116 tc.MockJobs.EXPECT().
117 ListPipelineJobs("OWNER/REPO", int64(123), gomock.Any(), gomock.Any()).
118 Return(nil, forbiddenResponse, fmt.Errorf("403 Forbidden"))
119 },
120 },
121 {
122 name: "when retry with job-name and last pipeline",
123 args: "lint -b main",
124 expectedOut: "Retried job (ID: 1123), status: pending, ref: branch-name, weburl: https://gitlab.com/OWNER/REPO/-/jobs/1123\n",
125 setupMock: func(tc *gitlabtesting.TestClient) {
126 // GetPipelineWithFallback tries GetLatestPipeline first
127 tc.MockPipelines.EXPECT().
128 GetLatestPipeline("OWNER/REPO", gomock.Any(), gomock.Any()).
129 Return(&gitlab.Pipeline{
130 ID: 123,
131 }, nil, nil)
132 // Check if pipeline has jobs
133 tc.MockJobs.EXPECT().
134 ListPipelineJobs("OWNER/REPO", int64(123), gomock.Any()).
135 Return([]*gitlab.Job{
136 {ID: 1, Name: "test"},
137 }, nil, nil)
139 // GetJobId lists all jobs for the pipeline
140 tc.MockJobs.EXPECT().
141 ListPipelineJobs("OWNER/REPO", int64(123), gomock.Any(), gomock.Any()).
142 Return([]*gitlab.Job{
143 {
144 ID: 1122,
145 Name: "lint",
146 Status: "failed",
147 },
148 {
149 ID: 1124,
150 Name: "publish",
151 Status: "failed",
152 },
153 }, lastPageResponse, nil)
155 tc.MockJobs.EXPECT().
156 RetryJob("OWNER/REPO", int64(1122), gomock.Any()).
157 Return(&gitlab.Job{
158 ID: 1123,
159 Status: "pending",
160 Stage: "build",
161 Name: "build-job",
162 Ref: "branch-name",
163 Tag: false,
164 AllowFailure: false,
165 CreatedAt: &createdAt,
166 WebURL: "https://gitlab.com/OWNER/REPO/-/jobs/1123",
167 }, nil, nil)
168 },
169 },
170 {
171 name: "when retry uses current git branch if no branch specified",
172 args: "lint",
173 expectedOut: "Retried job (ID: 1123), status: pending, ref: feature-branch, weburl: https://gitlab.com/OWNER/REPO/-/jobs/1123\n",
174 setupMock: func(tc *gitlabtesting.TestClient) {
175 // GetPipelineWithFallback tries GetLatestPipeline for current branch
176 tc.MockPipelines.EXPECT().
177 GetLatestPipeline("OWNER/REPO", gomock.Any(), gomock.Any()).
178 Return(&gitlab.Pipeline{
179 ID: 456,
180 }, nil, nil)
181 // Check if pipeline has jobs
182 tc.MockJobs.EXPECT().
183 ListPipelineJobs("OWNER/REPO", int64(456), gomock.Any()).
184 Return([]*gitlab.Job{
185 {ID: 1, Name: "test"},
186 }, nil, nil)
188 // GetJobId lists all jobs for the pipeline
189 tc.MockJobs.EXPECT().
190 ListPipelineJobs("OWNER/REPO", int64(456), gomock.Any(), gomock.Any()).
191 Return([]*gitlab.Job{
192 {
193 ID: 1122,
194 Name: "lint",
195 Status: "failed",
196 },
197 }, lastPageResponse, nil)
199 tc.MockJobs.EXPECT().
200 RetryJob("OWNER/REPO", int64(1122), gomock.Any()).
201 Return(&gitlab.Job{
202 ID: 1123,
203 Status: "pending",
204 Stage: "build",
205 Name: "lint",
206 Ref: "feature-branch",
207 Tag: false,
208 AllowFailure: false,
209 CreatedAt: &createdAt,
210 WebURL: "https://gitlab.com/OWNER/REPO/-/jobs/1123",
211 }, nil, nil)
212 },
213 },
214 }
216 for _, tc := range tests {
217 t.Run(tc.name, func(t *testing.T) {
218 t.Parallel()
220 // GIVEN
221 testClient := gitlabtesting.NewTestClient(t)
222 tc.setupMock(testClient)
224 exec := cmdtest.SetupCmdForTest(
225 t,
226 NewCmdRetry,
227 false,
228 cmdtest.WithGitLabClient(testClient.Client),
229 )
231 // WHEN
232 output, err := exec(tc.args)
234 // THEN
235 if tc.expectedError == "" {
236 require.NoError(t, err)
237 } else {
238 require.Error(t, err)
239 assert.Contains(t, err.Error(), tc.expectedError)
240 }
242 assert.Equal(t, tc.expectedOut, output.String())
243 if tc.expectedStderr != "" {
244 assert.Equal(t, tc.expectedStderr, output.Stderr())
245 } else {
246 assert.Empty(t, output.Stderr())
247 }
248 })
249 }
Diagnostics8 git calls · 222 ms
CallStatusSizeTime0222 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 KiB9 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=UPDATING
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=3
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:0200635 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=635
git.response.cache=HIT
git.response.duration_ms=2
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=178abb9456d874b7fd38fa21ad5cf9ca3ebcccaa
Open pack map
fetchtree:0200137 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=137
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=7d6a9ade386eab1e37ce41913a0f19098bb4656e
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=137
git.pkt.payload.max=110
git.sideband.band1.bytes=110
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=110
git.pack.trailer=44db91f98d6ad28788e74425747ec5295e6fb29b
Open pack map
fetch2001.5 KiB205 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=205
git.response.bytes=1516
git.response.cache=MISS
git.response.duration_ms=205
git.fetch.wants=9fbfa22d7a37f614104138000f80ed81dd8004e6
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=1516
git.pkt.payload.max=1489
git.sideband.band1.bytes=1489
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
a5e2fb3e9c9ecf81
Request
3b0390c1…
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