github.com/anuvu/storage@v1.12.13/pkg/mount/mount_unix_test.go (about)

     1  // +build !windows,!solaris
     2  
     3  package mount
     4  
     5  import (
     6  	"os"
     7  	"path"
     8  	"testing"
     9  )
    10  
    11  func TestMountOptionsParsing(t *testing.T) {
    12  	options := "noatime,ro,size=10k"
    13  
    14  	flag, data := ParseOptions(options)
    15  
    16  	if data != "size=10k" {
    17  		t.Fatalf("Expected size=10 got %s", data)
    18  	}
    19  
    20  	expectedFlag := NOATIME | RDONLY
    21  
    22  	if flag != expectedFlag {
    23  		t.Fatalf("Expected %d got %d", expectedFlag, flag)
    24  	}
    25  }
    26  
    27  func TestMounted(t *testing.T) {
    28  	tmp := path.Join(os.TempDir(), "mount-tests")
    29  	if err := os.MkdirAll(tmp, 0777); err != nil {
    30  		t.Fatal(err)
    31  	}
    32  	defer os.RemoveAll(tmp)
    33  
    34  	var (
    35  		sourceDir  = path.Join(tmp, "source")
    36  		targetDir  = path.Join(tmp, "target")
    37  		sourcePath = path.Join(sourceDir, "file.txt")
    38  		targetPath = path.Join(targetDir, "file.txt")
    39  	)
    40  
    41  	os.Mkdir(sourceDir, 0777)
    42  	os.Mkdir(targetDir, 0777)
    43  
    44  	f, err := os.Create(sourcePath)
    45  	if err != nil {
    46  		t.Fatal(err)
    47  	}
    48  	f.WriteString("hello")
    49  	f.Close()
    50  
    51  	f, err = os.Create(targetPath)
    52  	if err != nil {
    53  		t.Fatal(err)
    54  	}
    55  	f.Close()
    56  
    57  	if err := Mount(sourceDir, targetDir, "none", "bind,rw"); err != nil {
    58  		t.Fatal(err)
    59  	}
    60  	defer func() {
    61  		if err := Unmount(targetDir); err != nil {
    62  			t.Fatal(err)
    63  		}
    64  	}()
    65  
    66  	mounted, err := Mounted(targetDir)
    67  	if err != nil {
    68  		t.Fatal(err)
    69  	}
    70  	if !mounted {
    71  		t.Fatalf("Expected %s to be mounted", targetDir)
    72  	}
    73  	if _, err := os.Stat(targetDir); err != nil {
    74  		t.Fatal(err)
    75  	}
    76  }
    77  
    78  func TestMountReadonly(t *testing.T) {
    79  	tmp := path.Join(os.TempDir(), "mount-tests")
    80  	if err := os.MkdirAll(tmp, 0777); err != nil {
    81  		t.Fatal(err)
    82  	}
    83  	defer os.RemoveAll(tmp)
    84  
    85  	var (
    86  		sourceDir  = path.Join(tmp, "source")
    87  		targetDir  = path.Join(tmp, "target")
    88  		sourcePath = path.Join(sourceDir, "file.txt")
    89  		targetPath = path.Join(targetDir, "file.txt")
    90  	)
    91  
    92  	os.Mkdir(sourceDir, 0777)
    93  	os.Mkdir(targetDir, 0777)
    94  
    95  	f, err := os.Create(sourcePath)
    96  	if err != nil {
    97  		t.Fatal(err)
    98  	}
    99  	f.WriteString("hello")
   100  	f.Close()
   101  
   102  	f, err = os.Create(targetPath)
   103  	if err != nil {
   104  		t.Fatal(err)
   105  	}
   106  	f.Close()
   107  
   108  	if err := Mount(sourceDir, targetDir, "none", "bind,ro"); err != nil {
   109  		t.Fatal(err)
   110  	}
   111  	defer func() {
   112  		if err := Unmount(targetDir); err != nil {
   113  			t.Fatal(err)
   114  		}
   115  	}()
   116  
   117  	f, err = os.OpenFile(targetPath, os.O_RDWR, 0777)
   118  	if err == nil {
   119  		t.Fatal("Should not be able to open a ro file as rw")
   120  	}
   121  }
   122  
   123  func TestGetMounts(t *testing.T) {
   124  	mounts, err := GetMounts()
   125  	if err != nil {
   126  		t.Fatal(err)
   127  	}
   128  
   129  	root := false
   130  	for _, entry := range mounts {
   131  		if entry.Mountpoint == "/" {
   132  			root = true
   133  		}
   134  	}
   135  
   136  	if !root {
   137  		t.Fatal("/ should be mounted at least")
   138  	}
   139  }
   140  
   141  func TestMergeTmpfsOptions(t *testing.T) {
   142  	options := []string{"noatime", "ro", "size=10k", "defaults", "atime", "defaults", "rw", "rprivate", "size=1024k", "slave"}
   143  	expected := []string{"atime", "rw", "size=1024k", "slave"}
   144  	merged, err := MergeTmpfsOptions(options)
   145  	if err != nil {
   146  		t.Fatal(err)
   147  	}
   148  	if len(expected) != len(merged) {
   149  		t.Fatalf("Expected %s got %s", expected, merged)
   150  	}
   151  	for index := range merged {
   152  		if merged[index] != expected[index] {
   153  			t.Fatalf("Expected %s for the %dth option, got %s", expected, index, merged)
   154  		}
   155  	}
   156  
   157  	options = []string{"noatime", "ro", "size=10k", "atime", "rw", "rprivate", "size=1024k", "slave", "size"}
   158  	_, err = MergeTmpfsOptions(options)
   159  	if err == nil {
   160  		t.Fatal("Expected error got nil")
   161  	}
   162  }