-
Notifications
You must be signed in to change notification settings - Fork 51
/
ipsetprovidermock.go
241 lines (174 loc) · 5.81 KB
/
ipsetprovidermock.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package ipsetmanager
import (
"sync"
"testing"
"github.com/aporeto-inc/go-ipset/ipset"
)
type ipsetProviderMockedMethods struct {
newMockIPset func(name string, hasht string, p *ipset.Params) (Ipset, error)
getMockIPset func(name string) Ipset
destroyAllMock func(prefix string) error
listIPSetsMock func() ([]string, error)
}
// TestIpsetProvider is a test implementation for IpsetProvider
type TestIpsetProvider interface {
IpsetProvider
MockNewIpset(t *testing.T, impl func(name string, hasht string, p *ipset.Params) (Ipset, error))
MockGetIpset(t *testing.T, impl func(name string) Ipset)
MockDestroyAll(t *testing.T, impl func(string) error)
MockListIPSets(t *testing.T, impl func() ([]string, error))
}
type testIpsetProvider struct {
mocks map[*testing.T]*ipsetProviderMockedMethods
lock *sync.Mutex
currentTest *testing.T
}
// NewTestIpsetProvider returns a new TestManipulator.
func NewTestIpsetProvider() TestIpsetProvider {
return &testIpsetProvider{
lock: &sync.Mutex{},
mocks: map[*testing.T]*ipsetProviderMockedMethods{},
}
}
func (m *testIpsetProvider) MockNewIpset(t *testing.T, impl func(name string, hasht string, p *ipset.Params) (Ipset, error)) {
m.currentMocks(t).newMockIPset = impl
}
func (m *testIpsetProvider) MockGetIpset(t *testing.T, impl func(name string) Ipset) {
m.currentMocks(t).getMockIPset = impl
}
func (m *testIpsetProvider) MockDestroyAll(t *testing.T, impl func(string) error) {
m.currentMocks(t).destroyAllMock = impl
}
func (m *testIpsetProvider) MockListIPSets(t *testing.T, impl func() ([]string, error)) {
m.currentMocks(t).listIPSetsMock = impl
}
func (m *testIpsetProvider) NewIpset(name string, hasht string, p *ipset.Params) (Ipset, error) {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.newMockIPset != nil {
return mock.newMockIPset(name, hasht, p)
}
return NewTestIpset(), nil
}
func (m *testIpsetProvider) GetIpset(name string) Ipset {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.newMockIPset != nil {
return mock.getMockIPset(name)
}
return NewTestIpset()
}
func (m *testIpsetProvider) DestroyAll(prefix string) error {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.destroyAllMock != nil {
return mock.destroyAllMock(prefix)
}
return nil
}
func (m *testIpsetProvider) ListIPSets() ([]string, error) {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.listIPSetsMock != nil {
return mock.listIPSetsMock()
}
return nil, nil
}
func (m *testIpsetProvider) currentMocks(t *testing.T) *ipsetProviderMockedMethods {
m.lock.Lock()
defer m.lock.Unlock()
mocks := m.mocks[t]
if mocks == nil {
mocks = &ipsetProviderMockedMethods{}
m.mocks[t] = mocks
}
m.currentTest = t
return mocks
}
type ipsetMockedMethods struct {
addMock func(entry string, timeout int) error
addOptionMock func(entry string, option string, timeout int) error
delMock func(entry string) error
destroyMock func() error
flushMock func() error
testMock func(entry string) (bool, error)
}
// TestIpset is a test implementation for Ipset
type TestIpset interface {
Ipset
MockAdd(t *testing.T, impl func(entry string, timeout int) error)
MockAddOption(t *testing.T, impl func(entry string, option string, timeout int) error)
MockDel(t *testing.T, impl func(entry string) error)
MockDestroy(t *testing.T, impl func() error)
MockFlush(t *testing.T, impl func() error)
MockTest(t *testing.T, impl func(entry string) (bool, error))
}
type testIpset struct {
mocks map[*testing.T]*ipsetMockedMethods
lock *sync.Mutex
currentTest *testing.T
}
// NewTestIpset returns a new TestManipulator.
func NewTestIpset() TestIpset {
return &testIpset{
lock: &sync.Mutex{},
mocks: map[*testing.T]*ipsetMockedMethods{},
}
}
func (m *testIpset) MockAdd(t *testing.T, impl func(entry string, timeout int) error) {
m.currentMocks(t).addMock = impl
}
func (m *testIpset) MockAddOption(t *testing.T, impl func(entry string, option string, timeout int) error) {
m.currentMocks(t).addOptionMock = impl
}
func (m *testIpset) MockDel(t *testing.T, impl func(entry string) error) {
m.currentMocks(t).delMock = impl
}
func (m *testIpset) MockDestroy(t *testing.T, impl func() error) {
m.currentMocks(t).destroyMock = impl
}
func (m *testIpset) MockFlush(t *testing.T, impl func() error) {
m.currentMocks(t).flushMock = impl
}
func (m *testIpset) MockTest(t *testing.T, impl func(entry string) (bool, error)) {
m.currentMocks(t).testMock = impl
}
func (m *testIpset) Add(entry string, timeout int) error {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.addMock != nil {
return mock.addMock(entry, timeout)
}
return nil
}
func (m *testIpset) AddOption(entry string, option string, timeout int) error {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.addOptionMock != nil {
return mock.addOptionMock(entry, option, timeout)
}
return nil
}
func (m *testIpset) Del(entry string) error {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.delMock != nil {
return mock.delMock(entry)
}
return nil
}
func (m *testIpset) Destroy() error {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.destroyMock != nil {
return mock.destroyMock()
}
return nil
}
func (m *testIpset) Flush() error {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.flushMock != nil {
return mock.flushMock()
}
return nil
}
func (m *testIpset) Test(entry string) (bool, error) {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.testMock != nil {
return mock.testMock(entry)
}
return false, nil
}
func (m *testIpset) currentMocks(t *testing.T) *ipsetMockedMethods {
m.lock.Lock()
defer m.lock.Unlock()
mocks := m.mocks[t]
if mocks == nil {
mocks = &ipsetMockedMethods{}
m.mocks[t] = mocks
}
m.currentTest = t
return mocks
}