GitLabgitlab-org

cli

main
ply8 git calls215 ms99 KiB5 cachedprotocol v2
216 lines4.5 KiBRaw
1//go:build !integration
3package get
5import (
6 "bytes"
7 "testing"
9 "github.com/google/shlex"
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
12 "go.uber.org/mock/gomock"
14 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
15 gitlabtesting "gitlab.com/gitlab-org/api/client-go/v3/testing"
17 "gitlab.com/gitlab-org/cli/internal/api"
18 "gitlab.com/gitlab-org/cli/internal/glrepo"
19 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
22func Test_NewCmdGet(t *testing.T) {
23 tests := []struct {
24 name string
25 cli string
26 wants options
27 wantsErr bool
28 }{
29 {
30 name: "good key",
31 cli: "good_key",
32 wantsErr: false,
33 wants: options{
34 key: "good_key",
35 },
36 },
37 {
38 name: "bad key",
39 cli: "bad-key",
40 wantsErr: true,
41 },
42 {
43 name: "no key",
44 cli: "",
45 wantsErr: true,
46 },
47 {
48 name: "good key for group",
49 cli: "-g group good_key",
50 wants: options{
51 key: "good_key",
52 group: "group",
53 },
54 wantsErr: false,
55 },
56 {
57 name: "good key, with scope",
58 cli: "-s foo -g group good_key",
59 wants: options{
60 key: "good_key",
61 group: "group",
62 scope: "foo",
63 },
64 wantsErr: false,
65 },
66 {
67 name: "good key, with default scope",
68 cli: "-g group good_key",
69 wants: options{
70 key: "good_key",
71 group: "group",
72 scope: "*",
73 },
74 wantsErr: false,
75 },
76 {
77 name: "bad key for group",
78 cli: "-g group bad-key",
79 wants: options{
80 group: "group",
81 },
82 wantsErr: true,
83 },
84 {
85 name: "good key but no group",
86 cli: "good_key --group",
87 wantsErr: true,
88 },
89 {
90 name: "inherited",
91 cli: "--inherited good_key",
92 wants: options{
93 key: "good_key",
94 inherited: true,
95 },
96 },
97 {
98 name: "inherited from a group",
99 cli: "-g group --inherited good_key",
100 wants: options{
101 key: "good_key",
102 group: "group",
103 inherited: true,
104 },
105 },
106 }
108 for _, test := range tests {
109 t.Run(test.name, func(t *testing.T) {
110 io, _, _, _ := cmdtest.TestIOStreams()
111 f := cmdtest.NewTestFactory(io)
113 argv, err := shlex.Split(test.cli)
114 require.NoError(t, err)
116 var gotOpts *options
117 cmd := NewCmdGet(f, func(opts *options) error {
118 gotOpts = opts
119 return nil
120 })
121 cmd.SetArgs(argv)
122 cmd.SetIn(&bytes.Buffer{})
123 cmd.SetOut(&bytes.Buffer{})
124 cmd.SetErr(&bytes.Buffer{})
126 _, err = cmd.ExecuteC()
127 if test.wantsErr {
128 require.Error(t, err)
129 return
130 }
131 require.NoError(t, err)
133 assert.Equal(t, test.wants.key, gotOpts.key)
134 assert.Equal(t, test.wants.group, gotOpts.group)
135 assert.Equal(t, test.wants.inherited, gotOpts.inherited)
136 })
137 }
140func Test_getRun_project(t *testing.T) {
141 varContent := `
142 TEST variable\n
143 content
144 `
146 // GIVEN
147 testClient := gitlabtesting.NewTestClient(t)
148 testClient.MockProjectVariables.EXPECT().
149 GetVariable("owner/repo", "TEST_VAR", gomock.Any()).
150 Return(&gitlab.ProjectVariable{
151 Key: "TEST_VAR",
152 VariableType: "env_var",
153 Value: varContent,
154 Protected: false,
155 Masked: false,
156 EnvironmentScope: "*",
157 }, nil, nil)
159 io, _, stdout, _ := cmdtest.TestIOStreams()
161 opts := &options{
162 apiClient: func(repoHost string) (*api.Client, error) {
163 return cmdtest.NewTestApiClient(t, nil, "", "gitlab.com", api.WithGitLabClient(testClient.Client)), nil
164 },
165 baseRepo: func() (glrepo.Interface, error) {
166 return glrepo.New("owner", "repo", "gitlab.com"), nil
167 },
168 io: io,
169 key: "TEST_VAR",
170 }
172 // WHEN
173 err := opts.run()
175 // THEN
176 require.NoError(t, err)
177 assert.Equal(t, varContent, stdout.String())
180func Test_getRun_group(t *testing.T) {
181 varContent := `group variable content`
183 // GIVEN
184 testClient := gitlabtesting.NewTestClient(t)
185 testClient.MockGroupVariables.EXPECT().
186 GetVariable("mygroup", "GROUP_VAR", gomock.Any()).
187 Return(&gitlab.GroupVariable{
188 Key: "GROUP_VAR",
189 VariableType: "env_var",
190 Value: varContent,
191 Protected: false,
192 Masked: false,
193 EnvironmentScope: "*",
194 }, nil, nil)
196 io, _, stdout, _ := cmdtest.TestIOStreams()
198 opts := &options{
199 apiClient: func(repoHost string) (*api.Client, error) {
200 return cmdtest.NewTestApiClient(t, nil, "", "gitlab.com", api.WithGitLabClient(testClient.Client)), nil
201 },
202 baseRepo: func() (glrepo.Interface, error) {
203 return glrepo.New("owner", "repo", "gitlab.com"), nil
204 },
205 io: io,
206 key: "GROUP_VAR",
207 group: "mygroup",
208 }
210 // WHEN
211 err := opts.run()
213 // THEN
214 require.NoError(t, err)
215 assert.Equal(t, varContent, stdout.String())
Diagnostics8 git calls · 215 ms
CallStatusSizeTime0215 ms
discover200185 B4 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
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=4
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=3
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=2
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:0200375 B · 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=375
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=210e8ccdc9766a94adae919c6922ea88d77a8fb1
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=375
git.pkt.payload.max=348
git.sideband.band1.bytes=348
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=348
git.pack.trailer=8c3972a32424b06b4c572b8aa869b28eca201985
Open pack map
fetchtree:0200168 B · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=199
git.response.bytes=168
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=02c7459097a989bb30177cf9646054de1be6e826
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=be803fa1e1de2d46dfc3fb5d2a6d0a7ef128e660
Open pack map
fetch2001.3 KiB199 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=1299
git.response.cache=MISS
git.response.duration_ms=199
git.fetch.wants=c173da35aec3378af3904705cff941ad38ddcfc1
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=1299
git.pkt.payload.max=1272
git.sideband.band1.bytes=1272
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
a5e28a228db5bd4a
Request
61eeac51…
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