gitlab.com/SiaPrime/SiaPrime@v1.4.1/modules/renter/dirs_test.go (about) 1 package renter 2 3 import ( 4 "fmt" 5 "math" 6 "os" 7 "testing" 8 9 "gitlab.com/SiaPrime/SiaPrime/modules" 10 "gitlab.com/SiaPrime/SiaPrime/modules/renter/siadir" 11 "gitlab.com/SiaPrime/SiaPrime/siatest/dependencies" 12 ) 13 14 // TestRenterCreateDirectories checks that the renter properly created metadata files 15 // for direcotries 16 func TestRenterCreateDirectories(t *testing.T) { 17 if testing.Short() { 18 t.SkipNow() 19 } 20 rt, err := newRenterTesterWithDependency(t.Name(), &dependencies.DependencyDisableRepairAndHealthLoops{}) 21 if err != nil { 22 t.Fatal(err) 23 } 24 defer rt.Close() 25 26 // Test creating directory 27 siaPath, err := modules.NewSiaPath("foo/bar/baz") 28 if err != nil { 29 t.Fatal(err) 30 } 31 err = rt.renter.CreateDir(siaPath) 32 if err != nil { 33 t.Fatal(err) 34 } 35 36 // Confirm that directory metadata files were created in all directories 37 if err := rt.checkDirInitialized(modules.RootSiaPath()); err != nil { 38 t.Fatal(err) 39 } 40 siaPath, err = modules.NewSiaPath("foo") 41 if err != nil { 42 t.Fatal(err) 43 } 44 if err := rt.checkDirInitialized(siaPath); err != nil { 45 t.Fatal(err) 46 } 47 siaPath, err = modules.NewSiaPath("foo/bar") 48 if err != nil { 49 t.Fatal(err) 50 } 51 if err := rt.checkDirInitialized(siaPath); err != nil { 52 t.Fatal(err) 53 } 54 siaPath, err = modules.NewSiaPath("foo/bar/baz") 55 if err != nil { 56 t.Fatal(err) 57 } 58 if err := rt.checkDirInitialized(siaPath); err != nil { 59 t.Fatal(err) 60 } 61 } 62 63 // checkDirInitialized is a helper function that checks that the directory was 64 // initialized correctly and the metadata file exist and contain the correct 65 // information 66 func (rt *renterTester) checkDirInitialized(siaPath modules.SiaPath) error { 67 fullpath := siaPath.SiaDirMetadataSysPath(rt.renter.staticFilesDir) 68 if _, err := os.Stat(fullpath); err != nil { 69 return err 70 } 71 siaDir, err := rt.renter.staticDirSet.Open(siaPath) 72 if err != nil { 73 return fmt.Errorf("unable to load directory %v metadata: %v", siaPath, err) 74 } 75 defer siaDir.Close() 76 77 // Check that metadata is default value 78 metadata := siaDir.Metadata() 79 // Check Aggregate Fields 80 if metadata.AggregateHealth != siadir.DefaultDirHealth { 81 return fmt.Errorf("AggregateHealth not initialized properly: have %v expected %v", metadata.AggregateHealth, siadir.DefaultDirHealth) 82 } 83 if !metadata.AggregateLastHealthCheckTime.IsZero() { 84 return fmt.Errorf("AggregateLastHealthCheckTime should be a zero timestamp: %v", metadata.AggregateLastHealthCheckTime) 85 } 86 if metadata.AggregateModTime.IsZero() { 87 return fmt.Errorf("AggregateModTime not initialized: %v", metadata.AggregateModTime) 88 } 89 if metadata.AggregateStuckHealth != siadir.DefaultDirHealth { 90 return fmt.Errorf("AggregateStuckHealth not initialized properly: have %v expected %v", metadata.AggregateStuckHealth, siadir.DefaultDirHealth) 91 } 92 // Check SiaDir Fields 93 if metadata.Health != siadir.DefaultDirHealth { 94 return fmt.Errorf("Health not initialized properly: have %v expected %v", metadata.Health, siadir.DefaultDirHealth) 95 } 96 if !metadata.LastHealthCheckTime.IsZero() { 97 return fmt.Errorf("LastHealthCheckTime should be a zero timestamp: %v", metadata.LastHealthCheckTime) 98 } 99 if metadata.ModTime.IsZero() { 100 return fmt.Errorf("ModTime not initialized: %v", metadata.ModTime) 101 } 102 if metadata.StuckHealth != siadir.DefaultDirHealth { 103 return fmt.Errorf("StuckHealth not initialized properly: have %v expected %v", metadata.StuckHealth, siadir.DefaultDirHealth) 104 } 105 106 // Check that the SiaPath was initialized properly 107 if siaDir.SiaPath() != siaPath { 108 return fmt.Errorf("Expected siapath to be %v, got %v", siaPath, siaDir.SiaPath()) 109 } 110 return nil 111 } 112 113 // TestDirInfo probes the DirInfo method 114 func TestDirInfo(t *testing.T) { 115 if testing.Short() { 116 t.SkipNow() 117 } 118 rt, err := newRenterTesterWithDependency(t.Name(), &dependencies.DependencyDisableRepairAndHealthLoops{}) 119 if err != nil { 120 t.Fatal(err) 121 } 122 defer rt.Close() 123 124 // Create directory 125 siaPath, err := modules.NewSiaPath("foo/") 126 if err != nil { 127 t.Fatal(err) 128 } 129 err = rt.renter.CreateDir(siaPath) 130 if err != nil { 131 t.Fatal(err) 132 } 133 134 // Check that DirInfo returns the same information as stored in the metadata 135 fooDirInfo, err := rt.renter.staticDirSet.DirInfo(siaPath) 136 if err != nil { 137 t.Fatal(err) 138 } 139 rootDirInfo, err := rt.renter.staticDirSet.DirInfo(modules.RootSiaPath()) 140 if err != nil { 141 t.Fatal(err) 142 } 143 fooEntry, err := rt.renter.staticDirSet.Open(siaPath) 144 if err != nil { 145 t.Fatal(err) 146 } 147 rootEntry, err := rt.renter.staticDirSet.Open(modules.RootSiaPath()) 148 if err != nil { 149 t.Fatal(err) 150 } 151 err = compareDirectoryInfoAndMetadata(fooDirInfo, fooEntry) 152 if err != nil { 153 t.Fatal(err) 154 } 155 err = compareDirectoryInfoAndMetadata(rootDirInfo, rootEntry) 156 if err != nil { 157 t.Fatal(err) 158 } 159 } 160 161 // TestRenterListDirectory verifies that the renter properly lists the contents 162 // of a directory 163 func TestRenterListDirectory(t *testing.T) { 164 if testing.Short() { 165 t.SkipNow() 166 } 167 rt, err := newRenterTesterWithDependency(t.Name(), &dependencies.DependencyDisableRepairAndHealthLoops{}) 168 if err != nil { 169 t.Fatal(err) 170 } 171 defer rt.Close() 172 173 // Create directory 174 siaPath, err := modules.NewSiaPath("foo/") 175 if err != nil { 176 t.Fatal(err) 177 } 178 err = rt.renter.CreateDir(siaPath) 179 if err != nil { 180 t.Fatal(err) 181 } 182 183 // Upload a file 184 _, err = rt.renter.newRenterTestFile() 185 if err != nil { 186 t.Fatal(err) 187 } 188 189 // Confirm that DirList returns 1 FileInfo and 2 DirectoryInfos 190 directories, err := rt.renter.DirList(modules.RootSiaPath()) 191 if err != nil { 192 t.Fatal(err) 193 } 194 files, err := rt.renter.FileList(modules.RootSiaPath(), false, false) 195 if len(directories) != 2 { 196 t.Fatal("Expected 2 DirectoryInfos but got", len(directories)) 197 } 198 if len(files) != 1 { 199 t.Fatal("Expected 1 FileInfos but got", len(files)) 200 } 201 202 // Verify that the directory information matches the on disk information 203 rootDir, err := rt.renter.staticDirSet.Open(modules.RootSiaPath()) 204 if err != nil { 205 t.Fatal(err) 206 } 207 fooDir, err := rt.renter.staticDirSet.Open(siaPath) 208 if err != nil { 209 t.Fatal(err) 210 } 211 if err = compareDirectoryInfoAndMetadata(directories[0], rootDir); err != nil { 212 t.Fatal(err) 213 } 214 if err = compareDirectoryInfoAndMetadata(directories[1], fooDir); err != nil { 215 t.Fatal(err) 216 } 217 } 218 219 // compareDirectoryInfoAndMetadata is a helper that compares the information in 220 // a DirectoryInfo struct and a SiaDirSetEntry struct 221 func compareDirectoryInfoAndMetadata(di modules.DirectoryInfo, siaDir *siadir.SiaDirSetEntry) error { 222 md := siaDir.Metadata() 223 224 // Compare Aggregate Fields 225 if md.AggregateHealth != di.AggregateHealth { 226 return fmt.Errorf("AggregateHealths not equal, %v and %v", md.AggregateHealth, di.AggregateHealth) 227 } 228 if di.AggregateLastHealthCheckTime != md.AggregateLastHealthCheckTime { 229 return fmt.Errorf("AggregateLastHealthCheckTimes not equal %v and %v", di.AggregateLastHealthCheckTime, md.AggregateLastHealthCheckTime) 230 } 231 aggregateMaxHealth := math.Max(md.AggregateHealth, md.AggregateStuckHealth) 232 if di.AggregateMaxHealth != aggregateMaxHealth { 233 return fmt.Errorf("AggregateMaxHealths not equal %v and %v", di.AggregateMaxHealth, aggregateMaxHealth) 234 } 235 aggregateMaxHealthPercentage := siadir.HealthPercentage(aggregateMaxHealth) 236 if di.AggregateMaxHealthPercentage != aggregateMaxHealthPercentage { 237 return fmt.Errorf("AggregateMaxHealthPercentage not equal %v and %v", di.AggregateMaxHealthPercentage, aggregateMaxHealthPercentage) 238 } 239 if md.AggregateMinRedundancy != di.AggregateMinRedundancy { 240 return fmt.Errorf("AggregateMinRedundancy not equal, %v and %v", md.AggregateMinRedundancy, di.AggregateMinRedundancy) 241 } 242 if di.AggregateMostRecentModTime != md.AggregateModTime { 243 return fmt.Errorf("AggregateModTimes not equal %v and %v", di.AggregateMostRecentModTime, md.AggregateModTime) 244 } 245 if md.AggregateNumFiles != di.AggregateNumFiles { 246 return fmt.Errorf("AggregateNumFiles not equal, %v and %v", md.AggregateNumFiles, di.AggregateNumFiles) 247 } 248 if md.AggregateNumStuckChunks != di.AggregateNumStuckChunks { 249 return fmt.Errorf("AggregateNumStuckChunks not equal, %v and %v", md.AggregateNumStuckChunks, di.AggregateNumStuckChunks) 250 } 251 if md.AggregateNumSubDirs != di.AggregateNumSubDirs { 252 return fmt.Errorf("AggregateNumSubDirs not equal, %v and %v", md.AggregateNumSubDirs, di.AggregateNumSubDirs) 253 } 254 if md.AggregateSize != di.AggregateSize { 255 return fmt.Errorf("AggregateSizes not equal, %v and %v", md.AggregateSize, di.AggregateSize) 256 } 257 if md.NumStuckChunks != di.AggregateNumStuckChunks { 258 return fmt.Errorf("NumStuckChunks not equal, %v and %v", md.NumStuckChunks, di.AggregateNumStuckChunks) 259 } 260 // Compare Directory Fields 261 if md.Health != di.Health { 262 return fmt.Errorf("healths not equal, %v and %v", md.Health, di.Health) 263 } 264 if di.LastHealthCheckTime != md.LastHealthCheckTime { 265 return fmt.Errorf("LastHealthCheckTimes not equal %v and %v", di.LastHealthCheckTime, md.LastHealthCheckTime) 266 } 267 maxHealth := math.Max(md.Health, md.StuckHealth) 268 if di.MaxHealth != maxHealth { 269 return fmt.Errorf("MaxHealths not equal %v and %v", di.MaxHealth, maxHealth) 270 } 271 maxHealthPercentage := siadir.HealthPercentage(maxHealth) 272 if di.MaxHealthPercentage != maxHealthPercentage { 273 return fmt.Errorf("MaxHealthPercentage not equal %v and %v", di.MaxHealthPercentage, maxHealthPercentage) 274 } 275 if md.MinRedundancy != di.MinRedundancy { 276 return fmt.Errorf("MinRedundancy not equal, %v and %v", md.MinRedundancy, di.MinRedundancy) 277 } 278 if di.MostRecentModTime != md.ModTime { 279 return fmt.Errorf("ModTimes not equal %v and %v", di.MostRecentModTime, md.ModTime) 280 } 281 if md.NumFiles != di.NumFiles { 282 return fmt.Errorf("NumFiles not equal, %v and %v", md.NumFiles, di.NumFiles) 283 } 284 if md.NumStuckChunks != di.NumStuckChunks { 285 return fmt.Errorf("NumStuckChunks not equal, %v and %v", md.NumStuckChunks, di.NumStuckChunks) 286 } 287 if md.NumSubDirs != di.NumSubDirs { 288 return fmt.Errorf("NumSubDirs not equal, %v and %v", md.NumSubDirs, di.NumSubDirs) 289 } 290 if md.Size != di.Size { 291 return fmt.Errorf("Sizes not equal, %v and %v", md.Size, di.Size) 292 } 293 if md.StuckHealth != di.StuckHealth { 294 return fmt.Errorf("stuck healths not equal, %v and %v", md.StuckHealth, di.StuckHealth) 295 } 296 if !siaDir.SiaPath().Equals(di.SiaPath) { 297 return fmt.Errorf("siapaths not equal, %v and %v", siaDir.SiaPath(), di.SiaPath) 298 } 299 return nil 300 }