github.com/NebulousLabs/Sia@v1.3.7/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.oldContracts = map[types.FileContractID]modules.RenterContract{ 29 {0}: {ID: types.FileContractID{0}, HostPublicKey: types.SiaPublicKey{Key: []byte("foo")}}, 30 {1}: {ID: types.FileContractID{1}, HostPublicKey: types.SiaPublicKey{Key: []byte("bar")}}, 31 {2}: {ID: types.FileContractID{2}, HostPublicKey: types.SiaPublicKey{Key: []byte("baz")}}, 32 } 33 34 // save, clear, and reload 35 err := c.save() 36 if err != nil { 37 t.Fatal(err) 38 } 39 c.hdb = stubHostDB{} 40 c.oldContracts = make(map[types.FileContractID]modules.RenterContract) 41 err = c.load() 42 if err != nil { 43 t.Fatal(err) 44 } 45 // Check that all fields were restored 46 _, ok0 := c.oldContracts[types.FileContractID{0}] 47 _, ok1 := c.oldContracts[types.FileContractID{1}] 48 _, ok2 := c.oldContracts[types.FileContractID{2}] 49 if !ok0 || !ok1 || !ok2 { 50 t.Fatal("oldContracts were not restored properly:", c.oldContracts) 51 } 52 // use stdPersist instead of mock 53 c.persist = NewPersist(build.TempDir("contractor", t.Name())) 54 os.MkdirAll(build.TempDir("contractor", t.Name()), 0700) 55 56 // save, clear, and reload 57 err = c.save() 58 if err != nil { 59 t.Fatal(err) 60 } 61 c.oldContracts = make(map[types.FileContractID]modules.RenterContract) 62 err = c.load() 63 if err != nil { 64 t.Fatal(err) 65 } 66 // check that all fields were restored 67 _, ok0 = c.oldContracts[types.FileContractID{0}] 68 _, ok1 = c.oldContracts[types.FileContractID{1}] 69 _, ok2 = c.oldContracts[types.FileContractID{2}] 70 if !ok0 || !ok1 || !ok2 { 71 t.Fatal("oldContracts were not restored properly:", c.oldContracts) 72 } 73 } 74 75 // TestConvertPersist tests that contracts previously stored in the 76 // .journal format can be converted to the .contract format. 77 func TestConvertPersist(t *testing.T) { 78 dir := build.TempDir(filepath.Join("contractor", t.Name())) 79 os.MkdirAll(dir, 0700) 80 // copy the test data into the temp folder 81 testdata, err := ioutil.ReadFile(filepath.Join("testdata", "TestConvertPersist.journal")) 82 if err != nil { 83 t.Fatal(err) 84 } 85 err = ioutil.WriteFile(filepath.Join(dir, "contractor.journal"), testdata, 0600) 86 if err != nil { 87 t.Fatal(err) 88 } 89 90 // convert the journal 91 err = convertPersist(dir) 92 if err != nil { 93 t.Fatal(err) 94 } 95 96 // load the persist 97 var p contractorPersist 98 err = NewPersist(dir).load(&p) 99 if err != nil { 100 t.Fatal(err) 101 } 102 if !p.Allowance.Funds.Equals64(10) || p.Allowance.Hosts != 7 || p.Allowance.Period != 3 || p.Allowance.RenewWindow != 20 { 103 t.Fatal("recovered allowance was wrong:", p.Allowance) 104 } 105 106 // load the contracts 107 cs, err := proto.NewContractSet(filepath.Join(dir, "contracts"), modules.ProdDependencies) 108 if err != nil { 109 t.Fatal(err) 110 } 111 if cs.Len() != 1 { 112 t.Fatal("expected 1 contract, got", cs.Len()) 113 } 114 m := cs.ViewAll()[0] 115 if m.ID.String() != "792b5eec683819d78416a9e80cba454ebcb5a52eeac4f17b443d177bd425fc5c" { 116 t.Fatal("recovered contract has wrong ID", m.ID) 117 } 118 }