1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
package host
import (
"net"
"os"
"path/filepath"
"strings"
"testing"
"github.com/NebulousLabs/Sia/modules"
"github.com/NebulousLabs/Sia/persist"
)
// dependencyErrMkdirAll is a dependency set that returns an error when MkdirAll
// is called.
type dependencyErrMkdirAll struct {
productionDependencies
}
func (dependencyErrMkdirAll) mkdirAll(string, os.FileMode) error {
return mockErrMkdirAll
}
// TestHostFailedMkdirAll initializes the host using a call to MkdirAll that
// will fail.
func TestHostFailedMkdirAll(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
t.Parallel()
ht, err := blankHostTester("TestHostFailedMkdirAll")
if err != nil {
t.Fatal(err)
}
defer ht.Close()
err = ht.host.Close()
if err != nil {
t.Fatal(err)
}
ht.host, err = newHost(dependencyErrMkdirAll{}, ht.cs, ht.tpool, ht.wallet, "localhost:0", filepath.Join(ht.persistDir, modules.HostDir))
if err != mockErrMkdirAll {
t.Fatal(err)
}
// Set ht.host to something non-nil - nil was returned because startup was
// incomplete. If ht.host is nil at the end of the function, the ht.Close()
// operation will fail.
ht.host, err = newHost(productionDependencies{}, ht.cs, ht.tpool, ht.wallet, "localhost:0", filepath.Join(ht.persistDir, modules.HostDir))
if err != nil {
t.Fatal(err)
}
}
// dependencyErrNewLogger is a dependency set that returns an error when
// NewLogger is called.
type dependencyErrNewLogger struct {
productionDependencies
}
func (dependencyErrNewLogger) newLogger(string) (*persist.Logger, error) {
return nil, mockErrNewLogger
}
// TestHostFailedNewLogger initializes the host using a call to NewLogger that
// will fail.
func TestHostFailedNewLogger(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
t.Parallel()
ht, err := blankHostTester("TestHostFailedNewLogger")
if err != nil {
t.Fatal(err)
}
defer ht.Close()
err = ht.host.Close()
if err != nil {
t.Fatal(err)
}
ht.host, err = newHost(dependencyErrNewLogger{}, ht.cs, ht.tpool, ht.wallet, "localhost:0", filepath.Join(ht.persistDir, modules.HostDir))
if err != mockErrNewLogger {
t.Fatal(err)
}
// Set ht.host to something non-nil - nil was returned because startup was
// incomplete. If ht.host is nil at the end of the function, the ht.Close()
// operation will fail.
ht.host, err = newHost(productionDependencies{}, ht.cs, ht.tpool, ht.wallet, "localhost:0", filepath.Join(ht.persistDir, modules.HostDir))
if err != nil {
t.Fatal(err)
}
}
// dependencyErrOpenDatabase is a dependency that returns an error when
// OpenDatabase is called.
type dependencyErrOpenDatabase struct {
productionDependencies
}
func (dependencyErrOpenDatabase) openDatabase(persist.Metadata, string) (*persist.BoltDatabase, error) {
return nil, mockErrOpenDatabase
}
// TestHostFailedOpenDatabase initializes the host using a call to OpenDatabase
// that has been mocked to fail.
func TestHostFailedOpenDatabase(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
t.Parallel()
ht, err := blankHostTester("TestHostFailedOpenDatabase")
if err != nil {
t.Fatal(err)
}
defer ht.Close()
err = ht.host.Close()
if err != nil {
t.Fatal(err)
}
ht.host, err = newHost(dependencyErrOpenDatabase{}, ht.cs, ht.tpool, ht.wallet, "localhost:0", filepath.Join(ht.persistDir, modules.HostDir))
if !strings.Contains(err.Error(), "simulated OpenDatabase failure") {
t.Fatal(err)
}
// Set ht.host to something non-nil - nil was returned because startup was
// incomplete. If ht.host is nil at the end of the function, the ht.Close()
// operation will fail.
ht.host, err = newHost(productionDependencies{}, ht.cs, ht.tpool, ht.wallet, "localhost:0", filepath.Join(ht.persistDir, modules.HostDir))
if err != nil {
t.Fatal(err)
}
}
// dependencyErrLoadFile is a dependency that returns an error when
// LoadFile is called.
type dependencyErrLoadFile struct {
productionDependencies
}
func (dependencyErrLoadFile) loadFile(persist.Metadata, interface{}, string) error {
return mockErrLoadFile
}
// TestHostFailedLoadFile initializes the host using a call to LoadFile that
// has been mocked to fail.
func TestHostFailedLoadFile(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
t.Parallel()
ht, err := blankHostTester("TestHostFailedLoadFile")
if err != nil {
t.Fatal(err)
}
defer ht.Close()
err = ht.host.Close()
if err != nil {
t.Fatal(err)
}
ht.host, err = newHost(dependencyErrLoadFile{}, ht.cs, ht.tpool, ht.wallet, "localhost:0", filepath.Join(ht.persistDir, modules.HostDir))
if err != mockErrLoadFile {
t.Fatal(err)
}
// Set ht.host to something non-nil - nil was returned because startup was
// incomplete. If ht.host is nil at the end of the function, the ht.Close()
// operation will fail.
ht.host, err = newHost(productionDependencies{}, ht.cs, ht.tpool, ht.wallet, "localhost:0", filepath.Join(ht.persistDir, modules.HostDir))
if err != nil {
t.Fatal(err)
}
}
// dependencyErrListen is a dependency that returns an error when Listen is
// called.
type dependencyErrListen struct {
productionDependencies
}
func (dependencyErrListen) listen(string, string) (net.Listener, error) {
return nil, mockErrListen
}
// TestHostFailedListen initializes the host using a call to Listen that
// has been mocked to fail.
func TestHostFailedListen(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
t.Parallel()
ht, err := blankHostTester("TestHostFailedListen")
if err != nil {
t.Fatal(err)
}
defer ht.Close()
err = ht.host.Close()
if err != nil {
t.Fatal(err)
}
ht.host, err = newHost(dependencyErrListen{}, ht.cs, ht.tpool, ht.wallet, "localhost:0", filepath.Join(ht.persistDir, modules.HostDir))
if err != mockErrListen {
t.Fatal(err)
}
// Set ht.host to something non-nil - nil was returned because startup was
// incomplete. If ht.host is nil at the end of the function, the ht.Close()
// operation will fail.
ht.host, err = newHost(productionDependencies{}, ht.cs, ht.tpool, ht.wallet, "localhost:0", filepath.Join(ht.persistDir, modules.HostDir))
if err != nil {
t.Fatal(err)
}
}
|