-
Notifications
You must be signed in to change notification settings - Fork 51
/
processmonmock.go
106 lines (92 loc) · 3.26 KB
/
processmonmock.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
package processmon
import (
"sync"
"testing"
"github.com/aporeto-inc/trireme/enforcer/utils/rpcwrapper"
)
type mockedMethods struct {
GetExitStatusMock func(string) bool
KillProcessMock func(string)
LaunchProcessMock func(string, int, string, rpcwrapper.RPCClient, string, string, string) error
SetExitStatusMock func(string, bool) error
SetnsNetPathMock func(string)
}
// TestProcessManager is a mock process manager
type TestProcessManager interface {
ProcessManager
MockGetExitStatus(t *testing.T, impl func(string) bool)
MockKillProcess(t *testing.T, impl func(string))
MockLaunchProcess(t *testing.T, impl func(string, int, string, rpcwrapper.RPCClient, string, string, string) error)
MockSetExitStatus(t *testing.T, impl func(string, bool) error)
MockSetnsNetPath(t *testing.T, impl func(string))
}
type testProcessMon struct {
mocks map[*testing.T]*mockedMethods
lock *sync.Mutex
currentTest *testing.T
}
// NewTestProcessMon creates a mock process manager
func NewTestProcessMon() ProcessManager {
p := &testProcessMon{
lock: &sync.Mutex{},
mocks: map[*testing.T]*mockedMethods{},
}
return p
}
func (m *testProcessMon) 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 *testProcessMon) MockSetnsNetPath(t *testing.T, impl func(string)) {
m.currentMocks(t).SetnsNetPathMock = impl
}
func (m *testProcessMon) MockGetExitStatus(t *testing.T, impl func(string) bool) {
m.currentMocks(t).GetExitStatusMock = impl
}
func (m *testProcessMon) MockKillProcess(t *testing.T, impl func(string)) {
m.currentMocks(t).KillProcessMock = impl
}
func (m *testProcessMon) MockLaunchProcess(t *testing.T, impl func(string, int, string, rpcwrapper.RPCClient, string, string, string) error) {
m.currentMocks(t).LaunchProcessMock = impl
}
func (m *testProcessMon) MockSetExitStatus(t *testing.T, impl func(string, bool) error) {
m.currentMocks(t).SetExitStatusMock = impl
}
func (m *testProcessMon) SetnsNetPath(netpath string) {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.SetnsNetPathMock != nil {
mock.SetnsNetPathMock(netpath)
return
}
return
}
func (m *testProcessMon) GetExitStatus(contextID string) bool {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.GetExitStatusMock != nil {
return mock.GetExitStatusMock(contextID)
}
return true
}
func (m *testProcessMon) SetExitStatus(contextID string, status bool) error {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.SetExitStatusMock != nil {
return mock.SetExitStatusMock(contextID, status)
}
return nil
}
func (m *testProcessMon) KillProcess(contextID string) {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.KillProcessMock != nil {
mock.KillProcessMock(contextID)
return
}
}
func (m *testProcessMon) LaunchProcess(contextID string, refPid int, refNSPath string, rpchdl rpcwrapper.RPCClient, processname string, statssecret string, procMountPoint string) error {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.LaunchProcessMock != nil {
return mock.LaunchProcessMock(contextID, refPid, refNSPath, rpchdl, processname, statssecret, procMountPoint)
}
return nil
}