-
Notifications
You must be signed in to change notification settings - Fork 51
/
supervisorproxymock.go
122 lines (99 loc) · 3.57 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
112
113
114
115
116
117
118
119
120
121
122
package supervisorproxy
import (
"context"
"sync"
"testing"
"time"
"go.aporeto.io/trireme-lib/controller/internal/supervisor"
"go.aporeto.io/trireme-lib/controller/runtime"
"go.aporeto.io/trireme-lib/policy"
)
type mockedMethods struct {
SuperviseMock func(string, *policy.PUInfo) error
UnsuperviseMock func(string) error
RunMock func(ctx context.Context) error
SetTargetNetworksMock func(cfg *runtime.Configuration) error
CleanUpMock func() error
EnableIPTablesPacketTracingMock func(ctx context.Context, contextID string, interval time.Duration) 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) MockEnableIPTablesPacketTracing(t *testing.T, impl func(context.Context, string, time.Duration) error) {
m.currentMocks(t).EnableIPTablesPacketTracingMock = impl
}
func (m *testSupervisorLauncher) MockUnsupervise(t *testing.T, impl func(string) error) {
m.currentMocks(t).UnsuperviseMock = impl
}
func (m *testSupervisorLauncher) MockRun(t *testing.T, impl func(ctx context.Context) error) {
m.currentMocks(t).RunMock = impl
}
func (m *testSupervisorLauncher) MockSetTargetNetworks(t *testing.T, impl func(*runtime.Configuration) error) {
m.currentMocks(t).SetTargetNetworksMock = impl
}
func (m *testSupervisorLauncher) MockCleanUp(t *testing.T, impl func() error) {
m.currentMocks(t).CleanUpMock = 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) Run(ctx context.Context) error {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.RunMock != nil {
return mock.RunMock(ctx)
}
return nil
}
func (m *testSupervisorLauncher) SetTargetNetworks(cfg *runtime.Configuration) error {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.SetTargetNetworksMock != nil {
return mock.SetTargetNetworksMock(cfg)
}
return nil
}
func (m *testSupervisorLauncher) CleanUp() error {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.CleanUpMock != nil {
return mock.CleanUpMock()
}
return nil
}
func (m *testSupervisorLauncher) EnableIPTablesPacketTracing(ctx context.Context, contextID string, interval time.Duration) error {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.EnableIPTablesPacketTracingMock != nil {
return mock.EnableIPTablesPacketTracingMock(ctx, contextID, interval)
}
return nil
}