From 97f1ea5693e830e86743b0868bc42464b4ed385b Mon Sep 17 00:00:00 2001 From: Luciano Lo Giudice Date: Mon, 29 Nov 2021 12:08:28 -0300 Subject: [PATCH] Move functions into juju utilities. --- zaza/openstack/charm_tests/ceph/tests.py | 68 +----------------------- zaza/openstack/utilities/juju.py | 66 +++++++++++++++++++++++ 2 files changed, 68 insertions(+), 66 deletions(-) diff --git a/zaza/openstack/charm_tests/ceph/tests.py b/zaza/openstack/charm_tests/ceph/tests.py index 0b09781..3bc7c43 100644 --- a/zaza/openstack/charm_tests/ceph/tests.py +++ b/zaza/openstack/charm_tests/ceph/tests.py @@ -38,70 +38,6 @@ import zaza.utilities.juju as juju_utils import zaza.openstack.utilities.openstack as zaza_openstack -def add_storage(unit, label, pool, size): - """Add storage to a Juju unit. - - :param unit: The unit name (i.e: ceph-osd/0) - :type unit: str - - :param label: The storage label (i.e: osd-devices) - :type label: str - - :param pool: The pool on which to allocate the storage (i.e: cinder) - :type pool: str - - :size: The size in GB of the storage to attach. - :type size: int - - :returns: The name of the allocated storage. - """ - rv = subprocess.check_output(['juju', 'add-storage', unit, - '{}={},{}'.format(label, pool, - str(size) + 'GB')], - stderr=subprocess.STDOUT) - return rv.decode('UTF-8').replace('added storage ', '').split(' ')[0] - - -def detach_storage(storage_name): - """Detach previously allocated Juju storage.""" - subprocess.check_call(['juju', 'detach-storage', storage_name]) - - -def remove_storage(storage_name, force=False): - """Remove Juju storage. - - :param storage_name: The name of the previously allocated Juju storage. - :type storage_name: str - - :param force: If False (default), require that the storage be detached - before it can be removed. - :type force: bool - """ - cmd = ['juju', 'remove-storage', storage_name] - if force: - cmd.append('--force') - subprocess.check_call(cmd) - - -def add_loop_device(unit, size=10): - """Add a loopback device to a Juju unit. - - :param unit: The unit name on which to create the device. - :type unit: str - - :param size: The size in GB of the device. - :type size: int - - :returns: The device name. - """ - loop_name = '/home/ubuntu/loop.img' - truncate = 'truncate --size {}GB {}'.format(size, loop_name) - losetup = 'losetup --find {}'.format(loop_name) - lofind = 'losetup -a | grep {} | cut -f1 -d ":"'.format(loop_name) - cmd = "sudo sh -c '{} && {} && {}'".format(truncate, losetup, lofind) - return zaza_model.run_on_unit(unit, cmd) - - class CephLowLevelTest(test_utils.OpenStackBaseTest): """Ceph Low Level Test Class.""" @@ -615,8 +551,8 @@ class CephTest(test_utils.OpenStackBaseTest): logging.info('Running add-disk action with a caching device') osds = [x.entity_id for x in zaza_model.get_units('ceph-osd')] for unit in osds: - add_storage(unit, 'cache-devices', 'cinder', 10) - loop_dev = add_loop_device(unit, 10) + juju_utils.add_storage(unit, 'cache-devices', 'cinder', 10) + loop_dev = juju_utils.add_loop_device(unit, 10) action_obj = zaza_model.run_action( unit_name=unit, action_name='add-disk', diff --git a/zaza/openstack/utilities/juju.py b/zaza/openstack/utilities/juju.py index 073a26f..e4646a5 100644 --- a/zaza/openstack/utilities/juju.py +++ b/zaza/openstack/utilities/juju.py @@ -17,7 +17,9 @@ import logging import functools +import subprocess +import zaza.model import zaza.utilities.juju @@ -310,3 +312,67 @@ def get_subordinate_units(unit_list, charm_name=None, status=None, charm_name=charm_name, status=status, model_name=model_name) + + +def add_storage(unit, label, pool, size): + """Add storage to a Juju unit. + + :param unit: The unit name (i.e: ceph-osd/0) + :type unit: str + + :param label: The storage label (i.e: osd-devices) + :type label: str + + :param pool: The pool on which to allocate the storage (i.e: cinder) + :type pool: str + + :size: The size in GB of the storage to attach. + :type size: int + + :returns: The name of the allocated storage. + """ + rv = subprocess.check_output(['juju', 'add-storage', unit, + '{}={},{}'.format(label, pool, + str(size) + 'GB')], + stderr=subprocess.STDOUT) + return rv.decode('UTF-8').replace('added storage ', '').split(' ')[0] + + +def detach_storage(storage_name): + """Detach previously allocated Juju storage.""" + subprocess.check_call(['juju', 'detach-storage', storage_name]) + + +def remove_storage(storage_name, force=False): + """Remove Juju storage. + + :param storage_name: The name of the previously allocated Juju storage. + :type storage_name: str + + :param force: If False (default), require that the storage be detached + before it can be removed. + :type force: bool + """ + cmd = ['juju', 'remove-storage', storage_name] + if force: + cmd.append('--force') + subprocess.check_call(cmd) + + +def add_loop_device(unit, size=10): + """Add a loopback device to a Juju unit. + + :param unit: The unit name on which to create the device. + :type unit: str + + :param size: The size in GB of the device. + :type size: int + + :returns: The device name. + """ + loop_name = '/home/ubuntu/loop.img' + truncate = 'truncate --size {}GB {}'.format(size, loop_name) + losetup = 'losetup --find {}'.format(loop_name) + lofind = 'losetup -a | grep {} | cut -f1 -d ":"'.format(loop_name) + cmd = "sudo sh -c '{} && {} && {}'".format(truncate, losetup, lofind) + return zaza.model.run_on_unit(unit, cmd)