github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/pkg/system/rm_test.go (about)

     1  package system
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"runtime"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/docker/docker/pkg/mount"
    12  )
    13  
    14  func TestEnsureRemoveAllNotExist(t *testing.T) {
    15  	// should never return an error for a non-existent path
    16  	if err := EnsureRemoveAll("/non/existent/path"); err != nil {
    17  		t.Fatal(err)
    18  	}
    19  }
    20  
    21  func TestEnsureRemoveAllWithDir(t *testing.T) {
    22  	dir, err := ioutil.TempDir("", "test-ensure-removeall-with-dir")
    23  	if err != nil {
    24  		t.Fatal(err)
    25  	}
    26  	if err := EnsureRemoveAll(dir); err != nil {
    27  		t.Fatal(err)
    28  	}
    29  }
    30  
    31  func TestEnsureRemoveAllWithFile(t *testing.T) {
    32  	tmp, err := ioutil.TempFile("", "test-ensure-removeall-with-dir")
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  	tmp.Close()
    37  	if err := EnsureRemoveAll(tmp.Name()); err != nil {
    38  		t.Fatal(err)
    39  	}
    40  }
    41  
    42  func TestEnsureRemoveAllWithMount(t *testing.T) {
    43  	if runtime.GOOS == "windows" {
    44  		t.Skip("mount not supported on Windows")
    45  	}
    46  
    47  	dir1, err := ioutil.TempDir("", "test-ensure-removeall-with-dir1")
    48  	if err != nil {
    49  		t.Fatal(err)
    50  	}
    51  	dir2, err := ioutil.TempDir("", "test-ensure-removeall-with-dir2")
    52  	if err != nil {
    53  		t.Fatal(err)
    54  	}
    55  	defer os.RemoveAll(dir2)
    56  
    57  	bindDir := filepath.Join(dir1, "bind")
    58  	if err := os.MkdirAll(bindDir, 0755); err != nil {
    59  		t.Fatal(err)
    60  	}
    61  
    62  	if err := mount.Mount(dir2, bindDir, "none", "bind"); err != nil {
    63  		t.Fatal(err)
    64  	}
    65  
    66  	done := make(chan struct{})
    67  	go func() {
    68  		err = EnsureRemoveAll(dir1)
    69  		close(done)
    70  	}()
    71  
    72  	select {
    73  	case <-done:
    74  		if err != nil {
    75  			t.Fatal(err)
    76  		}
    77  	case <-time.After(5 * time.Second):
    78  		t.Fatal("timeout waiting for EnsureRemoveAll to finish")
    79  	}
    80  
    81  	if _, err := os.Stat(dir1); !os.IsNotExist(err) {
    82  		t.Fatalf("expected %q to not exist", dir1)
    83  	}
    84  }