diff --git a/pkg/system/chtimes_unix_test.go b/pkg/system/chtimes_linux_test.go similarity index 99% rename from pkg/system/chtimes_unix_test.go rename to pkg/system/chtimes_linux_test.go index 779ed4b64c..273fc0a893 100644 --- a/pkg/system/chtimes_unix_test.go +++ b/pkg/system/chtimes_linux_test.go @@ -1,5 +1,3 @@ -// +build !windows - package system // import "github.com/docker/docker/pkg/system" import ( diff --git a/pkg/system/chtimes_unix.go b/pkg/system/chtimes_nowindows.go similarity index 100% rename from pkg/system/chtimes_unix.go rename to pkg/system/chtimes_nowindows.go diff --git a/pkg/system/rm_unix.go b/pkg/system/rm.go similarity index 98% rename from pkg/system/rm_unix.go rename to pkg/system/rm.go index 6ba3fb3ae8..c5d80ebda1 100644 --- a/pkg/system/rm_unix.go +++ b/pkg/system/rm.go @@ -1,4 +1,4 @@ -// +build !windows +// +build !darwin,!windows package system // import "github.com/docker/docker/pkg/system" diff --git a/pkg/system/rm_nodarwin_test.go b/pkg/system/rm_nodarwin_test.go new file mode 100644 index 0000000000..f3db37ed10 --- /dev/null +++ b/pkg/system/rm_nodarwin_test.go @@ -0,0 +1,36 @@ +// +build !darwin + +package system // import "github.com/docker/docker/pkg/system" + +import ( + "io/ioutil" + "testing" +) + +func TestEnsureRemoveAllNotExist(t *testing.T) { + // should never return an error for a non-existent path + if err := EnsureRemoveAll("/non/existent/path"); err != nil { + t.Fatal(err) + } +} + +func TestEnsureRemoveAllWithDir(t *testing.T) { + dir, err := ioutil.TempDir("", "test-ensure-removeall-with-dir") + if err != nil { + t.Fatal(err) + } + if err := EnsureRemoveAll(dir); err != nil { + t.Fatal(err) + } +} + +func TestEnsureRemoveAllWithFile(t *testing.T) { + tmp, err := ioutil.TempFile("", "test-ensure-removeall-with-dir") + if err != nil { + t.Fatal(err) + } + tmp.Close() + if err := EnsureRemoveAll(tmp.Name()); err != nil { + t.Fatal(err) + } +} diff --git a/pkg/system/rm_test.go b/pkg/system/rm_test.go index fcb0ef932b..245edb2cca 100644 --- a/pkg/system/rm_test.go +++ b/pkg/system/rm_test.go @@ -1,34 +1,56 @@ +// +build !darwin,!windows + package system // import "github.com/docker/docker/pkg/system" import ( "io/ioutil" + "os" + "path/filepath" "testing" + "time" + + "github.com/moby/sys/mount" + "gotest.tools/v3/skip" ) -func TestEnsureRemoveAllNotExist(t *testing.T) { - // should never return an error for a non-existent path - if err := EnsureRemoveAll("/non/existent/path"); err != nil { - t.Fatal(err) - } -} +func TestEnsureRemoveAllWithMount(t *testing.T) { + skip.If(t, os.Getuid() != 0, "skipping test that requires root") -func TestEnsureRemoveAllWithDir(t *testing.T) { - dir, err := ioutil.TempDir("", "test-ensure-removeall-with-dir") + dir1, err := ioutil.TempDir("", "test-ensure-removeall-with-dir1") if err != nil { t.Fatal(err) } - if err := EnsureRemoveAll(dir); err != nil { - t.Fatal(err) - } -} - -func TestEnsureRemoveAllWithFile(t *testing.T) { - tmp, err := ioutil.TempFile("", "test-ensure-removeall-with-dir") + dir2, err := ioutil.TempDir("", "test-ensure-removeall-with-dir2") if err != nil { t.Fatal(err) } - tmp.Close() - if err := EnsureRemoveAll(tmp.Name()); err != nil { + defer os.RemoveAll(dir2) + + bindDir := filepath.Join(dir1, "bind") + if err := os.MkdirAll(bindDir, 0755); err != nil { t.Fatal(err) } + + if err := mount.Mount(dir2, bindDir, "none", "bind"); err != nil { + t.Fatal(err) + } + + done := make(chan struct{}, 1) + go func() { + err = EnsureRemoveAll(dir1) + close(done) + }() + + select { + case <-done: + if err != nil { + t.Fatal(err) + } + case <-time.After(5 * time.Second): + t.Fatal("timeout waiting for EnsureRemoveAll to finish") + } + + if _, err := os.Stat(dir1); !os.IsNotExist(err) { + t.Fatalf("expected %q to not exist", dir1) + } } diff --git a/pkg/system/rm_unix_test.go b/pkg/system/rm_unix_test.go deleted file mode 100644 index 934a98fd0d..0000000000 --- a/pkg/system/rm_unix_test.go +++ /dev/null @@ -1,58 +0,0 @@ -// +build !windows - -package system // import "github.com/docker/docker/pkg/system" - -import ( - "io/ioutil" - "os" - "path/filepath" - "runtime" - "testing" - "time" - - "github.com/moby/sys/mount" - "gotest.tools/v3/skip" -) - -func TestEnsureRemoveAllWithMount(t *testing.T) { - skip.If(t, runtime.GOOS == "windows", "mount not supported on Windows") - skip.If(t, os.Getuid() != 0, "skipping test that requires root") - - dir1, err := ioutil.TempDir("", "test-ensure-removeall-with-dir1") - if err != nil { - t.Fatal(err) - } - dir2, err := ioutil.TempDir("", "test-ensure-removeall-with-dir2") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(dir2) - - bindDir := filepath.Join(dir1, "bind") - if err := os.MkdirAll(bindDir, 0755); err != nil { - t.Fatal(err) - } - - if err := mount.Mount(dir2, bindDir, "none", "bind"); err != nil { - t.Fatal(err) - } - - done := make(chan struct{}, 1) - go func() { - err = EnsureRemoveAll(dir1) - close(done) - }() - - select { - case <-done: - if err != nil { - t.Fatal(err) - } - case <-time.After(5 * time.Second): - t.Fatal("timeout waiting for EnsureRemoveAll to finish") - } - - if _, err := os.Stat(dir1); !os.IsNotExist(err) { - t.Fatalf("expected %q to not exist", dir1) - } -}