Clean up service management

To ensure consistency and have our code better reflect reality, this
patch includes the following cleanups:

* The base ServiceManager class gets a start() method, thus giving all
  services the basic start/stop/restart methods.

* The libvirt service manager loses its restart() method in favor of
  start(). restart() was never actually used to restart libvirt, but
  rather to starts it from scratch after having previously stopped it.

* The return values of start/stop/restart were never used. Don't
  returning anything.

Change-Id: I6964475cca973a305d982b31d660858c81929772
This commit is contained in:
Artom Lifshitz 2020-09-04 14:30:15 -04:00
parent 875aa46cea
commit 1de36b3268
5 changed files with 41 additions and 22 deletions

View File

@ -18,7 +18,7 @@ function configure {
iniset $TEMPEST_CONFIG whitebox-nova-compute stop_command "$WHITEBOX_NOVA_COMPUTE_STOP_COMMAND"
iniset $TEMPEST_CONFIG whitebox-nova-compute start_command "$WHITEBOX_NOVA_COMPUTE_START_COMMAND"
iniset $TEMPEST_CONFIG whitebox-libvirt restart_command "$WHITEBOX_LIBVIRT_RESTART_COMMAND"
iniset $TEMPEST_CONFIG whitebox-libvirt start_command "$WHITEBOX_LIBVIRT_START_COMMAND"
iniset $TEMPEST_CONFIG whitebox-libvirt stop_command "$WHITEBOX_LIBVIRT_STOP_COMMAND"
iniset $TEMPEST_CONFIG whitebox-database user $DATABASE_USER

View File

@ -8,7 +8,7 @@ WHITEBOX_NOVA_COMPUTE_CONFIG_PATH=${WHITEBOX_NOVA_COMPUTE_CONFIG_PATH:-/etc/nova
WHITEBOX_NOVA_COMPUTE_STOP_COMMAND=${WHITEBOX_NOVA_COMPUTE_STOP_COMMAND:-'systemctl stop devstack@n-cpu'}
WHITEBOX_NOVA_COMPUTE_START_COMMAND=${WHITEBOX_NOVA_COMPUTE_START_COMMAND:-'systemctl start devstack@n-cpu'}
WHITEBOX_LIBVIRT_RESTART_COMMAND=${WHITEBOX_LIBVIRT_RESTART_COMMAND:-'systemctl restart libvirtd'}
WHITEBOX_LIBVIRT_START_COMMAND=${WHITEBOX_LIBVIRT_START_COMMAND:-'systemctl start libvirtd'}
WHITEBOX_LIBVIRT_STOP_COMMAND=${WHITEBOX_LIBVIRT_STOP_COMMAND:-'systemctl stop libvirtd'}
WHITEBOX_CPU_TOPOLOGY=${WHITEBOX_CPU_TOPOLOGY:-''}

View File

@ -101,11 +101,9 @@ libvirt_group = cfg.OptGroup(
libvirt_opts = [
cfg.StrOpt(
'restart_command',
help='Command to restart the libvirt service, without any '
'privilege management (ie, no sudo).',
deprecated_opts=[cfg.DeprecatedOpt('restart_command',
group='whitebox-nova-libvirt')]),
'start_command',
help='Command to start the libvirt service, without any '
'privilege management (ie, no sudo).'),
cfg.StrOpt(
'stop_command',
help='Command to stop the libvirt service, without any '

View File

@ -122,7 +122,7 @@ class ServiceManager(SSHClient):
try:
yield
finally:
self.restart()
self.start()
def get_conf_opt(self, section, option):
command = 'crudini --get %s %s %s' % (self.config_path, section,
@ -161,11 +161,14 @@ class ServiceManager(SSHClient):
option)
return self.execute(command, container_name=None, sudo=True)
def restart(self):
return self.execute(self.restart_command, sudo=True)
def start(self):
self.execute(self.start_command, sudo=True)
def stop(self):
return self.execute(self.stop_command, sudo=True)
self.execute(self.stop_command, sudo=True)
def restart(self):
self.execute(self.restart_command, sudo=True)
class NovaServiceManager(ServiceManager):

View File

@ -17,6 +17,7 @@ import tempfile
import textwrap
from oslo_concurrency import processutils
from oslo_config import cfg
from tempest.lib import exceptions as tempest_libexc
from whitebox_tempest_plugin import exceptions
@ -24,6 +25,9 @@ from whitebox_tempest_plugin.services import clients
from whitebox_tempest_plugin.tests import base
CONF = cfg.CONF
class SSHClientTestCase(base.WhiteboxPluginTestCase):
def setUp(self):
@ -142,21 +146,35 @@ class ServiceManagerTestCase(base.WhiteboxPluginTestCase):
self.assertRaises(tempest_libexc.SSHExecCommandFailed,
service.get_conf_opt, 'section', 'foo')
def test_restart(self):
self.flags(restart_command='fake restart command',
group='whitebox-libvirt')
service = clients.ServiceManager('fake-host', 'libvirt')
with mock.patch.object(service, 'execute') as mock_exec:
service.restart()
mock_exec.assert_called_with('fake restart command', sudo=True)
def test_stop(self):
def test_commands(self):
# NOTE(artom) There is currently no service that has all 3 start, stop
# and restart, so we set up a fake one for testing.
CONF.register_group(cfg.OptGroup(name='whitebox-fake-service'))
CONF.register_opt(cfg.StrOpt('start_command'),
group='whitebox-fake-service')
CONF.register_opt(cfg.StrOpt('stop_command'),
group='whitebox-fake-service')
CONF.register_opt(cfg.StrOpt('restart_command'),
group='whitebox-fake-service')
self.flags(start_command='fake start command',
group='whitebox-fake-service')
self.flags(stop_command='fake stop command',
group='whitebox-libvirt')
service = clients.ServiceManager('fake-host', 'libvirt')
group='whitebox-fake-service')
self.flags(restart_command='fake restart command',
group='whitebox-fake-service')
service = clients.ServiceManager('fake-host', 'fake-service')
with mock.patch.object(service, 'execute') as mock_exec:
# Start
service.start()
mock_exec.assert_called_with('fake start command', sudo=True)
mock_exec.reset_mock()
# Stop
service.stop()
mock_exec.assert_called_with('fake stop command', sudo=True)
mock_exec.reset_mock()
# Restart
service.restart()
mock_exec.assert_called_with('fake restart command', sudo=True)
class NUMAClientTestCase(base.WhiteboxPluginTestCase):