Skip to content
Snippets Groups Projects
Commit 4604ccac authored by Josh Brooks's avatar Josh Brooks
Browse files

Update tests in hostpool

parent affb3cb4
No related branches found
No related tags found
No related merge requests found
...@@ -22,7 +22,7 @@ import ( ...@@ -22,7 +22,7 @@ import (
// Unit test // Unit test
func TestNewHostPool(t *testing.T) { func TestNewHostPool(t *testing.T) {
manager := newHappyManager() manager := newMockManager()
rng := csprng.NewSystemRNG() rng := csprng.NewSystemRNG()
testNdf := getTestNdf(t) testNdf := getTestNdf(t)
testStorage := storage.InitTestingSession(t) testStorage := storage.InitTestingSession(t)
...@@ -56,7 +56,7 @@ func TestNewHostPool(t *testing.T) { ...@@ -56,7 +56,7 @@ func TestNewHostPool(t *testing.T) {
// Unit test // Unit test
func TestHostPool_ManageHostPool(t *testing.T) { func TestHostPool_ManageHostPool(t *testing.T) {
manager := newHappyManager() manager := newMockManager()
rng := csprng.NewSystemRNG() rng := csprng.NewSystemRNG()
testNdf := getTestNdf(t) testNdf := getTestNdf(t)
testStorage := storage.InitTestingSession(t) testStorage := storage.InitTestingSession(t)
...@@ -131,7 +131,7 @@ func TestHostPool_ManageHostPool(t *testing.T) { ...@@ -131,7 +131,7 @@ func TestHostPool_ManageHostPool(t *testing.T) {
// Full happy path test // Full happy path test
func TestHostPool_ReplaceHost(t *testing.T) { func TestHostPool_ReplaceHost(t *testing.T) {
manager := newHappyManager() manager := newMockManager()
testNdf := getTestNdf(t) testNdf := getTestNdf(t)
newIndex := uint32(20) newIndex := uint32(20)
...@@ -238,7 +238,7 @@ func TestHostPool_ReplaceHost(t *testing.T) { ...@@ -238,7 +238,7 @@ func TestHostPool_ReplaceHost(t *testing.T) {
// Error path, could not get host // Error path, could not get host
func TestHostPool_ReplaceHost_Error(t *testing.T) { func TestHostPool_ReplaceHost_Error(t *testing.T) {
manager := newHappyManager() manager := newMockManager()
// Construct a manager (bypass business logic in constructor) // Construct a manager (bypass business logic in constructor)
hostPool := &HostPool{ hostPool := &HostPool{
...@@ -259,7 +259,7 @@ func TestHostPool_ReplaceHost_Error(t *testing.T) { ...@@ -259,7 +259,7 @@ func TestHostPool_ReplaceHost_Error(t *testing.T) {
// Happy path // Happy path
func TestHostPool_PruneHostPool(t *testing.T) { func TestHostPool_PruneHostPool(t *testing.T) {
manager := newHappyManager() manager := newMockManager()
testNdf := getTestNdf(t) testNdf := getTestNdf(t)
newIndex := uint32(20) newIndex := uint32(20)
params := DefaultPoolParams() params := DefaultPoolParams()
...@@ -329,7 +329,7 @@ func TestHostPool_PruneHostPool(t *testing.T) { ...@@ -329,7 +329,7 @@ func TestHostPool_PruneHostPool(t *testing.T) {
// Error path: not enough gateways in ndf compared to // Error path: not enough gateways in ndf compared to
// required pool size // required pool size
func TestHostPool_PruneHostPool_Error(t *testing.T) { func TestHostPool_PruneHostPool_Error(t *testing.T) {
manager := newHappyManager() manager := newMockManager()
testNdf := getTestNdf(t) testNdf := getTestNdf(t)
newIndex := uint32(20) newIndex := uint32(20)
params := DefaultPoolParams() params := DefaultPoolParams()
...@@ -359,7 +359,7 @@ func TestHostPool_PruneHostPool_Error(t *testing.T) { ...@@ -359,7 +359,7 @@ func TestHostPool_PruneHostPool_Error(t *testing.T) {
// Unit test // Unit test
func TestHostPool_UpdateNdf(t *testing.T) { func TestHostPool_UpdateNdf(t *testing.T) {
manager := newHappyManager() manager := newMockManager()
testNdf := getTestNdf(t) testNdf := getTestNdf(t)
newIndex := uint32(20) newIndex := uint32(20)
...@@ -394,7 +394,7 @@ func TestHostPool_UpdateNdf(t *testing.T) { ...@@ -394,7 +394,7 @@ func TestHostPool_UpdateNdf(t *testing.T) {
// Full test // Full test
func TestHostPool_GetPreferred(t *testing.T) { func TestHostPool_GetPreferred(t *testing.T) {
manager := newHappyManager() manager := newMockManager()
rng := csprng.NewSystemRNG() rng := csprng.NewSystemRNG()
testNdf := getTestNdf(t) testNdf := getTestNdf(t)
testStorage := storage.InitTestingSession(t) testStorage := storage.InitTestingSession(t)
...@@ -466,7 +466,7 @@ func TestHostPool_GetPreferred(t *testing.T) { ...@@ -466,7 +466,7 @@ func TestHostPool_GetPreferred(t *testing.T) {
// Unit test // Unit test
func TestHostPool_GetAny(t *testing.T) { func TestHostPool_GetAny(t *testing.T) {
manager := newHappyManager() manager := newMockManager()
rng := csprng.NewSystemRNG() rng := csprng.NewSystemRNG()
testNdf := getTestNdf(t) testNdf := getTestNdf(t)
testStorage := storage.InitTestingSession(t) testStorage := storage.InitTestingSession(t)
...@@ -523,7 +523,7 @@ func TestHostPool_GetAny(t *testing.T) { ...@@ -523,7 +523,7 @@ func TestHostPool_GetAny(t *testing.T) {
// Unit test // Unit test
func TestHostPool_ForceAdd(t *testing.T) { func TestHostPool_ForceAdd(t *testing.T) {
manager := newHappyManager() manager := newMockManager()
rng := csprng.NewSystemRNG() rng := csprng.NewSystemRNG()
testNdf := getTestNdf(t) testNdf := getTestNdf(t)
testStorage := storage.InitTestingSession(t) testStorage := storage.InitTestingSession(t)
...@@ -584,7 +584,7 @@ func TestHostPool_ForceAdd(t *testing.T) { ...@@ -584,7 +584,7 @@ func TestHostPool_ForceAdd(t *testing.T) {
// Unit test which only adds information to ndf // Unit test which only adds information to ndf
func TestHostPool_UpdateConns_AddGateways(t *testing.T) { func TestHostPool_UpdateConns_AddGateways(t *testing.T) {
manager := newHappyManager() manager := newMockManager()
rng := csprng.NewSystemRNG() rng := csprng.NewSystemRNG()
testNdf := getTestNdf(t) testNdf := getTestNdf(t)
testStorage := storage.InitTestingSession(t) testStorage := storage.InitTestingSession(t)
...@@ -659,7 +659,7 @@ func TestHostPool_UpdateConns_AddGateways(t *testing.T) { ...@@ -659,7 +659,7 @@ func TestHostPool_UpdateConns_AddGateways(t *testing.T) {
// Unit test which only adds information to ndf // Unit test which only adds information to ndf
func TestHostPool_UpdateConns_RemoveGateways(t *testing.T) { func TestHostPool_UpdateConns_RemoveGateways(t *testing.T) {
manager := newHappyManager() manager := newMockManager()
rng := csprng.NewSystemRNG() rng := csprng.NewSystemRNG()
testNdf := getTestNdf(t) testNdf := getTestNdf(t)
testStorage := storage.InitTestingSession(t) testStorage := storage.InitTestingSession(t)
...@@ -732,7 +732,7 @@ func TestHostPool_UpdateConns_RemoveGateways(t *testing.T) { ...@@ -732,7 +732,7 @@ func TestHostPool_UpdateConns_RemoveGateways(t *testing.T) {
// Unit test // Unit test
func TestHostPool_AddGateway(t *testing.T) { func TestHostPool_AddGateway(t *testing.T) {
manager := newHappyManager() manager := newMockManager()
testNdf := getTestNdf(t) testNdf := getTestNdf(t)
newIndex := uint32(20) newIndex := uint32(20)
params := DefaultPoolParams() params := DefaultPoolParams()
...@@ -765,7 +765,7 @@ func TestHostPool_AddGateway(t *testing.T) { ...@@ -765,7 +765,7 @@ func TestHostPool_AddGateway(t *testing.T) {
// Unit test // Unit test
func TestHostPool_RemoveGateway(t *testing.T) { func TestHostPool_RemoveGateway(t *testing.T) {
manager := newHappyManager() manager := newMockManager()
testNdf := getTestNdf(t) testNdf := getTestNdf(t)
newIndex := uint32(20) newIndex := uint32(20)
params := DefaultPoolParams() params := DefaultPoolParams()
......
package gateway package gateway
import ( import (
"errors"
"gitlab.com/xx_network/comms/connect" "gitlab.com/xx_network/comms/connect"
"gitlab.com/xx_network/primitives/id" "gitlab.com/xx_network/primitives/id"
"gitlab.com/xx_network/primitives/ndf" "gitlab.com/xx_network/primitives/ndf"
) )
// Mock structure adhering to HostManager which returns the error
// path for all it's methods
type MangerErrorPath struct{}
// Constructor for MangerErrorPath
func newErrorManager() *MangerErrorPath {
return &MangerErrorPath{}
}
func (mep *MangerErrorPath) GetHost(hostId *id.ID) (*connect.Host, bool) {
return nil, false
}
func (mep *MangerErrorPath) AddHost(hid *id.ID, address string,
cert []byte, params connect.HostParams) (host *connect.Host, err error) {
return nil, errors.New("Failed to add host")
}
func (mep *MangerErrorPath) RemoveHost(hid *id.ID) {}
// Mock structure adhering to HostManager to be used for happy path // Mock structure adhering to HostManager to be used for happy path
type ManagerHappyPath struct { type mockManager struct {
hosts map[string]*connect.Host hosts map[string]*connect.Host
} }
// Constructor for ManagerHappyPath // Constructor for mockManager
func newHappyManager() *ManagerHappyPath { func newMockManager() *mockManager {
return &ManagerHappyPath{ return &mockManager{
hosts: make(map[string]*connect.Host), hosts: make(map[string]*connect.Host),
} }
} }
func (mhp *ManagerHappyPath) GetHost(hostId *id.ID) (*connect.Host, bool) { func (mhp *mockManager) GetHost(hostId *id.ID) (*connect.Host, bool) {
h, ok := mhp.hosts[hostId.String()] h, ok := mhp.hosts[hostId.String()]
return h, ok return h, ok
} }
func (mhp *ManagerHappyPath) AddHost(hid *id.ID, address string, func (mhp *mockManager) AddHost(hid *id.ID, address string,
cert []byte, params connect.HostParams) (host *connect.Host, err error) { cert []byte, params connect.HostParams) (host *connect.Host, err error) {
host, err = connect.NewHost(hid, address, cert, params) host, err = connect.NewHost(hid, address, cert, params)
if err != nil { if err != nil {
...@@ -55,7 +35,7 @@ func (mhp *ManagerHappyPath) AddHost(hid *id.ID, address string, ...@@ -55,7 +35,7 @@ func (mhp *ManagerHappyPath) AddHost(hid *id.ID, address string,
return return
} }
func (mhp *ManagerHappyPath) RemoveHost(hid *id.ID) { func (mhp *mockManager) RemoveHost(hid *id.ID) {
delete(mhp.hosts, hid.String()) delete(mhp.hosts, hid.String())
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment