moby/testutil/temp_files.go
Evan Lezar f7065ab207
Add testutil.TempDir function
This change adds a TempDir function that ensures the correct permissions for
the fake-root user in rootless mode.

Signed-off-by: Evan Lezar <elezar@nvidia.com>
2024-01-17 15:44:47 +01:00

26 lines
673 B
Go

package testutil // import "github.com/docker/docker/testutil"
import (
"os"
"path/filepath"
"testing"
)
// TempDir returns a temporary directory for use in tests.
// t.TempDir() can't be used as the temporary directory returned by
// that function cannot be accessed by the fake-root user for rootless
// Docker. It creates a nested hierarchy of directories where the
// outermost has permission 0700.
func TempDir(t *testing.T) string {
t.Helper()
dir := t.TempDir()
parent := filepath.Dir(dir)
if parent != "" {
if err := os.Chmod(parent, 0o777); err != nil {
t.Fatalf("Failed to chmod parent of temp directory %q: %v", parent, err)
}
}
return dir
}