GitLabgitlab-org

cli

main
ply6 git calls37 ms97 KiB4 cachedprotocol v2
152 lines5.2 KiBRaw
1//go:build !integration
3package fsx
5import (
6 "os"
7 "path/filepath"
8 "runtime"
9 "strings"
10 "syscall"
11 "testing"
13 "github.com/stretchr/testify/assert"
14 "github.com/stretchr/testify/require"
17func TestWriteOwnerOnly_CreatesFileWithMode0600(t *testing.T) {
18 if runtime.GOOS == "windows" {
19 t.Skip("POSIX file modes not enforced on Windows")
20 }
21 path := filepath.Join(t.TempDir(), "secret")
23 require.NoError(t, WriteOwnerOnly(path, []byte("token")))
25 got, err := os.ReadFile(path)
26 require.NoError(t, err)
27 assert.Equal(t, "token", string(got))
29 info, err := os.Stat(path)
30 require.NoError(t, err)
31 assert.Equal(t, os.FileMode(0o600), info.Mode().Perm())
34func TestWriteOwnerOnly_TightensExistingFile(t *testing.T) {
35 if runtime.GOOS == "windows" {
36 t.Skip("POSIX file modes not enforced on Windows")
37 }
38 path := filepath.Join(t.TempDir(), "secret")
39 require.NoError(t, os.WriteFile(path, []byte("old"), 0o644))
41 require.NoError(t, WriteOwnerOnly(path, []byte("new")))
43 info, err := os.Stat(path)
44 require.NoError(t, err)
45 assert.Equal(t, os.FileMode(0o600), info.Mode().Perm())
48func TestWriteOwnerOnly_IsAtomicOverwrite(t *testing.T) {
49 // Atomicity is not directly observable, but renameio's temp-file-plus-rename
50 // implementation changes the file's inode on every write. os.WriteFile, in
51 // contrast, truncates and rewrites the same inode. Assert the inode changes
52 // on overwrite as a proxy for "the new bytes appeared via rename, not via
53 // in-place truncation" — which is the observable property that guarantees
54 // a reader either sees the full old file or the full new file, never a
55 // half-written mix.
56 if runtime.GOOS == "windows" {
57 t.Skip("inode identity is a POSIX concept; atomicity path is only wired for !windows")
58 }
59 path := filepath.Join(t.TempDir(), "secret")
60 require.NoError(t, WriteOwnerOnly(path, []byte("old")))
62 before, err := os.Stat(path)
63 require.NoError(t, err)
64 beforeStat, ok := before.Sys().(*syscall.Stat_t)
65 require.True(t, ok, "expected *syscall.Stat_t on POSIX")
67 require.NoError(t, WriteOwnerOnly(path, []byte("new")))
69 after, err := os.Stat(path)
70 require.NoError(t, err)
71 afterStat, ok := after.Sys().(*syscall.Stat_t)
72 require.True(t, ok, "expected *syscall.Stat_t on POSIX")
74 assert.NotEqual(t, beforeStat.Ino, afterStat.Ino, "expected atomic replace (new inode) rather than in-place truncation")
77func TestWriteExecutable_CreatesFileWithMode0700(t *testing.T) {
78 if runtime.GOOS == "windows" {
79 t.Skip("POSIX file modes not enforced on Windows")
80 }
81 path := filepath.Join(t.TempDir(), "shim")
83 require.NoError(t, WriteExecutable(path, []byte("script")))
85 got, err := os.ReadFile(path)
86 require.NoError(t, err)
87 assert.Equal(t, "script", string(got))
89 info, err := os.Stat(path)
90 require.NoError(t, err)
91 assert.Equal(t, os.FileMode(0o700), info.Mode().Perm())
94// TestWriteExecutable_ForcesModeRegardlessOfExistingFile pins that the mode
95// is not copied from a pre-existing file the way WithExistingPermissions
96// would: a shim left at a stale mode by an older version must still come
97// back executable.
98func TestWriteExecutable_ForcesModeRegardlessOfExistingFile(t *testing.T) {
99 if runtime.GOOS == "windows" {
100 t.Skip("POSIX file modes not enforced on Windows")
101 }
102 path := filepath.Join(t.TempDir(), "shim")
103 require.NoError(t, os.WriteFile(path, []byte("old"), 0o644))
105 require.NoError(t, WriteExecutable(path, []byte("new")))
107 info, err := os.Stat(path)
108 require.NoError(t, err)
109 assert.Equal(t, os.FileMode(0o700), info.Mode().Perm())
112// TestWriteExecutable_IsAtomicOverwrite is the same inode-identity proxy
113// TestWriteOwnerOnly_IsAtomicOverwrite uses: the file only ever appears at
114// its final path via rename, never via in-place truncation, so a concurrent
115// reader cannot observe a torn or transiently non-executable file.
116func TestWriteExecutable_IsAtomicOverwrite(t *testing.T) {
117 if runtime.GOOS == "windows" {
118 t.Skip("inode identity is a POSIX concept; atomicity path is only wired for !windows")
119 }
120 path := filepath.Join(t.TempDir(), "shim")
121 require.NoError(t, WriteExecutable(path, []byte("old")))
123 before, err := os.Stat(path)
124 require.NoError(t, err)
125 beforeStat, ok := before.Sys().(*syscall.Stat_t)
126 require.True(t, ok, "expected *syscall.Stat_t on POSIX")
128 require.NoError(t, WriteExecutable(path, []byte("new")))
130 after, err := os.Stat(path)
131 require.NoError(t, err)
132 afterStat, ok := after.Sys().(*syscall.Stat_t)
133 require.True(t, ok, "expected *syscall.Stat_t on POSIX")
135 assert.NotEqual(t, beforeStat.Ino, afterStat.Ino, "expected atomic replace (new inode) rather than in-place truncation")
138func TestWriteJSONFile_CreatesParentAndAppendsNewline(t *testing.T) {
139 path := filepath.Join(t.TempDir(), "nested", "dir", "config.json")
141 require.NoError(t, WriteJSONFile(path, map[string]any{"k": "v"}))
143 raw, err := os.ReadFile(path)
144 require.NoError(t, err)
145 // Two-space indent is deliberate (users hand-edit this file), and
146 // the trailing newline is a POSIX text-file convention that keeps
147 // `git diff` clean on later edits — assert both explicitly rather
148 // than with JSONEq, which strips whitespace.
149 body := string(raw)
150 assert.True(t, strings.HasSuffix(body, "\n"), "file should end with newline, got %q", body)
151 assert.Contains(t, body, " \"k\": \"v\"", "expected two-space indent")
Diagnostics6 git calls · 37 ms
CallStatusSizeTime037 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=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 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=2
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 · 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=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=44
Open pack map
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=4
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:0200202 B · hit5 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=25
git.response.bytes=202
git.response.cache=HIT
git.response.duration_ms=5
git.fetch.filter=tree:0
git.fetch.wants=da30e344667699ee3f5c5b32590d1fa98350e85d
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=202
git.pkt.payload.max=175
git.sideband.band1.bytes=175
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=175
git.pack.trailer=fb90a53cb363fe86844dccc988be2a05632002e8
Open pack map
fetch2001.5 KiB · hit25 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=1534
git.response.cache=HIT
git.response.duration_ms=25
git.fetch.wants=17e25c3b3580fb9f81bcd7adafef44c9670e3beb
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=1534
git.pkt.payload.max=1507
git.sideband.band1.bytes=1507
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
a5e28591dffe4af4
Request
f794dfc9…
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