github.com/avahowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/modules/renter/renter_test.go (about)

     1  package renter
     2  
     3  import (
     4  	"path/filepath"
     5  
     6  	"github.com/NebulousLabs/Sia/build"
     7  	"github.com/NebulousLabs/Sia/crypto"
     8  	"github.com/NebulousLabs/Sia/modules"
     9  	"github.com/NebulousLabs/Sia/modules/consensus"
    10  	"github.com/NebulousLabs/Sia/modules/gateway"
    11  	"github.com/NebulousLabs/Sia/modules/miner"
    12  	"github.com/NebulousLabs/Sia/modules/renter/contractor"
    13  	"github.com/NebulousLabs/Sia/modules/transactionpool"
    14  	"github.com/NebulousLabs/Sia/modules/wallet"
    15  	"github.com/NebulousLabs/Sia/types"
    16  )
    17  
    18  // renterTester contains all of the modules that are used while testing the renter.
    19  type renterTester struct {
    20  	cs        modules.ConsensusSet
    21  	gateway   modules.Gateway
    22  	miner     modules.TestMiner
    23  	tpool     modules.TransactionPool
    24  	wallet    modules.Wallet
    25  	walletKey crypto.TwofishKey
    26  
    27  	renter *Renter
    28  }
    29  
    30  // Close shuts down the renter tester.
    31  func (rt *renterTester) Close() error {
    32  	rt.wallet.Lock()
    33  	rt.cs.Close()
    34  	rt.gateway.Close()
    35  	return nil
    36  }
    37  
    38  // newRenterTester creates a ready-to-use renter tester with money in the
    39  // wallet.
    40  func newRenterTester(name string) (*renterTester, error) {
    41  	// Create the modules.
    42  	testdir := build.TempDir("renter", name)
    43  	g, err := gateway.New("localhost:0", filepath.Join(testdir, modules.GatewayDir))
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  	cs, err := consensus.New(g, filepath.Join(testdir, modules.ConsensusDir))
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  	tp, err := transactionpool.New(cs, g, filepath.Join(testdir, modules.TransactionPoolDir))
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  	w, err := wallet.New(cs, tp, filepath.Join(testdir, modules.WalletDir))
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  	key, err := crypto.GenerateTwofishKey()
    60  	if err != nil {
    61  		return nil, err
    62  	}
    63  	_, err = w.Encrypt(key)
    64  	if err != nil {
    65  		return nil, err
    66  	}
    67  	err = w.Unlock(key)
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  	r, err := New(cs, w, tp, filepath.Join(testdir, modules.RenterDir))
    72  	if err != nil {
    73  		return nil, err
    74  	}
    75  	m, err := miner.New(cs, tp, w, filepath.Join(testdir, modules.MinerDir))
    76  	if err != nil {
    77  		return nil, err
    78  	}
    79  
    80  	// Assemble all pieces into a renter tester.
    81  	rt := &renterTester{
    82  		cs:      cs,
    83  		gateway: g,
    84  		miner:   m,
    85  		tpool:   tp,
    86  		wallet:  w,
    87  
    88  		renter: r,
    89  	}
    90  
    91  	// Mine blocks until there is money in the wallet.
    92  	for i := types.BlockHeight(0); i <= types.MaturityDelay; i++ {
    93  		_, err := rt.miner.AddBlock()
    94  		if err != nil {
    95  			return nil, err
    96  		}
    97  	}
    98  	return rt, nil
    99  }
   100  
   101  // newContractorTester creates a renterTester, but with the supplied
   102  // hostContractor.
   103  func newContractorTester(name string, hc hostContractor) (*renterTester, error) {
   104  	// Create the modules.
   105  	testdir := build.TempDir("renter", name)
   106  	g, err := gateway.New("localhost:0", filepath.Join(testdir, modules.GatewayDir))
   107  	if err != nil {
   108  		return nil, err
   109  	}
   110  	cs, err := consensus.New(g, filepath.Join(testdir, modules.ConsensusDir))
   111  	if err != nil {
   112  		return nil, err
   113  	}
   114  	tp, err := transactionpool.New(cs, g, filepath.Join(testdir, modules.TransactionPoolDir))
   115  	if err != nil {
   116  		return nil, err
   117  	}
   118  	w, err := wallet.New(cs, tp, filepath.Join(testdir, modules.WalletDir))
   119  	if err != nil {
   120  		return nil, err
   121  	}
   122  	key, err := crypto.GenerateTwofishKey()
   123  	if err != nil {
   124  		return nil, err
   125  	}
   126  	_, err = w.Encrypt(key)
   127  	if err != nil {
   128  		return nil, err
   129  	}
   130  	err = w.Unlock(key)
   131  	if err != nil {
   132  		return nil, err
   133  	}
   134  	r, err := New(cs, w, tp, filepath.Join(testdir, modules.RenterDir))
   135  	if err != nil {
   136  		return nil, err
   137  	}
   138  	r.hostContractor = hc
   139  	m, err := miner.New(cs, tp, w, filepath.Join(testdir, modules.MinerDir))
   140  	if err != nil {
   141  		return nil, err
   142  	}
   143  
   144  	// Assemble all pieces into a renter tester.
   145  	rt := &renterTester{
   146  		cs:      cs,
   147  		gateway: g,
   148  		miner:   m,
   149  		tpool:   tp,
   150  		wallet:  w,
   151  
   152  		renter: r,
   153  	}
   154  
   155  	// Mine blocks until there is money in the wallet.
   156  	for i := types.BlockHeight(0); i <= types.MaturityDelay; i++ {
   157  		_, err := rt.miner.AddBlock()
   158  		if err != nil {
   159  			return nil, err
   160  		}
   161  	}
   162  	return rt, nil
   163  }
   164  
   165  // stubHostDB is the minimal implementation of the hostDB interface. It can be
   166  // embedded in other mock hostDB types, removing the need to reimplement all
   167  // of the hostDB's methods on every mock.
   168  type stubHostDB struct{}
   169  
   170  func (stubHostDB) ActiveHosts() []modules.HostDBEntry { return nil }
   171  func (stubHostDB) AllHosts() []modules.HostDBEntry    { return nil }
   172  func (stubHostDB) AveragePrice() types.Currency       { return types.Currency{} }
   173  func (stubHostDB) Close() error                       { return nil }
   174  func (stubHostDB) IsOffline(modules.NetAddress) bool  { return true }
   175  
   176  // stubContractor is the minimal implementation of the hostContractor
   177  // interface.
   178  type stubContractor struct{}
   179  
   180  func (stubContractor) SetAllowance(modules.Allowance) error                     { return nil }
   181  func (stubContractor) Allowance() modules.Allowance                             { return modules.Allowance{} }
   182  func (stubContractor) Contracts() []modules.RenterContract                      { return nil }
   183  func (stubContractor) FinancialMetrics() (m modules.RenterFinancialMetrics)     { return }
   184  func (stubContractor) Editor(modules.RenterContract) (contractor.Editor, error) { return nil, nil }
   185  func (stubContractor) Downloader(modules.RenterContract) (contractor.Downloader, error) {
   186  	return nil, nil
   187  }