github.com/scaleoutsean/fusego@v0.0.0-20220224074057-4a6429e46bb8/samples/hellofs/hello_fs_test.go (about) 1 // Copyright 2015 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package hellofs_test 16 17 import ( 18 "io" 19 "io/ioutil" 20 "os" 21 "path" 22 "syscall" 23 "testing" 24 25 "github.com/scaleoutsean/fusego/fusetesting" 26 "github.com/scaleoutsean/fusego/samples" 27 "github.com/scaleoutsean/fusego/samples/hellofs" 28 . "github.com/jacobsa/oglematchers" 29 . "github.com/jacobsa/ogletest" 30 ) 31 32 func TestHelloFS(t *testing.T) { RunTests(t) } 33 34 //////////////////////////////////////////////////////////////////////// 35 // Boilerplate 36 //////////////////////////////////////////////////////////////////////// 37 38 type HelloFSTest struct { 39 samples.SampleTest 40 } 41 42 func init() { RegisterTestSuite(&HelloFSTest{}) } 43 44 func (t *HelloFSTest) SetUp(ti *TestInfo) { 45 var err error 46 47 t.Server, err = hellofs.NewHelloFS(&t.Clock) 48 AssertEq(nil, err) 49 50 t.SampleTest.SetUp(ti) 51 } 52 53 //////////////////////////////////////////////////////////////////////// 54 // Test functions 55 //////////////////////////////////////////////////////////////////////// 56 57 func (t *HelloFSTest) ReadDir_Root() { 58 entries, err := fusetesting.ReadDirPicky(t.Dir) 59 60 AssertEq(nil, err) 61 AssertEq(2, len(entries)) 62 var fi os.FileInfo 63 64 // dir 65 fi = entries[0] 66 ExpectEq("dir", fi.Name()) 67 ExpectEq(0, fi.Size()) 68 ExpectEq(os.ModeDir|0555, fi.Mode()) 69 ExpectEq(0, t.Clock.Now().Sub(fi.ModTime()), "ModTime: %v", fi.ModTime()) 70 ExpectTrue(fi.IsDir()) 71 72 // hello 73 fi = entries[1] 74 ExpectEq("hello", fi.Name()) 75 ExpectEq(len("Hello, world!"), fi.Size()) 76 ExpectEq(0444, fi.Mode()) 77 ExpectEq(0, t.Clock.Now().Sub(fi.ModTime()), "ModTime: %v", fi.ModTime()) 78 ExpectFalse(fi.IsDir()) 79 } 80 81 func (t *HelloFSTest) ReadDir_Dir() { 82 entries, err := fusetesting.ReadDirPicky(path.Join(t.Dir, "dir")) 83 84 AssertEq(nil, err) 85 AssertEq(1, len(entries)) 86 var fi os.FileInfo 87 88 // world 89 fi = entries[0] 90 ExpectEq("world", fi.Name()) 91 ExpectEq(len("Hello, world!"), fi.Size()) 92 ExpectEq(0444, fi.Mode()) 93 ExpectEq(0, t.Clock.Now().Sub(fi.ModTime()), "ModTime: %v", fi.ModTime()) 94 ExpectFalse(fi.IsDir()) 95 } 96 97 func (t *HelloFSTest) ReadDir_NonExistent() { 98 _, err := fusetesting.ReadDirPicky(path.Join(t.Dir, "foobar")) 99 100 AssertNe(nil, err) 101 ExpectThat(err, Error(HasSubstr("no such file"))) 102 } 103 104 func (t *HelloFSTest) Stat_Hello() { 105 fi, err := os.Stat(path.Join(t.Dir, "hello")) 106 AssertEq(nil, err) 107 108 ExpectEq("hello", fi.Name()) 109 ExpectEq(len("Hello, world!"), fi.Size()) 110 ExpectEq(0444, fi.Mode()) 111 ExpectEq(0, t.Clock.Now().Sub(fi.ModTime()), "ModTime: %v", fi.ModTime()) 112 ExpectFalse(fi.IsDir()) 113 ExpectEq(1, fi.Sys().(*syscall.Stat_t).Nlink) 114 } 115 116 func (t *HelloFSTest) Stat_Dir() { 117 fi, err := os.Stat(path.Join(t.Dir, "dir")) 118 AssertEq(nil, err) 119 120 ExpectEq("dir", fi.Name()) 121 ExpectEq(0, fi.Size()) 122 ExpectEq(0555|os.ModeDir, fi.Mode()) 123 ExpectEq(0, t.Clock.Now().Sub(fi.ModTime()), "ModTime: %v", fi.ModTime()) 124 ExpectTrue(fi.IsDir()) 125 ExpectEq(1, fi.Sys().(*syscall.Stat_t).Nlink) 126 } 127 128 func (t *HelloFSTest) Stat_World() { 129 fi, err := os.Stat(path.Join(t.Dir, "dir/world")) 130 AssertEq(nil, err) 131 132 ExpectEq("world", fi.Name()) 133 ExpectEq(len("Hello, world!"), fi.Size()) 134 ExpectEq(0444, fi.Mode()) 135 ExpectEq(0, t.Clock.Now().Sub(fi.ModTime()), "ModTime: %v", fi.ModTime()) 136 ExpectFalse(fi.IsDir()) 137 ExpectEq(1, fi.Sys().(*syscall.Stat_t).Nlink) 138 } 139 140 func (t *HelloFSTest) Stat_NonExistent() { 141 _, err := os.Stat(path.Join(t.Dir, "foobar")) 142 143 AssertNe(nil, err) 144 ExpectThat(err, Error(HasSubstr("no such file"))) 145 } 146 147 func (t *HelloFSTest) ReadFile_Hello() { 148 slice, err := ioutil.ReadFile(path.Join(t.Dir, "hello")) 149 150 AssertEq(nil, err) 151 ExpectEq("Hello, world!", string(slice)) 152 } 153 154 func (t *HelloFSTest) ReadFile_Dir() { 155 _, err := ioutil.ReadFile(path.Join(t.Dir, "dir")) 156 157 AssertNe(nil, err) 158 ExpectThat(err, Error(HasSubstr("is a directory"))) 159 } 160 161 func (t *HelloFSTest) ReadFile_World() { 162 slice, err := ioutil.ReadFile(path.Join(t.Dir, "dir/world")) 163 164 AssertEq(nil, err) 165 ExpectEq("Hello, world!", string(slice)) 166 } 167 168 func (t *HelloFSTest) OpenAndRead() { 169 var buf []byte = make([]byte, 1024) 170 var n int 171 var off int64 172 var err error 173 174 // Open the file. 175 f, err := os.Open(path.Join(t.Dir, "hello")) 176 defer func() { 177 if f != nil { 178 ExpectEq(nil, f.Close()) 179 } 180 }() 181 182 AssertEq(nil, err) 183 184 // Seeking shouldn't affect the random access reads below. 185 _, err = f.Seek(7, 0) 186 AssertEq(nil, err) 187 188 // Random access reads 189 n, err = f.ReadAt(buf[:2], 0) 190 AssertEq(nil, err) 191 ExpectEq(2, n) 192 ExpectEq("He", string(buf[:n])) 193 194 n, err = f.ReadAt(buf[:2], int64(len("Hel"))) 195 AssertEq(nil, err) 196 ExpectEq(2, n) 197 ExpectEq("lo", string(buf[:n])) 198 199 n, err = f.ReadAt(buf[:3], int64(len("Hello, wo"))) 200 AssertEq(nil, err) 201 ExpectEq(3, n) 202 ExpectEq("rld", string(buf[:n])) 203 204 // Read beyond end. 205 n, err = f.ReadAt(buf[:3], int64(len("Hello, world"))) 206 AssertEq(io.EOF, err) 207 ExpectEq(1, n) 208 ExpectEq("!", string(buf[:n])) 209 210 // Seek then read the rest. 211 off, err = f.Seek(int64(len("Hel")), 0) 212 AssertEq(nil, err) 213 AssertEq(len("Hel"), off) 214 215 n, err = io.ReadFull(f, buf[:len("lo, world!")]) 216 AssertEq(nil, err) 217 ExpectEq(len("lo, world!"), n) 218 ExpectEq("lo, world!", string(buf[:n])) 219 } 220 221 func (t *HelloFSTest) Open_NonExistent() { 222 _, err := os.Open(path.Join(t.Dir, "foobar")) 223 224 AssertNe(nil, err) 225 ExpectThat(err, Error(HasSubstr("no such file"))) 226 }