Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements test multi nic hotplug #5763

Merged
merged 2 commits into from
Oct 1, 2024
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
91 changes: 48 additions & 43 deletions tests/integration_tests/modules/test_hotplug.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import paramiko
import pytest
import yaml
from pycloudlib.ec2.instance import EC2Instance

from cloudinit.subp import subp
from tests.integration_tests.clouds import IntegrationCloud
Expand Down Expand Up @@ -244,62 +245,66 @@ def test_no_hotplug_in_userdata(client: IntegrationInstance):


@pytest.mark.skipif(PLATFORM != "ec2", reason="test is ec2 specific")
def test_multi_nic_hotplug(setup_image, session_cloud: IntegrationCloud):
@pytest.mark.user_data(USER_DATA)
def test_multi_nic_hotplug(client: IntegrationInstance):
"""Tests that additional secondary NICs are routable from non-local
networks after the hotplug hook is executed when network updates
are configured on the HOTPLUG event."""
with session_cloud.launch(launch_kwargs={}, user_data=USER_DATA) as client:
ips_before = _get_ip_addr(client)
secondary_priv_ip = client.instance.add_network_interface(
ipv4_public_ip_count=1,
)
_wait_till_hotplug_complete(client, expected_runs=1)
ips_before = _get_ip_addr(client)
secondary_priv_ip = client.instance.add_network_interface(
ipv4_public_ip_count=1,
)
_wait_till_hotplug_complete(client, expected_runs=1)

log_content = client.read_from_file("/var/log/cloud-init.log")
verify_clean_log(log_content)
verify_clean_boot(client)
log_content = client.read_from_file("/var/log/cloud-init.log")
verify_clean_log(log_content)
verify_clean_boot(client)

ips_after_add = _get_ip_addr(client)
ips_after_add = _get_ip_addr(client)

netplan_cfg = client.read_from_file("/etc/netplan/50-cloud-init.yaml")
config = yaml.safe_load(netplan_cfg)
new_addition = [
ip for ip in ips_after_add if ip.ip4 == secondary_priv_ip
][0]
assert new_addition.interface in config["network"]["ethernets"]
new_nic_cfg = config["network"]["ethernets"][new_addition.interface]
assert [{"from": secondary_priv_ip, "table": 101}] == new_nic_cfg[
"routing-policy"
]
netplan_cfg = client.read_from_file("/etc/netplan/50-cloud-init.yaml")
config = yaml.safe_load(netplan_cfg)
new_addition = [ip for ip in ips_after_add if ip.ip4 == secondary_priv_ip][
0
]
assert new_addition.interface in config["network"]["ethernets"]
new_nic_cfg = config["network"]["ethernets"][new_addition.interface]
assert [{"from": secondary_priv_ip, "table": 101}] == new_nic_cfg[
"routing-policy"
]

assert len(ips_after_add) == len(ips_before) + 1
public_ips = client.instance.public_ips
assert len(public_ips) == 2
assert len(ips_after_add) == len(ips_before) + 1

# SSH over all public ips works
for pub_ip in public_ips:
subp("nc -w 5 -zv " + pub_ip + " 22", shell=True)
# help mypy realize client.instance is an instance of EC2Instance as
# public_ips is only available on ec2 instances
assert isinstance(client.instance, EC2Instance)
public_ips = client.instance.public_ips
assert len(public_ips) == 2

# Remove new NIC
client.instance.remove_network_interface(secondary_priv_ip)
_wait_till_hotplug_complete(client, expected_runs=2)
# SSH over all public ips works
for pub_ip in public_ips:
subp("nc -w 10 -zv " + pub_ip + " 22", shell=True)

public_ips = client.instance.public_ips
assert len(public_ips) == 1
# SSH over primary NIC works
subp("nc -w 1 -zv " + public_ips[0] + " 22", shell=True)
# Remove new NIC
client.instance.remove_network_interface(secondary_priv_ip)
_wait_till_hotplug_complete(client, expected_runs=2)

ips_after_remove = _get_ip_addr(client)
assert len(ips_after_remove) == len(ips_before)
assert secondary_priv_ip not in [ip.ip4 for ip in ips_after_remove]
public_ips = client.instance.public_ips
assert len(public_ips) == 1
# SSH over primary NIC works
subp("nc -w 10 -zv " + public_ips[0] + " 22", shell=True)

netplan_cfg = client.read_from_file("/etc/netplan/50-cloud-init.yaml")
config = yaml.safe_load(netplan_cfg)
assert new_addition.interface not in config["network"]["ethernets"]
ips_after_remove = _get_ip_addr(client)
assert len(ips_after_remove) == len(ips_before)
assert secondary_priv_ip not in [ip.ip4 for ip in ips_after_remove]

log_content = client.read_from_file("/var/log/cloud-init.log")
verify_clean_log(log_content)
verify_clean_boot(client)
netplan_cfg = client.read_from_file("/etc/netplan/50-cloud-init.yaml")
config = yaml.safe_load(netplan_cfg)
assert new_addition.interface not in config["network"]["ethernets"]

log_content = client.read_from_file("/var/log/cloud-init.log")
verify_clean_log(log_content)
verify_clean_boot(client)


@pytest.mark.skipif(PLATFORM != "ec2", reason="test is ec2 specific")
Expand Down