@@ -369,8 +369,27 @@ func TestCopySBOMToChroot_MissingSourceSBOM(t *testing.T) {
369369}
370370
371371func TestCopySBOMToChroot_InvalidChrootPath (t * testing.T ) {
372- // Try to copy to a path that can't be created (requires root permissions)
373- invalidPath := "/root/should_not_exist/chroot"
372+ // Skip this test if running as root (e.g., in Docker/Earthly containers)
373+ // because root can write to read-only directories
374+ if os .Geteuid () == 0 {
375+ t .Skip ("Skipping test when running as root (permission checks don't apply)" )
376+ }
377+
378+ // Create a directory and make it read-only to simulate permission issues
379+ tmpDir := t .TempDir ()
380+ invalidPath := filepath .Join (tmpDir , "readonly" )
381+
382+ // Create the directory
383+ if err := os .MkdirAll (invalidPath , 0755 ); err != nil {
384+ t .Fatalf ("Failed to create test directory: %v" , err )
385+ }
386+
387+ // Make it read-only (no write permissions)
388+ if err := os .Chmod (invalidPath , 0555 ); err != nil {
389+ t .Fatalf ("Failed to change directory permissions: %v" , err )
390+ }
391+ // Restore permissions after test for cleanup
392+ defer os .Chmod (invalidPath , 0755 )
374393
375394 // Create source SBOM file
376395 tempDir := config .TempDir ()
@@ -386,10 +405,10 @@ func TestCopySBOMToChroot_InvalidChrootPath(t *testing.T) {
386405 }
387406 defer os .Remove (srcSBOM )
388407
389- // Should return an error
408+ // Should return an error when trying to create subdirectory in read-only dir
390409 err := CopySBOMToChroot (invalidPath )
391410 if err == nil {
392- t .Errorf ("Expected error when copying to invalid chroot path, got nil" )
411+ t .Errorf ("Expected error when copying to read-only chroot path, got nil" )
393412 }
394413}
395414
0 commit comments