From 1ef607c002f445d172d5522bff5be8687e0aee12 Mon Sep 17 00:00:00 2001 From: Roman Safronov Date: Thu, 18 Apr 2024 22:14:26 +0300 Subject: [PATCH] Make reset_node_service support inactive services Patch [1] introduced a unified way to restart services. As appeared, globbing does not work as expected in case a service is inactive. This patch makes reset_node_service() to work properly also with inactive services. Additionally, adjusted _get_vm_id_by_name() from test_vrrp.py to work properly in case VM hostname contains a domain name. [1] https://review.opendev.org/c/x/whitebox-neutron-tempest-plugin/+/914116 Change-Id: I0c4222165b89ac5673bc1d6d92899761b921b366 --- .../tests/scenario/base.py | 28 +++++++++---------- .../tests/scenario/test_vrrp.py | 15 ++-------- 2 files changed, 17 insertions(+), 26 deletions(-) diff --git a/whitebox_neutron_tempest_plugin/tests/scenario/base.py b/whitebox_neutron_tempest_plugin/tests/scenario/base.py index 66825c1..301c5d8 100644 --- a/whitebox_neutron_tempest_plugin/tests/scenario/base.py +++ b/whitebox_neutron_tempest_plugin/tests/scenario/base.py @@ -432,35 +432,35 @@ class BaseTempestWhiteboxTestCase(base.BaseTempestTestCase): return False @classmethod - def reset_node_service(cls, service_name, ssh_client, + def reset_node_service(cls, service_alias, ssh_client, wait_until_active=True, timeout=30): - # NOTE(mblue): Globbing works on podified/devstack/tripleo - service_glob = re.sub(r'[^a-zA-Z]', '?', service_name) host_ip = ssh_client.host + service_name = ssh_client.exec_command( + "systemctl list-unit-files --type service | grep {} | " + "cut -d' ' -f1".format( + service_alias.replace(" ", ".*"))).strip() LOG.debug("Restarting service '%s' on host '%s'.", - service_glob, host_ip) + service_name, host_ip) ssh_client.exec_command( - 'sudo systemctl restart *{}.service'.format(service_glob)) + 'sudo systemctl restart {}'.format(service_name)) if not wait_until_active: return def _is_service_active(): return 'active' in ssh_client.exec_command( - 'sudo systemctl is-active *{}.service; true'.format( - service_glob)) + 'sudo systemctl is-active {}; true'.format(service_name)) LOG.debug("Waiting for service '%s' to become active on host '%s'.", - service_glob, host_ip) + service_name, host_ip) common_utils.wait_until_true( _is_service_active, timeout=timeout, sleep=5, exception=RuntimeError( - "Timed out {} seconds, service {} (globbing '*{}.service') " + "Timed out {} seconds, service {} " "didn't become active after restart.\n\n'''\n{}\n'''".format( - timeout, service_name, service_glob, - ssh_client.exec_command( - 'sudo systemctl status *{}.service; true'.format( - service_glob))))) + timeout, service_name, ssh_client.exec_command( + 'sudo systemctl status {}; true'.format( + service_name))))) LOG.debug("Service '%s' active on host '%s'.", - service_glob, host_ip) + service_name, host_ip) def _create_server( self, create_floating_ip=True, exclude_hosts=None, diff --git a/whitebox_neutron_tempest_plugin/tests/scenario/test_vrrp.py b/whitebox_neutron_tempest_plugin/tests/scenario/test_vrrp.py index 5e50bc4..d8b1253 100644 --- a/whitebox_neutron_tempest_plugin/tests/scenario/test_vrrp.py +++ b/whitebox_neutron_tempest_plugin/tests/scenario/test_vrrp.py @@ -15,7 +15,6 @@ from neutron_lib import constants from neutron_tempest_plugin.common import ssh -from neutron_tempest_plugin.common import utils as common_utils from neutron_tempest_plugin import config from neutron_tempest_plugin import exceptions from oslo_log import log @@ -97,11 +96,6 @@ class VrrpTest(base.BaseTempestTestCaseAdvanced): raise self.skipException( "keepalived is not available on server %s" % (server_id)) - @staticmethod - def _is_keepalived_service_active(ssh_client): - output = ssh_client.exec_command("sudo systemctl is-active keepalived") - return 'active' == output.splitlines()[0] - def _prepare_server(self, ssh_client, interface, vip_ip): config_text = get_keepalived_config(interface, vip_ip) config_file = 'keepalived.conf' @@ -110,16 +104,13 @@ class VrrpTest(base.BaseTempestTestCaseAdvanced): 'sudo mv -Z /tmp/{1} /etc/keepalived/'.format( config_text, config_file)) ssh_client.exec_command("sudo systemctl restart keepalived") - # make sure keepalived is active - common_utils.wait_until_true( - lambda: self._is_keepalived_service_active(ssh_client=ssh_client), - timeout=20, - exception=RuntimeError("Timed out waiting for keepalived active")) + # restart and make sure keepalived is active + self.reset_node_service('keepalived', ssh_client) @staticmethod def _get_vm_id_by_name(name, vms): for vm in vms: - if vm['server']['name'] == name: + if vm['server']['name'] in name: return vm['server']['id'] return None