Skip to content

Commit

Permalink
tests: procfs: clean up mock test hook
Browse files Browse the repository at this point in the history
To make the code a little easier to follow, add some actual dummy
functions that we assign to rather than re-using previous functions in a
confusing way.

Fixes: 6864912 ("Isolate the testing import in test code")
Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
  • Loading branch information
cyphar committed Oct 8, 2024
1 parent 3ec6eed commit ecb1b8e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
23 changes: 11 additions & 12 deletions procfs_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,17 +425,16 @@ func checkProcSelfFdPath(path string, file *os.File) error {
return nil
}

// Test hooks
var hookForcePrivateProcRootOpenTree = func(_ *os.File) bool {
return false
}

var hookForcePrivateProcRootOpenTreeAtRecursive = hookForcePrivateProcRootOpenTree

var hookForceGetProcRootUnsafe = func() bool {
return false
}
// Test hooks used in the procfs tests to verify that the fallback logic works.
// See testing_mocks_linux_test.go and procfs_linux_test.go for more details.
var (
hookForcePrivateProcRootOpenTree = hookDummyFile
hookForcePrivateProcRootOpenTreeAtRecursive = hookDummyFile
hookForceGetProcRootUnsafe = hookDummy

var hookForceProcSelfTask = hookForceGetProcRootUnsafe
hookForceProcSelfTask = hookDummy
hookForceProcSelf = hookDummy
)

var hookForceProcSelf = hookForceGetProcRootUnsafe
func hookDummy() bool { return false }
func hookDummyFile(_ *os.File) bool { return false }
5 changes: 5 additions & 0 deletions procfs_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,3 +418,8 @@ func TestVerifyProcRoot_NotProc(t *testing.T) {
testVerifyProcRoot(t, t.TempDir(), errUnsafeProcfs, "incorrect procfs root filesystem type")
})
}

func TestProcfsDummyHooks(t *testing.T) {
assert.False(t, hookDummy(), "hookDummy should always return false")
assert.False(t, hookDummyFile(nil), "hookDummyFile should always return false")
}
1 change: 1 addition & 0 deletions testing_mocks_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func init() {
hookForceGetProcRootUnsafe = testingForceGetProcRootUnsafe
hookForcePrivateProcRootOpenTree = testingForcePrivateProcRootOpenTree
hookForcePrivateProcRootOpenTreeAtRecursive = testingForcePrivateProcRootOpenTreeAtRecursive

hookForceProcSelf = testingForceProcSelf
hookForceProcSelfTask = testingForceProcSelfTask
}

0 comments on commit ecb1b8e

Please sign in to comment.