From 14e6a6f85aa1b20203ab1e5b300b53323a37f9db Mon Sep 17 00:00:00 2001 From: Alberto Contreras Date: Tue, 1 Oct 2024 14:05:34 +0200 Subject: [PATCH 1/2] test(hotplug): increase nc timeout (#5763) Increase timeout check in netcat command for test_multi_nic_hotplug, as there are instances where the 5s timeout is not enough. --- tests/integration_tests/modules/test_hotplug.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/integration_tests/modules/test_hotplug.py b/tests/integration_tests/modules/test_hotplug.py index be4613313d5..1e0947b100e 100644 --- a/tests/integration_tests/modules/test_hotplug.py +++ b/tests/integration_tests/modules/test_hotplug.py @@ -278,7 +278,7 @@ def test_multi_nic_hotplug(setup_image, session_cloud: IntegrationCloud): # SSH over all public ips works for pub_ip in public_ips: - subp("nc -w 5 -zv " + pub_ip + " 22", shell=True) + subp("nc -w 10 -zv " + pub_ip + " 22", shell=True) # Remove new NIC client.instance.remove_network_interface(secondary_priv_ip) @@ -287,7 +287,7 @@ def test_multi_nic_hotplug(setup_image, session_cloud: IntegrationCloud): 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) + subp("nc -w 10 -zv " + public_ips[0] + " 22", shell=True) ips_after_remove = _get_ip_addr(client) assert len(ips_after_remove) == len(ips_before) From 8e48c238cdf309bf884f186391e275777d737c5d Mon Sep 17 00:00:00 2001 From: Alberto Contreras Date: Tue, 1 Oct 2024 14:09:28 +0200 Subject: [PATCH 2/2] test(hotplug): Simplify test_multi_nic_hotplug (#5763) No functional test, just noop simplification of the test code. --- .../integration_tests/modules/test_hotplug.py | 91 ++++++++++--------- 1 file changed, 48 insertions(+), 43 deletions(-) diff --git a/tests/integration_tests/modules/test_hotplug.py b/tests/integration_tests/modules/test_hotplug.py index 1e0947b100e..3d09ee09d18 100644 --- a/tests/integration_tests/modules/test_hotplug.py +++ b/tests/integration_tests/modules/test_hotplug.py @@ -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 @@ -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 10 -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 10 -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")