github.com/avahowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/modules/host/storagemanager/storagemanager_test.go (about) 1 package storagemanager 2 3 import ( 4 "os" 5 "path/filepath" 6 7 "github.com/NebulousLabs/Sia/build" 8 "github.com/NebulousLabs/Sia/crypto" 9 "github.com/NebulousLabs/Sia/modules" 10 "github.com/NebulousLabs/Sia/persist" 11 ) 12 13 // storageManagerTester holds a testing-initialized storage manager and any 14 // additional fields that may be useful while testing. 15 type storageManagerTester struct { 16 sm *StorageManager 17 18 persistDir string 19 } 20 21 // addRandFolder connects a storage folder to a random directory in the 22 // tester's persist dir. 23 func (smt *storageManagerTester) addRandFolder(size uint64) error { 24 dir := filepath.Join(smt.persistDir, persist.RandomSuffix()) 25 err := os.Mkdir(dir, 0700) 26 if err != nil { 27 return err 28 } 29 return smt.sm.AddStorageFolder(dir, size) 30 } 31 32 // capacity returns the amount of storage still available on the machine. The 33 // amount can be negative if the total capacity was reduced to below the active 34 // capacity. 35 func (sm *StorageManager) capacity() (total, remaining uint64) { 36 // Total storage can be computed by summing the size of all the storage 37 // folders. 38 for _, sf := range sm.storageFolders { 39 total += sf.Size 40 remaining += sf.SizeRemaining 41 } 42 return total, remaining 43 } 44 45 // createSector makes a random, unique sector that can be inserted into the 46 // storage manager. 47 func createSector() (sectorRoot crypto.Hash, sectorData []byte, err error) { 48 sectorData, err = crypto.RandBytes(int(modules.SectorSize)) 49 if err != nil { 50 return crypto.Hash{}, nil, err 51 } 52 sectorRoot = crypto.MerkleRoot(sectorData) 53 return sectorRoot, sectorData, nil 54 } 55 56 // newStorageManagerTester creates a storage tester ready for use. 57 func newStorageManagerTester(name string) (*storageManagerTester, error) { 58 testdir := build.TempDir(modules.StorageManagerDir, name) 59 sm, err := New(filepath.Join(testdir, modules.StorageManagerDir)) 60 if err != nil { 61 return nil, err 62 } 63 smt := &storageManagerTester{ 64 sm: sm, 65 66 persistDir: testdir, 67 } 68 return smt, nil 69 } 70 71 // probabilisticReset will probabilistically reboot the storage manager before 72 // continuing. This helps to verify that the persistence is working correctly. 73 // The reset is probabilistic to make sure that the test is not passing because 74 // of the reset. 75 func (smt *storageManagerTester) probabilisticReset() error { 76 rand, err := crypto.RandIntn(3) 77 if err != nil { 78 return err 79 } 80 if rand == 1 { 81 // Grab the potentially faulty dependencies and replace them with good 82 // dependencies so that closing happens without issues. 83 deps := smt.sm.dependencies 84 smt.sm.dependencies = productionDependencies{} 85 // Close the storage manager, then create a new storage manager to 86 // replace it. 87 err = smt.sm.Close() 88 if err != nil { 89 return err 90 } 91 // Open the storage manager with production dependencies so that there 92 // are no errors. 93 sm, err := New(filepath.Join(smt.persistDir, modules.StorageManagerDir)) 94 if err != nil { 95 return err 96 } 97 sm.dependencies = deps 98 smt.sm = sm 99 } 100 return nil 101 } 102 103 // Close shuts down all of the components of the storage manager tester. 104 func (smt *storageManagerTester) Close() error { 105 return smt.sm.Close() 106 }