github.com/johnathanhowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/modules/host/host_errors_test.go (about)

     1  package host
     2  
     3  import (
     4  	"net"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/NebulousLabs/Sia/modules"
    10  	"github.com/NebulousLabs/Sia/persist"
    11  )
    12  
    13  // dependencyErrMkdirAll is a dependency set that returns an error when MkdirAll
    14  // is called.
    15  type dependencyErrMkdirAll struct {
    16  	productionDependencies
    17  }
    18  
    19  func (dependencyErrMkdirAll) mkdirAll(string, os.FileMode) error {
    20  	return mockErrMkdirAll
    21  }
    22  
    23  // TestHostFailedMkdirAll initializes the host using a call to MkdirAll that
    24  // will fail.
    25  func TestHostFailedMkdirAll(t *testing.T) {
    26  	if testing.Short() {
    27  		t.SkipNow()
    28  	}
    29  	t.Parallel()
    30  	ht, err := blankHostTester("TestHostFailedMkdirAll")
    31  	if err != nil {
    32  		t.Fatal(err)
    33  	}
    34  	err = ht.host.Close()
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  	_, err = newHost(dependencyErrMkdirAll{}, ht.cs, ht.tpool, ht.wallet, "localhost:0", filepath.Join(ht.persistDir, modules.HostDir))
    39  	if err != mockErrMkdirAll {
    40  		t.Fatal(err)
    41  	}
    42  }
    43  
    44  // dependencyErrNewLogger is a dependency set that returns an error when
    45  // NewLogger is called.
    46  type dependencyErrNewLogger struct {
    47  	productionDependencies
    48  }
    49  
    50  func (dependencyErrNewLogger) newLogger(string) (*persist.Logger, error) {
    51  	return nil, mockErrNewLogger
    52  }
    53  
    54  // TestHostFailedNewLogger initializes the host using a call to NewLogger that
    55  // will fail.
    56  func TestHostFailedNewLogger(t *testing.T) {
    57  	if testing.Short() {
    58  		t.SkipNow()
    59  	}
    60  	t.Parallel()
    61  	ht, err := blankHostTester("TestHostFailedNewLogger")
    62  	if err != nil {
    63  		t.Fatal(err)
    64  	}
    65  	err = ht.host.Close()
    66  	if err != nil {
    67  		t.Fatal(err)
    68  	}
    69  	_, err = newHost(dependencyErrNewLogger{}, ht.cs, ht.tpool, ht.wallet, "localhost:0", filepath.Join(ht.persistDir, modules.HostDir))
    70  	if err != mockErrNewLogger {
    71  		t.Fatal(err)
    72  	}
    73  }
    74  
    75  // dependencyErrOpenDatabase is a dependency that returns an error when
    76  // OpenDatabase is called.
    77  type dependencyErrOpenDatabase struct {
    78  	productionDependencies
    79  }
    80  
    81  func (dependencyErrOpenDatabase) openDatabase(persist.Metadata, string) (*persist.BoltDatabase, error) {
    82  	return nil, mockErrOpenDatabase
    83  }
    84  
    85  // TestHostFailedOpenDatabase initializes the host using a call to OpenDatabase
    86  // that has been mocked to fail.
    87  func TestHostFailedOpenDatabase(t *testing.T) {
    88  	if testing.Short() {
    89  		t.SkipNow()
    90  	}
    91  	t.Parallel()
    92  	ht, err := blankHostTester("TestHostFailedOpenDatabase")
    93  	if err != nil {
    94  		t.Fatal(err)
    95  	}
    96  	err = ht.host.Close()
    97  	if err != nil {
    98  		t.Fatal(err)
    99  	}
   100  	_, err = newHost(dependencyErrOpenDatabase{}, ht.cs, ht.tpool, ht.wallet, "localhost:0", filepath.Join(ht.persistDir, modules.HostDir))
   101  	if err != mockErrOpenDatabase {
   102  		t.Fatal(err)
   103  	}
   104  }
   105  
   106  // dependencyErrLoadFile is a dependency that returns an error when
   107  // LoadFile is called.
   108  type dependencyErrLoadFile struct {
   109  	productionDependencies
   110  }
   111  
   112  func (dependencyErrLoadFile) loadFile(persist.Metadata, interface{}, string) error {
   113  	return mockErrLoadFile
   114  }
   115  
   116  // TestHostFailedLoadFile initializes the host using a call to LoadFile that
   117  // has been mocked to fail.
   118  func TestHostFailedLoadFile(t *testing.T) {
   119  	if testing.Short() {
   120  		t.SkipNow()
   121  	}
   122  	t.Parallel()
   123  	ht, err := blankHostTester("TestHostFailedLoadFile")
   124  	if err != nil {
   125  		t.Fatal(err)
   126  	}
   127  	err = ht.host.Close()
   128  	if err != nil {
   129  		t.Fatal(err)
   130  	}
   131  	_, err = newHost(dependencyErrLoadFile{}, ht.cs, ht.tpool, ht.wallet, "localhost:0", filepath.Join(ht.persistDir, modules.HostDir))
   132  	if err != mockErrLoadFile {
   133  		t.Fatal(err)
   134  	}
   135  }
   136  
   137  // dependencyErrListen is a dependency that returns an error when Listen is
   138  // called.
   139  type dependencyErrListen struct {
   140  	productionDependencies
   141  }
   142  
   143  func (dependencyErrListen) listen(string, string) (net.Listener, error) {
   144  	return nil, mockErrListen
   145  }
   146  
   147  // TestHostFailedListen initializes the host using a call to Listen that
   148  // has been mocked to fail.
   149  func TestHostFailedListen(t *testing.T) {
   150  	if testing.Short() {
   151  		t.SkipNow()
   152  	}
   153  	t.Parallel()
   154  	ht, err := blankHostTester("TestHostFailedListen")
   155  	if err != nil {
   156  		t.Fatal(err)
   157  	}
   158  	err = ht.host.Close()
   159  	if err != nil {
   160  		t.Fatal(err)
   161  	}
   162  	_, err = newHost(dependencyErrListen{}, ht.cs, ht.tpool, ht.wallet, "localhost:0", filepath.Join(ht.persistDir, modules.HostDir))
   163  	if err != mockErrListen {
   164  		t.Fatal(err)
   165  	}
   166  }