GitLabgitlab-org
cli
main
1//go:build !integration
3package pm
5import (
6 "bytes"
7 "io/fs"
8 "os"
9 "path/filepath"
10 "strings"
11 "testing"
13 "github.com/stretchr/testify/assert"
14 "github.com/stretchr/testify/require"
16 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
17)
19func TestBundleConfigCACert(t *testing.T) {
20 t.Parallel()
21 cases := []struct {
22 line string
23 want string
24 ok bool
25 }{
26 {`BUNDLE_SSL_CA_CERT: "/a/ca.pem"`, "/a/ca.pem", true},
27 {`BUNDLE_SSL_CA_CERT: '/s/ca.pem'`, "/s/ca.pem", true},
28 {`BUNDLE_SSL_CA_CERT: /n/ca.pem`, "/n/ca.pem", true},
29 {`BUNDLE_SSL_CA_CERT: "/crlf"` + "\r", "/crlf", true},
30 {`bundle_ssl_ca_cert: /lower/ca.pem`, "", false},
31 {` BUNDLE_SSL_CA_CERT: "/nested"`, "", false},
32 {`# BUNDLE_SSL_CA_CERT: "/comment"`, "", false},
33 {`BUNDLE_SSL_VERIFY_MODE: "1"`, "", false},
34 }
35 for _, c := range cases {
36 got, ok := bundleConfigCACert(c.line)
37 assert.Equalf(t, c.ok, ok, "line %q", c.line)
38 assert.Equalf(t, c.want, got, "line %q", c.line)
39 }
40}
42type bundleProject struct {
43 root string
44 config string
45 userPEM []byte
46 caPath string
47 proxyPEM []byte
48}
50func newBundleProject(t *testing.T, extraConfig string) bundleProject {
51 t.Helper()
52 root, err := filepath.EvalSymlinks(t.TempDir())
53 require.NoError(t, err)
54 p := bundleProject{root: root, userPEM: newTestAuthority(t).pem(), proxyPEM: newTestAuthority(t).pem()}
55 userCA := filepath.Join(root, "corp-ca.pem")
56 require.NoError(t, os.WriteFile(userCA, p.userPEM, 0o600))
57 p.caPath = filepath.Join(root, "glab-ca.pem")
58 require.NoError(t, os.WriteFile(p.caPath, p.proxyPEM, 0o600))
59 require.NoError(t, os.WriteFile(filepath.Join(root, "Gemfile"), []byte("source \"https://rubygems.org\"\n"), 0o600))
60 require.NoError(t, os.MkdirAll(filepath.Join(root, ".bundle", "plugin"), 0o700))
61 p.config = filepath.Join(root, ".bundle", "config")
62 content := "---\nBUNDLE_SSL_CA_CERT: \"" + userCA + "\"\n" + extraConfig
63 require.NoError(t, os.WriteFile(p.config, []byte(content), 0o600))
64 sub := filepath.Join(root, "app")
65 require.NoError(t, os.Mkdir(sub, 0o700))
66 t.Chdir(sub)
67 return p
68}
70func bundleSetup(t *testing.T, caPath string, parent ...string) (setupResult, *bytes.Buffer) {
71 t.Helper()
72 ios, _, _, errOut := cmdtest.TestIOStreams()
73 res, err := bundleManager{}.Setup(t.Context(), setupInput{parent: parent, caPath: caPath, io: ios})
74 require.NoError(t, err)
75 return res, errOut
76}
78func generatedCACert(t *testing.T, appConfig string) string {
79 t.Helper()
80 raw, err := os.ReadFile(filepath.Join(appConfig, "config"))
81 require.NoError(t, err)
82 var caFile string
83 for line := range strings.SplitSeq(string(raw), "\n") {
84 if v, ok := bundleConfigCACert(line); ok {
85 caFile = v
86 }
87 }
88 require.NotEmpty(t, caFile, "the generated config must set BUNDLE_SSL_CA_CERT")
89 return caFile
90}
92func TestBundleSetupReplacesLocalConfigCACert(t *testing.T) {
93 p := newBundleProject(t, "BUNDLE_JOBS: \"3\"\n")
94 original, err := os.ReadFile(p.config)
95 require.NoError(t, err)
97 res, _ := bundleSetup(t, p.caPath)
99 appConfig := lookupEnv(res.env, "BUNDLE_APP_CONFIG")
100 require.NotEmpty(t, appConfig, "a local BUNDLE_SSL_CA_CERT outranks the environment, so the local config must be replaced")
101 generated, err := os.ReadFile(filepath.Join(appConfig, "config"))
102 require.NoError(t, err)
103 assert.Contains(t, string(generated), "BUNDLE_JOBS: \"3\"\n", "the user's other local settings must be kept")
104 caPEM, err := os.ReadFile(generatedCACert(t, appConfig))
105 require.NoError(t, err)
106 assert.Equal(t, append(append([]byte{}, p.userPEM...), p.proxyPEM...), caPEM, "Bundler must trust the user's CA and the proxy")
107 plugins, err := os.Stat(filepath.Join(appConfig, "plugin"))
108 require.NoError(t, err)
109 assert.True(t, plugins.IsDir(), "local Bundler plugins must stay reachable")
111 res.stop()
112 assert.NoDirExists(t, appConfig)
113 after, err := os.ReadFile(p.config)
114 require.NoError(t, err)
115 assert.Equal(t, original, after, "an unchanged config must not be rewritten")
116}
118func TestBundleSetupWritesBackBundlerConfigChanges(t *testing.T) {
119 p := newBundleProject(t, "")
120 original, err := os.ReadFile(p.config)
121 require.NoError(t, err)
122 res, _ := bundleSetup(t, p.caPath)
123 appConfig := lookupEnv(res.env, "BUNDLE_APP_CONFIG")
124 require.NotEmpty(t, appConfig)
126 generated, err := os.ReadFile(filepath.Join(appConfig, "config"))
127 require.NoError(t, err)
128 require.NoError(t, os.WriteFile(filepath.Join(appConfig, "config"), append(generated, "BUNDLE_PATH: \"vendor/bundle\"\n"...), 0o600))
129 res.stop()
131 after, err := os.ReadFile(p.config)
132 require.NoError(t, err)
133 assert.Equal(t, string(original)+"BUNDLE_PATH: \"vendor/bundle\"\n", string(after))
134}
136func TestBundleSetupWarnsWhenCopiedConfigIsUnreadable(t *testing.T) {
137 p := newBundleProject(t, "")
138 original, err := os.ReadFile(p.config)
139 require.NoError(t, err)
140 res, errOut := bundleSetup(t, p.caPath)
141 appConfig := lookupEnv(res.env, "BUNDLE_APP_CONFIG")
142 require.NotEmpty(t, appConfig)
143 require.NoError(t, os.Remove(filepath.Join(appConfig, "config")))
145 res.stop()
147 assert.Contains(t, errOut.String(), "could not save Bundler's changes to "+p.config)
148 after, err := os.ReadFile(p.config)
149 require.NoError(t, err)
150 assert.Equal(t, original, after, "the user's config must be left as it was")
151 assert.NoDirExists(t, appConfig)
152}
154func TestBundleSetupWarnsWhenCopiedConfigDirIsGone(t *testing.T) {
155 p := newBundleProject(t, "")
156 original, err := os.ReadFile(p.config)
157 require.NoError(t, err)
158 res, errOut := bundleSetup(t, p.caPath)
159 appConfig := lookupEnv(res.env, "BUNDLE_APP_CONFIG")
160 require.NotEmpty(t, appConfig)
161 require.NoError(t, os.RemoveAll(appConfig))
163 res.stop()
165 assert.Contains(t, errOut.String(), "could not check "+appConfig+" for files Bundler added")
166 after, err := os.ReadFile(p.config)
167 require.NoError(t, err)
168 assert.Equal(t, original, after)
169 assert.NoDirExists(t, filepath.Dir(appConfig), "with nothing left to save, the temporary directory must be removed")
170}
172func TestBundleSetupKeepsUserConfigWhenCALineCannotBeRestored(t *testing.T) {
173 p := newBundleProject(t, "")
174 original, err := os.ReadFile(p.config)
175 require.NoError(t, err)
176 res, errOut := bundleSetup(t, p.caPath)
177 appConfig := lookupEnv(res.env, "BUNDLE_APP_CONFIG")
178 require.NotEmpty(t, appConfig)
179 rewritten := "---\nBUNDLE_SSL_CA_CERT: \"" + generatedCACert(t, appConfig) + "\\\"\"\nBUNDLE_PATH: \"vendor/bundle\"\n"
180 require.NoError(t, os.WriteFile(filepath.Join(appConfig, "config"), []byte(rewritten), 0o600))
182 res.stop()
184 assert.Contains(t, errOut.String(), "could not restore BUNDLE_SSL_CA_CERT")
185 after, err := os.ReadFile(p.config)
186 require.NoError(t, err)
187 assert.Equal(t, original, after, "the user's config must not name glab's temporary CA file")
188}
190func TestBundleSetupKeepsEntriesBundlerCreates(t *testing.T) {
191 p := newBundleProject(t, "")
192 res, errOut := bundleSetup(t, p.caPath)
193 appConfig := lookupEnv(res.env, "BUNDLE_APP_CONFIG")
194 require.NotEmpty(t, appConfig)
195 require.NoError(t, os.MkdirAll(filepath.Join(appConfig, "created"), 0o700))
196 require.NoError(t, os.WriteFile(filepath.Join(appConfig, "created", "index"), []byte("x"), 0o600))
198 res.stop()
200 assert.Empty(t, errOut.String())
201 assert.FileExists(t, filepath.Join(p.root, ".bundle", "created", "index"), "an entry Bundler added during the run must be kept")
202 assert.NoDirExists(t, appConfig)
203}
205func TestBundleSetupWarnsWhenACreatedEntryCannotBeSaved(t *testing.T) {
206 p := newBundleProject(t, "")
207 res, errOut := bundleSetup(t, p.caPath)
208 appConfig := lookupEnv(res.env, "BUNDLE_APP_CONFIG")
209 require.NotEmpty(t, appConfig)
210 t.Cleanup(func() { _ = os.RemoveAll(filepath.Dir(appConfig)) })
211 require.NoError(t, os.MkdirAll(filepath.Join(appConfig, "created", "new"), 0o700))
212 require.NoError(t, os.MkdirAll(filepath.Join(p.root, ".bundle", "created", "existing"), 0o700))
214 res.stop()
216 left := filepath.Join(appConfig, "created")
217 assert.Contains(t, errOut.String(), "could not save created")
218 assert.Contains(t, errOut.String(), "it is still in "+left)
219 assert.DirExists(t, filepath.Join(left, "new"), "the only copy of what Bundler added must be kept")
220 assert.DirExists(t, filepath.Join(p.root, ".bundle", "created", "existing"), "an existing entry must not be replaced")
221}
223func TestCopyEntryCopiesFilesAndDirectories(t *testing.T) {
224 t.Parallel()
225 src, dst := t.TempDir(), t.TempDir()
226 require.NoError(t, os.MkdirAll(filepath.Join(src, "plugin", "gems"), 0o700))
227 require.NoError(t, os.WriteFile(filepath.Join(src, "plugin", "index"), []byte("idx"), 0o600))
228 require.NoError(t, os.WriteFile(filepath.Join(src, "note"), []byte("n"), 0o700))
230 require.NoError(t, copyEntry(filepath.Join(src, "plugin"), filepath.Join(dst, "plugin")))
231 require.NoError(t, copyEntry(filepath.Join(src, "note"), filepath.Join(dst, "note")))
233 index, err := os.ReadFile(filepath.Join(dst, "plugin", "index"))
234 require.NoError(t, err)
235 assert.Equal(t, "idx", string(index))
236 assert.DirExists(t, filepath.Join(dst, "plugin", "gems"))
237 srcInfo, err := os.Stat(filepath.Join(src, "note"))
238 require.NoError(t, err)
239 info, err := os.Stat(filepath.Join(dst, "note"))
240 require.NoError(t, err)
241 assert.Equal(t, srcInfo.Mode().Perm(), info.Mode().Perm(), "the copy must keep the file's mode")
242 note, err := os.ReadFile(filepath.Join(dst, "note"))
243 require.NoError(t, err)
244 assert.Equal(t, "n", string(note))
245}
247func TestCopyEntryRefusesAnExistingTarget(t *testing.T) {
248 t.Parallel()
249 src, dst := t.TempDir(), t.TempDir()
250 require.NoError(t, os.WriteFile(filepath.Join(src, "note"), []byte("new"), 0o600))
251 require.NoError(t, os.WriteFile(filepath.Join(dst, "note"), []byte("old"), 0o600))
253 err := copyEntry(filepath.Join(src, "note"), filepath.Join(dst, "note"))
255 require.ErrorIs(t, err, fs.ErrExist)
256 kept, err := os.ReadFile(filepath.Join(dst, "note"))
257 require.NoError(t, err)
258 assert.Equal(t, "old", string(kept))
259}
261func TestBundleSetupFindsLocalConfig(t *testing.T) {
262 cases := []struct {
263 name string
264 parent func(p bundleProject) []string
265 moveTo string
266 }{
267 {name: "from a subdirectory"},
268 {
269 name: "BUNDLE_GEMFILE",
270 parent: func(p bundleProject) []string { return []string{"BUNDLE_GEMFILE=" + filepath.Join("..", "Gemfile")} },
271 },
272 {
273 name: "absolute BUNDLE_APP_CONFIG",
274 parent: func(p bundleProject) []string { return []string{"BUNDLE_APP_CONFIG=" + filepath.Join(p.root, "conf")} },
275 moveTo: "conf",
276 },
277 {
278 name: "relative BUNDLE_APP_CONFIG",
279 parent: func(bundleProject) []string { return []string{"BUNDLE_APP_CONFIG=conf"} },
280 moveTo: "conf",
281 },
282 }
283 for _, c := range cases {
284 t.Run(c.name, func(t *testing.T) {
285 p := newBundleProject(t, "")
286 var parent []string
287 if c.parent != nil {
288 parent = c.parent(p)
289 }
290 if c.moveTo != "" {
291 require.NoError(t, os.Rename(filepath.Join(p.root, ".bundle"), filepath.Join(p.root, c.moveTo)))
292 }
293 res, _ := bundleSetup(t, p.caPath, parent...)
294 t.Cleanup(res.stop)
295 appConfig := lookupEnv(res.env, "BUNDLE_APP_CONFIG")
296 require.NotEmpty(t, appConfig)
297 caPEM, err := os.ReadFile(generatedCACert(t, appConfig))
298 require.NoError(t, err)
299 assert.True(t, bytes.HasPrefix(caPEM, p.userPEM))
300 })
301 }
302}
304func TestBundleSetupLeavesOtherConfigsAlone(t *testing.T) {
305 cases := []struct {
306 name string
307 prep func(p bundleProject) error
308 parent []string
309 }{
310 {
311 name: "no CA in the local config",
312 prep: func(p bundleProject) error {
313 return os.WriteFile(p.config, []byte("---\nBUNDLE_JOBS: \"3\"\n"), 0o600)
314 },
315 },
316 {
317 name: "no local config",
318 prep: func(p bundleProject) error { return os.Remove(p.config) },
319 },
320 {
321 name: "no Gemfile",
322 prep: func(p bundleProject) error { return os.Remove(filepath.Join(p.root, "Gemfile")) },
323 },
324 {
325 name: "BUNDLE_APP_CONFIG elsewhere",
326 parent: []string{"BUNDLE_APP_CONFIG=elsewhere"},
327 },
328 }
329 for _, c := range cases {
330 t.Run(c.name, func(t *testing.T) {
331 p := newBundleProject(t, "")
332 if c.prep != nil {
333 require.NoError(t, c.prep(p))
334 }
335 res, _ := bundleSetup(t, p.caPath, c.parent...)
336 t.Cleanup(res.stop)
337 assert.Empty(t, lookupEnv(res.env, "BUNDLE_APP_CONFIG"))
338 })
339 }
340}
342func TestBundleSetupWarnsWhenLocalCACertIsUnreadable(t *testing.T) {
343 p := newBundleProject(t, "")
344 require.NoError(t, os.Remove(filepath.Join(p.root, "corp-ca.pem")))
346 res, errOut := bundleSetup(t, p.caPath)
347 t.Cleanup(res.stop)
349 assert.Contains(t, errOut.String(), "could not read BUNDLE_SSL_CA_CERT")
350 caPEM, err := os.ReadFile(generatedCACert(t, lookupEnv(res.env, "BUNDLE_APP_CONFIG")))
351 require.NoError(t, err)
352 assert.Equal(t, p.proxyPEM, caPEM, "the proxy must still be trusted")
353}
355func TestRunTrustsProxyDespiteLocalBundleConfig(t *testing.T) {
356 t.Setenv("GITLAB_CI", "")
357 t.Setenv("GLAB_DF_FAKE_DEFAULT", "allow")
358 for _, k := range []string{"BUNDLE_APP_CONFIG", "BUNDLE_GEMFILE"} {
359 t.Setenv(k, "")
360 require.NoError(t, os.Unsetenv(k))
361 }
362 p := newBundleProject(t, "")
363 ios, _, _, _ := cmdtest.TestIOStreams()
364 exec := &fakeExecutor{}
365 var caPEM, bundle []byte
366 exec.onExec = func() {
367 caPEM, _ = os.ReadFile(generatedCACert(t, lookupEnv(exec.env, "BUNDLE_APP_CONFIG")))
368 bundle, _ = os.ReadFile(lookupEnv(exec.env, "BUNDLE_SSL_CA_CERT"))
369 }
371 require.NoError(t, Run(t.Context(), Bundle(), RunOptions{
372 IO: ios, Executor: exec, BaseDir: t.TempDir(), ProjectID: "group/proj", Args: []string{"install"},
373 }))
375 require.NotEmpty(t, bundle)
376 assert.True(t, bytes.HasSuffix(caPEM, bundle), "the local config must name a file that holds glab's bundle")
377 assert.True(t, bytes.HasPrefix(caPEM, p.userPEM))
378}
Diagnostics7 git calls · 206 ms
CallStatusSizeTime0206 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:0200361 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=361 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=506cba622ceda1473ee3cde63969809c144b7247 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=361 git.pkt.payload.max=334 git.sideband.band1.bytes=334 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=334 git.pack.trailer=26d32585a3a474717e00a318584ccd16918c9d32Open pack map
fetchtree:02001.3 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=1327 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=2758711b27b3e124482ce287b5df86092bb20d62 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=1327 git.pkt.payload.max=1300 git.sideband.band1.bytes=1300 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=1300 git.pack.trailer=c83fc6004a9a1360845513c0fb16272b5c14a3e4Open pack map
fetch2003.1 KiB197 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=197 git.response.bytes=3170 git.response.cache=MISS git.response.duration_ms=197 git.fetch.wants=2289302deb9d6d340826ff7eee81a91f479aa6d5 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=3170 git.pkt.payload.max=3143 git.sideband.band1.bytes=3143 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
- a5e259d36eb5a23e
- Request
- 7c798c27…
- 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