-
Notifications
You must be signed in to change notification settings - Fork 51
/
supervisormock.go
163 lines (120 loc) · 4.45 KB
/
supervisormock.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
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
package supervisor
import (
"sync"
"testing"
"go.aporeto.io/trireme-lib/policy"
)
type mockedMethods struct {
// Supervise adds a new supervised processing unit.
superviseMock func(contextID string, puInfo *policy.PUInfo) error
// Unsupervise unsupervises the given PU
unsuperviseMock func(contextID string) error
// Start starts the Supervisor.
startMock func() error
// Stop stops the Supervisor.
stopMock func() error
//AddExcludedIP adds exlcluded iplist
AddExcludedIPsMock func(iplist []string) error
// SetTargetNetworksMock adds the SetTargetNetworks implementation
SetTargetNetworksMock func(networks []string) error
}
// TestSupervisor is a test implementation for IptablesProvider
type TestSupervisor interface {
Supervisor
MockSupervise(t *testing.T, impl func(contextID string, puInfo *policy.PUInfo) error)
MockUnsupervise(t *testing.T, impl func(contextID string) error)
MockStart(t *testing.T, impl func() error)
MockStop(t *testing.T, impl func() error)
MockAddExcludedIPs(t *testing.T, impl func(ips []string) error)
MockSetTargetNetworks(t *testing.T, impl func(networks []string) error)
}
// A TestSupervisorInst is an empty TransactionalManipulator that can be easily mocked.
type TestSupervisorInst struct {
mocks map[*testing.T]*mockedMethods
lock *sync.Mutex
currentTest *testing.T
}
// NewTestSupervisor returns a new TestManipulator.
func NewTestSupervisor() *TestSupervisorInst {
return &TestSupervisorInst{
lock: &sync.Mutex{},
mocks: map[*testing.T]*mockedMethods{},
}
}
// MockAddExcludedIPs mocks AddExcludedIPs
func (m *TestSupervisorInst) MockAddExcludedIPs(t *testing.T, impl func(ip []string) error) {
m.currentMocks(t).AddExcludedIPsMock = impl
}
// MockSupervise mocks the Supervise method
func (m *TestSupervisorInst) MockSupervise(t *testing.T, impl func(contextID string, puInfo *policy.PUInfo) error) {
m.currentMocks(t).superviseMock = impl
}
// MockUnsupervise mocks the unsupervise method
func (m *TestSupervisorInst) MockUnsupervise(t *testing.T, impl func(contextID string) error) {
m.currentMocks(t).unsuperviseMock = impl
}
// MockStart mocks the Start method
func (m *TestSupervisorInst) MockStart(t *testing.T, impl func() error) {
m.currentMocks(t).startMock = impl
}
// MockStop mocks the Stop method
func (m *TestSupervisorInst) MockStop(t *testing.T, impl func() error) {
m.currentMocks(t).stopMock = impl
}
// MockSetTargetNetworks mocks the SetTargetNetworks method
func (m *TestSupervisorInst) MockSetTargetNetworks(t *testing.T, impl func(networks []string) error) {
m.currentMocks(t).SetTargetNetworksMock = impl
}
// Supervise is a test implementation of the Supervise interface
func (m *TestSupervisorInst) 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
}
// Unsupervise is a test implementation of the Unsupervise interface
func (m *TestSupervisorInst) Unsupervise(contextID string) error {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.unsuperviseMock != nil {
return mock.unsuperviseMock(contextID)
}
return nil
}
// AddExcludedIPs is a test implementation of the AddExcludedIPs interface
func (m *TestSupervisorInst) AddExcludedIPs(ips []string) error {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.AddExcludedIPsMock != nil {
return mock.AddExcludedIPsMock(ips)
}
return nil
}
// Start is a test implementation of the Start interface method
func (m *TestSupervisorInst) Start() error {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.startMock != nil {
return mock.startMock()
}
return nil
}
// Stop is a test implementation of the Stop interface method
func (m *TestSupervisorInst) Stop() error {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.stopMock != nil {
return mock.stopMock()
}
return nil
}
// SetTargetNetworks is a test implementation of the SetTargetNetworks interface method
func (m *TestSupervisorInst) SetTargetNetworks(networks []string) error {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.SetTargetNetworksMock != nil {
return mock.SetTargetNetworksMock(networks)
}
return nil
}
func (m *TestSupervisorInst) 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
}