diff options
| author | Eric Biggers <ebiggers@google.com> | 2019-10-29 00:33:54 -0700 |
|---|---|---|
| committer | Eric Biggers <ebiggers@google.com> | 2019-10-30 09:25:38 -0700 |
| commit | e71c5e4f70632b99a08d127b35e80a9e291e1938 (patch) | |
| tree | c41774c7422e3cb5e55a753c79d4c45fe3692501 /filesystem/mountpoint.go | |
| parent | dbafdbaa9b0767f71affaf15fb8c626f64e27122 (diff) | |
filesystem: add unit tests for loadMountInfo()
Add a version of loadMountInfo() that takes an io.Reader parameter to
allow injecting a custom mountinfo file, then add some unit tests.
Diffstat (limited to 'filesystem/mountpoint.go')
| -rw-r--r-- | filesystem/mountpoint.go | 34 |
1 files changed, 21 insertions, 13 deletions
diff --git a/filesystem/mountpoint.go b/filesystem/mountpoint.go index 50389c0..d9dbf37 100644 --- a/filesystem/mountpoint.go +++ b/filesystem/mountpoint.go @@ -24,6 +24,7 @@ package filesystem import ( "bufio" "fmt" + "io" "io/ioutil" "log" "os" @@ -133,21 +134,12 @@ func parseMountInfoLine(line string) *Mount { return mnt } -// loadMountInfo populates the Mount mappings by parsing /proc/self/mountinfo. -// It returns an error if the Mount mappings cannot be populated. -func loadMountInfo() error { - if mountsInitialized { - return nil - } +// This is separate from loadMountInfo() only for unit testing. +func readMountInfo(r io.Reader) error { mountsByPath := make(map[string]*Mount) mountsByDevice = make(map[DeviceNumber]*Mount) - file, err := os.Open("/proc/self/mountinfo") - if err != nil { - return err - } - defer file.Close() - scanner := bufio.NewScanner(file) + scanner := bufio.NewScanner(r) for scanner.Scan() { line := scanner.Text() mnt := parseMountInfoLine(line) @@ -185,7 +177,23 @@ func loadMountInfo() error { mountsByDevice[mnt.DeviceNumber] = mnt } } - mountsInitialized = true + return nil +} + +// loadMountInfo populates the Mount mappings by parsing /proc/self/mountinfo. +// It returns an error if the Mount mappings cannot be populated. +func loadMountInfo() error { + if !mountsInitialized { + file, err := os.Open("/proc/self/mountinfo") + if err != nil { + return err + } + defer file.Close() + if err := readMountInfo(file); err != nil { + return err + } + mountsInitialized = true + } return nil } |