Skip to content

Commit

Permalink
Merge pull request #46158 from elezar/refactor-rootless-tempdir
Browse files Browse the repository at this point in the history
Add testutil.TempDir function
  • Loading branch information
thaJeztah committed Jan 17, 2024
2 parents 436bf27 + f7065ab commit 4f9c865
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
12 changes: 1 addition & 11 deletions integration/container/run_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,18 +209,8 @@ func TestRunWithAlternativeContainerdShim(t *testing.T) {
realShimPath, err = filepath.Abs(realShimPath)
assert.Assert(t, err)

// t.TempDir() can't be used here 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.
shimDir, err := os.MkdirTemp("", t.Name())
shimDir := testutil.TempDir(t)
assert.Assert(t, err)
t.Cleanup(func() {
if err := os.RemoveAll(shimDir); err != nil {
t.Errorf("shimDir RemoveAll cleanup: %v", err)
}
})
assert.Assert(t, os.Chmod(shimDir, 0o777))
shimDir, err = filepath.Abs(shimDir)
assert.Assert(t, err)
assert.Assert(t, os.Symlink(realShimPath, filepath.Join(shimDir, "containerd-shim-realfake-v42")))
Expand Down
26 changes: 26 additions & 0 deletions testutil/temp_files.go
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
}

0 comments on commit 4f9c865

Please sign in to comment.