/kind testing
/area unikontainers
Description
TestCopyFile and TestMoveFile in pkg/unikontainers/utils_test.go contain test cases that assume permission based errors will occur, but these assertions fail when tests run as root (which is common in containers and CI environments).
Failing Tests
When running make unittest as root (e.g., inside a dev container or CI runner):
| Test Case |
Expected |
Actual |
TestCopyFile/copy_file_target_dir_creation_failed |
mkdir returns permission denied |
nil error root can create any directory |
TestCopyFile/copy_file_target_file_creation_failed |
Read only filesystem error |
nil error root ignores read only flags |
TestMoveFile/move_file_target_file_creation_failed |
Read-only filesystem error + source preserved |
File gets moved....root bypasses read only |
Root Cause
The tests use permission based tricks to force errors:
copy_file_target_dir_creation_failed: Creates a file at the target path, then tries to mkdir a directory at the same path (expecting ENOTDIR)
copy_file_target_file_creation_failed: Mounts a tmpfs with ro (read-only) flag, expects write to fail
move_file_target_file_creation_failed: Same read-only tmpfs trick
These approaches don't work under root because:
- Root can create directories even where files exist (different error path)
- Root can write to read only mounts
- Root can move files off read only mounts
Proposed Fix
Option A: Skip when root
if os.Getuid() == 0 {
t.Skip("permission-based test skipped when running as root")
}
This preserves the real permission tests for normal users and CI runners that don't run as root. It's what most Go projects usually do.
Option B: Make the tests work for root too
For copy_file_target_dir_creation_failed: .....instead of relying on permissions, create a regular file at the parent path, then try to MkdirAll inside it. Even root gets ENOTDIR when trying to create a directory inside a file. For copy_file_target_file_creation_failed and move_file_target_file_creation_failed: These genuinely cannot be tested as root because root bypasses read only mounts. You'd need t.Skip() for these two.
Acceptance Criteria
Related
/kind testing
/area unikontainers
Description
TestCopyFileandTestMoveFileinpkg/unikontainers/utils_test.gocontain test cases that assume permission based errors will occur, but these assertions fail when tests run asroot(which is common in containers and CI environments).Failing Tests
When running
make unittestas root (e.g., inside a dev container or CI runner):TestCopyFile/copy_file_target_dir_creation_failedmkdirreturns permission deniednilerror root can create any directoryTestCopyFile/copy_file_target_file_creation_failednilerror root ignores read only flagsTestMoveFile/move_file_target_file_creation_failedRoot Cause
The tests use permission based tricks to force errors:
copy_file_target_dir_creation_failed: Creates a file at the target path, then tries tomkdira directory at the same path (expectingENOTDIR)copy_file_target_file_creation_failed: Mounts a tmpfs withro(read-only) flag, expects write to failmove_file_target_file_creation_failed: Same read-only tmpfs trickThese approaches don't work under root because:
Proposed Fix
Option A: Skip when root
This preserves the real permission tests for normal users and CI runners that don't run as root. It's what most Go projects usually do.
Option B: Make the tests work for root too
For
copy_file_target_dir_creation_failed:.....instead of relying on permissions, create a regular file at the parent path, then try toMkdirAllinside it. Even root getsENOTDIRwhen trying to create a directory inside a file. Forcopy_file_target_file_creation_failedandmove_file_target_file_creation_failed:These genuinely cannot be tested as root because root bypasses read only mounts. You'd needt.Skip()for these two.Acceptance Criteria
go test ./pkg/unikontainers/...passes when run as root.go test ./pkg/unikontainers/...still passes when run as non root.Related