github.com/alok87/docker@v1.6.0/pkg/mount/mount_test.go (about) 1 package mount 2 3 import ( 4 "os" 5 "path" 6 "testing" 7 ) 8 9 func TestMountOptionsParsing(t *testing.T) { 10 options := "noatime,ro,size=10k" 11 12 flag, data := parseOptions(options) 13 14 if data != "size=10k" { 15 t.Fatalf("Expected size=10 got %s", data) 16 } 17 18 expectedFlag := NOATIME | RDONLY 19 20 if flag != expectedFlag { 21 t.Fatalf("Expected %d got %d", expectedFlag, flag) 22 } 23 } 24 25 func TestMounted(t *testing.T) { 26 tmp := path.Join(os.TempDir(), "mount-tests") 27 if err := os.MkdirAll(tmp, 0777); err != nil { 28 t.Fatal(err) 29 } 30 defer os.RemoveAll(tmp) 31 32 var ( 33 sourceDir = path.Join(tmp, "source") 34 targetDir = path.Join(tmp, "target") 35 sourcePath = path.Join(sourceDir, "file.txt") 36 targetPath = path.Join(targetDir, "file.txt") 37 ) 38 39 os.Mkdir(sourceDir, 0777) 40 os.Mkdir(targetDir, 0777) 41 42 f, err := os.Create(sourcePath) 43 if err != nil { 44 t.Fatal(err) 45 } 46 f.WriteString("hello") 47 f.Close() 48 49 f, err = os.Create(targetPath) 50 if err != nil { 51 t.Fatal(err) 52 } 53 f.Close() 54 55 if err := Mount(sourceDir, targetDir, "none", "bind,rw"); err != nil { 56 t.Fatal(err) 57 } 58 defer func() { 59 if err := Unmount(targetDir); err != nil { 60 t.Fatal(err) 61 } 62 }() 63 64 mounted, err := Mounted(targetDir) 65 if err != nil { 66 t.Fatal(err) 67 } 68 if !mounted { 69 t.Fatalf("Expected %s to be mounted", targetDir) 70 } 71 if _, err := os.Stat(targetDir); err != nil { 72 t.Fatal(err) 73 } 74 } 75 76 func TestMountReadonly(t *testing.T) { 77 tmp := path.Join(os.TempDir(), "mount-tests") 78 if err := os.MkdirAll(tmp, 0777); err != nil { 79 t.Fatal(err) 80 } 81 defer os.RemoveAll(tmp) 82 83 var ( 84 sourceDir = path.Join(tmp, "source") 85 targetDir = path.Join(tmp, "target") 86 sourcePath = path.Join(sourceDir, "file.txt") 87 targetPath = path.Join(targetDir, "file.txt") 88 ) 89 90 os.Mkdir(sourceDir, 0777) 91 os.Mkdir(targetDir, 0777) 92 93 f, err := os.Create(sourcePath) 94 if err != nil { 95 t.Fatal(err) 96 } 97 f.WriteString("hello") 98 f.Close() 99 100 f, err = os.Create(targetPath) 101 if err != nil { 102 t.Fatal(err) 103 } 104 f.Close() 105 106 if err := Mount(sourceDir, targetDir, "none", "bind,ro"); err != nil { 107 t.Fatal(err) 108 } 109 defer func() { 110 if err := Unmount(targetDir); err != nil { 111 t.Fatal(err) 112 } 113 }() 114 115 f, err = os.OpenFile(targetPath, os.O_RDWR, 0777) 116 if err == nil { 117 t.Fatal("Should not be able to open a ro file as rw") 118 } 119 } 120 121 func TestGetMounts(t *testing.T) { 122 mounts, err := GetMounts() 123 if err != nil { 124 t.Fatal(err) 125 } 126 127 root := false 128 for _, entry := range mounts { 129 if entry.Mountpoint == "/" { 130 root = true 131 } 132 } 133 134 if !root { 135 t.Fatal("/ should be mounted at least") 136 } 137 }