GitLabgitlab-org
cli
main
1//go:build !integration
3package trace
5import (
6 "bytes"
7 "fmt"
8 "net/http"
9 "testing"
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"
19)
21func TestCiTrace(t *testing.T) {
22 t.Parallel()
24 // Response indicating last page
25 lastPageResponse := &gitlab.Response{
26 Response: &http.Response{StatusCode: http.StatusOK},
27 NextPage: 0,
28 }
30 type testCase struct {
31 name string
32 args string
33 expectedOut string
34 expectedError string
35 setupMock func(tc *gitlabtesting.TestClient)
36 }
38 tests := []testCase{
39 {
40 name: "when trace for job-id is requested",
41 args: "1122",
42 expectedOut: "\nGetting job trace...\nShowing logs for lint job #1122.\nLorem ipsum",
43 setupMock: func(tc *gitlabtesting.TestClient) {
44 tc.MockJobs.EXPECT().
45 GetJob("OWNER/REPO", int64(1122), gomock.Any()).
46 Return(&gitlab.Job{
47 ID: 1122,
48 Name: "lint",
49 Status: "success",
50 }, nil, nil)
52 tc.MockJobs.EXPECT().
53 GetTraceFile("OWNER/REPO", int64(1122), gomock.Any()).
54 Return(bytes.NewReader([]byte("Lorem ipsum")), nil, nil)
55 },
56 },
57 {
58 name: "when trace for job-id is requested and getTrace throws error",
59 args: "1122",
60 expectedError: "failed to find job",
61 expectedOut: "\nGetting job trace...\nShowing logs for lint job #1122.\n",
62 setupMock: func(tc *gitlabtesting.TestClient) {
63 tc.MockJobs.EXPECT().
64 GetJob("OWNER/REPO", int64(1122), gomock.Any()).
65 Return(&gitlab.Job{
66 ID: 1122,
67 Name: "lint",
68 Status: "success",
69 }, nil, nil)
71 forbiddenResponse := &gitlab.Response{Response: &http.Response{StatusCode: http.StatusForbidden}}
72 tc.MockJobs.EXPECT().
73 GetTraceFile("OWNER/REPO", int64(1122), gomock.Any()).
74 Return(nil, forbiddenResponse, fmt.Errorf("GET https://gitlab.com/api/v4/projects/OWNER%%2FREPO/jobs/1122/trace: 403"))
75 },
76 },
77 {
78 name: "when trace for job-id is requested and getJob throws error",
79 args: "1122",
80 expectedError: "failed to find job",
81 expectedOut: "\nGetting job trace...\n",
82 setupMock: func(tc *gitlabtesting.TestClient) {
83 forbiddenResponse := &gitlab.Response{Response: &http.Response{StatusCode: http.StatusForbidden}}
84 tc.MockJobs.EXPECT().
85 GetJob("OWNER/REPO", int64(1122), gomock.Any()).
86 Return(nil, forbiddenResponse, fmt.Errorf("GET https://gitlab.com/api/v4/projects/OWNER%%2FREPO/jobs/1122: 403"))
87 },
88 },
89 {
90 name: "when trace for job-name is requested",
91 args: "lint -b main -p 123",
92 expectedOut: "\nGetting job trace...\nShowing logs for lint job #1122.\nLorem ipsum",
93 setupMock: func(tc *gitlabtesting.TestClient) {
94 tc.MockJobs.EXPECT().
95 ListPipelineJobs("OWNER/REPO", int64(123), gomock.Any(), gomock.Any()).
96 Return([]*gitlab.Job{
97 {
98 ID: 1122,
99 Name: "lint",
100 Status: "failed",
101 },
102 {
103 ID: 1124,
104 Name: "publish",
105 Status: "failed",
106 },
107 }, lastPageResponse, nil)
109 tc.MockJobs.EXPECT().
110 GetJob("OWNER/REPO", int64(1122), gomock.Any()).
111 Return(&gitlab.Job{
112 ID: 1122,
113 Name: "lint",
114 Status: "success",
115 }, nil, nil)
117 tc.MockJobs.EXPECT().
118 GetTraceFile("OWNER/REPO", int64(1122), gomock.Any()).
119 Return(bytes.NewReader([]byte("Lorem ipsum")), nil, nil)
120 },
121 },
122 {
123 name: "when trace for job-name and last pipeline is requested",
124 args: "lint -b main",
125 expectedOut: "\nGetting job trace...\nShowing logs for lint job #1122.\nLorem ipsum",
126 setupMock: func(tc *gitlabtesting.TestClient) {
127 // GetPipelineWithFallback tries GetLatestPipeline first
128 tc.MockPipelines.EXPECT().
129 GetLatestPipeline("OWNER/REPO", gomock.Any(), gomock.Any()).
130 Return(&gitlab.Pipeline{
131 ID: 123,
132 }, nil, nil)
133 // Check if pipeline has jobs
134 tc.MockJobs.EXPECT().
135 ListPipelineJobs("OWNER/REPO", int64(123), gomock.Any()).
136 Return([]*gitlab.Job{
137 {ID: 1, Name: "test"},
138 }, nil, nil)
140 // GetJobId lists all jobs for the pipeline
141 tc.MockJobs.EXPECT().
142 ListPipelineJobs("OWNER/REPO", int64(123), gomock.Any(), gomock.Any()).
143 Return([]*gitlab.Job{
144 {
145 ID: 1122,
146 Name: "lint",
147 Status: "failed",
148 },
149 {
150 ID: 1124,
151 Name: "publish",
152 Status: "failed",
153 },
154 }, lastPageResponse, nil)
156 tc.MockJobs.EXPECT().
157 GetJob("OWNER/REPO", int64(1122), gomock.Any()).
158 Return(&gitlab.Job{
159 ID: 1122,
160 Name: "lint",
161 Status: "success",
162 }, nil, nil)
164 tc.MockJobs.EXPECT().
165 GetTraceFile("OWNER/REPO", int64(1122), gomock.Any()).
166 Return(bytes.NewReader([]byte("Lorem ipsum")), nil, nil)
167 },
168 },
169 }
171 for _, tc := range tests {
172 t.Run(tc.name, func(t *testing.T) {
173 t.Parallel()
175 // GIVEN
176 testClient := gitlabtesting.NewTestClient(t)
177 tc.setupMock(testClient)
179 exec := cmdtest.SetupCmdForTest(
180 t,
181 NewCmdTrace,
182 false,
183 cmdtest.WithGitLabClient(testClient.Client),
184 )
186 // WHEN
187 output, err := exec(tc.args)
189 // THEN
190 if tc.expectedError == "" {
191 require.NoError(t, err)
192 } else {
193 require.Error(t, err)
194 assert.Contains(t, err.Error(), tc.expectedError)
195 }
197 assert.Equal(t, tc.expectedOut, output.String())
198 assert.Empty(t, output.Stderr())
199 })
200 }
201}
Diagnostics8 git calls · 269 ms
CallStatusSizeTime0269 ms
discover200185 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch 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 · 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 · 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
fetchtree:02001.8 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=1857 git.response.cache=HIT git.response.duration_ms=2 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 · hit2 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=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=178abb9456d874b7fd38fa21ad5cf9ca3ebcccaaOpen pack map
fetchtree:0200136 B · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=256 git.response.bytes=136 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=8fe7ff1b61d441ceb25909f6d23725302537cf2d 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=136 git.pkt.payload.max=109 git.sideband.band1.bytes=109 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=109 git.pack.trailer=e3ddd426ff37a5621b2ea8cc8c359a45bcaca22cOpen pack map
fetch2001.3 KiB256 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=1335 git.response.cache=MISS git.response.duration_ms=256 git.fetch.wants=ea775730c900101965f70ad4f86f81ac73651f53 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=1335 git.pkt.payload.max=1308 git.sideband.band1.bytes=1308 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
- a5e2c3dc28c7aedd
- Request
- 7e4fb3b8…
- 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