rm_unix_test.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // +build !windows
  2. package system // import "github.com/docker/docker/pkg/system"
  3. import (
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "runtime"
  8. "testing"
  9. "time"
  10. "github.com/moby/sys/mount"
  11. "gotest.tools/v3/skip"
  12. )
  13. func TestEnsureRemoveAllWithMount(t *testing.T) {
  14. skip.If(t, runtime.GOOS == "windows", "mount not supported on Windows")
  15. skip.If(t, os.Getuid() != 0, "skipping test that requires root")
  16. dir1, err := ioutil.TempDir("", "test-ensure-removeall-with-dir1")
  17. if err != nil {
  18. t.Fatal(err)
  19. }
  20. dir2, err := ioutil.TempDir("", "test-ensure-removeall-with-dir2")
  21. if err != nil {
  22. t.Fatal(err)
  23. }
  24. defer os.RemoveAll(dir2)
  25. bindDir := filepath.Join(dir1, "bind")
  26. if err := os.MkdirAll(bindDir, 0755); err != nil {
  27. t.Fatal(err)
  28. }
  29. if err := mount.Mount(dir2, bindDir, "none", "bind"); err != nil {
  30. t.Fatal(err)
  31. }
  32. done := make(chan struct{}, 1)
  33. go func() {
  34. err = EnsureRemoveAll(dir1)
  35. close(done)
  36. }()
  37. select {
  38. case <-done:
  39. if err != nil {
  40. t.Fatal(err)
  41. }
  42. case <-time.After(5 * time.Second):
  43. t.Fatal("timeout waiting for EnsureRemoveAll to finish")
  44. }
  45. if _, err := os.Stat(dir1); !os.IsNotExist(err) {
  46. t.Fatalf("expected %q to not exist", dir1)
  47. }
  48. }