GitLabgitlab-org
cli
main
1//go:build !integration
3package importcmd
5import (
6 "net/http"
7 "os"
8 "path/filepath"
9 "testing"
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/glrepo"
20 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
21)
23// alreadyTakenErr mimics the *gitlab.ErrorResponse GitLab returns for a
24// duplicate variable key: HTTP 400 with a Rails-style validation message.
25func alreadyTakenErr() error {
26 return &gitlab.ErrorResponse{
27 StatusCode: http.StatusBadRequest,
28 Message: "{key: [has already been taken]}",
29 }
30}
32func writeVariablesFile(t *testing.T, contents string) string {
33 t.Helper()
34 file := filepath.Join(t.TempDir(), "variables.json")
35 require.NoError(t, os.WriteFile(file, []byte(contents), 0o600))
36 return file
37}
39func newOptions(t *testing.T, testClient *gitlabtesting.TestClient, file string) (*options, func() string) {
40 t.Helper()
41 io, _, stdout, _ := cmdtest.TestIOStreams()
42 opts := &options{
43 apiClient: func(repoHost string) (*api.Client, error) {
44 return cmdtest.NewTestApiClient(t, nil, "", "gitlab.com", api.WithGitLabClient(testClient.Client)), nil
45 },
46 baseRepo: func() (glrepo.Interface, error) {
47 return glrepo.New("owner", "repo", "gitlab.com"), nil
48 },
49 io: io,
50 inputFile: file,
51 }
52 return opts, stdout.String
53}
55func Test_importRun_project(t *testing.T) {
56 // GIVEN
57 testClient := gitlabtesting.NewTestClient(t)
58 testClient.MockProjectVariables.EXPECT().
59 CreateVariable("owner/repo", gomock.Any(), gomock.Any()).
60 Return(&gitlab.ProjectVariable{}, nil, nil).
61 Times(2)
63 file := writeVariablesFile(t, `[
64 {"key":"ONE","value":"1","variable_type":"env_var","environment_scope":"*"},
65 {"key":"TWO","value":"2","variable_type":"env_var","environment_scope":"*"}
66 ]`)
67 opts, stdout := newOptions(t, testClient, file)
69 // WHEN
70 err := opts.run(t.Context())
72 // THEN
73 require.NoError(t, err)
74 out := stdout()
75 assert.Contains(t, out, "Imported variable ONE.")
76 assert.Contains(t, out, "Imported variable TWO.")
77 assert.Contains(t, out, "Imported 2 variables into owner/repo.")
78}
80func Test_importRun_group(t *testing.T) {
81 // GIVEN
82 testClient := gitlabtesting.NewTestClient(t)
83 testClient.MockGroupVariables.EXPECT().
84 CreateVariable("mygroup", gomock.Any(), gomock.Any()).
85 Return(&gitlab.GroupVariable{}, nil, nil)
87 file := writeVariablesFile(t, `[{"key":"ONE","value":"1","variable_type":"env_var","environment_scope":"*"}]`)
88 opts, stdout := newOptions(t, testClient, file)
89 opts.group = "mygroup"
91 // WHEN
92 err := opts.run(t.Context())
94 // THEN
95 require.NoError(t, err)
96 assert.Contains(t, stdout(), "Imported 1 variables into mygroup.")
97}
99func Test_importRun_skipExisting(t *testing.T) {
100 // GIVEN
101 testClient := gitlabtesting.NewTestClient(t)
102 testClient.MockProjectVariables.EXPECT().
103 CreateVariable("owner/repo", gomock.Any(), gomock.Any()).
104 Return(nil, nil, alreadyTakenErr())
106 file := writeVariablesFile(t, `[{"key":"DUP","value":"x","variable_type":"env_var","environment_scope":"*"}]`)
107 opts, stdout := newOptions(t, testClient, file)
108 opts.skipExisting = true
110 // WHEN
111 err := opts.run(t.Context())
113 // THEN
114 require.NoError(t, err)
115 out := stdout()
116 assert.Contains(t, out, "Skipped existing variable DUP.")
117 assert.Contains(t, out, "Imported 0 variables into owner/repo (1 skipped).")
118}
120func Test_importRun_failsOnExistingByDefault(t *testing.T) {
121 // GIVEN
122 testClient := gitlabtesting.NewTestClient(t)
123 testClient.MockProjectVariables.EXPECT().
124 CreateVariable("owner/repo", gomock.Any(), gomock.Any()).
125 Return(nil, nil, alreadyTakenErr())
127 file := writeVariablesFile(t, `[{"key":"DUP","value":"x","variable_type":"env_var","environment_scope":"*"}]`)
128 opts, _ := newOptions(t, testClient, file)
130 // WHEN
131 err := opts.run(t.Context())
133 // THEN
134 require.Error(t, err)
135 assert.ErrorContains(t, err, "DUP")
136}
138func Test_importRun_emptyInput(t *testing.T) {
139 // GIVEN
140 testClient := gitlabtesting.NewTestClient(t)
141 file := writeVariablesFile(t, `[]`)
142 opts, stdout := newOptions(t, testClient, file)
144 // WHEN
145 err := opts.run(t.Context())
147 // THEN
148 require.NoError(t, err)
149 assert.Contains(t, stdout(), "No variables to import.")
150}
152func Test_importRun_invalidKey(t *testing.T) {
153 // GIVEN
154 testClient := gitlabtesting.NewTestClient(t)
155 // No CreateVariable expectation: an invalid key must be caught before any
156 // variable is created, so a later bad entry can't leave a partial import.
157 file := writeVariablesFile(t, `[
158 {"key":"ONE","value":"1","variable_type":"env_var","environment_scope":"*"},
159 {"key":"not a valid key!","value":"2","variable_type":"env_var","environment_scope":"*"}
160 ]`)
161 opts, _ := newOptions(t, testClient, file)
163 // WHEN
164 err := opts.run(t.Context())
166 // THEN
167 require.Error(t, err)
168 assert.ErrorContains(t, err, "not a valid key!")
169}
171func Test_importRun_defaultsEmptyEnvironmentScope(t *testing.T) {
172 // GIVEN
173 testClient := gitlabtesting.NewTestClient(t)
174 testClient.MockProjectVariables.EXPECT().
175 CreateVariable("owner/repo", gomock.Cond(func(opt *gitlab.CreateProjectVariableOptions) bool {
176 return opt.EnvironmentScope != nil && *opt.EnvironmentScope == "*"
177 }), gomock.Any()).
178 Return(&gitlab.ProjectVariable{}, nil, nil)
180 file := writeVariablesFile(t, `[{"key":"ONE","value":"1","variable_type":"env_var"}]`)
181 opts, _ := newOptions(t, testClient, file)
183 // WHEN
184 err := opts.run(t.Context())
186 // THEN
187 require.NoError(t, err)
188}
190func Test_importRun_skipsHiddenWithEmptyValue(t *testing.T) {
191 // GIVEN
192 testClient := gitlabtesting.NewTestClient(t)
193 // No CreateVariable expectation: a hidden variable with no value (as
194 // `export` produces, since hidden values aren't retrievable) is skipped
195 // rather than recreated empty.
197 file := writeVariablesFile(t, `[{"key":"SECRET","value":"","variable_type":"env_var","environment_scope":"*","hidden":true}]`)
198 opts, stdout := newOptions(t, testClient, file)
200 // WHEN
201 err := opts.run(t.Context())
203 // THEN
204 require.NoError(t, err)
205 out := stdout()
206 assert.Contains(t, out, "Skipped SECRET: hidden variables' values aren't included")
207 assert.Contains(t, out, "Imported 0 variables into owner/repo (1 skipped).")
208}
210func Test_importRun_project_masked_and_hidden_omitted(t *testing.T) {
211 testClient := gitlabtesting.NewTestClient(t)
213 var capturedOpts *gitlab.CreateProjectVariableOptions
214 testClient.MockProjectVariables.EXPECT().
215 CreateVariable("owner/repo", gomock.Any(), gomock.Any()).
216 DoAndReturn(func(pid any, opts *gitlab.CreateProjectVariableOptions, _ ...gitlab.RequestOptionFunc) (*gitlab.ProjectVariable, *gitlab.Response, error) {
217 capturedOpts = opts
218 return &gitlab.ProjectVariable{Key: "MY_VAR"}, nil, nil
219 })
221 file := writeVariablesFile(t, `[{"key":"MY_VAR","value":"val","variable_type":"env_var","environment_scope":"*"}]`)
222 opts, _ := newOptions(t, testClient, file)
224 require.NoError(t, opts.run(t.Context()))
225 assert.Nil(t, capturedOpts.MaskedAndHidden)
226}
228func Test_importRun_project_masked_and_hidden_sent(t *testing.T) {
229 testClient := gitlabtesting.NewTestClient(t)
231 var capturedOpts *gitlab.CreateProjectVariableOptions
232 testClient.MockProjectVariables.EXPECT().
233 CreateVariable("owner/repo", gomock.Any(), gomock.Any()).
234 DoAndReturn(func(pid any, opts *gitlab.CreateProjectVariableOptions, _ ...gitlab.RequestOptionFunc) (*gitlab.ProjectVariable, *gitlab.Response, error) {
235 capturedOpts = opts
236 return &gitlab.ProjectVariable{Key: "MY_VAR"}, nil, nil
237 })
239 file := writeVariablesFile(t, `[{"key":"MY_VAR","value":"val","variable_type":"env_var","environment_scope":"*","hidden":true}]`)
240 opts, _ := newOptions(t, testClient, file)
242 require.NoError(t, opts.run(t.Context()))
243 require.NotNil(t, capturedOpts.MaskedAndHidden)
244 assert.True(t, *capturedOpts.MaskedAndHidden)
245}
247func Test_importRun_group_masked_and_hidden_omitted(t *testing.T) {
248 testClient := gitlabtesting.NewTestClient(t)
250 var capturedOpts *gitlab.CreateGroupVariableOptions
251 testClient.MockGroupVariables.EXPECT().
252 CreateVariable("mygroup", gomock.Any(), gomock.Any()).
253 DoAndReturn(func(gid any, opts *gitlab.CreateGroupVariableOptions, _ ...gitlab.RequestOptionFunc) (*gitlab.GroupVariable, *gitlab.Response, error) {
254 capturedOpts = opts
255 return &gitlab.GroupVariable{Key: "MY_VAR"}, nil, nil
256 })
258 file := writeVariablesFile(t, `[{"key":"MY_VAR","value":"val","variable_type":"env_var","environment_scope":"*"}]`)
259 opts, _ := newOptions(t, testClient, file)
260 opts.group = "mygroup"
262 require.NoError(t, opts.run(t.Context()))
263 assert.Nil(t, capturedOpts.MaskedAndHidden)
264}
266func Test_importRun_group_masked_and_hidden_sent(t *testing.T) {
267 testClient := gitlabtesting.NewTestClient(t)
269 var capturedOpts *gitlab.CreateGroupVariableOptions
270 testClient.MockGroupVariables.EXPECT().
271 CreateVariable("mygroup", gomock.Any(), gomock.Any()).
272 DoAndReturn(func(gid any, opts *gitlab.CreateGroupVariableOptions, _ ...gitlab.RequestOptionFunc) (*gitlab.GroupVariable, *gitlab.Response, error) {
273 capturedOpts = opts
274 return &gitlab.GroupVariable{Key: "MY_VAR"}, nil, nil
275 })
277 file := writeVariablesFile(t, `[{"key":"MY_VAR","value":"val","variable_type":"env_var","environment_scope":"*","hidden":true}]`)
278 opts, _ := newOptions(t, testClient, file)
279 opts.group = "mygroup"
281 require.NoError(t, opts.run(t.Context()))
282 require.NotNil(t, capturedOpts.MaskedAndHidden)
283 assert.True(t, *capturedOpts.MaskedAndHidden)
284}
Diagnostics8 git calls · 237 ms
CallStatusSizeTime0237 ms
discover200185 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs 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=2 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=44Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 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=4273 git.response.cache=HIT git.response.duration_ms=2 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=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:0200137 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=137 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=51e6d5d34052c13671ee0c8708ecaea0ab5e66fa 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=137 git.pkt.payload.max=110 git.sideband.band1.bytes=110 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=110 git.pack.trailer=64e6b6c2411ed206e42401749e85c55d827ddf40Open pack map
fetch2001.8 KiB225 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=225 git.response.bytes=1849 git.response.cache=MISS git.response.duration_ms=225 git.fetch.wants=2e6513029943ca4f3e988f1df7f7eeea24eb15bd 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=1849 git.pkt.payload.max=1822 git.sideband.band1.bytes=1822 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
- a5e19003e92b5b65
- Request
- cf4b0ec0…
- 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