Skip to content

Commit

Permalink
vfs: add MemFS.TestingSetDiskUsage
Browse files Browse the repository at this point in the history
Adding a testing facility to allow `MemFS` to mock disk usage stats.
This will be used in CRDB for a test for the low disk space event
(together with the sticky registry).
  • Loading branch information
RaduBerinde committed Jan 14, 2025
1 parent 8cbd9c6 commit ba0eeec
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion vfs/mem_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ type MemFS struct {
// open files. In tests meant to exercise this behavior, this flag can be
// set to error if removing an open file.
windowsSemantics bool
usage DiskUsage
}

var _ FS = &MemFS{}
Expand Down Expand Up @@ -596,8 +597,23 @@ func (*MemFS) PathDir(p string) string {
return path.Dir(p)
}

// TestingSetDiskUsage sets the disk usage that will be reported on subsequent
// GetDiskUsage calls; used for testing. If usage is empty, GetDiskUsage will
// return ErrUnsupported (which is also the default when TestingSetDiskUsage is
// not called).
func (y *MemFS) TestingSetDiskUsage(usage DiskUsage) {
y.mu.Lock()
defer y.mu.Unlock()
y.usage = usage
}

// GetDiskUsage implements FS.GetDiskUsage.
func (*MemFS) GetDiskUsage(string) (DiskUsage, error) {
func (y *MemFS) GetDiskUsage(string) (DiskUsage, error) {
y.mu.Lock()
defer y.mu.Unlock()
if y.usage != (DiskUsage{}) {
return y.usage, nil
}
return DiskUsage{}, ErrUnsupported
}

Expand Down

0 comments on commit ba0eeec

Please sign in to comment.