2020-11-10 11:43:44 -05:00
|
|
|
// +build !darwin,!windows
|
|
|
|
|
2018-02-05 16:05:59 -05:00
|
|
|
package system // import "github.com/docker/docker/pkg/system"
|
2017-02-14 13:35:20 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
2020-11-10 11:43:44 -05:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-02-14 13:35:20 -05:00
|
|
|
"testing"
|
2020-11-10 11:43:44 -05:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/moby/sys/mount"
|
|
|
|
"gotest.tools/v3/skip"
|
2017-02-14 13:35:20 -05:00
|
|
|
)
|
|
|
|
|
2020-11-10 11:43:44 -05:00
|
|
|
func TestEnsureRemoveAllWithMount(t *testing.T) {
|
|
|
|
skip.If(t, os.Getuid() != 0, "skipping test that requires root")
|
2017-02-14 13:35:20 -05:00
|
|
|
|
2020-11-10 11:43:44 -05:00
|
|
|
dir1, err := ioutil.TempDir("", "test-ensure-removeall-with-dir1")
|
2017-02-14 13:35:20 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-11-10 11:43:44 -05:00
|
|
|
dir2, err := ioutil.TempDir("", "test-ensure-removeall-with-dir2")
|
|
|
|
if err != nil {
|
2017-02-14 13:35:20 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-11-10 11:43:44 -05:00
|
|
|
defer os.RemoveAll(dir2)
|
2017-02-14 13:35:20 -05:00
|
|
|
|
2020-11-10 11:43:44 -05:00
|
|
|
bindDir := filepath.Join(dir1, "bind")
|
|
|
|
if err := os.MkdirAll(bindDir, 0755); err != nil {
|
2017-02-14 13:35:20 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-11-10 11:43:44 -05:00
|
|
|
|
|
|
|
if err := mount.Mount(dir2, bindDir, "none", "bind"); err != nil {
|
2017-02-14 13:35:20 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-11-10 11:43:44 -05:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2017-02-14 13:35:20 -05:00
|
|
|
}
|