2014-01-20 18:52:26 +00:00
|
|
|
package mount
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"syscall"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMountOptionsParsing(t *testing.T) {
|
|
|
|
options := "bind,ro,size=10k"
|
|
|
|
|
|
|
|
flag, data := parseOptions(options)
|
|
|
|
|
|
|
|
if data != "size=10k" {
|
|
|
|
t.Fatalf("Expected size=10 got %s", data)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedFlag := syscall.MS_BIND | syscall.MS_RDONLY
|
|
|
|
|
|
|
|
if flag != expectedFlag {
|
|
|
|
t.Fatalf("Expected %d got %d", expectedFlag, flag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMounted(t *testing.T) {
|
|
|
|
tmp := path.Join(os.TempDir(), "mount-tests")
|
|
|
|
if err := os.MkdirAll(tmp, 0777); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
|
|
|
var (
|
2014-03-22 03:03:09 +00:00
|
|
|
sourceDir = path.Join(tmp, "source")
|
|
|
|
targetDir = path.Join(tmp, "target")
|
|
|
|
sourcePath = path.Join(sourceDir, "file.txt")
|
|
|
|
targetPath = path.Join(targetDir, "file.txt")
|
2014-01-20 18:52:26 +00:00
|
|
|
)
|
|
|
|
|
2014-03-22 03:03:09 +00:00
|
|
|
os.Mkdir(sourceDir, 0777)
|
|
|
|
os.Mkdir(targetDir, 0777)
|
|
|
|
|
2014-01-20 18:52:26 +00:00
|
|
|
f, err := os.Create(sourcePath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
f.WriteString("hello")
|
|
|
|
f.Close()
|
|
|
|
|
|
|
|
f, err = os.Create(targetPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
|
2014-03-22 03:03:09 +00:00
|
|
|
if err := Mount(sourceDir, targetDir, "none", "bind,rw"); err != nil {
|
2014-01-20 18:52:26 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
2014-03-22 03:03:09 +00:00
|
|
|
if err := Unmount(targetDir); err != nil {
|
2014-01-20 18:52:26 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2014-03-22 03:03:09 +00:00
|
|
|
mounted, err := Mounted(targetDir)
|
2014-01-20 18:52:26 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !mounted {
|
2014-03-22 03:03:09 +00:00
|
|
|
t.Fatalf("Expected %s to be mounted", targetDir)
|
2014-01-20 18:52:26 +00:00
|
|
|
}
|
2014-03-22 03:03:09 +00:00
|
|
|
if _, err := os.Stat(targetDir); err != nil {
|
2014-01-20 18:52:26 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMountReadonly(t *testing.T) {
|
|
|
|
tmp := path.Join(os.TempDir(), "mount-tests")
|
|
|
|
if err := os.MkdirAll(tmp, 0777); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
|
|
|
var (
|
2014-03-22 03:03:09 +00:00
|
|
|
sourceDir = path.Join(tmp, "source")
|
|
|
|
targetDir = path.Join(tmp, "target")
|
|
|
|
sourcePath = path.Join(sourceDir, "file.txt")
|
|
|
|
targetPath = path.Join(targetDir, "file.txt")
|
2014-01-20 18:52:26 +00:00
|
|
|
)
|
|
|
|
|
2014-03-22 03:03:09 +00:00
|
|
|
os.Mkdir(sourceDir, 0777)
|
|
|
|
os.Mkdir(targetDir, 0777)
|
|
|
|
|
2014-01-20 18:52:26 +00:00
|
|
|
f, err := os.Create(sourcePath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
f.WriteString("hello")
|
|
|
|
f.Close()
|
|
|
|
|
|
|
|
f, err = os.Create(targetPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
|
2014-03-22 03:03:09 +00:00
|
|
|
if err := Mount(sourceDir, targetDir, "none", "bind,ro"); err != nil {
|
2014-01-20 18:52:26 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := Unmount(targetPath); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
f, err = os.OpenFile(targetPath, os.O_RDWR, 0777)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Should not be able to open a ro file as rw")
|
|
|
|
}
|
|
|
|
}
|
2014-03-22 03:02:30 +00:00
|
|
|
|
|
|
|
func TestGetMounts(t *testing.T) {
|
|
|
|
mounts, err := GetMounts()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
root := false
|
|
|
|
for _, entry := range mounts {
|
|
|
|
if entry.Mountpoint == "/" {
|
|
|
|
root = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !root {
|
|
|
|
t.Fatal("/ should be mounted at least")
|
|
|
|
}
|
|
|
|
}
|