/
enforcerproxymock.go
109 lines (92 loc) · 3.05 KB
/
enforcerproxymock.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
package enforcerproxy
import (
"sync"
"testing"
"github.com/aporeto-inc/trireme/enforcer"
"github.com/aporeto-inc/trireme/enforcer/utils/fqconfig"
"github.com/aporeto-inc/trireme/policy"
)
type mockedMethods struct {
EnforceMock func(contextID string, puInfo *policy.PUInfo) error
UnenforceMock func(contextID string) error
GetFilterQueueMock func() *fqconfig.FilterQueue
StartMock func() error
StopMock func() error
}
// TestEnforcerLauncher is a mock
type TestEnforcerLauncher interface {
enforcer.PolicyEnforcer
MockEnforce(t *testing.T, impl func(contextID string, puInfo *policy.PUInfo) error)
MockUnenforce(t *testing.T, impl func(contextID string) error)
MockGetFilterQueue(t *testing.T, impl func() *fqconfig.FilterQueue)
MockStart(t *testing.T, impl func() error)
MockStop(t *testing.T, impl func() error)
}
type testEnforcerLauncher struct {
mocks map[*testing.T]*mockedMethods
lock *sync.Mutex
currentTest *testing.T
}
// NewTestEnforcerLauncher mocks an enforcer
func NewTestEnforcerLauncher() enforcer.PolicyEnforcer {
return &testEnforcerLauncher{
lock: &sync.Mutex{},
mocks: map[*testing.T]*mockedMethods{},
}
}
func (m *testEnforcerLauncher) 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 *testEnforcerLauncher) MockEnforce(t *testing.T, impl func(contextID string, puInfo *policy.PUInfo) error) {
m.currentMocks(t).EnforceMock = impl
}
func (m *testEnforcerLauncher) MockUnenforce(t *testing.T, impl func(contextID string) error) {
m.currentMocks(t).UnenforceMock = impl
}
func (m *testEnforcerLauncher) MockGetFilterQueue(t *testing.T, impl func() *fqconfig.FilterQueue) {
m.currentMocks(t).GetFilterQueueMock = impl
}
func (m *testEnforcerLauncher) MockStart(t *testing.T, impl func() error) {
m.currentMocks(t).StartMock = impl
}
func (m *testEnforcerLauncher) MockStop(t *testing.T, impl func() error) {
m.currentMocks(t).StartMock = impl
}
func (m *testEnforcerLauncher) Enforce(contextID string, puInfo *policy.PUInfo) error {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.EnforceMock != nil {
return mock.EnforceMock(contextID, puInfo)
}
return nil
}
func (m *testEnforcerLauncher) Unenforce(contextID string) error {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.UnenforceMock != nil {
return mock.UnenforceMock(contextID)
}
return nil
}
func (m *testEnforcerLauncher) GetFilterQueue() *fqconfig.FilterQueue {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.GetFilterQueueMock != nil {
return mock.GetFilterQueueMock()
}
return nil
}
func (m *testEnforcerLauncher) Start() error {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.StartMock != nil {
return mock.StartMock()
}
return nil
}
func (m *testEnforcerLauncher) Stop() error {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.EnforceMock != nil {
return mock.StartMock()
}
return nil
}