GitLabgitlab-org

cli

main
ply8 git calls246 ms99 KiB5 cachedprotocol v2
420 lines11 KiBRaw
1//go:build !integration
3package set
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_NewCmdSet(t *testing.T) {
23 tests := []struct {
24 name string
25 cli string
26 wants options
27 stdinTTY bool
28 wantsErr bool
29 }{
30 {
31 name: "invalid type",
32 cli: "cool_secret -g coolGroup -t 'mV'",
33 wantsErr: true,
34 },
35 {
36 name: "value argument and value flag specified",
37 cli: `cool_secret value -v"another value"`,
38 wantsErr: true,
39 },
40 {
41 name: "no name",
42 cli: "",
43 wantsErr: true,
44 },
45 {
46 name: "no value, stdin is terminal",
47 cli: "cool_secret",
48 stdinTTY: true,
49 wantsErr: true,
50 },
51 {
52 name: "protected var",
53 cli: `cool_secret -v"a secret" -p`,
54 wants: options{
55 key: "cool_secret",
56 protected: true,
57 value: "a secret",
58 group: "",
59 scope: "*",
60 typ: "env_var",
61 },
62 },
63 {
64 name: "protected var in group",
65 cli: `cool_secret --group coolGroup -v"cool"`,
66 wants: options{
67 key: "cool_secret",
68 protected: false,
69 value: "cool",
70 group: "coolGroup",
71 scope: "*",
72 typ: "env_var",
73 },
74 },
75 {
76 name: "leading numbers in name",
77 cli: `123_TOKEN -v"cool"`,
78 wants: options{
79 key: "123_TOKEN",
80 protected: false,
81 value: "cool",
82 group: "",
83 scope: "*",
84 typ: "env_var",
85 },
86 },
87 {
88 name: "invalid characters in name",
89 cli: `BAD-SECRET -v"cool"`,
90 wantsErr: true,
91 },
92 {
93 name: "environment scope in group",
94 cli: `cool_secret --group coolGroup -v"cool" -s"production"`,
95 wants: options{
96 key: "cool_secret",
97 scope: "production",
98 value: "cool",
99 group: "coolGroup",
100 typ: "env_var",
101 },
102 },
103 {
104 name: "raw variable with flag",
105 cli: `cool_secret -r -v"$variable_name"`,
106 wants: options{
107 key: "cool_secret",
108 value: "$variable_name",
109 raw: true,
110 group: "",
111 scope: "*",
112 typ: "env_var",
113 },
114 },
115 {
116 name: "raw variable with flag in group",
117 cli: `cool_secret -r --group coolGroup -v"$variable_name"`,
118 wants: options{
119 key: "cool_secret",
120 value: "$variable_name",
121 raw: true,
122 group: "coolGroup",
123 scope: "*",
124 typ: "env_var",
125 },
126 },
127 {
128 name: "raw is false by default",
129 cli: `cool_secret -v"$variable_name"`,
130 wants: options{
131 key: "cool_secret",
132 value: "$variable_name",
133 raw: false,
134 group: "",
135 scope: "*",
136 protected: false,
137 typ: "env_var",
138 },
139 },
140 {
141 name: "var with description",
142 cli: `var_desc -v"var_desc" -d "cool var description"`,
143 wants: options{
144 key: "var_desc",
145 protected: false,
146 value: "var_desc",
147 group: "",
148 description: "cool var description",
149 scope: "*",
150 typ: "env_var",
151 },
152 },
153 {
154 name: "hidden variable",
155 cli: `var_desc -v "var_desc" --hidden`,
156 wants: options{
157 key: "var_desc",
158 protected: false,
159 value: "var_desc",
160 group: "",
161 description: "",
162 scope: "*",
163 typ: "env_var",
164 hidden: true,
165 },
166 },
167 }
169 for _, tt := range tests {
170 t.Run(tt.name, func(t *testing.T) {
171 io, _, _, _ := cmdtest.TestIOStreams()
172 f := cmdtest.NewTestFactory(io)
174 io.IsInTTY = tt.stdinTTY
176 argv, err := shlex.Split(tt.cli)
177 require.NoError(t, err)
179 var gotOpts *options
180 cmd := NewCmdSet(f, func(opts *options) error {
181 gotOpts = opts
182 return nil
183 })
184 cmd.SetArgs(argv)
185 cmd.SetIn(&bytes.Buffer{})
186 cmd.SetOut(&bytes.Buffer{})
187 cmd.SetErr(&bytes.Buffer{})
189 _, err = cmd.ExecuteC()
190 if tt.wantsErr {
191 require.Error(t, err)
192 return
193 }
194 require.NoError(t, err)
196 assert.Equal(t, tt.wants.key, gotOpts.key)
197 assert.Equal(t, tt.wants.value, gotOpts.value)
198 assert.Equal(t, tt.wants.group, gotOpts.group)
199 assert.Equal(t, tt.wants.scope, gotOpts.scope)
200 assert.Equal(t, tt.wants.protected, gotOpts.protected)
201 assert.Equal(t, tt.wants.description, gotOpts.description)
202 assert.Equal(t, tt.wants.raw, gotOpts.raw)
203 assert.Equal(t, tt.wants.masked, gotOpts.masked)
204 assert.Equal(t, tt.wants.hidden, gotOpts.hidden)
205 assert.Equal(t, tt.wants.typ, gotOpts.typ)
206 })
207 }
210func Test_setRun_project(t *testing.T) {
211 // GIVEN
212 testClient := gitlabtesting.NewTestClient(t)
213 testClient.MockProjectVariables.EXPECT().
214 CreateVariable("owner/repo", gomock.Any()).
215 Return(&gitlab.ProjectVariable{
216 Key: "NEW_VARIABLE",
217 Value: "new value",
218 VariableType: "env_var",
219 Protected: false,
220 Masked: false,
221 Raw: false,
222 EnvironmentScope: "production",
223 }, nil, nil)
225 io, _, stdout, _ := cmdtest.TestIOStreams()
227 opts := &options{
228 apiClient: func(repoHost string) (*api.Client, error) {
229 return cmdtest.NewTestApiClient(t, nil, "", "gitlab.com", api.WithGitLabClient(testClient.Client)), nil
230 },
231 baseRepo: func() (glrepo.Interface, error) {
232 return glrepo.New("owner", "repo", "gitlab.com"), nil
233 },
234 io: io,
235 key: "NEW_VARIABLE",
236 value: "new value",
237 scope: "*",
238 }
240 // WHEN
241 err := opts.run()
243 // THEN
244 require.NoError(t, err)
245 assert.Equal(t, "✓ Created variable NEW_VARIABLE for owner/repo with scope *.\n", stdout.String())
248func Test_setRun_group(t *testing.T) {
249 // GIVEN
250 testClient := gitlabtesting.NewTestClient(t)
251 testClient.MockGroupVariables.EXPECT().
252 CreateVariable("mygroup", gomock.Any()).
253 Return(&gitlab.GroupVariable{
254 Key: "NEW_VARIABLE",
255 Value: "new value",
256 VariableType: "env_var",
257 Protected: false,
258 Masked: false,
259 Raw: false,
260 EnvironmentScope: "production",
261 }, nil, nil)
263 io, _, stdout, _ := cmdtest.TestIOStreams()
265 opts := &options{
266 apiClient: func(repoHost string) (*api.Client, error) {
267 return cmdtest.NewTestApiClient(t, nil, "", "gitlab.com", api.WithGitLabClient(testClient.Client)), nil
268 },
269 baseRepo: func() (glrepo.Interface, error) {
270 return glrepo.New("owner", "repo", "gitlab.com"), nil
271 },
272 io: io,
273 key: "NEW_VARIABLE",
274 value: "new value",
275 group: "mygroup",
276 }
278 // WHEN
279 err := opts.run()
281 // THEN
282 require.NoError(t, err)
283 assert.Equal(t, "✓ Created variable NEW_VARIABLE for group mygroup.\n", stdout.String())
286func Test_setRun_group_masked_and_hidden_omitted(t *testing.T) {
287 t.Parallel()
289 testClient := gitlabtesting.NewTestClient(t)
291 var capturedOpts *gitlab.CreateGroupVariableOptions
292 testClient.MockGroupVariables.EXPECT().
293 CreateVariable("mygroup", gomock.Any()).
294 DoAndReturn(func(group any, opts *gitlab.CreateGroupVariableOptions, _ ...gitlab.RequestOptionFunc) (*gitlab.GroupVariable, *gitlab.Response, error) {
295 capturedOpts = opts
296 return &gitlab.GroupVariable{Key: "MY_VAR"}, nil, nil
297 })
299 io, _, _, _ := cmdtest.TestIOStreams()
301 opts := &options{
302 apiClient: func(repoHost string) (*api.Client, error) {
303 return cmdtest.NewTestApiClient(t, nil, "", "gitlab.com", api.WithGitLabClient(testClient.Client)), nil
304 },
305 baseRepo: func() (glrepo.Interface, error) {
306 return glrepo.New("owner", "repo", "gitlab.com"), nil
307 },
308 io: io,
309 key: "MY_VAR",
310 value: "some value",
311 group: "mygroup",
312 }
314 err := opts.run()
315 require.NoError(t, err)
316 assert.Nil(t, capturedOpts.MaskedAndHidden)
319func Test_setRun_group_masked_and_hidden_sent(t *testing.T) {
320 t.Parallel()
322 testClient := gitlabtesting.NewTestClient(t)
324 var capturedOpts *gitlab.CreateGroupVariableOptions
325 testClient.MockGroupVariables.EXPECT().
326 CreateVariable("mygroup", gomock.Any()).
327 DoAndReturn(func(group any, opts *gitlab.CreateGroupVariableOptions, _ ...gitlab.RequestOptionFunc) (*gitlab.GroupVariable, *gitlab.Response, error) {
328 capturedOpts = opts
329 return &gitlab.GroupVariable{Key: "MY_VAR"}, nil, nil
330 })
332 io, _, _, _ := cmdtest.TestIOStreams()
334 opts := &options{
335 apiClient: func(repoHost string) (*api.Client, error) {
336 return cmdtest.NewTestApiClient(t, nil, "", "gitlab.com", api.WithGitLabClient(testClient.Client)), nil
337 },
338 baseRepo: func() (glrepo.Interface, error) {
339 return glrepo.New("owner", "repo", "gitlab.com"), nil
340 },
341 io: io,
342 key: "MY_VAR",
343 value: "some value",
344 group: "mygroup",
345 hidden: true,
346 }
348 err := opts.run()
349 require.NoError(t, err)
350 require.NotNil(t, capturedOpts.MaskedAndHidden)
351 assert.True(t, *capturedOpts.MaskedAndHidden)
354func Test_setRun_project_masked_and_hidden_omitted(t *testing.T) {
355 t.Parallel()
357 testClient := gitlabtesting.NewTestClient(t)
359 var capturedOpts *gitlab.CreateProjectVariableOptions
360 testClient.MockProjectVariables.EXPECT().
361 CreateVariable("owner/repo", gomock.Any()).
362 DoAndReturn(func(pid any, opts *gitlab.CreateProjectVariableOptions, _ ...gitlab.RequestOptionFunc) (*gitlab.ProjectVariable, *gitlab.Response, error) {
363 capturedOpts = opts
364 return &gitlab.ProjectVariable{Key: "MY_VAR"}, nil, nil
365 })
367 io, _, _, _ := cmdtest.TestIOStreams()
369 opts := &options{
370 apiClient: func(repoHost string) (*api.Client, error) {
371 return cmdtest.NewTestApiClient(t, nil, "", "gitlab.com", api.WithGitLabClient(testClient.Client)), nil
372 },
373 baseRepo: func() (glrepo.Interface, error) {
374 return glrepo.New("owner", "repo", "gitlab.com"), nil
375 },
376 io: io,
377 key: "MY_VAR",
378 value: "some value",
379 scope: "*",
380 }
382 err := opts.run()
383 require.NoError(t, err)
384 assert.Nil(t, capturedOpts.MaskedAndHidden)
387func Test_setRun_project_masked_and_hidden_sent(t *testing.T) {
388 t.Parallel()
390 testClient := gitlabtesting.NewTestClient(t)
392 var capturedOpts *gitlab.CreateProjectVariableOptions
393 testClient.MockProjectVariables.EXPECT().
394 CreateVariable("owner/repo", gomock.Any()).
395 DoAndReturn(func(pid any, opts *gitlab.CreateProjectVariableOptions, _ ...gitlab.RequestOptionFunc) (*gitlab.ProjectVariable, *gitlab.Response, error) {
396 capturedOpts = opts
397 return &gitlab.ProjectVariable{Key: "MY_VAR"}, nil, nil
398 })
400 io, _, _, _ := cmdtest.TestIOStreams()
402 opts := &options{
403 apiClient: func(repoHost string) (*api.Client, error) {
404 return cmdtest.NewTestApiClient(t, nil, "", "gitlab.com", api.WithGitLabClient(testClient.Client)), nil
405 },
406 baseRepo: func() (glrepo.Interface, error) {
407 return glrepo.New("owner", "repo", "gitlab.com"), nil
408 },
409 io: io,
410 key: "MY_VAR",
411 value: "some value",
412 scope: "*",
413 hidden: true,
414 }
416 err := opts.run()
417 require.NoError(t, err)
418 require.NotNil(t, capturedOpts.MaskedAndHidden)
419 assert.True(t, *capturedOpts.MaskedAndHidden)
Diagnostics8 git calls · 246 ms
CallStatusSizeTime0246 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=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 KiB2 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=UPDATING
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
fetchcombine:blob:none+tree:2 · deepen 12004.2 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=4273
git.response.cache=HIT
git.response.duration_ms=3
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 · hit5 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=5
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 · 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=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:0200375 B · hit1 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=375
git.response.cache=HIT
git.response.duration_ms=1
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:0200134 B · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=233
git.response.bytes=134
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=9636692405552af7a4ff3c20e1588b5499dea105
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=134
git.pkt.payload.max=107
git.sideband.band1.bytes=107
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=107
git.pack.trailer=23fbc292075ac7563d781cd93a1c3a9896ec306b
Open pack map
fetch2001.9 KiB233 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=1988
git.response.cache=MISS
git.response.duration_ms=233
git.fetch.wants=ea10f201c1e204cc27122f756a10a414c136cf89
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=1988
git.pkt.payload.max=1961
git.sideband.band1.bytes=1961
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
a5e24a75ccab0ecd
Request
4991c0af…
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