1+ import ipaddress
12import os
23import random
34import time
45from typing import Set
56
67import pytest
8+ import requests
79
810from linode_api4 import ApiError
911from linode_api4 .linode_client import LinodeClient
@@ -50,16 +52,90 @@ def run_long_tests():
5052 return os .environ .get (RUN_LONG_TESTS , None )
5153
5254
55+ @pytest .fixture (autouse = True , scope = "session" )
56+ def e2e_test_firewall (test_linode_client ):
57+ def is_valid_ipv4 (address ):
58+ try :
59+ ipaddress .IPv4Address (address )
60+ return True
61+ except ipaddress .AddressValueError :
62+ return False
63+
64+ def is_valid_ipv6 (address ):
65+ try :
66+ ipaddress .IPv6Address (address )
67+ return True
68+ except ipaddress .AddressValueError :
69+ return False
70+
71+ def get_public_ip (ip_version = "ipv4" ):
72+ url = (
73+ f"https://api64.ipify.org?format=json"
74+ if ip_version == "ipv6"
75+ else f"https://api.ipify.org?format=json"
76+ )
77+ response = requests .get (url )
78+ return str (response .json ()["ip" ])
79+
80+ def create_inbound_rule (ipv4_address , ipv6_address ):
81+ rule = [
82+ {
83+ "protocol" : "TCP" ,
84+ "ports" : "22" ,
85+ "addresses" : {},
86+ "action" : "ACCEPT" ,
87+ }
88+ ]
89+ if is_valid_ipv4 (ipv4_address ):
90+ rule [0 ]["addresses" ]["ipv4" ] = [f"{ ipv4_address } /32" ]
91+
92+ if is_valid_ipv6 (ipv6_address ):
93+ rule [0 ]["addresses" ]["ipv6" ] = [f"{ ipv6_address } /128" ]
94+
95+ return rule
96+
97+ # Fetch the public IP addresses
98+
99+ ipv4_address = get_public_ip ("ipv4" )
100+ ipv6_address = get_public_ip ("ipv6" )
101+
102+ inbound_rule = create_inbound_rule (ipv4_address , ipv6_address )
103+
104+ client = test_linode_client
105+
106+ rules = {
107+ "outbound" : [],
108+ "outbound_policy" : "ACCEPT" ,
109+ "inbound" : inbound_rule ,
110+ "inbound_policy" : "DROP" ,
111+ }
112+
113+ label = "cloud_firewall_" + str (int (time .time ()))
114+
115+ firewall = client .networking .firewall_create (
116+ label = label , rules = rules , status = "enabled"
117+ )
118+
119+ yield firewall
120+
121+ firewall .delete ()
122+
123+
53124@pytest .fixture (scope = "session" )
54- def create_linode (test_linode_client ):
125+ def create_linode (test_linode_client , e2e_test_firewall ):
55126 client = test_linode_client
127+
56128 available_regions = client .regions ()
57129 chosen_region = available_regions [4 ]
58130 timestamp = str (time .time_ns ())
59131 label = "TestSDK-" + timestamp
60132
61133 linode_instance , password = client .linode .instance_create (
62- "g6-nanode-1" , chosen_region , image = "linode/debian10" , label = label
134+ "g6-nanode-1" ,
135+ chosen_region ,
136+ image = "linode/debian12" ,
137+ label = label ,
138+ firewall = e2e_test_firewall ,
63139 )
64140
65141 yield linode_instance
@@ -68,15 +144,20 @@ def create_linode(test_linode_client):
68144
69145
70146@pytest .fixture
71- def create_linode_for_pass_reset (test_linode_client ):
147+ def create_linode_for_pass_reset (test_linode_client , e2e_test_firewall ):
72148 client = test_linode_client
149+
73150 available_regions = client .regions ()
74151 chosen_region = available_regions [4 ]
75152 timestamp = str (time .time_ns ())
76153 label = "TestSDK-" + timestamp
77154
78155 linode_instance , password = client .linode .instance_create (
79- "g6-nanode-1" , chosen_region , image = "linode/debian10" , label = label
156+ "g6-nanode-1" ,
157+ chosen_region ,
158+ image = "linode/debian10" ,
159+ label = label ,
160+ firewall = e2e_test_firewall ,
80161 )
81162
82163 yield linode_instance , password
@@ -303,15 +384,19 @@ def create_vpc_with_subnet(test_linode_client, create_vpc):
303384
304385@pytest .fixture (scope = "session" )
305386def create_vpc_with_subnet_and_linode (
306- test_linode_client , create_vpc_with_subnet
387+ test_linode_client , create_vpc_with_subnet , e2e_test_firewall
307388):
308389 vpc , subnet = create_vpc_with_subnet
309390
310391 timestamp = str (int (time .time ()))
311392 label = "TestSDK-" + timestamp
312393
313394 instance , password = test_linode_client .linode .instance_create (
314- "g6-standard-1" , vpc .region , image = "linode/debian11" , label = label
395+ "g6-standard-1" ,
396+ vpc .region ,
397+ image = "linode/debian11" ,
398+ label = label ,
399+ firewall = e2e_test_firewall ,
315400 )
316401
317402 yield vpc , subnet , instance , password
0 commit comments