GitLabgitlab-org
cli
main
1//go:build !integration
3package fsx
5import (
6 "errors"
7 "os"
8 "path/filepath"
9 "runtime"
10 "sync"
11 "syscall"
12 "testing"
13 "time"
15 "github.com/google/renameio/v2"
16 "github.com/stretchr/testify/assert"
17 "github.com/stretchr/testify/require"
18)
20func TestWriteOwnerOnly_CreatesFileWithMode0600(t *testing.T) {
21 t.Parallel()
22 if runtime.GOOS == "windows" {
23 t.Skip("POSIX file modes not enforced on Windows")
24 }
25 path := filepath.Join(t.TempDir(), "secret")
27 require.NoError(t, WriteOwnerOnly(path, []byte("token")))
29 got, err := os.ReadFile(path)
30 require.NoError(t, err)
31 assert.Equal(t, "token", string(got))
33 info, err := os.Stat(path)
34 require.NoError(t, err)
35 assert.Equal(t, os.FileMode(0o600), info.Mode().Perm())
36}
38func TestWriteOwnerOnly_TightensExistingFile(t *testing.T) {
39 t.Parallel()
40 if runtime.GOOS == "windows" {
41 t.Skip("POSIX file modes not enforced on Windows")
42 }
43 path := filepath.Join(t.TempDir(), "secret")
44 require.NoError(t, os.WriteFile(path, []byte("old"), 0o644))
46 require.NoError(t, WriteOwnerOnly(path, []byte("new")))
48 info, err := os.Stat(path)
49 require.NoError(t, err)
50 assert.Equal(t, os.FileMode(0o600), info.Mode().Perm())
51}
53func TestWriteOwnerOnly_IsAtomicOverwrite(t *testing.T) {
54 t.Parallel()
55 // Atomicity is not directly observable, but renameio's temp-file-plus-rename
56 // implementation changes the file's inode on every write. os.WriteFile, in
57 // contrast, truncates and rewrites the same inode. Assert the inode changes
58 // on overwrite as a proxy for "the new bytes appeared via rename, not via
59 // in-place truncation" — which is the observable property that guarantees
60 // a reader either sees the full old file or the full new file, never a
61 // half-written mix.
62 if runtime.GOOS == "windows" {
63 t.Skip("inode identity is a POSIX concept; atomicity path is only wired for !windows")
64 }
65 path := filepath.Join(t.TempDir(), "secret")
66 require.NoError(t, WriteOwnerOnly(path, []byte("old")))
68 before, err := os.Stat(path)
69 require.NoError(t, err)
70 beforeStat, ok := before.Sys().(*syscall.Stat_t)
71 require.True(t, ok, "expected *syscall.Stat_t on POSIX")
73 require.NoError(t, WriteOwnerOnly(path, []byte("new")))
75 after, err := os.Stat(path)
76 require.NoError(t, err)
77 afterStat, ok := after.Sys().(*syscall.Stat_t)
78 require.True(t, ok, "expected *syscall.Stat_t on POSIX")
80 assert.NotEqual(t, beforeStat.Ino, afterStat.Ino, "expected atomic replace (new inode) rather than in-place truncation")
81}
83func TestWriteOwnerOnly_PendingFileIs0600BeforeWrite(t *testing.T) {
84 t.Parallel()
85 // The end-state-mode assertions above cannot distinguish this
86 // implementation from the write-then-chmod shape the package doc rejects:
87 // both land on 0o600. Pin the mechanism instead. WriteOwnerOnly drives
88 // renameio.NewPendingFile with WithStaticPermissions(0o600); build a
89 // pending file the same way and assert the temp file is already 0o600
90 // before any bytes are written and before the atomic rename — proving the
91 // mode is set at creation, not tightened afterwards through a chmod window.
92 if runtime.GOOS == "windows" {
93 t.Skip("atomic pending-file path is only wired for !windows")
94 }
95 path := filepath.Join(t.TempDir(), "secret")
96 f, err := renameio.NewPendingFile(path, renameio.WithStaticPermissions(0o600))
97 require.NoError(t, err)
98 defer func() { _ = f.Cleanup() }()
100 info, err := os.Stat(f.Name())
101 require.NoError(t, err)
102 assert.Equal(t, os.FileMode(0o600), info.Mode().Perm(),
103 "temp file must be 0o600 at creation, before any write or rename")
104}
106func TestWriteOwnerOnly_TargetNeverDisappearsDuringOverwrite(t *testing.T) {
107 t.Parallel()
108 // A delete+recreate impostor (os.Remove then os.WriteFile) would also
109 // change the inode and land on 0o600, passing the atomicity/mode tests
110 // above. Rule it out: while an overwrite runs concurrently, the target
111 // path must never be observed as ENOENT — the atomic rename replaces the
112 // file in a single step and there is no window where it is absent.
113 if runtime.GOOS == "windows" {
114 t.Skip("atomic replace is only wired for !windows")
115 }
116 path := filepath.Join(t.TempDir(), "secret")
117 require.NoError(t, WriteOwnerOnly(path, []byte("old")))
119 // The writer goroutine reports any write error over a channel rather than
120 // calling require/assert/t.Fatal itself: those must run only in the test's
121 // own goroutine (testifylint go-require).
122 writeErr := make(chan error, 1)
123 done := make(chan struct{})
124 go func() {
125 defer close(done)
126 for range 200 {
127 if err := WriteOwnerOnly(path, []byte("new")); err != nil {
128 writeErr <- err
129 return
130 }
131 }
132 }()
133 for {
134 select {
135 case <-done:
136 select {
137 case err := <-writeErr:
138 require.NoError(t, err)
139 default:
140 }
141 return
142 default:
143 if _, err := os.Stat(path); os.IsNotExist(err) {
144 t.Fatal("target path was momentarily absent during overwrite; write is not an atomic replace")
145 }
146 }
147 }
148}
150func TestWithLock_SerializesConcurrentCriticalSections(t *testing.T) {
151 if runtime.GOOS == "windows" {
152 t.Skip("WithLock is a no-op on Windows")
153 }
154 // Two goroutines contending for the same lock must never overlap inside
155 // the critical section. Without the flock this races and inflight peaks
156 // above 1; with it, the second caller blocks until the first releases.
157 lockPath := filepath.Join(t.TempDir(), "df", "ci-log.lock")
159 var (
160 mu sync.Mutex
161 inflight int
162 maxSeen int
163 )
165 var wg sync.WaitGroup
166 for range 4 {
167 wg.Go(func() {
168 err := WithLock(lockPath, func() error {
169 mu.Lock()
170 inflight++
171 if inflight > maxSeen {
172 maxSeen = inflight
173 }
174 mu.Unlock()
176 time.Sleep(20 * time.Millisecond) // widen the overlap window
178 mu.Lock()
179 inflight--
180 mu.Unlock()
181 return nil
182 })
183 assert.NoError(t, err)
184 })
185 }
186 wg.Wait()
188 assert.Equal(t, 1, maxSeen, "WithLock must serialize the critical section")
189}
191func TestWithLock_PropagatesError(t *testing.T) {
192 if runtime.GOOS == "windows" {
193 t.Skip("WithLock is a no-op on Windows")
194 }
195 lockPath := filepath.Join(t.TempDir(), "ci-log.lock")
196 sentinel := errors.New("boom")
197 err := WithLock(lockPath, func() error { return sentinel })
198 assert.ErrorIs(t, err, sentinel, "WithLock must return fn's error unchanged")
199}
Diagnostics7 git calls · 222 ms
CallStatusSizeTime0222 ms
discover200185 B2 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 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 · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=6 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 · hit6 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=6 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=3 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:0200256 B · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=209 git.response.bytes=256 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=75748a4e96b6bf353ebfd55a68dfa7e7557dd079 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=256 git.pkt.payload.max=229 git.sideband.band1.bytes=229 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=229 git.pack.trailer=408da6a74c29650ab3e4fdb9be85188528fb026bOpen pack map
fetch2002.1 KiB209 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=2196 git.response.cache=MISS git.response.duration_ms=209 git.fetch.wants=c39f5cf74637f82e19e834115ae89de94fd9bc5b 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=2196 git.pkt.payload.max=2169 git.sideband.band1.bytes=2169 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
- a5e2abaa1bb2e6b0
- Request
- b83f7a27…
- 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