1
+ import sys
2
+ import os
3
+ from StringIO import StringIO
4
+ import mock
5
+
6
+ test_path = os .path .dirname (os .path .abspath (__file__ ))
7
+ modules_path = os .path .dirname (test_path )
8
+ scripts_path = os .path .join (modules_path , "scripts" )
9
+ sys .path .insert (0 , modules_path )
10
+
11
+ from imp import load_source
12
+ load_source ('aclshow' , scripts_path + '/aclshow' )
13
+ from aclshow import *
14
+
15
+ import mock_tables .dbconnector
16
+
17
+ # Expected output for aclshow
18
+ default_output = '' + \
19
+ """RULE NAME TABLE NAME PRIO PACKETS COUNT BYTES COUNT
20
+ ------------ ------------ ------ --------------- -------------
21
+ RULE_1 DATAACL 9999 101 100
22
+ RULE_2 DATAACL 9998 201 200
23
+ RULE_3 DATAACL 9997 301 300
24
+ RULE_4 DATAACL 9996 401 400
25
+ RULE_7 DATAACL 9993 701 700
26
+ RULE_9 DATAACL 9991 901 900
27
+ DEFAULT_RULE DATAACL 1 2 1
28
+ RULE_6 EVERFLOW 9994 601 600
29
+ """
30
+
31
+ # Expected output for aclshow -a
32
+ all_output = '' + \
33
+ """RULE NAME TABLE NAME PRIO PACKETS COUNT BYTES COUNT
34
+ ------------ ------------ ------ --------------- -------------
35
+ RULE_1 DATAACL 9999 101 100
36
+ RULE_2 DATAACL 9998 201 200
37
+ RULE_3 DATAACL 9997 301 300
38
+ RULE_4 DATAACL 9996 401 400
39
+ RULE_05 DATAACL 9995 0 0
40
+ RULE_7 DATAACL 9993 701 700
41
+ RULE_9 DATAACL 9991 901 900
42
+ DEFAULT_RULE DATAACL 1 2 1
43
+ RULE_6 EVERFLOW 9994 601 600
44
+ RULE_08 EVERFLOW 9992 0 0
45
+ """
46
+
47
+ # Expected output for aclshow -r RULE_1 -t DATAACL
48
+ rule1_dataacl_output = '' + \
49
+ """RULE NAME TABLE NAME PRIO PACKETS COUNT BYTES COUNT
50
+ ----------- ------------ ------ --------------- -------------
51
+ RULE_1 DATAACL 9999 101 100
52
+ """
53
+
54
+ # Expected output for aclshow -a -r RULE_05
55
+ rule05_all_output = '' + \
56
+ """RULE NAME TABLE NAME PRIO PACKETS COUNT BYTES COUNT
57
+ ----------- ------------ ------ --------------- -------------
58
+ RULE_05 DATAACL 9995 0 0
59
+ """
60
+
61
+ # Expected output for aclshow -r RULE_0
62
+ rule0_output = '' + \
63
+ """RULE NAME TABLE NAME PRIO PACKETS COUNT BYTES COUNT
64
+ ----------- ------------ ------ --------------- -------------
65
+ """
66
+
67
+ # Expected output for aclshow -r RULE_4,RULE_6 -vv
68
+ rule4_rule6_verbose_output = '' + \
69
+ """Reading ACL info...
70
+ Total number of ACL Tables: 4
71
+ Total number of ACL Rules: 10
72
+
73
+ RULE NAME TABLE NAME PRIO PACKETS COUNT BYTES COUNT
74
+ ----------- ------------ ------ --------------- -------------
75
+ RULE_4 DATAACL 9996 401 400
76
+ RULE_6 EVERFLOW 9994 601 600
77
+ """
78
+
79
+ # Expected output for aclshow -t EVERFLOW
80
+ everflow_output = '' + \
81
+ """RULE NAME TABLE NAME PRIO PACKETS COUNT BYTES COUNT
82
+ ----------- ------------ ------ --------------- -------------
83
+ RULE_6 EVERFLOW 9994 601 600
84
+ """
85
+
86
+ # Expected output for aclshow -t DATAACL
87
+ dataacl_output = '' + \
88
+ """RULE NAME TABLE NAME PRIO PACKETS COUNT BYTES COUNT
89
+ ------------ ------------ ------ --------------- -------------
90
+ RULE_1 DATAACL 9999 101 100
91
+ RULE_2 DATAACL 9998 201 200
92
+ RULE_3 DATAACL 9997 301 300
93
+ RULE_4 DATAACL 9996 401 400
94
+ RULE_7 DATAACL 9993 701 700
95
+ RULE_9 DATAACL 9991 901 900
96
+ DEFAULT_RULE DATAACL 1 2 1
97
+ """
98
+
99
+ # Expected output for aclshow -c
100
+ clear_output = ''
101
+
102
+ # Expected output for
103
+ # aclshow -a -c ; aclshow -a
104
+ all_after_clear_output = '' + \
105
+ """RULE NAME TABLE NAME PRIO PACKETS COUNT BYTES COUNT
106
+ ------------ ------------ ------ --------------- -------------
107
+ RULE_1 DATAACL 9999 0 0
108
+ RULE_2 DATAACL 9998 0 0
109
+ RULE_3 DATAACL 9997 0 0
110
+ RULE_4 DATAACL 9996 0 0
111
+ RULE_05 DATAACL 9995 0 0
112
+ RULE_7 DATAACL 9993 0 0
113
+ RULE_9 DATAACL 9991 0 0
114
+ DEFAULT_RULE DATAACL 1 0 0
115
+ RULE_6 EVERFLOW 9994 0 0
116
+ RULE_08 EVERFLOW 9992 0 0
117
+ """
118
+
119
+ class Aclshow ():
120
+ def __init__ (self , * args , ** kwargs ):
121
+ """
122
+ nullify_on_start, nullify_on_exit will call nullify_counters()
123
+ before and/or after the test. By default - clear on start and exit.
124
+ """
125
+ self .nullify_on_start , self .nullify_on_exit = args if args else (True , True )
126
+ self .kwargs = kwargs
127
+ self .setUp ()
128
+ self .runTest ()
129
+ self .tearDown ()
130
+
131
+ def nullify_counters (self ):
132
+ """
133
+ This method is used to empty dumped counters
134
+ if exist in /tmp/.counters_acl.p (by default).
135
+ """
136
+ if os .path .isfile (COUNTER_POSITION ):
137
+ with open (COUNTER_POSITION , 'wb' ) as fp :
138
+ json .dump ([], fp )
139
+
140
+ def runTest (self ):
141
+ """
142
+ This method invokes main() from aclshow utility (parametrized by argparse)
143
+ parametrized by mock argparse.
144
+ """
145
+ @mock .patch ('argparse.ArgumentParser.parse_args' , return_value = argparse .Namespace (** self .kwargs ))
146
+ def run (mock_args ):
147
+ main ()
148
+ run ()
149
+
150
+ def setUp (self ):
151
+ if self .nullify_on_start :
152
+ self .nullify_counters ()
153
+ self .old_stdout = sys .stdout
154
+ self .result = StringIO ()
155
+ sys .stdout = self .result
156
+
157
+ def tearDown (self ):
158
+ if self .nullify_on_exit :
159
+ self .nullify_counters ()
160
+ sys .stdout = self .old_stdout
161
+
162
+ # aclshow
163
+ def test_default ():
164
+ test = Aclshow (all = None , clear = None , rules = None , tables = None , verbose = None )
165
+ assert test .result .getvalue () == default_output
166
+
167
+ # aclshow -a
168
+ def test_all ():
169
+ test = Aclshow (all = True , clear = None , rules = None , tables = None , verbose = None )
170
+ assert test .result .getvalue () == all_output
171
+
172
+ # aclshow -r RULE_1 -t DATAACL
173
+ def test_rule1_dataacl ():
174
+ test = Aclshow (all = None , clear = None , rules = 'RULE_1' , tables = 'DATAACL' , verbose = None )
175
+ assert test .result .getvalue () == rule1_dataacl_output
176
+
177
+ # aclshow -a -r RULE_05
178
+ def test_rule05_all ():
179
+ test = Aclshow (all = True , clear = None , rules = 'RULE_05' , tables = None , verbose = None )
180
+ assert test .result .getvalue () == rule05_all_output
181
+
182
+ # aclshow -r RULE_0
183
+ def test_rule0 ():
184
+ test = Aclshow (all = None , clear = None , rules = 'RULE_0' , tables = None , verbose = None )
185
+ assert test .result .getvalue () == rule0_output
186
+
187
+ # aclshow -r RULE_4,RULE_6 -vv
188
+ def test_rule4_rule6_verbose ():
189
+ test = Aclshow (all = None , clear = None , rules = 'RULE_4,RULE_6' , tables = None , verbose = True )
190
+ assert test .result .getvalue () == rule4_rule6_verbose_output
191
+
192
+ # aclshow -t EVERFLOW
193
+ def test_everflow ():
194
+ test = Aclshow (all = None , clear = None , rules = None , tables = 'EVERFLOW' , verbose = None )
195
+ assert test .result .getvalue () == everflow_output
196
+
197
+ # aclshow -t DATAACL
198
+ def test_dataacl ():
199
+ test = Aclshow (all = None , clear = None , rules = None , tables = 'DATAACL' , verbose = None )
200
+ assert test .result .getvalue () == dataacl_output
201
+
202
+ # aclshow -c
203
+ def test_clear ():
204
+ test = Aclshow (all = None , clear = True , rules = None , tables = None , verbose = None )
205
+ assert test .result .getvalue () == clear_output
206
+
207
+ # aclshow -a -c ; aclshow -a
208
+ def test_all_after_clear ():
209
+ nullify_on_start , nullify_on_exit = True , False
210
+ test = Aclshow (nullify_on_start , nullify_on_exit , all = True , clear = True , rules = None , tables = None , verbose = None )
211
+ assert test .result .getvalue () == clear_output
212
+ nullify_on_start , nullify_on_exit = False , True
213
+ test = Aclshow (nullify_on_start , nullify_on_exit , all = True , clear = False , rules = None , tables = None , verbose = None )
214
+ assert test .result .getvalue () == all_after_clear_output
0 commit comments