GitLabgitlab-org

cli

main
ply9 git calls255 ms99 KiB7 cachedprotocol v2
173 lines4.5 KiBRaw
1//go:build !integration
3package list
5import (
6 "strings"
7 "testing"
8 "time"
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 gitlab_testing "gitlab.com/gitlab-org/api/client-go/v3/testing"
18 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
21func TestAgentList(t *testing.T) {
22 // GIVEN
23 // NOTE: we need to force disable colors, otherwise we'd need ANSI sequences in our test output assertions.
24 t.Setenv("NO_COLOR", "true")
26 tc := gitlab_testing.NewTestClient(t)
27 exec := cmdtest.SetupCmdForTest(t, NewCmdAgentList, false, cmdtest.WithGitLabClient(tc.Client))
29 tc.MockClusterAgents.EXPECT().
30 ListAgents("OWNER/REPO", &gitlab.ListAgentsOptions{ListOptions: gitlab.ListOptions{Page: 1, PerPage: 30}}).
31 Return([]*gitlab.Agent{
32 {
33 ID: 1,
34 Name: "local",
35 CreatedAt: new(time.Now().Add(-24 * time.Hour)),
36 },
37 {
38 ID: 2,
39 Name: "prd",
40 CreatedAt: new(time.Now().Add(-24 * time.Hour)),
41 },
42 }, &gitlab.Response{}, nil).
43 Times(1)
45 // WHEN
46 output, err := exec("")
47 if err != nil {
48 t.Errorf("error running command `cluster agent list`: %v", err)
49 }
51 // THEN
52 assert.Equal(t, heredoc.Doc(`
53 Showing 2 agents on OWNER/REPO. (Page 1)
55 ID Name Created At
56 1 local about 1 day ago
57 2 prd about 1 day ago
59 `), output.String())
60 assert.Empty(t, output.Stderr())
63func TestAgentList_Pagination(t *testing.T) {
64 // GIVEN
65 // NOTE: we need to force disable colors, otherwise we'd need ANSI sequences in our test output assertions.
66 t.Setenv("NO_COLOR", "true")
68 tc := gitlab_testing.NewTestClient(t)
69 exec := cmdtest.SetupCmdForTest(t, NewCmdAgentList, false, cmdtest.WithGitLabClient(tc.Client))
71 tc.MockClusterAgents.EXPECT().
72 ListAgents("OWNER/REPO", &gitlab.ListAgentsOptions{ListOptions: gitlab.ListOptions{Page: 2, PerPage: 1}}).
73 Return([]*gitlab.Agent{
74 {
75 ID: 2,
76 Name: "prd",
77 CreatedAt: new(time.Now().Add(-24 * time.Hour)),
78 },
79 }, &gitlab.Response{NextPage: 0}, nil)
81 // WHEN
82 output, err := exec("--page 2 --per-page 1")
83 if err != nil {
84 t.Errorf("error running command `cluster agent list`: %v", err)
85 }
87 assert.Equal(t, heredoc.Doc(`
88 Showing 1 agent on OWNER/REPO. (Page 2)
90 ID Name Created At
91 2 prd about 1 day ago
93 `), output.String())
94 assert.Empty(t, output.Stderr())
97func TestAgentList_HeaderTotal(t *testing.T) {
98 t.Parallel()
100 agents := []*gitlab.Agent{
101 {ID: 2, Name: "prd", CreatedAt: new(time.Now().Add(-24 * time.Hour))},
102 }
104 tests := []struct {
105 name string
106 cli string
107 resp *gitlab.Response
108 wantHeader string
109 }{
110 {
111 name: "shows the total from the API",
112 cli: "--page 2 --per-page 1",
113 resp: &gitlab.Response{CurrentPage: 2, TotalPages: 7, TotalItems: 7},
114 wantHeader: "Showing 1 of 7 agents on OWNER/REPO. (Page 2)",
115 },
116 {
117 name: "without a total from the API",
118 resp: &gitlab.Response{CurrentPage: 1, NextPage: 2},
119 wantHeader: "Showing 1 agent on OWNER/REPO. (Page 1)",
120 },
121 }
123 for _, tt := range tests {
124 t.Run(tt.name, func(t *testing.T) {
125 t.Parallel()
127 tc := gitlab_testing.NewTestClient(t)
128 tc.MockClusterAgents.EXPECT().
129 ListAgents("OWNER/REPO", gomock.Any()).
130 Return(agents, tt.resp, nil)
132 exec := cmdtest.SetupCmdForTest(t, NewCmdAgentList, false, cmdtest.WithGitLabClient(tc.Client))
134 output, err := exec(tt.cli)
135 require.NoError(t, err)
137 header, _, _ := strings.Cut(output.String(), "\n")
138 assert.Equal(t, tt.wantHeader, header)
139 assert.Empty(t, output.Stderr())
140 })
141 }
144func TestAgentList_JSON(t *testing.T) {
145 t.Parallel()
147 tc := gitlab_testing.NewTestClient(t)
148 exec := cmdtest.SetupCmdForTest(t, NewCmdAgentList, false, cmdtest.WithGitLabClient(tc.Client))
150 tc.MockClusterAgents.EXPECT().
151 ListAgents("OWNER/REPO", &gitlab.ListAgentsOptions{ListOptions: gitlab.ListOptions{Page: 1, PerPage: 30}}).
152 Return([]*gitlab.Agent{
153 {
154 ID: 1,
155 Name: "local",
156 CreatedAt: new(time.Now().Add(-24 * time.Hour)),
157 },
158 {
159 ID: 2,
160 Name: "prd",
161 CreatedAt: new(time.Now().Add(-24 * time.Hour)),
162 },
163 }, &gitlab.Response{}, nil)
165 output, err := exec("--output json")
166 require.NoError(t, err)
168 assert.Contains(t, output.String(), `"id":1`)
169 assert.Contains(t, output.String(), `"name":"local"`)
170 assert.Contains(t, output.String(), `"id":2`)
171 assert.Contains(t, output.String(), `"name":"prd"`)
172 assert.Empty(t, output.Stderr())
Diagnostics9 git calls · 255 ms
CallStatusSizeTime0255 ms
discover200185 B2 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 · 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=4
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 · hit4 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=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 · 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=fb03183b7909876778f740d196d7516811003517
Open pack map
fetchtree:0200200 B · hit2 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=200
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=9988d47506dcbe3004b8b72cbdb9d66d6d75cda4
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=200
git.pkt.payload.max=173
git.sideband.band1.bytes=173
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=173
git.pack.trailer=948ee807a1ee817af67f2df94421a202e82325f9
Open pack map
fetchtree:0200405 B · hit1 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=405
git.response.cache=HIT
git.response.duration_ms=1
git.fetch.filter=tree:0
git.fetch.wants=d271680e941992259de0ff84d4a04bcbba55f531
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=405
git.pkt.payload.max=378
git.sideband.band1.bytes=378
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=378
git.pack.trailer=adfacba5ff1f4fcf28dcaac12e2874f6d48a7934
Open pack map
fetchtree:0200140 B · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=241
git.response.bytes=140
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=5b521a5bc478dc3ca07de19b3e536f98a592b2de
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=140
git.pkt.payload.max=113
git.sideband.band1.bytes=113
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=113
git.pack.trailer=4808e6c4c64311f8ca8e41091dd8f88e2b0d68f5
Open pack map
fetch2001.3 KiB241 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=1319
git.response.cache=MISS
git.response.duration_ms=241
git.fetch.wants=1be8c390ab86088aecef8eb908c28e9c33e003dc
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=1319
git.pkt.payload.max=1292
git.sideband.band1.bytes=1292
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
a5eb08d3edff21ca
Request
008598db…
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