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