github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+incompatible/plugin/backend_linux_test.go (about) 1 package plugin // import "github.com/docker/docker/plugin" 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "testing" 8 ) 9 10 func TestAtomicRemoveAllNormal(t *testing.T) { 11 dir, err := ioutil.TempDir("", "atomic-remove-with-normal") 12 if err != nil { 13 t.Fatal(err) 14 } 15 defer os.RemoveAll(dir) // just try to make sure this gets cleaned up 16 17 if err := atomicRemoveAll(dir); err != nil { 18 t.Fatal(err) 19 } 20 21 if _, err := os.Stat(dir); !os.IsNotExist(err) { 22 t.Fatalf("dir should be gone: %v", err) 23 } 24 if _, err := os.Stat(dir + "-removing"); !os.IsNotExist(err) { 25 t.Fatalf("dir should be gone: %v", err) 26 } 27 } 28 29 func TestAtomicRemoveAllAlreadyExists(t *testing.T) { 30 dir, err := ioutil.TempDir("", "atomic-remove-already-exists") 31 if err != nil { 32 t.Fatal(err) 33 } 34 defer os.RemoveAll(dir) // just try to make sure this gets cleaned up 35 36 if err := os.MkdirAll(dir+"-removing", 0755); err != nil { 37 t.Fatal(err) 38 } 39 defer os.RemoveAll(dir + "-removing") 40 41 if err := atomicRemoveAll(dir); err != nil { 42 t.Fatal(err) 43 } 44 45 if _, err := os.Stat(dir); !os.IsNotExist(err) { 46 t.Fatalf("dir should be gone: %v", err) 47 } 48 if _, err := os.Stat(dir + "-removing"); !os.IsNotExist(err) { 49 t.Fatalf("dir should be gone: %v", err) 50 } 51 } 52 53 func TestAtomicRemoveAllNotExist(t *testing.T) { 54 if err := atomicRemoveAll("/not-exist"); err != nil { 55 t.Fatal(err) 56 } 57 58 dir, err := ioutil.TempDir("", "atomic-remove-already-exists") 59 if err != nil { 60 t.Fatal(err) 61 } 62 defer os.RemoveAll(dir) // just try to make sure this gets cleaned up 63 64 // create the removing dir, but not the "real" one 65 foo := filepath.Join(dir, "foo") 66 removing := dir + "-removing" 67 if err := os.MkdirAll(removing, 0755); err != nil { 68 t.Fatal(err) 69 } 70 71 if err := atomicRemoveAll(dir); err != nil { 72 t.Fatal(err) 73 } 74 75 if _, err := os.Stat(foo); !os.IsNotExist(err) { 76 t.Fatalf("dir should be gone: %v", err) 77 } 78 if _, err := os.Stat(removing); !os.IsNotExist(err) { 79 t.Fatalf("dir should be gone: %v", err) 80 } 81 }