From 478ad82c183f97187e79e6f5de7e5cb1bb32cbaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20R=C3=B6hrich?= Date: Wed, 17 Jan 2024 15:03:18 +0100 Subject: [PATCH 1/2] networking tests: fix ssh auth, clean up code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Fix SSH auth using password. Paramiko can get confused when using password authentication while having SSH keys in the SSH agent. In case the test suite is being run using password based authentication, the options `allow_agent=False` and `look_for_key=False` prevent paramiko from trying to get keys from the SSH agent, allowing password based authentication to proceed without error. - Clean up duplicated fixture `vlan_id` and `vlan_nic`. These pytest fixtures were copy-and-pasted into several places, so to simplify the code they are consolidated in a single place where other network fixtures can be kept in the future as well. Signed-off-by: Moritz Röhrich --- harvester_e2e_tests/fixtures/api_client.py | 7 +++++++ harvester_e2e_tests/fixtures/networks.py | 15 +++++++++++++++ .../integration/test_0_storage_network.py | 11 ++--------- .../integration/test_networks.py | 17 +---------------- .../integration/test_vm_networks.py | 17 +---------------- 5 files changed, 26 insertions(+), 41 deletions(-) create mode 100644 harvester_e2e_tests/fixtures/networks.py diff --git a/harvester_e2e_tests/fixtures/api_client.py b/harvester_e2e_tests/fixtures/api_client.py index 9f9d55327..16ab53a87 100644 --- a/harvester_e2e_tests/fixtures/api_client.py +++ b/harvester_e2e_tests/fixtures/api_client.py @@ -220,6 +220,13 @@ def login(self, ipaddr, port=22, jumphost=False, **kwargs): cli.set_missing_host_key_policy(MissingHostKeyPolicy()) kws = dict(username=self.username, password=self.password, pkey=self.pkey) kws.update(kwargs) + + # in case we're using a password to log into the host, this + # prevents paramiko from getting confused by ssh keys in the ssh + # agent: + if self.password and not self.pkey: + kws.update(dict(allow_agent=False, look_for_keys=False)) + cli.connect(ipaddr, port, **kws) self._client = cli diff --git a/harvester_e2e_tests/fixtures/networks.py b/harvester_e2e_tests/fixtures/networks.py new file mode 100644 index 000000000..5d25b4eb1 --- /dev/null +++ b/harvester_e2e_tests/fixtures/networks.py @@ -0,0 +1,15 @@ +import pytest + + +@pytest.fixture(scope="session") +def vlan_id(request): + vlan_id = request.config.getoption('--vlan-id') + assert 0 < vlan_id < 4095, f"VLAN ID should be in range 1-4094, not {vlan_id}" + return vlan_id + + +@pytest.fixture(scope="session") +def vlan_nic(request): + vlan_nic = request.config.getoption('--vlan-nic') + assert vlan_nic, f"VLAN NIC {vlan_nic} not configured correctly." + return vlan_nic diff --git a/harvester_e2e_tests/integration/test_0_storage_network.py b/harvester_e2e_tests/integration/test_0_storage_network.py index c72cea1c7..498221030 100644 --- a/harvester_e2e_tests/integration/test_0_storage_network.py +++ b/harvester_e2e_tests/integration/test_0_storage_network.py @@ -8,7 +8,8 @@ import pytest pytest_plugins = [ - "harvester_e2e_tests.fixtures.api_client" + "harvester_e2e_tests.fixtures.api_client", + "harvester_e2e_tests.fixtures.networks" ] @@ -73,14 +74,6 @@ def cluster_network(request, api_client, unique_name): assert 200 == code, (code, data) -@pytest.fixture(scope='module') -def vlan_id(request): - vlan_id = request.config.getoption('--vlan-id') - assert 4095 > vlan_id > 0, (f"VLAN ID should in range 1-4094, not {vlan_id}") - - return vlan_id - - @pytest.mark.p0 @pytest.mark.settings @pytest.mark.networks diff --git a/harvester_e2e_tests/integration/test_networks.py b/harvester_e2e_tests/integration/test_networks.py index 756fcd602..07ae1f0d5 100644 --- a/harvester_e2e_tests/integration/test_networks.py +++ b/harvester_e2e_tests/integration/test_networks.py @@ -12,6 +12,7 @@ pytest_plugins = [ "harvester_e2e_tests.fixtures.api_client", + "harvester_e2e_tests.fixtures.networks", "harvester_e2e_tests.fixtures.virtualmachines" ] @@ -51,22 +52,6 @@ def client(): client.close() -@pytest.fixture(scope='session') -def vlan_id(request): - vlan_id = request.config.getoption('--vlan-id') - assert 4095 > vlan_id > 0, f"VLAN ID should in range 1-4094, not {vlan_id}" - - return vlan_id - - -@pytest.fixture(scope="session") -def vlan_nic(request): - vlan_nic = request.config.getoption('--vlan-nic') - assert vlan_nic, f"VLAN NIC {vlan_nic} not configured correctly." - - return vlan_nic - - @pytest.fixture(scope='module') def cluster_network(vlan_nic, api_client, unique_name): code, data = api_client.clusternetworks.get_config() diff --git a/harvester_e2e_tests/integration/test_vm_networks.py b/harvester_e2e_tests/integration/test_vm_networks.py index 51144ba0e..44e7da793 100644 --- a/harvester_e2e_tests/integration/test_vm_networks.py +++ b/harvester_e2e_tests/integration/test_vm_networks.py @@ -12,26 +12,11 @@ pytest_plugins = [ "harvester_e2e_tests.fixtures.api_client", "harvester_e2e_tests.fixtures.images", + "harvester_e2e_tests.fixtures.networks", "harvester_e2e_tests.fixtures.virtualmachines" ] -@pytest.fixture(scope='session') -def vlan_id(request): - vlan_id = request.config.getoption('--vlan-id') - assert 4095 > vlan_id > 0, (f"VLAN ID should in range 1-4094, not {vlan_id}") - - return vlan_id - - -@pytest.fixture(scope="session") -def vlan_nic(request): - vlan_nic = request.config.getoption('--vlan-nic') - assert vlan_nic, f"VLAN NIC {vlan_nic} not configured correctly." - - return vlan_nic - - @pytest.fixture(scope="session") def gen_ifconfig(): # eth/eno/ens(idx) | enp(idx)s[0-9] From 2e15ec7de85cc55f592c9c2f1e2928c50fd0ceed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20R=C3=B6hrich?= Date: Fri, 19 Jan 2024 13:58:41 +0100 Subject: [PATCH 2/2] networking tests: give ssh params defaults MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Make ssh parameters `allow_agent` and `look_for_keys` default to `False`, but let them be overridden on method call if necessary. Signed-off-by: Moritz Röhrich --- harvester_e2e_tests/fixtures/api_client.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/harvester_e2e_tests/fixtures/api_client.py b/harvester_e2e_tests/fixtures/api_client.py index 16ab53a87..a4c05eba9 100644 --- a/harvester_e2e_tests/fixtures/api_client.py +++ b/harvester_e2e_tests/fixtures/api_client.py @@ -214,7 +214,8 @@ def reconnect(self, ipaddr, port=22, **kwargs): kws.update(kwargs) cli.connect(ipaddr, port, **kws) - def login(self, ipaddr, port=22, jumphost=False, **kwargs): + def login(self, ipaddr, port=22, jumphost=False, allow_agent=False, + look_for_keys=False, **kwargs): if not self.client: cli = SSHClient() cli.set_missing_host_key_policy(MissingHostKeyPolicy()) @@ -225,7 +226,8 @@ def login(self, ipaddr, port=22, jumphost=False, **kwargs): # prevents paramiko from getting confused by ssh keys in the ssh # agent: if self.password and not self.pkey: - kws.update(dict(allow_agent=False, look_for_keys=False)) + kws.update(dict(allow_agent=allow_agent, + look_for_keys=look_for_keys)) cli.connect(ipaddr, port, **kws) self._client = cli