-
Notifications
You must be signed in to change notification settings - Fork 51
/
iptablesprovidermock.go
205 lines (149 loc) · 6.07 KB
/
iptablesprovidermock.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
package provider
import (
"sync"
"testing"
)
type iptablesProviderMockedMethods struct {
appendMock func(table, chain string, rulespec ...string) error
insertMock func(table, chain string, pos int, rulespec ...string) error
deleteMock func(table, chain string, rulespec ...string) error
listChainsMock func(table string) ([]string, error)
clearChainMock func(table, chain string) error
deleteChainMock func(table, chain string) error
newChainMock func(table, chain string) error
commitMock func() error
retrieveTableMock func() map[string]map[string][]string
resetMock func(subs string) error
listRulesMock func(table, chain string) ([]string, error)
}
// TestIptablesProvider is a test implementation for IptablesProvider
type TestIptablesProvider interface {
IptablesProvider
MockAppend(t *testing.T, impl func(table, chain string, rulespec ...string) error)
MockInsert(t *testing.T, impl func(table, chain string, pos int, rulespec ...string) error)
MockDelete(t *testing.T, impl func(table, chain string, rulespec ...string) error)
MockListChains(t *testing.T, impl func(table string) ([]string, error))
MockClearChain(t *testing.T, impl func(table, chain string) error)
MockDeleteChain(t *testing.T, impl func(table, chain string) error)
MockNewChain(t *testing.T, impl func(table, chain string) error)
MockCommit(t *testing.T, impl func() error)
MockReset(t *testing.T, impl func(subs string) error)
MockListRules(t *testing.T, impl func(table, chain string) ([]string, error))
}
// A testIptablesProvider is an empty TransactionalManipulator that can be easily mocked.
type testIptablesProvider struct {
mocks map[*testing.T]*iptablesProviderMockedMethods
lock *sync.Mutex
currentTest *testing.T
}
// NewTestIptablesProvider returns a new TestManipulator.
func NewTestIptablesProvider() TestIptablesProvider {
return &testIptablesProvider{lock: &sync.Mutex{}, mocks: map[*testing.T]*iptablesProviderMockedMethods{}}
}
func (m *testIptablesProvider) MockListRules(t *testing.T, impl func(table, chain string) ([]string, error)) {
m.currentMocks(t).listRulesMock = impl
}
func (m *testIptablesProvider) MockAppend(t *testing.T, impl func(table, chain string, rulespec ...string) error) {
m.currentMocks(t).appendMock = impl
}
func (m *testIptablesProvider) MockReset(t *testing.T, impl func(subs string) error) {
m.currentMocks(t).resetMock = impl
}
func (m *testIptablesProvider) MockInsert(t *testing.T, impl func(table, chain string, pos int, rulespec ...string) error) {
m.currentMocks(t).insertMock = impl
}
func (m *testIptablesProvider) MockDelete(t *testing.T, impl func(table, chain string, rulespec ...string) error) {
m.currentMocks(t).deleteMock = impl
}
func (m *testIptablesProvider) MockListChains(t *testing.T, impl func(table string) ([]string, error)) {
m.currentMocks(t).listChainsMock = impl
}
func (m *testIptablesProvider) MockClearChain(t *testing.T, impl func(table, chain string) error) {
m.currentMocks(t).clearChainMock = impl
}
func (m *testIptablesProvider) MockDeleteChain(t *testing.T, impl func(table, chain string) error) {
m.currentMocks(t).deleteChainMock = impl
}
func (m *testIptablesProvider) MockNewChain(t *testing.T, impl func(table, chain string) error) {
m.currentMocks(t).newChainMock = impl
}
func (m *testIptablesProvider) MockCommit(t *testing.T, impl func() error) {
m.currentMocks(t).commitMock = impl
}
func (m *testIptablesProvider) Append(table, chain string, rulespec ...string) error {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.appendMock != nil {
return mock.appendMock(table, chain, rulespec...)
}
return nil
}
func (m *testIptablesProvider) ListRules(table, chain string) ([]string, error) {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.listRulesMock != nil {
return mock.listRulesMock(table, chain)
}
return []string{}, nil
}
func (m *testIptablesProvider) Insert(table, chain string, pos int, rulespec ...string) error {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.insertMock != nil {
return mock.insertMock(table, chain, pos, rulespec...)
}
return nil
}
func (m *testIptablesProvider) ResetRules(subs string) error {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.resetMock != nil {
return mock.resetMock(subs)
}
return nil
}
func (m *testIptablesProvider) Delete(table, chain string, rulespec ...string) error {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.deleteMock != nil {
return mock.deleteMock(table, chain, rulespec...)
}
return nil
}
func (m *testIptablesProvider) ListChains(table string) ([]string, error) {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.listChainsMock != nil {
return mock.listChainsMock(table)
}
return nil, nil
}
func (m *testIptablesProvider) ClearChain(table, chain string) error {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.clearChainMock != nil {
return mock.clearChainMock(table, chain)
}
return nil
}
func (m *testIptablesProvider) DeleteChain(table, chain string) error {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.deleteChainMock != nil {
return mock.deleteChainMock(table, chain)
}
return nil
}
func (m *testIptablesProvider) NewChain(table, chain string) error {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.newChainMock != nil {
return mock.newChainMock(table, chain)
}
return nil
}
func (m *testIptablesProvider) Commit() error {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.commitMock != nil {
return mock.commitMock()
}
return nil
}
func (m *testIptablesProvider) RetrieveTable() map[string]map[string][]string {
if mock := m.currentMocks(m.currentTest); mock != nil && mock.retrieveTableMock != nil {
return mock.retrieveTableMock()
}
return nil
}
func (m *testIptablesProvider) currentMocks(t *testing.T) *iptablesProviderMockedMethods {
m.lock.Lock()
defer m.lock.Unlock()
mocks := m.mocks[t]
if mocks == nil {
mocks = &iptablesProviderMockedMethods{}
m.mocks[t] = mocks
}
m.currentTest = t
return mocks
}