-
Notifications
You must be signed in to change notification settings - Fork 18.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46158 from elezar/refactor-rootless-tempdir
Add testutil.TempDir function
- Loading branch information
Showing
2 changed files
with
27 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
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 | ||
} |