-
Notifications
You must be signed in to change notification settings - Fork 51
/
supervisorproxymock.go
111 lines (86 loc) · 2.7 KB
/
supervisorproxymock.go
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
package supervisorproxy
import (
"sync"
"testing"
"github.com/aporeto-inc/trireme/policy"
"github.com/aporeto-inc/trireme/supervisor"
)
type mockedMethods struct {
SuperviseMock func(string, *policy.PUInfo) error
UnsuperviseMock func(string) error
StartMock func() error
StopMock func() error
SetTargetNetworksMock func([]string) error
}
// TestSupervisorLauncher is a mock
type TestSupervisorLauncher interface {
supervisor.Supervisor
}
type testSupervisorLauncher struct {
mocks map[*testing.T]*mockedMethods
lock *sync.Mutex
currentTest *testing.T
}
// NewTestSupervisorLauncher creates a mock supervisor
func NewTestSupervisorLauncher() supervisor.Supervisor {
return &testSupervisorLauncher{
lock: &sync.Mutex{},
mocks: map[*testing.T]*mockedMethods{},
}
}
func (m *testSupervisorLauncher) currentMocks(t *testing.T) *mockedMethods {
m.lock.Lock()
defer m.lock.Unlock()
mocks := m.mocks[t]
if mocks == nil {
mocks = &mockedMethods{}
m.mocks[t] = mocks
}
m.currentTest = t
return mocks
}
func (m *testSupervisorLauncher) MockSupervise(t *testing.T, impl func(string, *policy.PUInfo) error) {
m.currentMocks(t).SuperviseMock = impl
}
func (m *testSupervisorLauncher) MockUnsupervise(t *testing.T, impl func(string) error) {
m.currentMocks(t).UnsuperviseMock = impl
}
func (m *testSupervisorLauncher) MockStart(t *testing.T, impl func() error) {
m.currentMocks(t).StartMock = impl
}
func (m *testSupervisorLauncher) MockSetTargetNetworks(t *testing.T, impl func([]string) error) {
m.currentMocks(t).SetTargetNetworksMock = impl
}
func (m *testSupervisorLauncher) MockStop(t *testing.T, impl func() error) {
m.currentMocks(t).StopMock = impl
}
func (m *testSupervisorLauncher) Supervise(contextID string, puInfo *policy.PUInfo) error {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.SuperviseMock != nil {
return mock.SuperviseMock(contextID, puInfo)
}
return nil
}
func (m *testSupervisorLauncher) Unsupervise(contextID string) error {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.UnsuperviseMock != nil {
return mock.UnsuperviseMock(contextID)
}
return nil
}
func (m *testSupervisorLauncher) Start() error {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.StartMock != nil {
return mock.StartMock()
}
return nil
}
func (m *testSupervisorLauncher) SetTargetNetworks(networls []string) error {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.StartMock != nil {
return mock.StartMock()
}
return nil
}
func (m *testSupervisorLauncher) Stop() error {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.StopMock != nil {
return mock.StopMock()
}
return nil
}