Skip to content

Commit 53a2c07

Browse files
test: Add error handling in e2e_cloud_firewall fixture (#431)
* Add some safety to e2e_test_firewall fxiture for ConnectionError * delete comment * Update test/integration/conftest.py Co-authored-by: Zhiwei Liang <[email protected]> --------- Co-authored-by: Zhiwei Liang <[email protected]>
1 parent 8fe7c51 commit 53a2c07

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

test/integration/conftest.py

+23-7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import pytest
88
import requests
9+
from requests.exceptions import ConnectionError, RequestException
910

1011
from linode_api4 import ApiError, PlacementGroupAffinityType
1112
from linode_api4.linode_client import LinodeClient
@@ -68,14 +69,22 @@ def is_valid_ipv6(address):
6869
except ipaddress.AddressValueError:
6970
return False
7071

71-
def get_public_ip(ip_version="ipv4"):
72+
def get_public_ip(ip_version: str = "ipv4", retries: int = 3):
7273
url = (
7374
f"https://api64.ipify.org?format=json"
7475
if ip_version == "ipv6"
7576
else f"https://api.ipify.org?format=json"
7677
)
77-
response = requests.get(url)
78-
return str(response.json()["ip"])
78+
for attempt in range(retries):
79+
try:
80+
response = requests.get(url)
81+
response.raise_for_status()
82+
return str(response.json()["ip"])
83+
except (RequestException, ConnectionError) as e:
84+
if attempt < retries - 1:
85+
time.sleep(2) # Wait before retrying
86+
else:
87+
raise e
7988

8089
def create_inbound_rule(ipv4_address, ipv6_address):
8190
rule = [
@@ -94,12 +103,19 @@ def create_inbound_rule(ipv4_address, ipv6_address):
94103

95104
return rule
96105

97-
# Fetch the public IP addresses
106+
try:
107+
ipv4_address = get_public_ip("ipv4")
108+
except (RequestException, ConnectionError, ValueError, KeyError):
109+
ipv4_address = None
98110

99-
ipv4_address = get_public_ip("ipv4")
100-
ipv6_address = get_public_ip("ipv6")
111+
try:
112+
ipv6_address = get_public_ip("ipv6")
113+
except (RequestException, ConnectionError, ValueError, KeyError):
114+
ipv6_address = None
101115

102-
inbound_rule = create_inbound_rule(ipv4_address, ipv6_address)
116+
inbound_rule = []
117+
if ipv4_address or ipv6_address:
118+
inbound_rule = create_inbound_rule(ipv4_address, ipv6_address)
103119

104120
client = test_linode_client
105121

0 commit comments

Comments
 (0)