2017-11-01 19:37:53 -04:00
|
|
|
// +build !windows
|
2016-02-10 21:09:15 -05:00
|
|
|
|
2013-12-18 19:42:49 -05:00
|
|
|
package mount
|
2013-12-17 21:40:20 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMountOptionsParsing(t *testing.T) {
|
2014-03-20 11:52:12 -04:00
|
|
|
options := "noatime,ro,size=10k"
|
2013-12-17 21:40:20 -05:00
|
|
|
|
|
|
|
flag, data := parseOptions(options)
|
|
|
|
|
|
|
|
if data != "size=10k" {
|
|
|
|
t.Fatalf("Expected size=10 got %s", data)
|
|
|
|
}
|
|
|
|
|
2014-03-20 11:52:12 -04:00
|
|
|
expectedFlag := NOATIME | RDONLY
|
2013-12-17 21:40:20 -05:00
|
|
|
|
|
|
|
if flag != expectedFlag {
|
|
|
|
t.Fatalf("Expected %d got %d", expectedFlag, flag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMounted(t *testing.T) {
|
2013-12-18 19:42:49 -05:00
|
|
|
tmp := path.Join(os.TempDir(), "mount-tests")
|
2013-12-17 21:40:20 -05:00
|
|
|
if err := os.MkdirAll(tmp, 0777); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
|
|
|
var (
|
2014-03-21 23:03:09 -04:00
|
|
|
sourceDir = path.Join(tmp, "source")
|
|
|
|
targetDir = path.Join(tmp, "target")
|
|
|
|
sourcePath = path.Join(sourceDir, "file.txt")
|
|
|
|
targetPath = path.Join(targetDir, "file.txt")
|
2013-12-17 21:40:20 -05:00
|
|
|
)
|
|
|
|
|
2014-03-21 23:03:09 -04:00
|
|
|
os.Mkdir(sourceDir, 0777)
|
|
|
|
os.Mkdir(targetDir, 0777)
|
|
|
|
|
2013-12-17 21:40:20 -05: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-21 23:03:09 -04:00
|
|
|
if err := Mount(sourceDir, targetDir, "none", "bind,rw"); err != nil {
|
2013-12-17 21:40:20 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
2014-03-21 23:03:09 -04:00
|
|
|
if err := Unmount(targetDir); err != nil {
|
2013-12-17 21:40:20 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2014-03-21 23:03:09 -04:00
|
|
|
mounted, err := Mounted(targetDir)
|
2013-12-17 21:40:20 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !mounted {
|
2014-03-21 23:03:09 -04:00
|
|
|
t.Fatalf("Expected %s to be mounted", targetDir)
|
2013-12-17 21:40:20 -05:00
|
|
|
}
|
2014-03-21 23:03:09 -04:00
|
|
|
if _, err := os.Stat(targetDir); err != nil {
|
2014-01-14 17:28:19 -05: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-21 23:03:09 -04: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-14 17:28:19 -05:00
|
|
|
)
|
|
|
|
|
2014-03-21 23:03:09 -04:00
|
|
|
os.Mkdir(sourceDir, 0777)
|
|
|
|
os.Mkdir(targetDir, 0777)
|
|
|
|
|
2014-01-14 17:28:19 -05: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-21 23:03:09 -04:00
|
|
|
if err := Mount(sourceDir, targetDir, "none", "bind,ro"); err != nil {
|
2014-01-14 17:28:19 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
2014-03-20 11:52:12 -04:00
|
|
|
if err := Unmount(targetDir); err != nil {
|
2014-01-14 17:28:19 -05:00
|
|
|
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")
|
|
|
|
}
|
2013-12-17 21:40:20 -05:00
|
|
|
}
|
2014-03-21 23:02:30 -04: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")
|
|
|
|
}
|
|
|
|
}
|
Inconsistent --tmpfs behavior
This fix tries to address the issue raised in #22420. When
`--tmpfs` is specified with `/tmp`, the default value is
`rw,nosuid,nodev,noexec,relatime,size=65536k`. When `--tmpfs`
is specified with `/tmp:rw`, then the value changed to
`rw,nosuid,nodev,noexec,relatime`.
The reason for such an inconsistency is because docker tries
to add `size=65536k` option only when user provides no option.
This fix tries to address this issue by always pre-progating
`size=65536k` along with `rw,nosuid,nodev,noexec,relatime`.
If user provides a different value (e.g., `size=8192k`), it
will override the `size=65536k` anyway since the combined
options will be parsed and merged to remove any duplicates.
Additional test cases have been added to cover the changes
in this fix.
This fix fixes #22420.
Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
2016-04-30 22:42:19 -04:00
|
|
|
|
|
|
|
func TestMergeTmpfsOptions(t *testing.T) {
|
|
|
|
options := []string{"noatime", "ro", "size=10k", "defaults", "atime", "defaults", "rw", "rprivate", "size=1024k", "slave"}
|
|
|
|
expected := []string{"atime", "rw", "size=1024k", "slave"}
|
|
|
|
merged, err := MergeTmpfsOptions(options)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(expected) != len(merged) {
|
|
|
|
t.Fatalf("Expected %s got %s", expected, merged)
|
|
|
|
}
|
|
|
|
for index := range merged {
|
|
|
|
if merged[index] != expected[index] {
|
|
|
|
t.Fatalf("Expected %s for the %dth option, got %s", expected, index, merged)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
options = []string{"noatime", "ro", "size=10k", "atime", "rw", "rprivate", "size=1024k", "slave", "size"}
|
|
|
|
_, err = MergeTmpfsOptions(options)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected error got nil")
|
|
|
|
}
|
|
|
|
}
|