1
+ import json
1
2
import re
2
3
import time
3
4
@@ -42,7 +43,7 @@ def test_add_rule_to_existing_firewall(create_firewall):
42
43
firewall_id = create_firewall
43
44
inbound_rule = '[{"ports": "22", "protocol": "TCP", "addresses": {"ipv4": ["198.0.0.1/32"]}, "action": "ACCEPT", "label": "accept-inbound-SSH"}]'
44
45
outbound_rule = '[{"ports": "22", "protocol": "TCP", "addresses": {"ipv4": ["198.0.0.2/32"]}, "action": "ACCEPT", "label": "accept-outbound-SSH"}]'
45
- result = (
46
+ result = json . loads (
46
47
exec_test_command (
47
48
BASE_CMD
48
49
+ [
@@ -51,50 +52,32 @@ def test_add_rule_to_existing_firewall(create_firewall):
51
52
inbound_rule ,
52
53
"--outbound" ,
53
54
outbound_rule ,
54
- "--text" ,
55
- "--no-headers" ,
56
- "--delimiter" ,
57
- "," ,
55
+ "--json" ,
58
56
]
59
57
)
60
58
.stdout .decode ()
61
59
.rstrip ()
62
60
)
63
61
64
- # search strings for assertion since output replaces all the double quotes in json with single quote
65
- ir_str = inbound_rule [1 :- 1 ].replace ('"' , "'" )
66
- or_str = outbound_rule [1 :- 1 ].replace ('"' , "'" )
67
-
68
- assert ir_str in result
69
- assert or_str in result
62
+ assert result [0 ]["inbound" ][0 ] == json .loads (inbound_rule )[0 ]
63
+ assert result [0 ]["outbound" ][0 ] == json .loads (outbound_rule )[0 ]
70
64
71
65
72
66
def test_add_multiple_rules (create_firewall ):
73
67
firewall_id = create_firewall
74
68
inbound_rule_1 = '{"ports": "22", "protocol": "TCP", "addresses": {"ipv4": ["198.0.0.1/32"]}, "action": "ACCEPT", "label": "accept-inbound-SSH"}'
75
69
inbound_rule_2 = '{"ports": "22", "protocol": "TCP", "addresses": {"ipv4": ["198.0.0.2/32"]}, "action": "ACCEPT", "label": "accept-inbound-SSH-2"}'
76
70
inbound_rules = "[" + inbound_rule_1 + "," + inbound_rule_2 + "]"
77
- result = (
71
+ result = json . loads (
78
72
exec_test_command (
79
- BASE_CMD
80
- + [
81
- firewall_id ,
82
- "--inbound" ,
83
- inbound_rules ,
84
- "--text" ,
85
- "--no-headers" ,
86
- ]
73
+ BASE_CMD + [firewall_id , "--inbound" , inbound_rules , "--json" ]
87
74
)
88
75
.stdout .decode ()
89
76
.rstrip ()
90
77
)
91
78
92
- assert (
93
- inbound_rule_1 .replace ('"' , "'" )
94
- + " "
95
- + inbound_rule_2 .replace ('"' , "'" )
96
- in result
97
- )
79
+ assert result [0 ]["inbound" ][0 ] == json .loads (inbound_rule_1 )
80
+ assert result [0 ]["inbound" ][1 ] == json .loads (inbound_rule_2 )
98
81
99
82
100
83
def test_swap_rules ():
@@ -130,27 +113,22 @@ def test_swap_rules():
130
113
swapped_rules = "[" + inbound_rule_2 + "," + inbound_rule_1 + "]"
131
114
132
115
# swapping rules
133
- result = (
116
+ result = json . loads (
134
117
exec_test_command (
135
118
BASE_CMD
136
119
+ [
137
120
firewall_id ,
138
121
"--inbound" ,
139
122
swapped_rules ,
140
- "--text" ,
141
- "--no-headers" ,
123
+ "--json" ,
142
124
]
143
125
)
144
126
.stdout .decode ()
145
127
.rstrip ()
146
128
)
147
129
148
- assert (
149
- inbound_rule_2 .replace ('"' , "'" )
150
- + " "
151
- + inbound_rule_1 .replace ('"' , "'" )
152
- in result
153
- )
130
+ assert result [0 ]["inbound" ][0 ] == json .loads (inbound_rule_2 )
131
+ assert result [0 ]["inbound" ][1 ] == json .loads (inbound_rule_1 )
154
132
155
133
delete_target_id (target = "firewalls" , id = firewall_id )
156
134
@@ -213,17 +191,18 @@ def test_remove_one_rule_via_rules_update():
213
191
214
192
new_rule = "[" + inbound_rule_1 + "]"
215
193
# swapping rules
216
- result = (
194
+ result = json . loads (
217
195
exec_test_command (
218
196
BASE_CMD
219
- + [firewall_id , "--inbound" , new_rule , "--text " , "--no-headers" ]
197
+ + [firewall_id , "--inbound" , new_rule , "--json " , "--no-headers" ]
220
198
)
221
199
.stdout .decode ()
222
200
.rstrip ()
223
201
)
224
202
225
- assert inbound_rule_1 .replace ('"' , "'" ) in result
226
- assert inbound_rule_2 .replace ('"' , "'" ) not in result
203
+ rule_labels = [v ["label" ] for v in result [0 ]["inbound" ]]
204
+ assert "test_rule_1" in rule_labels
205
+ assert "rule_to_delete" not in rule_labels
227
206
228
207
delete_target_id (target = "firewalls" , id = firewall_id )
229
208
@@ -264,4 +243,44 @@ def test_list_rules(create_firewall):
264
243
.rstrip ()
265
244
)
266
245
267
- assert new_label .replace ('"' , "'" ) in result
246
+ assert new_label .replace ('"' , "" ) in result
247
+
248
+
249
+ def test_list_rules_json (create_firewall ):
250
+ firewall_id = create_firewall
251
+ new_label = '"rules-list-test"'
252
+ inbound_rule = (
253
+ '[{"ports": "22", "protocol": "TCP", "addresses": {"ipv4": ["198.0.0.1/32"]}, "action": "ACCEPT", "label": '
254
+ + new_label
255
+ + "}]"
256
+ )
257
+ # adding a rule
258
+ exec_test_command (
259
+ BASE_CMD
260
+ + [
261
+ firewall_id ,
262
+ "--inbound" ,
263
+ inbound_rule ,
264
+ "--text" ,
265
+ "--no-headers" ,
266
+ "--delimiter" ,
267
+ "," ,
268
+ ]
269
+ ).stdout .decode ().rstrip ()
270
+ result = json .loads (
271
+ exec_test_command (
272
+ [
273
+ "linode-cli" ,
274
+ "firewalls" ,
275
+ "rules-list" ,
276
+ firewall_id ,
277
+ "--json" ,
278
+ ]
279
+ )
280
+ .stdout .decode ()
281
+ .rstrip ()
282
+ )
283
+
284
+ assert result [0 ]["inbound" ][0 ]["action" ] == "ACCEPT"
285
+ assert result [0 ]["inbound" ][0 ]["label" ] == "rules-list-test"
286
+ assert result [0 ]["inbound" ][0 ]["addresses" ]["ipv4" ] == ["198.0.0.1/32" ]
0 commit comments