GitLabgitlab-org

cli

main
ply8 git calls213 ms99 KiB5 cachedprotocol v2
172 lines4.5 KiBRaw
1//go:build !integration
3package jobs
5import (
6 "encoding/json"
7 "strings"
8 "testing"
10 "github.com/MakeNowJust/heredoc/v2"
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/api"
19 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
22func TestRunnerJobs_Success(t *testing.T) {
23 t.Parallel()
25 tc := gitlabtesting.NewTestClient(t)
26 exec := cmdtest.SetupCmdForTest(
27 t,
28 NewCmd,
29 false,
30 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "", api.WithGitLabClient(tc.Client))),
31 )
33 jobsResponse := []*gitlab.Job{
34 {ID: 2, Status: "success", Stage: "test", Name: "test", Ref: "main", Project: &gitlab.Project{PathWithNamespace: "owner/repo"}},
35 }
37 tc.MockRunners.EXPECT().
38 ListRunnerJobs(int64(9), gomock.Any(), gomock.Any()).
39 Return(jobsResponse, nil, nil)
41 out, err := exec("9")
42 require.NoError(t, err)
44 expectedOutput := heredoc.Docf(`
45 Showing 1 job on . (Page 1)
47 ID%[1]sStatus%[1]sStage%[1]sName%[1]sRef%[1]sProject
48 2%[1]ssuccess%[1]stest%[1]stest%[1]smain%[1]sowner/repo
50 `, "\t")
51 assert.Equal(t, expectedOutput, out.OutBuf.String())
54func TestRunnerJobs_OutputJSON(t *testing.T) {
55 t.Parallel()
57 tc := gitlabtesting.NewTestClient(t)
58 exec := cmdtest.SetupCmdForTest(
59 t,
60 NewCmd,
61 false,
62 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "", api.WithGitLabClient(tc.Client))),
63 )
65 jobsResponse := []*gitlab.Job{
66 {ID: 7, Status: "running", Stage: "build", Name: "compile", Ref: "feature", Project: &gitlab.Project{PathWithNamespace: "group/proj"}},
67 }
69 tc.MockRunners.EXPECT().
70 ListRunnerJobs(int64(3), gomock.Any(), gomock.Any()).
71 Return(jobsResponse, nil, nil)
73 out, err := exec("3 --output json")
74 require.NoError(t, err)
75 var decoded []*gitlab.Job
76 err = json.Unmarshal(out.OutBuf.Bytes(), &decoded)
77 require.NoError(t, err)
78 require.Len(t, decoded, 1)
79 assert.Equal(t, int64(7), decoded[0].ID)
80 assert.Equal(t, "running", decoded[0].Status)
81 assert.Equal(t, "build", decoded[0].Stage)
82 assert.Equal(t, "compile", decoded[0].Name)
83 assert.Equal(t, "feature", decoded[0].Ref)
84 require.NotNil(t, decoded[0].Project)
85 assert.Equal(t, "group/proj", decoded[0].Project.PathWithNamespace)
88func TestRunnerJobs_HeaderTotal(t *testing.T) {
89 t.Parallel()
91 jobs := []*gitlab.Job{
92 {ID: 2, Status: "success", Stage: "test", Name: "test", Ref: "main", Project: &gitlab.Project{PathWithNamespace: "owner/repo"}},
93 }
95 tests := []struct {
96 name string
97 cli string
98 resp *gitlab.Response
99 wantHeader string
100 }{
101 {
102 name: "shows the total from the API",
103 cli: "9 --page 3 --per-page 1",
104 resp: &gitlab.Response{CurrentPage: 3, TotalPages: 53, TotalItems: 53},
105 wantHeader: "Showing 1 of 53 jobs on . (Page 3)\n",
106 },
107 {
108 name: "without a total from the API",
109 cli: "9",
110 resp: &gitlab.Response{CurrentPage: 1, NextPage: 2},
111 wantHeader: "Showing 1 job on . (Page 1)\n",
112 },
113 }
115 for _, tt := range tests {
116 t.Run(tt.name, func(t *testing.T) {
117 t.Parallel()
119 tc := gitlabtesting.NewTestClient(t)
120 tc.MockRunners.EXPECT().
121 ListRunnerJobs(int64(9), gomock.Any(), gomock.Any()).
122 Return(jobs, tt.resp, nil)
124 exec := cmdtest.SetupCmdForTest(
125 t,
126 NewCmd,
127 false,
128 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "", api.WithGitLabClient(tc.Client))),
129 )
131 out, err := exec(tt.cli)
132 require.NoError(t, err)
134 assert.True(t, strings.HasPrefix(out.String(), tt.wantHeader), "got %q", out.String())
135 assert.Empty(t, out.Stderr())
136 })
137 }
140func TestRunnerJobs_InvalidRunnerID(t *testing.T) {
141 t.Parallel()
142 exec := cmdtest.SetupCmdForTest(t, NewCmd, false)
143 _, err := exec("invalid")
144 require.Error(t, err)
145 assert.Contains(t, err.Error(), `invalid runner ID: "invalid"`)
148func TestRunnerJobs_WithStatus(t *testing.T) {
149 t.Parallel()
151 tc := gitlabtesting.NewTestClient(t)
152 exec := cmdtest.SetupCmdForTest(
153 t,
154 NewCmd,
155 false,
156 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "", api.WithGitLabClient(tc.Client))),
157 )
159 tc.MockRunners.EXPECT().
160 ListRunnerJobs(int64(5), gomock.Any(), gomock.Any()).
161 Return([]*gitlab.Job{}, nil, nil)
163 out, err := exec("5 --status running")
164 require.NoError(t, err)
166 expectedOutput := heredoc.Docf(`
167 No jobs available on .
168 ID%[1]sStatus%[1]sStage%[1]sName%[1]sRef%[1]sProject
170 `, "\t")
171 assert.Equal(t, expectedOutput, out.OutBuf.String())
Diagnostics8 git calls · 213 ms
CallStatusSizeTime0213 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 KiB3 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=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
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=3
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
fetchcombine:blob:none+tree:2 · deepen 12004.2 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=4273
git.response.cache=HIT
git.response.duration_ms=4
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 · 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=1857
git.response.cache=HIT
git.response.duration_ms=3
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 · 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=314
git.response.cache=HIT
git.response.duration_ms=3
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: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=4
git.response.bytes=136
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=38f7f95c04f88f1ad745ad11891ebca6c1bde10b
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=84f92e84cf8c6917e7451046591f286215fca2fb
Open pack map
fetch2001.4 KiB196 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=196
git.response.bytes=1391
git.response.cache=MISS
git.response.duration_ms=196
git.fetch.wants=db7392e3a78febd1b4a3d1d328c171e2073861c0
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=1391
git.pkt.payload.max=1364
git.sideband.band1.bytes=1364
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
a5e34ab63d538a31
Request
3dedab09…
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