github.com/stolowski/snapd@v0.0.0-20210407085831-115137ce5a22/osutil/mountprofile_linux_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2017 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package osutil_test 21 22 import ( 23 "bytes" 24 "io/ioutil" 25 "os" 26 "path/filepath" 27 "strings" 28 29 . "gopkg.in/check.v1" 30 31 "github.com/snapcore/snapd/osutil" 32 "github.com/snapcore/snapd/testutil" 33 ) 34 35 type profileSuite struct{} 36 37 var _ = Suite(&profileSuite{}) 38 39 // Test that loading a profile from inexisting file returns an empty profile. 40 func (s *profileSuite) TestLoadMountProfile1(c *C) { 41 dir := c.MkDir() 42 p, err := osutil.LoadMountProfile(filepath.Join(dir, "missing")) 43 c.Assert(err, IsNil) 44 c.Assert(p.Entries, HasLen, 0) 45 } 46 47 // Test that loading profile from a file works as expected. 48 func (s *profileSuite) TestLoadMountProfile2(c *C) { 49 dir := c.MkDir() 50 fname := filepath.Join(dir, "existing") 51 err := ioutil.WriteFile(fname, []byte("name-1 dir-1 type-1 options-1 1 1 # 1st entry"), 0644) 52 c.Assert(err, IsNil) 53 p, err := osutil.LoadMountProfile(fname) 54 c.Assert(err, IsNil) 55 c.Assert(p.Entries, HasLen, 1) 56 c.Assert(p.Entries, DeepEquals, []osutil.MountEntry{ 57 {Name: "name-1", Dir: "dir-1", Type: "type-1", Options: []string{"options-1"}, DumpFrequency: 1, CheckPassNumber: 1}, 58 }) 59 } 60 61 // Test that loading profile with various comments works as expected. 62 func (s *profileSuite) TestLoadMountProfile3(c *C) { 63 dir := c.MkDir() 64 fname := filepath.Join(dir, "existing") 65 err := ioutil.WriteFile(fname, []byte(` 66 # comment with leading spaces 67 name#-1 dir#-1 type#-1 options#-1 1 1 # inline comment 68 # comment without leading spaces 69 70 71 `), 0644) 72 c.Assert(err, IsNil) 73 p, err := osutil.LoadMountProfile(fname) 74 c.Assert(err, IsNil) 75 c.Assert(p.Entries, HasLen, 1) 76 c.Assert(p.Entries, DeepEquals, []osutil.MountEntry{ 77 {Name: "name#-1", Dir: "dir#-1", Type: "type#-1", Options: []string{"options#-1"}, DumpFrequency: 1, CheckPassNumber: 1}, 78 }) 79 } 80 81 func (s *profileSuite) TestLoadMountProfileText(c *C) { 82 p1, err := osutil.LoadMountProfileText("tmpfs /tmp tmpfs defaults 0 0") 83 c.Assert(err, IsNil) 84 c.Assert(p1.Entries, DeepEquals, []osutil.MountEntry{ 85 {Name: "tmpfs", Dir: "/tmp", Type: "tmpfs", Options: []string{"defaults"}}, 86 }) 87 88 p2, err := osutil.LoadMountProfileText( 89 "tmpfs /tmp tmpfs defaults 0 0\n" + 90 "/tmp /var/tmp none bind 0 0\n") 91 c.Assert(err, IsNil) 92 c.Assert(p2.Entries, DeepEquals, []osutil.MountEntry{ 93 {Name: "tmpfs", Dir: "/tmp", Type: "tmpfs", Options: []string{"defaults"}}, 94 {Name: "/tmp", Dir: "/var/tmp", Type: "none", Options: []string{"bind"}}, 95 }) 96 } 97 98 // Test that saving a profile to a file works correctly. 99 func (s *profileSuite) TestSaveMountProfile1(c *C) { 100 dir := c.MkDir() 101 fname := filepath.Join(dir, "profile") 102 p := &osutil.MountProfile{ 103 Entries: []osutil.MountEntry{ 104 {Name: "name-1", Dir: "dir-1", Type: "type-1", Options: []string{"options-1"}, DumpFrequency: 1, CheckPassNumber: 1}, 105 }, 106 } 107 err := p.Save(fname) 108 c.Assert(err, IsNil) 109 110 stat, err := os.Stat(fname) 111 c.Assert(err, IsNil) 112 c.Assert(stat.Mode().Perm(), Equals, os.FileMode(0644)) 113 114 c.Assert(fname, testutil.FileEquals, "name-1 dir-1 type-1 options-1 1 1\n") 115 } 116 117 // Test that empty fstab is parsed without errors 118 func (s *profileSuite) TestReadMountProfile1(c *C) { 119 p, err := osutil.ReadMountProfile(strings.NewReader("")) 120 c.Assert(err, IsNil) 121 c.Assert(p.Entries, HasLen, 0) 122 } 123 124 // Test that '#'-comments are skipped 125 func (s *profileSuite) TestReadMountProfile2(c *C) { 126 p, err := osutil.ReadMountProfile(strings.NewReader("# comment")) 127 c.Assert(err, IsNil) 128 c.Assert(p.Entries, HasLen, 0) 129 } 130 131 // Test that simple profile can be loaded correctly. 132 func (s *profileSuite) TestReadMountProfile3(c *C) { 133 p, err := osutil.ReadMountProfile(strings.NewReader(` 134 name-1 dir-1 type-1 options-1 1 1 # 1st entry 135 name-2 dir-2 type-2 options-2 2 2 # 2nd entry`)) 136 c.Assert(err, IsNil) 137 c.Assert(p.Entries, HasLen, 2) 138 c.Assert(p.Entries, DeepEquals, []osutil.MountEntry{ 139 {Name: "name-1", Dir: "dir-1", Type: "type-1", Options: []string{"options-1"}, DumpFrequency: 1, CheckPassNumber: 1}, 140 {Name: "name-2", Dir: "dir-2", Type: "type-2", Options: []string{"options-2"}, DumpFrequency: 2, CheckPassNumber: 2}, 141 }) 142 } 143 144 // Test that writing an empty fstab file works correctly. 145 func (s *profileSuite) TestWriteTo1(c *C) { 146 p := &osutil.MountProfile{} 147 var buf bytes.Buffer 148 n, err := p.WriteTo(&buf) 149 c.Assert(err, IsNil) 150 c.Assert(n, Equals, int64(0)) 151 c.Assert(buf.String(), Equals, "") 152 } 153 154 // Test that writing an trivial fstab file works correctly. 155 func (s *profileSuite) TestWriteTo2(c *C) { 156 p := &osutil.MountProfile{ 157 Entries: []osutil.MountEntry{ 158 {Name: "name-1", Dir: "dir-1", Type: "type-1", Options: []string{"options-1"}, DumpFrequency: 1, CheckPassNumber: 1}, 159 {Name: "name-2", Dir: "dir-2", Type: "type-2", Options: []string{"options-2"}, DumpFrequency: 2, CheckPassNumber: 2}, 160 }, 161 } 162 var buf bytes.Buffer 163 n, err := p.WriteTo(&buf) 164 c.Assert(err, IsNil) 165 c.Assert(n, Equals, int64(68)) 166 c.Assert(buf.String(), Equals, ("" + 167 "name-1 dir-1 type-1 options-1 1 1\n" + 168 "name-2 dir-2 type-2 options-2 2 2\n")) 169 }