Skip to content

test(utils): Fix root related test failures in CopyFile and MoveFile #976

Description

@Princess0407

/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:

  1. Root can create directories even where files exist (different error path)
  2. Root can write to read only mounts
  3. 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

  • go test ./pkg/unikontainers/... passes when run as root.
  • go test ./pkg/unikontainers/... still passes when run as non root.

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions