From ecb1b8e3a0e8c026cdfb59a66f4f7005b9aaacbd Mon Sep 17 00:00:00 2001 From: Aleksa Sarai Date: Wed, 9 Oct 2024 01:38:56 +1100 Subject: [PATCH] tests: procfs: clean up mock test hook 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: 686491226941 ("Isolate the testing import in test code") Signed-off-by: Aleksa Sarai --- procfs_linux.go | 23 +++++++++++------------ procfs_linux_test.go | 5 +++++ testing_mocks_linux_test.go | 1 + 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/procfs_linux.go b/procfs_linux.go index ba179de..8cc827d 100644 --- a/procfs_linux.go +++ b/procfs_linux.go @@ -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 } diff --git a/procfs_linux_test.go b/procfs_linux_test.go index 5f7f9a1..61bfed6 100644 --- a/procfs_linux_test.go +++ b/procfs_linux_test.go @@ -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") +} diff --git a/testing_mocks_linux_test.go b/testing_mocks_linux_test.go index c3d554c..d7a2bf5 100644 --- a/testing_mocks_linux_test.go +++ b/testing_mocks_linux_test.go @@ -71,6 +71,7 @@ func init() { hookForceGetProcRootUnsafe = testingForceGetProcRootUnsafe hookForcePrivateProcRootOpenTree = testingForcePrivateProcRootOpenTree hookForcePrivateProcRootOpenTreeAtRecursive = testingForcePrivateProcRootOpenTreeAtRecursive + hookForceProcSelf = testingForceProcSelf hookForceProcSelfTask = testingForceProcSelfTask }