-
Notifications
You must be signed in to change notification settings - Fork 51
/
trirememock.go
82 lines (60 loc) · 2.37 KB
/
trirememock.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
package trireme
import (
"sync"
"testing"
"github.com/aporeto-inc/trireme/monitor"
"github.com/aporeto-inc/trireme/policy"
)
type mockedMethodsPolicyResolver struct {
// ResolvePolicy returns the policy.PUPolicy associated with the given contextID using the given policy.RuntimeReader.
resolvePolicyMock func(contextID string, RuntimeReader policy.RuntimeReader) (*policy.PUPolicy, error)
// HandleDeletePU is called when a PU is removed.
handlePUEventMock func(contextID string, eventType monitor.Event)
}
// TestPolicyResolver us
type TestPolicyResolver interface {
PolicyResolver
MockResolvePolicy(t *testing.T, impl func(contextID string, RuntimeReader policy.RuntimeReader) (*policy.PUPolicy, error))
MockHandlePUEvent(t *testing.T, impl func(contextID string, eventType monitor.Event))
}
// A testPolicyResolver is an empty TransactionalManipulator that can be easily mocked.
type testPolicyResolver struct {
mocks map[*testing.T]*mockedMethodsPolicyResolver
lock *sync.Mutex
currentTest *testing.T
}
// NewTestPolicyResolver returns a new TestManipulator.
func NewTestPolicyResolver() TestPolicyResolver {
return &testPolicyResolver{
lock: &sync.Mutex{},
mocks: map[*testing.T]*mockedMethodsPolicyResolver{},
}
}
func (m *testPolicyResolver) MockResolvePolicy(t *testing.T, impl func(contextID string, RuntimeReader policy.RuntimeReader) (*policy.PUPolicy, error)) {
m.currentMocks(t).resolvePolicyMock = impl
}
func (m *testPolicyResolver) MockHandlePUEvent(t *testing.T, impl func(contextID string, eventType monitor.Event)) {
m.currentMocks(t).handlePUEventMock = impl
}
func (m *testPolicyResolver) ResolvePolicy(contextID string, RuntimeReader policy.RuntimeReader) (*policy.PUPolicy, error) {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.resolvePolicyMock != nil {
return mock.resolvePolicyMock(contextID, RuntimeReader)
}
return nil, nil
}
func (m *testPolicyResolver) HandlePUEvent(contextID string, eventType monitor.Event) {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.handlePUEventMock != nil {
mock.handlePUEventMock(contextID, eventType)
}
}
func (m *testPolicyResolver) currentMocks(t *testing.T) *mockedMethodsPolicyResolver {
m.lock.Lock()
defer m.lock.Unlock()
mocks := m.mocks[t]
if mocks == nil {
mocks = &mockedMethodsPolicyResolver{}
m.mocks[t] = mocks
}
m.currentTest = t
return mocks
}