github.com/avahowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/modules/renter/hostdb/hostdb_test.go (about) 1 package hostdb 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/persist" 12 ) 13 14 // bareHostDB returns a HostDB with its fields initialized, but without any 15 // dependencies or scanning threads. It is only intended for use in unit tests. 16 func bareHostDB() *HostDB { 17 return &HostDB{ 18 log: persist.NewLogger(ioutil.Discard), 19 20 activeHosts: make(map[modules.NetAddress]*hostNode), 21 allHosts: make(map[modules.NetAddress]*hostEntry), 22 scanPool: make(chan *hostEntry, scanPoolSize), 23 } 24 } 25 26 // newStub is used to test the New function. It implements all of the hostdb's 27 // dependencies. 28 type newStub struct{} 29 30 // consensus set stubs 31 func (newStub) ConsensusSetSubscribe(modules.ConsensusSetSubscriber, modules.ConsensusChangeID) error { 32 return nil 33 } 34 35 // TestNew tests the New function. 36 func TestNew(t *testing.T) { 37 // Using a stub implementation of the dependencies is fine, as long as its 38 // non-nil. 39 var stub newStub 40 dir := build.TempDir("hostdb", "TestNew") 41 42 // Sane values. 43 _, err := New(stub, dir) 44 if err != nil { 45 t.Fatalf("expected nil, got %v", err) 46 } 47 48 // Nil consensus set. 49 _, err = New(nil, dir) 50 if err != errNilCS { 51 t.Fatalf("expected %v, got %v", errNilCS, err) 52 } 53 54 // Bad persistDir. 55 _, err = New(stub, "") 56 if !os.IsNotExist(err) { 57 t.Fatalf("expected invalid directory, got %v", err) 58 } 59 60 // Corrupted logfile. 61 os.RemoveAll(filepath.Join(dir, "hostdb.log")) 62 f, err := os.OpenFile(filepath.Join(dir, "hostdb.log"), os.O_CREATE, 0000) 63 if err != nil { 64 t.Fatal(err) 65 } 66 defer f.Close() 67 _, err = New(stub, dir) 68 if !os.IsPermission(err) { 69 t.Fatalf("expected permissions error, got %v", err) 70 } 71 }