Skip to content

Commit e59ba4d

Browse files
azhivovkclaude
andcommitted
net, libs: Refactor random ip address helpers
Return CIDR strings directly from the low-level helpers. Callers that need a plain IP strip the prefix with .split("/")[0]; callers that need a non-standard mask strip and reapply it. Remove random_ip_addresses_by_family — it became identical to random_cidr_addresses_by_family after this change. Signed-off-by: Asia Khromov <azhivovk@redhat.com> Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
1 parent 6921c7f commit e59ba4d

28 files changed

Lines changed: 75 additions & 126 deletions

File tree

libs/net/ip.py

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,35 +30,16 @@ def random_cidr_addresses_by_family(net_seed: int, host_address: int) -> list[st
3030
Returns:
3131
List of CIDR strings (e.g. ["192.168.1.1/24", "fd00::1/64"]).
3232
"""
33-
return [
34-
f"{ip}/64" if ipaddress.ip_address(ip).version == 6 else f"{ip}/24"
35-
for ip in random_ip_addresses_by_family(net_seed=net_seed, host_address=host_address)
36-
]
37-
38-
39-
def random_ip_addresses_by_family(
40-
net_seed: int,
41-
host_address: int,
42-
) -> list[str]:
43-
"""Generate IP addresses for each IP family supported by the cluster network stack.
44-
45-
Args:
46-
net_seed: Seed index for selecting the random network portion of the address.
47-
host_address: Host portion of the address, used to place VMs on the same subnet.
48-
49-
Returns:
50-
List of IP address strings, one per IP family supported by the cluster.
51-
"""
52-
ips = []
33+
addresses = []
5334
if ipv4_supported_cluster():
54-
ips.append(random_ipv4_address(net_seed=net_seed, host_address=host_address))
35+
addresses.append(random_ipv4_address(net_seed=net_seed, host_address=host_address))
5536
if ipv6_supported_cluster():
56-
ips.append(random_ipv6_address(net_seed=net_seed, host_address=host_address))
57-
return ips
37+
addresses.append(random_ipv6_address(net_seed=net_seed, host_address=host_address))
38+
return addresses
5839

5940

6041
def random_ipv4_address(net_seed: int, host_address: int) -> str:
61-
"""Construct a random IPv4 address using a cached list of random third octets.
42+
"""Construct a random IPv4 CIDR address using a cached list of random third octets.
6243
6344
Uses a pre-defined network address, a cached random third octet and the given
6445
host address to generate deterministic yet randomized IPv4 addresses.
@@ -68,10 +49,10 @@ def random_ipv4_address(net_seed: int, host_address: int) -> str:
6849
host_address (int): The last (fourth) octet of the IPv4 address.
6950
7051
Returns:
71-
str: A string representing a randomized IPv4 address.
52+
str: A CIDR string representing a randomized IPv4 address.
7253
"""
7354
third_octets = _random_octets(count=_MAX_NUM_OF_RANDOM_OCTETS_PER_SESSION)
74-
return f"{_IPV4_ADDRESS_SUBNET_PREFIX_VMI}.{third_octets[net_seed]}.{host_address}"
55+
return f"{_IPV4_ADDRESS_SUBNET_PREFIX_VMI}.{third_octets[net_seed]}.{host_address}/24"
7556

7657

7758
@cache
@@ -91,7 +72,7 @@ def _random_octets(count: int) -> list[int]:
9172

9273

9374
def random_ipv6_address(net_seed: int, host_address: int) -> str:
94-
"""Construct a random IPv6 address using a cached list of random seventh hextets.
75+
"""Construct a random IPv6 CIDR address using a cached list of random seventh hextets.
9576
9677
Uses a pre-defined network prefix, a cached random seventh hextet and the given
9778
host address to generate deterministic yet randomized IPv6 addresses.
@@ -101,10 +82,10 @@ def random_ipv6_address(net_seed: int, host_address: int) -> str:
10182
host_address (int): The last (eighth) hextet of the IPv6 address.
10283
10384
Returns:
104-
str: A string representing a randomized IPv6 address.
85+
str: A CIDR string representing a randomized IPv6 address.
10586
"""
10687
seventh_hextets = _random_hextets(count=_MAX_NUM_OF_RANDOM_HEXTETS_PER_SESSION)
107-
return f"{_IPV6_ADDRESS_SUBNET_PREFIX_VMI}::{seventh_hextets[net_seed]:x}:{host_address:x}"
88+
return f"{_IPV6_ADDRESS_SUBNET_PREFIX_VMI}::{seventh_hextets[net_seed]:x}:{host_address:x}/64"
10889

10990

11091
@cache

tests/conftest.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,8 +1648,8 @@ def running_vm_upgrade_a(
16481648
"ethernets": {
16491649
"eth1": {
16501650
"addresses": [
1651-
f"{random_ipv4_address(net_seed=0, host_address=1)}/24",
1652-
f"{random_ipv6_address(net_seed=0, host_address=1)}/64",
1651+
random_ipv4_address(net_seed=0, host_address=1),
1652+
random_ipv6_address(net_seed=0, host_address=1),
16531653
]
16541654
}
16551655
}
@@ -1692,8 +1692,8 @@ def running_vm_upgrade_b(
16921692
"ethernets": {
16931693
"eth1": {
16941694
"addresses": [
1695-
f"{random_ipv4_address(net_seed=0, host_address=2)}/24",
1696-
f"{random_ipv6_address(net_seed=0, host_address=2)}/64",
1695+
random_ipv4_address(net_seed=0, host_address=2),
1696+
random_ipv6_address(net_seed=0, host_address=2),
16971697
]
16981698
}
16991699
}

tests/network/bgp/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040
BGP_DATA_PATH: Final[Path] = Path(__file__).resolve().parent / "data" / "frr-config"
4141
CUDN_BGP_LABEL: Final[dict] = {"cudn-bgp": "blue"}
4242
CUDN_SUBNET_IPV4: Final[str] = "192.168.10.0/24"
43-
EXTERNAL_PROVIDER_SUBNET_IPV4: Final[str] = f"{random_ipv4_address(net_seed=1, host_address=0)}/24"
44-
EXTERNAL_PROVIDER_IP_V4: Final[str] = f"{random_ipv4_address(net_seed=1, host_address=150)}/24"
43+
EXTERNAL_PROVIDER_SUBNET_IPV4: Final[str] = random_ipv4_address(net_seed=1, host_address=0)
44+
EXTERNAL_PROVIDER_IP_V4: Final[str] = random_ipv4_address(net_seed=1, host_address=150)
4545
IPERF3_SERVER_PORT: Final[int] = 2354
4646
LOCALNET_NETWORK_NAME: Final[str] = "localnet-network-bgp"
4747

tests/network/bgp/evpn/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
EVPN_ADVERTISE_LABEL: Final[dict] = {"advertise": "evpn"}
4242
APP_EVPN_CUDN_LABEL: Final[dict] = {**EVPN_ADVERTISE_LABEL, "app": "cudn-evpn"}
4343
CUDN_EVPN_BGP_LABEL: Final[dict] = {"cudn-bgp": "evpn"}
44-
EXTERNAL_L2_ENDPOINT_IPV4: Final[str] = f"{random_ipv4_address(net_seed=5, host_address=250)}/24"
45-
EXTERNAL_L2_ENDPOINT_IPV6: Final[str] = f"{random_ipv6_address(net_seed=5, host_address=250)}/64"
44+
EXTERNAL_L2_ENDPOINT_IPV4: Final[str] = random_ipv4_address(net_seed=5, host_address=250)
45+
EXTERNAL_L2_ENDPOINT_IPV6: Final[str] = random_ipv6_address(net_seed=5, host_address=250)
4646
EXTERNAL_L3_ENDPOINT_IPV4: Final[str] = "192.168.100.100/24"
4747
EXTERNAL_L3_ENDPOINT_IPV6: Final[str] = "fd01:1234:5678::64/64"
4848
EXTERNAL_L3_GATEWAY_IPV4: Final[str] = "192.168.100.1/24"

tests/network/bgp/evpn/libevpn.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525
LOGGER = logging.getLogger(__name__)
2626

27-
CUDN_EVPN_SUBNET_IPV4: str = f"{random_ipv4_address(net_seed=5, host_address=0)}/24"
28-
CUDN_EVPN_SUBNET_IPV6: str = f"{random_ipv6_address(net_seed=5, host_address=0)}/64"
27+
CUDN_EVPN_SUBNET_IPV4: str = random_ipv4_address(net_seed=5, host_address=0)
28+
CUDN_EVPN_SUBNET_IPV6: str = random_ipv6_address(net_seed=5, host_address=0)
2929

3030
_BRIDGE_NAME: str = "br0"
3131
_VXLAN_NAME: str = "vxlan0"

tests/network/bond/test_l2_bridge_over_bond.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def ovs_linux_bond_bridge_attached_vma(
142142
networks = OrderedDict()
143143
networks[ovs_linux_br1bond_nad.name] = ovs_linux_br1bond_nad.name
144144
netdata = netcloud.NetworkData(
145-
ethernets={"eth1": netcloud.EthernetDevice(addresses=[f"{random_ipv4_address(net_seed=0, host_address=1)}/24"])}
145+
ethernets={"eth1": netcloud.EthernetDevice(addresses=[random_ipv4_address(net_seed=0, host_address=1)])}
146146
)
147147

148148
with VirtualMachineForTests(
@@ -170,9 +170,7 @@ def ovs_linux_bond_bridge_attached_vmb(
170170
name = "bond-vmb"
171171
networks = OrderedDict()
172172
networks[ovs_linux_br1bond_nad.name] = ovs_linux_br1bond_nad.name
173-
network_data_data = {
174-
"ethernets": {"eth1": {"addresses": [f"{random_ipv4_address(net_seed=0, host_address=2)}/24"]}}
175-
}
173+
network_data_data = {"ethernets": {"eth1": {"addresses": [random_ipv4_address(net_seed=0, host_address=2)]}}}
176174
cloud_init_data = cloud_init_network_data(data=network_data_data)
177175

178176
with VirtualMachineForTests(

tests/network/connectivity/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ def secondary_interfaces_cloud_init_data(
4141
interface_name = f"eth{i + 1}"
4242
addresses = []
4343
if ipv4_supported_cluster():
44-
addresses.append(f"{random_ipv4_address(net_seed=i, host_address=host_id)}/24")
44+
addresses.append(random_ipv4_address(net_seed=i, host_address=host_id))
4545
if ipv6_supported_cluster():
46-
addresses.append(f"{random_ipv6_address(net_seed=i, host_address=host_id)}/64")
46+
addresses.append(random_ipv6_address(net_seed=i, host_address=host_id))
4747

4848
ethernets[interface_name] = {"addresses": addresses}
4949

tests/network/flat_overlay/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ def vmb_ingress_multi_network_policy(
322322
network_name=flat_overlay_vma_vmb_nad.name,
323323
policy_types=["Ingress"],
324324
ingress=create_ip_block(
325-
ip_address=f"{random_ipv4_address(net_seed=0, host_address=123)}/{SPECIFIC_HOST_MASK}",
325+
ip_address=f"{random_ipv4_address(net_seed=0, host_address=123).split('/')[0]}/{SPECIFIC_HOST_MASK}",
326326
),
327327
client=admin_client,
328328
) as mnp:

tests/network/flat_overlay/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def create_flat_overlay_vm(
3434
networks = {nad_name: nad_name}
3535
network_data = {
3636
"ethernets": {
37-
"eth1": {"addresses": [f"{random_ipv4_address(net_seed=0, host_address=host_ip_suffix)}/24"]},
37+
"eth1": {"addresses": [random_ipv4_address(net_seed=0, host_address=host_ip_suffix)]},
3838
}
3939
}
4040
cloud_init_data = compose_cloud_init_data_dict(network_data=network_data)

tests/network/general/test_cnv_tuning_regression.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ def linux_bridge_device(nmstate_dependent_placeholder, admin_client, worker_node
3636
def cnv_tuning_vm(unprivileged_client, worker_node1, linux_bridge_nad, linux_bridge_device):
3737
name = "tuning-vma"
3838
networks = {"net1": linux_bridge_nad.name}
39-
network_data_data = {
40-
"ethernets": {"eth1": {"addresses": [f"{random_ipv4_address(net_seed=0, host_address=1)}/24"]}}
41-
}
39+
network_data_data = {"ethernets": {"eth1": {"addresses": [random_ipv4_address(net_seed=0, host_address=1)]}}}
4240

4341
with VirtualMachineForTests(
4442
namespace=linux_bridge_nad.namespace,

0 commit comments

Comments
 (0)