1
- import sys
1
+ import json
2
2
import os
3
- from imp import load_source
3
+ import sys
4
4
from io import StringIO
5
5
from unittest import mock
6
6
7
+ from utilities_common .general import load_module_from_source
7
8
8
9
test_path = os .path .dirname (os .path .abspath (__file__ ))
9
10
modules_path = os .path .dirname (test_path )
10
11
scripts_path = os .path .join (modules_path , "scripts" )
11
12
sys .path .insert (0 , modules_path )
12
13
13
- load_source ('aclshow' , scripts_path + '/aclshow' )
14
- from aclshow import *
14
+ # Load the file under test
15
+ aclshow_path = os .path .join (scripts_path , 'aclshow' )
16
+ aclshow = load_module_from_source ('aclshow' , aclshow_path )
15
17
16
18
from .mock_tables import dbconnector
17
19
18
20
19
21
# Expected output for aclshow
20
- default_output = '' + \
21
- """ RULE NAME TABLE NAME PRIO PACKETS COUNT BYTES COUNT
22
+ default_output = """ \
23
+ RULE NAME TABLE NAME PRIO PACKETS COUNT BYTES COUNT
22
24
------------ ------------ ------ --------------- -------------
23
25
RULE_1 DATAACL 9999 101 100
24
26
RULE_2 DATAACL 9998 201 200
32
34
"""
33
35
34
36
# Expected output for aclshow -a
35
- all_output = '' + \
36
- """ RULE NAME TABLE NAME PRIO PACKETS COUNT BYTES COUNT
37
+ all_output = """ \
38
+ RULE NAME TABLE NAME PRIO PACKETS COUNT BYTES COUNT
37
39
------------ ------------ ------ --------------- -------------
38
40
RULE_1 DATAACL 9999 101 100
39
41
RULE_2 DATAACL 9998 201 200
49
51
"""
50
52
51
53
# Expected output for aclshow -r RULE_1 -t DATAACL
52
- rule1_dataacl_output = '' + \
53
- """ RULE NAME TABLE NAME PRIO PACKETS COUNT BYTES COUNT
54
+ rule1_dataacl_output = """ \
55
+ RULE NAME TABLE NAME PRIO PACKETS COUNT BYTES COUNT
54
56
----------- ------------ ------ --------------- -------------
55
57
RULE_1 DATAACL 9999 101 100
56
58
"""
57
59
58
60
# Expected output for aclshow -r RULE_1 -t DATAACL
59
- rule10_dataacl_output = '' + \
60
- """ RULE NAME TABLE NAME PRIO PACKETS COUNT BYTES COUNT
61
+ rule10_dataacl_output = """ \
62
+ RULE NAME TABLE NAME PRIO PACKETS COUNT BYTES COUNT
61
63
----------- ------------ ------ --------------- -------------
62
64
RULE_10 DATAACL 9989 1001 1000
63
65
"""
64
66
65
67
# Expected output for aclshow -a -r RULE_05
66
- rule05_all_output = '' + \
67
- """ RULE NAME TABLE NAME PRIO PACKETS COUNT BYTES COUNT
68
+ rule05_all_output = """ \
69
+ RULE NAME TABLE NAME PRIO PACKETS COUNT BYTES COUNT
68
70
----------- ------------ ------ --------------- -------------
69
71
RULE_05 DATAACL 9995 0 0
70
72
"""
71
73
72
74
# Expected output for aclshow -r RULE_0
73
- rule0_output = '' + \
74
- """ RULE NAME TABLE NAME PRIO PACKETS COUNT BYTES COUNT
75
+ rule0_output = """ \
76
+ RULE NAME TABLE NAME PRIO PACKETS COUNT BYTES COUNT
75
77
----------- ------------ ------ --------------- -------------
76
78
"""
77
79
78
80
# Expected output for aclshow -r RULE_4,RULE_6 -vv
79
- rule4_rule6_verbose_output = '' + \
80
- """ Reading ACL info...
81
+ rule4_rule6_verbose_output = """ \
82
+ Reading ACL info...
81
83
Total number of ACL Tables: 8
82
84
Total number of ACL Rules: 11
83
85
88
90
"""
89
91
90
92
# Expected output for aclshow -t EVERFLOW
91
- everflow_output = '' + \
92
- """ RULE NAME TABLE NAME PRIO PACKETS COUNT BYTES COUNT
93
+ everflow_output = """ \
94
+ RULE NAME TABLE NAME PRIO PACKETS COUNT BYTES COUNT
93
95
----------- ------------ ------ --------------- -------------
94
96
RULE_6 EVERFLOW 9994 601 600
95
97
"""
96
98
97
99
# Expected output for aclshow -t DATAACL
98
- dataacl_output = '' + \
99
- """ RULE NAME TABLE NAME PRIO PACKETS COUNT BYTES COUNT
100
+ dataacl_output = """ \
101
+ RULE NAME TABLE NAME PRIO PACKETS COUNT BYTES COUNT
100
102
------------ ------------ ------ --------------- -------------
101
103
RULE_1 DATAACL 9999 101 100
102
104
RULE_2 DATAACL 9998 201 200
113
115
114
116
# Expected output for
115
117
# aclshow -a -c ; aclshow -a
116
- all_after_clear_output = '' + \
117
- """ RULE NAME TABLE NAME PRIO PACKETS COUNT BYTES COUNT
118
+ all_after_clear_output = """ \
119
+ RULE NAME TABLE NAME PRIO PACKETS COUNT BYTES COUNT
118
120
------------ ------------ ------ --------------- -------------
119
121
RULE_1 DATAACL 9999 0 0
120
122
RULE_2 DATAACL 9998 0 0
129
131
RULE_08 EVERFLOW 9992 0 0
130
132
"""
131
133
134
+
132
135
class Aclshow ():
133
136
def __init__ (self , * args , ** kwargs ):
134
137
"""
@@ -146,19 +149,19 @@ def nullify_counters(self):
146
149
This method is used to empty dumped counters
147
150
if exist in /tmp/.counters_acl.p (by default).
148
151
"""
149
- if os .path .isfile (COUNTER_POSITION ):
150
- with open (COUNTER_POSITION , 'w' ) as fp :
152
+ if os .path .isfile (aclshow . COUNTER_POSITION ):
153
+ with open (aclshow . COUNTER_POSITION , 'w' ) as fp :
151
154
json .dump ([], fp )
152
155
153
156
def runTest (self ):
154
157
"""
155
158
This method invokes main() from aclshow utility (parametrized by argparse)
156
159
parametrized by mock argparse.
157
160
"""
158
- @ mock .patch ( ' argparse.ArgumentParser.parse_args' , return_value = argparse . Namespace ( ** self . kwargs ))
159
- def run ( mock_args ):
160
- main ()
161
- run ()
161
+ with mock .patch . object ( aclshow . argparse .ArgumentParser ,
162
+ 'parse_args' ,
163
+ return_value = aclshow . argparse . Namespace ( ** self . kwargs )):
164
+ aclshow . main ()
162
165
163
166
def setUp (self ):
164
167
if self .nullify_on_start :
@@ -173,56 +176,78 @@ def tearDown(self):
173
176
sys .stdout = self .old_stdout
174
177
175
178
# aclshow
179
+
180
+
176
181
def test_default ():
177
- test = Aclshow (all = None , clear = None , rules = None , tables = None , verbose = None )
182
+ test = Aclshow (all = None , clear = None , rules = None , tables = None , verbose = None )
178
183
assert test .result .getvalue () == default_output
179
184
180
185
# aclshow -a
186
+
187
+
181
188
def test_all ():
182
- test = Aclshow (all = True , clear = None , rules = None , tables = None , verbose = None )
189
+ test = Aclshow (all = True , clear = None , rules = None , tables = None , verbose = None )
183
190
assert test .result .getvalue () == all_output
184
191
185
192
# aclshow -r RULE_1 -t DATAACL
193
+
194
+
186
195
def test_rule1_dataacl ():
187
- test = Aclshow (all = None , clear = None , rules = 'RULE_1' , tables = 'DATAACL' , verbose = None )
196
+ test = Aclshow (all = None , clear = None , rules = 'RULE_1' , tables = 'DATAACL' , verbose = None )
188
197
assert test .result .getvalue () == rule1_dataacl_output
189
198
190
199
# aclshow -a -r RULE_05
200
+
201
+
191
202
def test_rule05_all ():
192
- test = Aclshow (all = True , clear = None , rules = 'RULE_05' , tables = None , verbose = None )
203
+ test = Aclshow (all = True , clear = None , rules = 'RULE_05' , tables = None , verbose = None )
193
204
assert test .result .getvalue () == rule05_all_output
194
205
195
206
# aclshow -r RULE_0
207
+
208
+
196
209
def test_rule0 ():
197
- test = Aclshow (all = None , clear = None , rules = 'RULE_0' , tables = None , verbose = None )
210
+ test = Aclshow (all = None , clear = None , rules = 'RULE_0' , tables = None , verbose = None )
198
211
assert test .result .getvalue () == rule0_output
199
212
200
213
# aclshow -r RULE_10 -t DATAACL
214
+
215
+
201
216
def test_rule10_lowercase_priority ():
202
- test = Aclshow (all = None , clear = None , rules = 'RULE_10' , tables = 'DATAACL' , verbose = None )
217
+ test = Aclshow (all = None , clear = None , rules = 'RULE_10' , tables = 'DATAACL' , verbose = None )
203
218
assert test .result .getvalue () == rule10_dataacl_output
204
219
205
220
# aclshow -r RULE_4,RULE_6 -vv
221
+
222
+
206
223
def test_rule4_rule6_verbose ():
207
- test = Aclshow (all = None , clear = None , rules = 'RULE_4,RULE_6' , tables = None , verbose = True )
224
+ test = Aclshow (all = None , clear = None , rules = 'RULE_4,RULE_6' , tables = None , verbose = True )
208
225
assert test .result .getvalue () == rule4_rule6_verbose_output
209
226
210
227
# aclshow -t EVERFLOW
228
+
229
+
211
230
def test_everflow ():
212
231
test = Aclshow (all = None , clear = None , rules = None , tables = 'EVERFLOW' , verbose = None )
213
232
assert test .result .getvalue () == everflow_output
214
233
215
234
# aclshow -t DATAACL
235
+
236
+
216
237
def test_dataacl ():
217
238
test = Aclshow (all = None , clear = None , rules = None , tables = 'DATAACL' , verbose = None )
218
239
assert test .result .getvalue () == dataacl_output
219
240
220
241
# aclshow -c
242
+
243
+
221
244
def test_clear ():
222
245
test = Aclshow (all = None , clear = True , rules = None , tables = None , verbose = None )
223
246
assert test .result .getvalue () == clear_output
224
247
225
248
# aclshow -a -c ; aclshow -a
249
+
250
+
226
251
def test_all_after_clear ():
227
252
nullify_on_start , nullify_on_exit = True , False
228
253
test = Aclshow (nullify_on_start , nullify_on_exit , all = True , clear = True , rules = None , tables = None , verbose = None )
0 commit comments