github.com/scaleoutsean/fusego@v0.0.0-20220224074057-4a6429e46bb8/samples/dynamicfs/dynamic_fs_test.go (about) 1 package dynamicfs_test 2 3 import ( 4 "testing" 5 6 "github.com/scaleoutsean/fusego/fusetesting" 7 "github.com/scaleoutsean/fusego/samples" 8 "github.com/scaleoutsean/fusego/samples/dynamicfs" 9 10 "bytes" 11 "fmt" 12 "io/ioutil" 13 "os" 14 "path" 15 "syscall" 16 "time" 17 18 . "github.com/jacobsa/oglematchers" 19 . "github.com/jacobsa/ogletest" 20 ) 21 22 func TestDynamicFS(t *testing.T) { RunTests(t) } 23 24 type DynamicFSTest struct { 25 samples.SampleTest 26 } 27 28 func init() { 29 RegisterTestSuite(&DynamicFSTest{}) 30 } 31 32 var gCreateTime = time.Date(2017, 5, 4, 14, 53, 10, 0, time.UTC) 33 34 func (t *DynamicFSTest) SetUp(ti *TestInfo) { 35 var err error 36 t.Clock.SetTime(gCreateTime) 37 t.Server, err = dynamicfs.NewDynamicFS(&t.Clock) 38 AssertEq(nil, err) 39 t.SampleTest.SetUp(ti) 40 } 41 42 func (t *DynamicFSTest) ReadDir_Root() { 43 entries, err := fusetesting.ReadDirPicky(t.Dir) 44 AssertEq(nil, err) 45 AssertEq(2, len(entries)) 46 47 var fi os.FileInfo 48 fi = entries[0] 49 ExpectEq("age", fi.Name()) 50 ExpectEq(0, fi.Size()) 51 ExpectEq(0444, fi.Mode()) 52 ExpectFalse(fi.IsDir()) 53 54 fi = entries[1] 55 ExpectEq("weekday", fi.Name()) 56 ExpectEq(0, fi.Size()) 57 ExpectEq(0444, fi.Mode()) 58 ExpectFalse(fi.IsDir()) 59 } 60 61 func (t *DynamicFSTest) ReadDir_NonExistent() { 62 _, err := fusetesting.ReadDirPicky(path.Join(t.Dir, "nosuchfile")) 63 64 AssertNe(nil, err) 65 ExpectThat(err, Error(HasSubstr("no such file"))) 66 } 67 68 func (t *DynamicFSTest) Stat_Age() { 69 fi, err := os.Stat(path.Join(t.Dir, "age")) 70 AssertEq(nil, err) 71 72 ExpectEq("age", fi.Name()) 73 ExpectEq(0, fi.Size()) 74 ExpectEq(0444, fi.Mode()) 75 ExpectFalse(fi.IsDir()) 76 ExpectEq(1, fi.Sys().(*syscall.Stat_t).Nlink) 77 } 78 79 func (t *DynamicFSTest) Stat_Weekday() { 80 fi, err := os.Stat(path.Join(t.Dir, "weekday")) 81 AssertEq(nil, err) 82 83 ExpectEq("weekday", fi.Name()) 84 ExpectEq(0, fi.Size()) 85 ExpectEq(0444, fi.Mode()) 86 ExpectFalse(fi.IsDir()) 87 ExpectEq(1, fi.Sys().(*syscall.Stat_t).Nlink) 88 } 89 90 func (t *DynamicFSTest) Stat_NonExistent() { 91 _, err := os.Stat(path.Join(t.Dir, "nosuchfile")) 92 93 AssertNe(nil, err) 94 ExpectThat(err, Error(HasSubstr("no such file"))) 95 } 96 97 func (t *DynamicFSTest) ReadFile_AgeZero() { 98 t.Clock.SetTime(gCreateTime) 99 slice, err := ioutil.ReadFile(path.Join(t.Dir, "age")) 100 101 AssertEq(nil, err) 102 ExpectEq("This filesystem is 0 seconds old.", string(slice)) 103 } 104 105 func (t *DynamicFSTest) ReadFile_Age1000() { 106 t.Clock.SetTime(gCreateTime.Add(1000 * time.Second)) 107 slice, err := ioutil.ReadFile(path.Join(t.Dir, "age")) 108 109 AssertEq(nil, err) 110 ExpectEq("This filesystem is 1000 seconds old.", string(slice)) 111 } 112 113 func (t *DynamicFSTest) ReadFile_WeekdayNow() { 114 now := t.Clock.Now() 115 // Does simulated clock advance itself by default? 116 // Manually set time to ensure it's frozen. 117 t.Clock.SetTime(now) 118 slice, err := ioutil.ReadFile(path.Join(t.Dir, "weekday")) 119 120 AssertEq(nil, err) 121 ExpectEq(fmt.Sprintf("Today is %s.", now.Weekday().String()), string(slice)) 122 } 123 124 func (t *DynamicFSTest) ReadFile_WeekdayCreateTime() { 125 t.Clock.SetTime(gCreateTime) 126 slice, err := ioutil.ReadFile(path.Join(t.Dir, "weekday")) 127 128 AssertEq(nil, err) 129 ExpectEq(fmt.Sprintf("Today is %s.", gCreateTime.Weekday().String()), string(slice)) 130 } 131 132 func (t *DynamicFSTest) ReadFile_AgeUnchangedForHandle() { 133 t.Clock.SetTime(gCreateTime.Add(100 * time.Second)) 134 var err error 135 var file *os.File 136 file, err = os.Open(path.Join(t.Dir, "age")) 137 AssertEq(nil, err) 138 139 // Ensure that all reads from the same handle return the contents created at 140 // file open time. 141 func(file *os.File) { 142 defer file.Close() 143 144 var expectedContents string 145 var buffer bytes.Buffer 146 var bytesRead int64 147 148 expectedContents = "This filesystem is 100 seconds old." 149 bytesRead, err = buffer.ReadFrom(file) 150 AssertEq(nil, err) 151 ExpectEq(len(expectedContents), bytesRead) 152 ExpectEq(expectedContents, buffer.String()) 153 154 t.Clock.SetTime(gCreateTime.Add(1000 * time.Second)) 155 // Seek back to the beginning of the file. The contents should be unchanged 156 // for the life of the file handle. 157 _, err = file.Seek(0, 0) 158 AssertEq(nil, err) 159 160 buffer.Reset() 161 bytesRead, err = buffer.ReadFrom(file) 162 AssertEq(nil, err) 163 ExpectEq(len(expectedContents), bytesRead) 164 ExpectEq(expectedContents, buffer.String()) 165 }(file) 166 167 // The clock was advanced while the old handle was open. The content change 168 // should be reflected by the new handle. 169 file, err = os.Open(path.Join(t.Dir, "age")) 170 AssertEq(nil, err) 171 func(file *os.File) { 172 defer file.Close() 173 174 expectedContents := "This filesystem is 1000 seconds old." 175 buffer := bytes.Buffer{} 176 bytesRead, err := buffer.ReadFrom(file) 177 AssertEq(nil, err) 178 ExpectEq(len(expectedContents), bytesRead) 179 ExpectEq(expectedContents, buffer.String()) 180 }(file) 181 }