1+ import json
12import re
23import time
34
@@ -42,7 +43,7 @@ def test_add_rule_to_existing_firewall(create_firewall):
4243 firewall_id = create_firewall
4344 inbound_rule = '[{"ports": "22", "protocol": "TCP", "addresses": {"ipv4": ["198.0.0.1/32"]}, "action": "ACCEPT", "label": "accept-inbound-SSH"}]'
4445 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 (
4647 exec_test_command (
4748 BASE_CMD
4849 + [
@@ -51,50 +52,32 @@ def test_add_rule_to_existing_firewall(create_firewall):
5152 inbound_rule ,
5253 "--outbound" ,
5354 outbound_rule ,
54- "--text" ,
55- "--no-headers" ,
56- "--delimiter" ,
57- "," ,
55+ "--json" ,
5856 ]
5957 )
6058 .stdout .decode ()
6159 .rstrip ()
6260 )
6361
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 ]
7064
7165
7266def test_add_multiple_rules (create_firewall ):
7367 firewall_id = create_firewall
7468 inbound_rule_1 = '{"ports": "22", "protocol": "TCP", "addresses": {"ipv4": ["198.0.0.1/32"]}, "action": "ACCEPT", "label": "accept-inbound-SSH"}'
7569 inbound_rule_2 = '{"ports": "22", "protocol": "TCP", "addresses": {"ipv4": ["198.0.0.2/32"]}, "action": "ACCEPT", "label": "accept-inbound-SSH-2"}'
7670 inbound_rules = "[" + inbound_rule_1 + "," + inbound_rule_2 + "]"
77- result = (
71+ result = json . loads (
7872 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" ]
8774 )
8875 .stdout .decode ()
8976 .rstrip ()
9077 )
9178
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 )
9881
9982
10083def test_swap_rules ():
@@ -130,27 +113,22 @@ def test_swap_rules():
130113 swapped_rules = "[" + inbound_rule_2 + "," + inbound_rule_1 + "]"
131114
132115 # swapping rules
133- result = (
116+ result = json . loads (
134117 exec_test_command (
135118 BASE_CMD
136119 + [
137120 firewall_id ,
138121 "--inbound" ,
139122 swapped_rules ,
140- "--text" ,
141- "--no-headers" ,
123+ "--json" ,
142124 ]
143125 )
144126 .stdout .decode ()
145127 .rstrip ()
146128 )
147129
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 )
154132
155133 delete_target_id (target = "firewalls" , id = firewall_id )
156134
@@ -213,17 +191,18 @@ def test_remove_one_rule_via_rules_update():
213191
214192 new_rule = "[" + inbound_rule_1 + "]"
215193 # swapping rules
216- result = (
194+ result = json . loads (
217195 exec_test_command (
218196 BASE_CMD
219- + [firewall_id , "--inbound" , new_rule , "--text " , "--no-headers" ]
197+ + [firewall_id , "--inbound" , new_rule , "--json " , "--no-headers" ]
220198 )
221199 .stdout .decode ()
222200 .rstrip ()
223201 )
224202
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
227206
228207 delete_target_id (target = "firewalls" , id = firewall_id )
229208
@@ -264,4 +243,44 @@ def test_list_rules(create_firewall):
264243 .rstrip ()
265244 )
266245
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