GitLabgitlab-org
cli
main
1//go:build !integration
3package get
5import (
6 "errors"
7 "net/http"
8 "testing"
10 "github.com/spf13/cobra"
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/cmdutils"
20 "gitlab.com/gitlab-org/cli/internal/glrepo"
21 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
22 "gitlab.com/gitlab-org/cli/test"
23)
25// apiError builds the error shape the API client returns for a status code.
26// ErrorResponse.Error reads the originating request, so one is supplied.
27func apiError(status int) error {
28 request, err := http.NewRequest(http.MethodGet, "https://gitlab.com/api/v4/variables/TEST_VAR", http.NoBody)
29 if err != nil {
30 panic(err)
31 }
33 return &gitlab.ErrorResponse{
34 StatusCode: status,
35 Response: &http.Response{StatusCode: status, Request: request},
36 Message: http.StatusText(status),
37 }
38}
40func inheritedOpts(t *testing.T, testClient *gitlabtesting.TestClient, owner, group string) (*options, *testCapture) {
41 t.Helper()
43 io, _, stdout, stderr := cmdtest.TestIOStreams()
44 return &options{
45 apiClient: func(repoHost string) (*api.Client, error) {
46 return cmdtest.NewTestApiClient(t, nil, "", "gitlab.com", api.WithGitLabClient(testClient.Client)), nil
47 },
48 baseRepo: func() (glrepo.Interface, error) {
49 return glrepo.New(owner, "api", "gitlab.com"), nil
50 },
51 io: io,
52 key: "TEST_VAR",
53 scope: "*",
54 group: group,
55 inherited: true,
56 }, &testCapture{stdout: stdout.String, stderr: stderr.String}
57}
59type testCapture struct {
60 stdout func() string
61 stderr func() string
62}
64func Test_getRun_inherited_fallsBackToParentGroup(t *testing.T) {
65 testClient := gitlabtesting.NewTestClient(t)
67 testClient.MockProjectVariables.EXPECT().
68 GetVariable("acme/platform/api", "TEST_VAR", gomock.Any()).
69 Return(nil, nil, apiError(http.StatusNotFound))
71 // The closest group is read first and defines the key, so the top-level
72 // group is never asked.
73 testClient.MockGroupVariables.EXPECT().
74 GetVariable("acme/platform", "TEST_VAR", gomock.Any()).
75 Return(&gitlab.GroupVariable{Key: "TEST_VAR", Value: "from_subgroup"}, nil, nil)
77 opts, out := inheritedOpts(t, testClient, "acme/platform", "")
79 require.NoError(t, opts.run())
80 assert.Equal(t, "from_subgroup", out.stdout())
81 assert.Contains(t, out.stderr(), "Using the value from group acme/platform")
82}
84func Test_getRun_inherited_walksToTopLevelGroup(t *testing.T) {
85 testClient := gitlabtesting.NewTestClient(t)
87 testClient.MockProjectVariables.EXPECT().
88 GetVariable("acme/platform/api", "TEST_VAR", gomock.Any()).
89 Return(nil, nil, apiError(http.StatusNotFound))
90 testClient.MockGroupVariables.EXPECT().
91 GetVariable("acme/platform", "TEST_VAR", gomock.Any()).
92 Return(nil, nil, apiError(http.StatusNotFound))
93 testClient.MockGroupVariables.EXPECT().
94 GetVariable("acme", "TEST_VAR", gomock.Any()).
95 Return(&gitlab.GroupVariable{Key: "TEST_VAR", Value: "from_root_group"}, nil, nil)
97 opts, out := inheritedOpts(t, testClient, "acme/platform", "")
99 require.NoError(t, opts.run())
100 assert.Equal(t, "from_root_group", out.stdout())
101 assert.Contains(t, out.stderr(), "Using the value from group acme")
102}
104func Test_getRun_inherited_fallsBackToInstanceVariables(t *testing.T) {
105 testClient := gitlabtesting.NewTestClient(t)
107 testClient.MockProjectVariables.EXPECT().
108 GetVariable("acme/api", "TEST_VAR", gomock.Any()).
109 Return(nil, nil, apiError(http.StatusNotFound))
110 testClient.MockGroupVariables.EXPECT().
111 GetVariable("acme", "TEST_VAR", gomock.Any()).
112 Return(nil, nil, apiError(http.StatusNotFound))
113 testClient.MockInstanceVariables.EXPECT().
114 GetVariable("TEST_VAR").
115 Return(&gitlab.InstanceVariable{Key: "TEST_VAR", Value: "from_instance"}, nil, nil)
117 opts, out := inheritedOpts(t, testClient, "acme", "")
119 require.NoError(t, opts.run())
120 assert.Equal(t, "from_instance", out.stdout())
121 assert.Contains(t, out.stderr(), "Using the value from instance variables")
122}
124// Reading a group or instance variable needs permission the user may not have.
125// A walk must treat that as "not defined here" and carry on.
126func Test_getRun_inherited_skipsLevelsTheUserCannotRead(t *testing.T) {
127 testClient := gitlabtesting.NewTestClient(t)
129 testClient.MockProjectVariables.EXPECT().
130 GetVariable("acme/platform/api", "TEST_VAR", gomock.Any()).
131 Return(nil, nil, apiError(http.StatusNotFound))
132 testClient.MockGroupVariables.EXPECT().
133 GetVariable("acme/platform", "TEST_VAR", gomock.Any()).
134 Return(nil, nil, apiError(http.StatusForbidden))
135 testClient.MockGroupVariables.EXPECT().
136 GetVariable("acme", "TEST_VAR", gomock.Any()).
137 Return(&gitlab.GroupVariable{Key: "TEST_VAR", Value: "from_root_group"}, nil, nil)
139 opts, out := inheritedOpts(t, testClient, "acme/platform", "")
141 require.NoError(t, opts.run())
142 assert.Equal(t, "from_root_group", out.stdout())
143}
145// Instance variables are administrator-only, so a 403 there ends the walk
146// quietly and the project error is reported, as it is without the flag.
147func Test_getRun_inherited_reportsTheProjectErrorWhenNothingDefinesTheKey(t *testing.T) {
148 testClient := gitlabtesting.NewTestClient(t)
150 testClient.MockProjectVariables.EXPECT().
151 GetVariable("acme/api", "TEST_VAR", gomock.Any()).
152 Return(nil, nil, apiError(http.StatusNotFound))
153 testClient.MockGroupVariables.EXPECT().
154 GetVariable("acme", "TEST_VAR", gomock.Any()).
155 Return(nil, nil, apiError(http.StatusNotFound))
156 testClient.MockInstanceVariables.EXPECT().
157 GetVariable("TEST_VAR").
158 Return(nil, nil, apiError(http.StatusForbidden))
160 opts, _ := inheritedOpts(t, testClient, "acme", "")
162 err := opts.run()
163 require.Error(t, err)
164 assert.Contains(t, err.Error(), "Not Found")
165}
167// A failure that is not "missing" or "forbidden" means something is wrong, and
168// hiding it behind the next level would mislead the user.
169func Test_getRun_inherited_surfacesUnexpectedErrors(t *testing.T) {
170 testClient := gitlabtesting.NewTestClient(t)
172 testClient.MockProjectVariables.EXPECT().
173 GetVariable("acme/api", "TEST_VAR", gomock.Any()).
174 Return(nil, nil, apiError(http.StatusNotFound))
175 testClient.MockGroupVariables.EXPECT().
176 GetVariable("acme", "TEST_VAR", gomock.Any()).
177 Return(nil, nil, apiError(http.StatusInternalServerError))
179 opts, _ := inheritedOpts(t, testClient, "acme", "")
181 err := opts.run()
182 require.Error(t, err)
183 assert.Contains(t, err.Error(), "Internal Server Error")
184}
186// Without the flag the command still reads one level and reports its error.
187func Test_getRun_withoutInherited_doesNotWalk(t *testing.T) {
188 testClient := gitlabtesting.NewTestClient(t)
190 testClient.MockProjectVariables.EXPECT().
191 GetVariable("acme/api", "TEST_VAR", gomock.Any()).
192 Return(nil, nil, apiError(http.StatusNotFound))
194 opts, out := inheritedOpts(t, testClient, "acme", "")
195 opts.inherited = false
197 err := opts.run()
198 require.Error(t, err)
199 assert.Empty(t, out.stdout())
200}
202// --group selects the starting level, and --inherited continues above it.
203func Test_getRun_inherited_startsFromTheRequestedGroup(t *testing.T) {
204 testClient := gitlabtesting.NewTestClient(t)
206 testClient.MockGroupVariables.EXPECT().
207 GetVariable("acme/platform", "TEST_VAR", gomock.Any()).
208 Return(nil, nil, apiError(http.StatusNotFound))
209 testClient.MockGroupVariables.EXPECT().
210 GetVariable("acme", "TEST_VAR", gomock.Any()).
211 Return(&gitlab.GroupVariable{Key: "TEST_VAR", Value: "from_root_group"}, nil, nil)
213 opts, out := inheritedOpts(t, testClient, "acme/platform", "acme/platform")
215 require.NoError(t, opts.run())
216 assert.Equal(t, "from_root_group", out.stdout())
217}
219func Test_skippableLevel(t *testing.T) {
220 t.Parallel()
222 tests := []struct {
223 name string
224 err error
225 want bool
226 }{
227 {name: "not found", err: apiError(http.StatusNotFound), want: true},
228 {name: "forbidden", err: apiError(http.StatusForbidden), want: true},
229 {name: "unauthorized", err: apiError(http.StatusUnauthorized), want: true},
230 {name: "server error", err: apiError(http.StatusInternalServerError), want: false},
231 // The client also returns a sentinel that carries no HTTP response.
232 {name: "sentinel not found", err: gitlab.ErrNotFound, want: true},
233 {name: "plain error", err: errors.New("dial tcp: connection refused"), want: false},
234 }
236 for _, tt := range tests {
237 t.Run(tt.name, func(t *testing.T) {
238 t.Parallel()
239 assert.Equal(t, tt.want, skippableLevel(tt.err))
240 })
241 }
242}
244func execGetCmd(t *testing.T, testClient *gitlabtesting.TestClient, cli string) (*test.CmdOut, error) {
245 t.Helper()
247 exec := cmdtest.SetupCmdForTest(
248 t,
249 func(f cmdutils.Factory) *cobra.Command {
250 return NewCmdGet(f, nil)
251 },
252 false,
253 cmdtest.WithApiClient(cmdtest.NewTestApiClient(t, nil, "", "gitlab.com", api.WithGitLabClient(testClient.Client))),
254 cmdtest.WithBaseRepo("acme/platform", "api", ""),
255 )
256 return exec(cli)
257}
259func Test_getCmd_inherited_namesTheProjectItStartedFrom(t *testing.T) {
260 testClient := gitlabtesting.NewTestClient(t)
262 testClient.MockProjectVariables.EXPECT().
263 GetVariable("acme/platform/api", "TEST_VAR", gomock.Any()).
264 Return(nil, nil, apiError(http.StatusNotFound))
265 testClient.MockGroupVariables.EXPECT().
266 GetVariable("acme/platform", "TEST_VAR", gomock.Any()).
267 Return(&gitlab.GroupVariable{Key: "TEST_VAR", Value: "from_subgroup"}, nil, nil)
269 out, err := execGetCmd(t, testClient, "--inherited TEST_VAR")
271 require.NoError(t, err)
272 assert.Equal(t, "from_subgroup", out.OutBuf.String())
273 assert.Equal(t, "TEST_VAR is not defined in project acme/platform/api. Using the value from group acme/platform.\n", out.ErrBuf.String())
274}
276func Test_getCmd_inherited_namesTheGroupItStartedFrom(t *testing.T) {
277 testClient := gitlabtesting.NewTestClient(t)
279 testClient.MockGroupVariables.EXPECT().
280 GetVariable("acme/platform", "TEST_VAR", gomock.Any()).
281 Return(nil, nil, apiError(http.StatusNotFound))
282 testClient.MockGroupVariables.EXPECT().
283 GetVariable("acme", "TEST_VAR", gomock.Any()).
284 Return(&gitlab.GroupVariable{Key: "TEST_VAR", Value: "from_root_group"}, nil, nil)
286 out, err := execGetCmd(t, testClient, "--group acme/platform --inherited TEST_VAR")
288 require.NoError(t, err)
289 assert.Equal(t, "from_root_group", out.OutBuf.String())
290 assert.Equal(t, "TEST_VAR is not defined in group acme/platform. Using the value from group acme.\n", out.ErrBuf.String())
291}
293// A numeric ID has no path to derive parent groups from, so the walk must
294// look the group up first.
295func Test_getCmd_inherited_resolvesANumericGroupID(t *testing.T) {
296 testClient := gitlabtesting.NewTestClient(t)
298 testClient.MockGroupVariables.EXPECT().
299 GetVariable("123", "TEST_VAR", gomock.Any()).
300 Return(nil, nil, apiError(http.StatusNotFound))
301 testClient.MockGroups.EXPECT().
302 GetGroup("123", gomock.Any()).
303 Return(&gitlab.Group{ID: 123, FullPath: "acme/platform"}, nil, nil)
304 testClient.MockGroupVariables.EXPECT().
305 GetVariable("acme", "TEST_VAR", gomock.Any()).
306 Return(&gitlab.GroupVariable{Key: "TEST_VAR", Value: "from_root_group"}, nil, nil)
308 out, err := execGetCmd(t, testClient, "--group 123 --inherited TEST_VAR")
310 require.NoError(t, err)
311 assert.Equal(t, "from_root_group", out.OutBuf.String())
312 assert.Equal(t, "TEST_VAR is not defined in group acme/platform. Using the value from group acme.\n", out.ErrBuf.String())
313}
315func Test_getCmd_inherited_reportsANumericGroupThatCannotBeResolved(t *testing.T) {
316 testClient := gitlabtesting.NewTestClient(t)
318 testClient.MockGroupVariables.EXPECT().
319 GetVariable("123", "TEST_VAR", gomock.Any()).
320 Return(nil, nil, apiError(http.StatusNotFound))
321 testClient.MockGroups.EXPECT().
322 GetGroup("123", gomock.Any()).
323 Return(nil, nil, apiError(http.StatusNotFound))
325 out, err := execGetCmd(t, testClient, "--group 123 --inherited TEST_VAR")
327 require.Error(t, err)
328 assert.Contains(t, err.Error(), "finding the parent groups of group 123")
329 assert.Empty(t, out.OutBuf.String())
330}
Diagnostics8 git calls · 247 ms
CallStatusSizeTime0247 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 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
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=44Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit5 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=5 git.response.bytes=4273 git.response.cache=HIT git.response.duration_ms=5 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 · 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=1857 git.response.cache=HIT git.response.duration_ms=4 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: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=2 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=8c3972a32424b06b4c572b8aa869b28eca201985Open 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=3 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=be803fa1e1de2d46dfc3fb5d2a6d0a7ef128e660Open pack map
fetch2002.4 KiB230 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=230 git.response.bytes=2446 git.response.cache=MISS git.response.duration_ms=230 git.fetch.wants=28a596f2030d1f0f542ea4e9e5157fee7419afbe 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=2446 git.pkt.payload.max=2419 git.sideband.band1.bytes=2419 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
- a5e288fd28884af4
- Request
- ebdb37d1…
- 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