From 4f75b4a719b6b1b889903fe9fb60a6f53b1d9f4b Mon Sep 17 00:00:00 2001 From: Kien Nguyen Date: Mon, 23 Oct 2017 15:08:47 +0700 Subject: [PATCH] Cleanup the containers at zunclient funtional tests - Force delete containers. - Consolidate _zun & _zun_osc method. Closes-Bug: #1695992 Change-Id: Ice6d4e00612fb092caeb59b15393d3c37ca4f49d --- zunclient/tests/functional/base.py | 40 ++++++++----------- zunclient/tests/functional/osc/v1/base.py | 10 +++-- .../tests/functional/osc/v1/test_container.py | 16 ++++---- 3 files changed, 31 insertions(+), 35 deletions(-) diff --git a/zunclient/tests/functional/base.py b/zunclient/tests/functional/base.py index b9c93583..4b5caa1d 100644 --- a/zunclient/tests/functional/base.py +++ b/zunclient/tests/functional/base.py @@ -121,19 +121,29 @@ class FunctionalTestBase(base.ClientTestBase): return base.execute(cmd, action, flags, params, cli_dir=self.client.cli_dir) - def _zun(self, action, flags='', params=''): - """Execute zun command for the given action. + def _zun(self, action, cmd='zun', flags='', params='', merge_stderr=False): + """Execute Zun command for the given action. - :param action: the cli command to run using Container + :param action: the cli command to run using Zun + :type action: string + :param cmd: the base of cli command to run :type action: string :param flags: any optional cli flags to use :type flags: string :param params: any optional positional args to use :type params: string + :param merge_stderr: whether to merge stderr into the result + :type merge_stderr: bool """ - flags += ' --os-endpoint-type publicURL' + if cmd == 'openstack': + config = self._get_config() + id_api_version = config['os_identity_api_version'] + flags += ' --os-identity-api-version {0}'.format(id_api_version) + else: + flags += ' --os-endpoint-type publicURL' + if hasattr(self, 'os_auth_token'): - return self._cmd_no_auth('appcontainer', action, flags, params) + return self._cmd_no_auth(cmd, action, flags, params) else: for keystone_object in 'user', 'project': domain_attr = 'os_%s_domain_id' % keystone_object @@ -142,24 +152,8 @@ class FunctionalTestBase(base.ClientTestBase): 'ks_obj': keystone_object, 'value': getattr(self, domain_attr) } - return self.client.cmd_with_auth('zun', - action, flags, params) - - def _zun_osc(self, action, flags='', params=''): - """Execute container commands via OpenStack Client.""" - config = self._get_config() - identity_api_version = config.get('os_identity_api_version') - flags += ' --os-identity-api-version {0}'.format(identity_api_version) - - for keystone_object in 'user', 'project': - domain_attr = 'os_%s_domain_id' % keystone_object - if hasattr(self, domain_attr): - flags += ' --os-%(ks_obj)s-domain-id %(value)s' % { - 'ks_obj': keystone_object, - 'value': getattr(self, domain_attr) - } - return self.client.cmd_with_auth( - 'openstack', action, flags, params) + return self.client.cmd_with_auth( + cmd, action, flags, params, merge_stderr=merge_stderr) def zun(self, action, flags='', params='', parse=True): """Return parsed list of dicts with basic item info. diff --git a/zunclient/tests/functional/osc/v1/base.py b/zunclient/tests/functional/osc/v1/base.py index 3dcb21c5..458758b4 100644 --- a/zunclient/tests/functional/osc/v1/base.py +++ b/zunclient/tests/functional/osc/v1/base.py @@ -21,7 +21,7 @@ from zunclient.tests.functional import base class TestCase(base.FunctionalTestBase): def openstack(self, *args, **kwargs): - return self._zun_osc(*args, **kwargs) + return self._zun(cmd='openstack', *args, **kwargs) def get_opts(self, fields=None, output_format='json'): """Get options for OSC output fields format. @@ -77,7 +77,8 @@ class TestCase(base.FunctionalTestBase): self.fail('Container has not run!') return container - def container_delete(self, identifier, ignore_exceptions=False): + def container_delete(self, identifier, force=True, + ignore_exceptions=False): """Try to delete container by name or UUID. :param String identifier: Name or UUID of the container @@ -86,9 +87,10 @@ class TestCase(base.FunctionalTestBase): :raise: CommandFailed exception when command fails to delete a container """ + arg = '--force' if force else '' try: - return self.openstack('appcontainer delete {0}' - .format(identifier)) + return self.openstack('appcontainer delete {0} {1}' + .format(arg, identifier)) except exceptions.CommandFailed: if not ignore_exceptions: raise diff --git a/zunclient/tests/functional/osc/v1/test_container.py b/zunclient/tests/functional/osc/v1/test_container.py index ddc0e86b..29b8ba22 100644 --- a/zunclient/tests/functional/osc/v1/test_container.py +++ b/zunclient/tests/functional/osc/v1/test_container.py @@ -13,7 +13,6 @@ # under the License. import ddt -from tempest.lib.common.utils import data_utils import time from zunclient.tests.functional.osc.v1 import base @@ -30,7 +29,7 @@ class ContainerTests(base.TestCase): """Check container list command. """ - container = self.container_create(name='test') + container = self.container_create(name='test_list') container_list = self.container_list() self.assertIn(container['name'], [x['name'] for x in container_list]) @@ -44,12 +43,11 @@ class ContainerTests(base.TestCase): """Check container create command. """ - name = data_utils.rand_name('test_container') - container_info = self.container_create(name=name) - self.assertEqual(container_info['name'], name) + container_info = self.container_create(name='test_create') + self.assertEqual(container_info['name'], 'test_create') self.assertEqual(container_info['image'], 'cirros') container_list = self.container_list() - self.assertIn(name, [x['name'] for x in container_list]) + self.assertIn('test_create', [x['name'] for x in container_list]) self.container_delete(container_info['name']) def test_delete(self): @@ -58,9 +56,9 @@ class ContainerTests(base.TestCase): Test steps: 1) Create container in setUp. 2) Delete container by name/UUID. - 3) Check that node deleted successfully. + 3) Check that container deleted successfully. """ - container = self.container_create(name='test_del') + container = self.container_create(name='test_delete') container_list = self.container_list() self.assertIn(container['name'], [x['name'] for x in container_list]) @@ -109,6 +107,7 @@ class ContainerTests(base.TestCase): self.container_rename(container['name'], new_name) container_list = self.container_list() self.assertIn(new_name, [x['name'] for x in container_list]) + self.container_delete(new_name) def test_execute(self): """Check container execute command with name and UUID arguments. @@ -131,3 +130,4 @@ class ContainerTests(base.TestCase): command = "sh -c 'echo hello'" result = self.container_execute(container['name'], command) self.assertIn('hello', result) + self.container_delete(container['name'])