@@ -40,7 +40,8 @@ type mockIPTables struct {
40
40
}
41
41
42
42
func TestKeepConsistent (t * testing.T ) {
43
- s , fw , mock := dummyState ()
43
+ s := testDummyState ()
44
+ fw , mock := mockFirewallIPTables ()
44
45
45
46
// Initialization: Apply generated rules
46
47
fw .Apply (& s )
@@ -109,7 +110,8 @@ func TestKeepConsistent(t *testing.T) {
109
110
}
110
111
111
112
func TestApply (t * testing.T ) {
112
- s , fw , _ := dummyState ()
113
+ s := testDummyState ()
114
+ fw , _ := mockFirewallIPTables ()
113
115
fw .Apply (& s )
114
116
115
117
// Expect patterns in output
@@ -153,7 +155,7 @@ func TestApply(t *testing.T) {
153
155
"swap tmp_[a-zA-Z0-9]* C" ,
154
156
}, t )
155
157
156
- // Unexpect pattrins in ipset output
158
+ // Unexpect patterns in ipset output
157
159
unexpects (ipsetResult , []string {"add tmp_[a-zA-Z0-9]* 42\\ .2\\ .3\\ .4" ,
158
160
"add tmp_[a-zA-Z0-9]* 5\\ .1\\ ." ,
159
161
}, t )
@@ -181,7 +183,8 @@ func TestApply(t *testing.T) {
181
183
}
182
184
183
185
func TestRulesGenerate (t * testing.T ) {
184
- s , fw , _ := dummyState ()
186
+ s := testDummyState ()
187
+ fw , _ := mockFirewallIPTables ()
185
188
test := fw .rulesGenerate (& s , s .StaticIPSets , false )
186
189
187
190
// Expect patterns in output
@@ -198,7 +201,8 @@ func TestRulesGenerate(t *testing.T) {
198
201
}
199
202
200
203
func TestIpsetGenerateServices (t * testing.T ) {
201
- s , fw , _ := dummyState ()
204
+ s := testDummyState ()
205
+ fw , _ := mockFirewallIPTables ()
202
206
203
207
var result string = ""
204
208
for _ , srv := range s .NodeServices {
@@ -220,7 +224,8 @@ func TestIpsetGenerateServices(t *testing.T) {
220
224
}
221
225
222
226
func TestIpsetGenerateStaticSetList (t * testing.T ) {
223
- s , fw , _ := dummyState ()
227
+ s := testDummyState ()
228
+ fw , _ := mockFirewallIPTables ()
224
229
var result string = ""
225
230
for name , set := range s .StaticIPSets {
226
231
result += fw .ipsetGenerate (name , set )
@@ -235,7 +240,8 @@ func TestIpsetGenerateStaticSetList(t *testing.T) {
235
240
}
236
241
237
242
func TestIpsetGenerateConstSetList (t * testing.T ) {
238
- s , fw , _ := dummyState ()
243
+ s := testDummyState ()
244
+ fw , _ := mockFirewallIPTables ()
239
245
240
246
// Check expectations
241
247
expects := []string {"swap tmp_" , "destroy tmp_" , "create tmp_" }
@@ -255,7 +261,8 @@ func TestIpsetGenerateConstSetList(t *testing.T) {
255
261
256
262
// Test IPv6 support (ipset)
257
263
func TestIpsetV6 (t * testing.T ) {
258
- s , fw , _ := dummyState ()
264
+ s := testDummyState ()
265
+ fw , _ := mockFirewallIPTables ()
259
266
name := "TestService"
260
267
srv6 := asService (name , []string {"22/tcp" }, []string {"4.4.4.4/28" , "::1:5ee:bad:c0de/80" })
261
268
s .NodeServices = append (s .NodeServices , srv6 )
@@ -290,7 +297,8 @@ func TestIpsetV6(t *testing.T) {
290
297
}
291
298
292
299
func TestIptablesV6 (t * testing.T ) {
293
- s , fw , _ := dummyState ()
300
+ s := testDummyState ()
301
+ fw , _ := mockFirewallIPTables ()
294
302
295
303
// Exist v6
296
304
name := "TestService"
@@ -310,25 +318,29 @@ func TestIptablesV6(t *testing.T) {
310
318
311
319
// ==========[ Util Func ]==========
312
320
313
- func dummyState () ( state , * fwIPTables , * mockIPTables ) {
321
+ func testDummyState () state {
314
322
// State:
315
323
s := state {
316
324
StaticIPSets : map [string ][]string {
317
325
"A" : []string {"1.2.3.1/32" , "0.0.0.0/0" , "10.10.10.10/28" , "192.168.1.1/32" , "42.2.3.4" , "::0/0" },
318
326
"B" : []string {"1.2.3.2/32" , "0.0.0.0/0" , "10.10.10.10/28" , "192.168.1.1/32" , "42.2.3.4" },
319
327
},
320
328
NodeServices : []bService {
321
- asService ("B" , []string {"10/tcp" , "8080:8090" }, []string {"1.2.3.3/32" , "0.0.0.0/0" , "10.0.0.0/12" , "5.1.1.3/32" , "42.2.3.4" }),
329
+ asService ("B" , []string {"10/tcp" , "8080:8090" , "443/tcp" , "443/tcp" }, []string {"1.2.3.3/32" , "0.0.0.0/0" , "10.0.0.0/12" , "5.1.1.3/32" , "42.2.3.4" }),
322
330
asService ("C" , []string {"20/tcp" , "8085:9090" }, []string {"1.2.3.4/32" , "10.0.0.0/12" , "1.2.3.6/32" , "43.2.3.4" , "5.1.1.4/32" }),
323
- asService ("D " , []string {"30/tcp" }, []string {"1.2.3.4/32" , "10.0.0.0/12" , "1.2.3.6/32" , "43.2.3.4" , "5.1.1.4/32" }),
324
- asService ("E " , []string {}, []string {"1.2.3.4/32" , "10.0.0.0/12" , "1.2.3.6/32" , "43.2.3.4" , "5.1.1.4/32" }),
331
+ asService ("test.srv2.stg-hiera_tcp_19080 " , []string {"30/tcp" }, []string {"1.2.3.4/32" , "10.0.0.0/12" , "1.2.3.6/32" , "43.2.3.4" , "5.1.1.4/32" }),
332
+ asService ("ThiS_is_very_and_Very_LONG_service " , []string {}, []string {"1.2.3.4/32" , "10.0.0.0/12" , "1.2.3.6/32" , "43.2.3.4" , "5.1.1.4/32" }),
325
333
},
326
334
}
327
335
s .Config = & config {
328
336
StaticSetList : staticIPSetList ,
329
337
}
330
338
s .fillMandatoryIPSet ()
331
339
340
+ return s
341
+ }
342
+
343
+ func mockFirewallIPTables () (* fwIPTables , * mockIPTables ) {
332
344
// Firewall:
333
345
var fw * fwIPTables = newIPTables ()
334
346
mock := mockIPTables {
@@ -339,7 +351,7 @@ func dummyState() (state, *fwIPTables, *mockIPTables) {
339
351
340
352
// ipset
341
353
mock .mockIpsetList = func (name string ) (string , error ) { return mock .ipsets [name ], nil }
342
- mock .mockIpsetRestore = func (name , rules string ) error { mock .ipsets [name ] = rules ; return nil }
354
+ mock .mockIpsetRestore = func (name string , rules string ) error { mock .ipsets [name ] = rules ; return nil }
343
355
// iptables
344
356
mock .mockIptablesList = func () (string , error ) { return mock .rules , nil }
345
357
mock .mockIptablesRestore = func (rules string ) error { mock .rules = rules ; return nil }
@@ -348,7 +360,7 @@ func dummyState() (state, *fwIPTables, *mockIPTables) {
348
360
mock .mockIp6tablesRestore = func (rules string ) error { mock .rules6 = rules ; return nil }
349
361
350
362
fw .bin = & mock
351
- return s , fw , & mock
363
+ return fw , & mock
352
364
}
353
365
354
366
func asService (name string , ports []string , clients []string ) bService {
0 commit comments