Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions test/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import pytest
import requests
from requests.exceptions import ConnectionError, RequestException

from linode_api4 import ApiError, PlacementGroupAffinityType
from linode_api4.linode_client import LinodeClient
Expand Down Expand Up @@ -68,14 +69,22 @@ def is_valid_ipv6(address):
except ipaddress.AddressValueError:
return False

def get_public_ip(ip_version="ipv4"):
def get_public_ip(ip_version: str = "ipv4", retries: int = 3):
url = (
f"https://api64.ipify.org?format=json"
if ip_version == "ipv6"
else f"https://api.ipify.org?format=json"
)
response = requests.get(url)
return str(response.json()["ip"])
for attempt in range(retries):
try:
response = requests.get(url)
response.raise_for_status()
return str(response.json()["ip"])
except (RequestException, ConnectionError) as e:
if attempt < retries - 1:
time.sleep(2) # Wait before retrying
else:
raise e

def create_inbound_rule(ipv4_address, ipv6_address):
rule = [
Expand All @@ -94,12 +103,19 @@ def create_inbound_rule(ipv4_address, ipv6_address):

return rule

# Fetch the public IP addresses
try:
ipv4_address = get_public_ip("ipv4")
except (RequestException, ConnectionError, ValueError, KeyError):
ipv4_address = None

ipv4_address = get_public_ip("ipv4")
ipv6_address = get_public_ip("ipv6")
try:
ipv6_address = get_public_ip("ipv6")
except (RequestException, ConnectionError, ValueError, KeyError):
ipv6_address = None

inbound_rule = create_inbound_rule(ipv4_address, ipv6_address)
inbound_rule = []
if ipv4_address or ipv6_address:
inbound_rule = create_inbound_rule(ipv4_address, ipv6_address)

client = test_linode_client

Expand Down