gitlab.com/jokerrs1/Sia@v1.3.2/modules/renter/contractor/persist_test.go (about) 1 package contractor 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "testing" 8 9 "github.com/NebulousLabs/Sia/build" 10 "github.com/NebulousLabs/Sia/modules" 11 "github.com/NebulousLabs/Sia/modules/renter/proto" 12 "github.com/NebulousLabs/Sia/types" 13 ) 14 15 // memPersist implements the persister interface in-memory. 16 type memPersist contractorPersist 17 18 func (m *memPersist) save(data contractorPersist) error { *m = memPersist(data); return nil } 19 func (m memPersist) load(data *contractorPersist) error { *data = contractorPersist(m); return nil } 20 21 // TestSaveLoad tests that the contractor can save and load itself. 22 func TestSaveLoad(t *testing.T) { 23 // create contractor with mocked persist dependency 24 c := &Contractor{ 25 persist: new(memPersist), 26 } 27 28 c.renewedIDs = map[types.FileContractID]types.FileContractID{ 29 {0}: {1}, 30 {1}: {2}, 31 {2}: {3}, 32 } 33 c.oldContracts = map[types.FileContractID]modules.RenterContract{ 34 {0}: {ID: types.FileContractID{0}, HostPublicKey: types.SiaPublicKey{Key: []byte("foo")}}, 35 {1}: {ID: types.FileContractID{1}, HostPublicKey: types.SiaPublicKey{Key: []byte("bar")}}, 36 {2}: {ID: types.FileContractID{2}, HostPublicKey: types.SiaPublicKey{Key: []byte("baz")}}, 37 } 38 39 // save, clear, and reload 40 err := c.save() 41 if err != nil { 42 t.Fatal(err) 43 } 44 c.hdb = stubHostDB{} 45 c.renewedIDs = make(map[types.FileContractID]types.FileContractID) 46 c.oldContracts = make(map[types.FileContractID]modules.RenterContract) 47 err = c.load() 48 if err != nil { 49 t.Fatal(err) 50 } 51 // check that all fields were restored 52 _, ok0 := c.renewedIDs[types.FileContractID{0}] 53 _, ok1 := c.renewedIDs[types.FileContractID{1}] 54 _, ok2 := c.renewedIDs[types.FileContractID{2}] 55 if !ok0 || !ok1 || !ok2 { 56 t.Fatal("renewed IDs were not restored properly:", c.renewedIDs) 57 } 58 _, ok0 = c.oldContracts[types.FileContractID{0}] 59 _, ok1 = c.oldContracts[types.FileContractID{1}] 60 _, ok2 = c.oldContracts[types.FileContractID{2}] 61 if !ok0 || !ok1 || !ok2 { 62 t.Fatal("oldContracts were not restored properly:", c.oldContracts) 63 } 64 65 // use stdPersist instead of mock 66 c.persist = newPersist(build.TempDir("contractor", t.Name())) 67 os.MkdirAll(build.TempDir("contractor", t.Name()), 0700) 68 69 // save, clear, and reload 70 err = c.save() 71 if err != nil { 72 t.Fatal(err) 73 } 74 c.renewedIDs = make(map[types.FileContractID]types.FileContractID) 75 c.oldContracts = make(map[types.FileContractID]modules.RenterContract) 76 err = c.load() 77 if err != nil { 78 t.Fatal(err) 79 } 80 // check that all fields were restored 81 _, ok0 = c.renewedIDs[types.FileContractID{0}] 82 _, ok1 = c.renewedIDs[types.FileContractID{1}] 83 _, ok2 = c.renewedIDs[types.FileContractID{2}] 84 if !ok0 || !ok1 || !ok2 { 85 t.Fatal("renewed IDs were not restored properly:", c.renewedIDs) 86 } 87 _, ok0 = c.oldContracts[types.FileContractID{0}] 88 _, ok1 = c.oldContracts[types.FileContractID{1}] 89 _, ok2 = c.oldContracts[types.FileContractID{2}] 90 if !ok0 || !ok1 || !ok2 { 91 t.Fatal("oldContracts were not restored properly:", c.oldContracts) 92 } 93 } 94 95 // TestConvertPersist tests that contracts previously stored in the 96 // .journal format can be converted to the .contract format. 97 func TestConvertPersist(t *testing.T) { 98 dir := build.TempDir(filepath.Join("contractor", t.Name())) 99 os.MkdirAll(dir, 0700) 100 // copy the test data into the temp folder 101 testdata, err := ioutil.ReadFile(filepath.Join("testdata", "TestConvertPersist.journal")) 102 if err != nil { 103 t.Fatal(err) 104 } 105 err = ioutil.WriteFile(filepath.Join(dir, "contractor.journal"), testdata, 0600) 106 if err != nil { 107 t.Fatal(err) 108 } 109 110 // convert the journal 111 err = convertPersist(dir) 112 if err != nil { 113 t.Fatal(err) 114 } 115 116 // load the persist 117 var p contractorPersist 118 err = newPersist(dir).load(&p) 119 if err != nil { 120 t.Fatal(err) 121 } 122 if !p.Allowance.Funds.Equals64(10) || p.Allowance.Hosts != 7 || p.Allowance.Period != 3 || p.Allowance.RenewWindow != 20 { 123 t.Fatal("recovered allowance was wrong:", p.Allowance) 124 } 125 126 // load the contracts 127 cs, err := proto.NewContractSet(filepath.Join(dir, "contracts")) 128 if err != nil { 129 t.Fatal(err) 130 } 131 if cs.Len() != 1 { 132 t.Fatal("expected 1 contract, got", cs.Len()) 133 } 134 m := cs.ViewAll()[0] 135 if m.ID.String() != "792b5eec683819d78416a9e80cba454ebcb5a52eeac4f17b443d177bd425fc5c" { 136 t.Fatal("recovered contract has wrong ID", m.ID) 137 } 138 }