From ba106c51c593982dda2f92daf1942c5bca68c6cd Mon Sep 17 00:00:00 2001 From: Roman Safronov Date: Sun, 28 Jul 2024 16:49:49 +0300 Subject: [PATCH] Try to get a valid hypervisor_host from ssh config Usually testing setups have openstack nodes implemented as virtual machines running on the same hypervisor host. For tests that perform power operations for the nodes it is necessary to have an ability to run virsh commands from the hypervisor host. While there is a config parameter hypervisor_host that allows to set the value, in some cases it is not possible to know what will a hypervisor host name since it is selected randomally. This patch adds code that as a fallback scenario tries to determine hypervisor host name from ssh config file, that is usually available on the proxy host (ansible controller host) in case environment was deployed by ci-framework. Change-Id: Icc71c5a0511f04c19ef9509a32278f4e70e92bd2 --- .../tests/scenario/base.py | 28 ++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/whitebox_neutron_tempest_plugin/tests/scenario/base.py b/whitebox_neutron_tempest_plugin/tests/scenario/base.py index df97544..58a5add 100644 --- a/whitebox_neutron_tempest_plugin/tests/scenario/base.py +++ b/whitebox_neutron_tempest_plugin/tests/scenario/base.py @@ -1366,30 +1366,44 @@ class BaseDisruptiveTempestTestCase(BaseTempestWhiteboxTestCase): try: cls.proxy_host_client.exec_command( "timeout 10 ssh {} virsh list".format(WB_CONF.hypervisor_host)) + cls.hypervisor_host = WB_CONF.hypervisor_host + return + except lib_exceptions.SSHExecCommandFailed: + LOG.debug("Attempt to execute virsh command on hypervisor_host: " + "'{}' failed. Trying to discover hypervisor host from " + ".ssh/config file.".format(WB_CONF.hypervisor_host)) + host = cls.proxy_host_client.exec_command( + r"grep 'Host.*\ hypervisor$' ~/.ssh/config " + "| cut -d' ' -f 2").strip() + + try: + cls.proxy_host_client.exec_command( + "timeout 10 ssh {} virsh list".format(host)) except lib_exceptions.SSHExecCommandFailed: raise cls.skipException( "No access to virsh tool on hypervisor node. Please make sure " "that hypervisor_host is configured properly and/or virsh " "is deployed there.") + cls.hypervisor_host = host @classmethod def find_host_virsh_name(cls, host): cmd = ("timeout 10 ssh {} sudo virsh list --all --name " "| grep -w {}").format( - WB_CONF.hypervisor_host, host) + cls.hypervisor_host, host) return cls.proxy_host_client.exec_command(cmd).strip() @classmethod def is_host_state_is_shut_off(cls, host): cmd = ("timeout 10 ssh {} virsh list --state-shutoff | grep -w {} " - "|| true".format(WB_CONF.hypervisor_host, host)) + "|| true".format(cls.hypervisor_host, host)) output = cls.proxy_host_client.exec_command(cmd) return True if host in output else False @classmethod def is_host_loginable(cls, host): cmd = "timeout 10 ssh {} ssh {} hostname || true".format( - WB_CONF.hypervisor_host, host) + cls.hypervisor_host, host) output = cls.proxy_host_client.exec_command(cmd) return True if host in output else False @@ -1398,7 +1412,7 @@ class BaseDisruptiveTempestTestCase(BaseTempestWhiteboxTestCase): if not WB_CONF.run_power_operations_tests: raise cls.skipException("Power operations are not allowed") cmd = "timeout 10 ssh {} sudo virsh destroy {}".format( - WB_CONF.hypervisor_host, cls.find_host_virsh_name(host)) + cls.hypervisor_host, cls.find_host_virsh_name(host)) cls.proxy_host_client.exec_command(cmd) common_utils.wait_until_true( lambda: cls.is_host_state_is_shut_off(host), @@ -1409,7 +1423,7 @@ class BaseDisruptiveTempestTestCase(BaseTempestWhiteboxTestCase): if not WB_CONF.run_power_operations_tests: raise cls.skipException("Power operations are not allowed") cmd = "timeout 10 ssh {} sudo virsh start {}".format( - WB_CONF.hypervisor_host, cls.find_host_virsh_name(host)) + cls.hypervisor_host, cls.find_host_virsh_name(host)) cls.proxy_host_client.exec_command(cmd) # TODO(rsafrono): implement and apply additional health checks common_utils.wait_until_true( @@ -1421,7 +1435,7 @@ class BaseDisruptiveTempestTestCase(BaseTempestWhiteboxTestCase): if not WB_CONF.run_power_operations_tests: raise cls.skipException("Power operations are not allowed") cmd = "timeout 10 ssh {} sudo virsh reboot {}".format( - WB_CONF.hypervisor_host, cls.find_host_virsh_name(host)) + cls.hypervisor_host, cls.find_host_virsh_name(host)) cls.proxy_host_client.exec_command(cmd) common_utils.wait_until_true( lambda: cls.is_host_loginable(host), @@ -1432,7 +1446,7 @@ class BaseDisruptiveTempestTestCase(BaseTempestWhiteboxTestCase): """ hosts = self.proxy_host_client.exec_command( "timeout 10 ssh {} sudo virsh list --all --name".format( - WB_CONF.hypervisor_host)).strip().split() + self.hypervisor_host)).strip().split() for host in hosts: if self.is_host_state_is_shut_off(host): self.power_on_host(host)