From 50ec15fd9317966f9e9d163924e00bdaed359dfd Mon Sep 17 00:00:00 2001 From: Hongbin Lu Date: Sat, 3 Nov 2018 23:33:22 +0000 Subject: [PATCH] Move docker network cleanup logic Right now, both container and capsule tests are doing docker network cleanup at the end of the tests. This commit moves the cleanup logic to a common base class so that the code becomes more DRY. Change-Id: I9320f73f68b50954e57a1b9f0e3cc01c53ba102a --- .../tests/tempest/api/test_capsules.py | 29 ------------------- .../tests/tempest/api/test_containers.py | 17 ----------- .../tests/tempest/api/test_services.py | 7 +---- zun_tempest_plugin/tests/tempest/base.py | 26 +++++++++++++++++ 4 files changed, 27 insertions(+), 52 deletions(-) diff --git a/zun_tempest_plugin/tests/tempest/api/test_capsules.py b/zun_tempest_plugin/tests/tempest/api/test_capsules.py index b707da5..20c9423 100644 --- a/zun_tempest_plugin/tests/tempest/api/test_capsules.py +++ b/zun_tempest_plugin/tests/tempest/api/test_capsules.py @@ -23,33 +23,6 @@ class TestCapsule(base.BaseZunTest): credentials = ['primary', 'admin'] min_microversion = '1.12' - @classmethod - def tearDownClass(cls): - cls.cleanup_network() - super(TestCapsule, cls).tearDownClass() - - @classmethod - def cleanup_network(cls): - creds_provider = cls._get_credentials_provider() - creds = creds_provider.get_primary_creds() - network = getattr(creds, 'network', None) - if not network: - return - - docker_base_url = cls._get_docker_url() - networks = cls.docker_client.list_networks( - network['id'], docker_auth_url=docker_base_url) - for network in networks: - cls.docker_client.remove_network( - network['Id'], docker_auth_url=docker_base_url) - - @classmethod - def _get_docker_url(cls, host='localhost'): - protocol = 'tcp' - port = '2375' - base_url = '%s://%s:%s' % (protocol, host, port) - return base_url - @classmethod def get_client_manager(cls, credential_type=None, roles=None, force_new=None): @@ -63,8 +36,6 @@ class TestCapsule(base.BaseZunTest): @classmethod def setup_clients(cls): super(TestCapsule, cls).setup_clients() - cls.container_client = cls.os_primary.container_client - cls.docker_client = clients.DockerClient() cls.images_client = cls.os_primary.images_client cls.ports_client = cls.os_primary.ports_client cls.sgs_client = cls.os_primary.sgs_client diff --git a/zun_tempest_plugin/tests/tempest/api/test_containers.py b/zun_tempest_plugin/tests/tempest/api/test_containers.py index 52fcd96..14fafba 100644 --- a/zun_tempest_plugin/tests/tempest/api/test_containers.py +++ b/zun_tempest_plugin/tests/tempest/api/test_containers.py @@ -46,8 +46,6 @@ class TestContainer(base.BaseZunTest): @classmethod def setup_clients(cls): super(TestContainer, cls).setup_clients() - cls.container_client = cls.os_primary.container_client - cls.docker_client = clients.DockerClient() cls.images_client = cls.os_primary.images_client cls.ports_client = cls.os_primary.ports_client cls.sgs_client = cls.os_primary.sgs_client @@ -62,13 +60,10 @@ class TestContainer(base.BaseZunTest): self.containers = [] def tearDown(self): - hosts = [] _, model = self.os_admin.container_client.list_containers( params={'all_projects': True}) for c in model.containers: if c['uuid'] in self.containers: - if c['host'] and c['host'] not in hosts: - hosts.append(c['host']) # NOTE(kiennt): From version 1.7, Zun disallowed non-admin # users to force delete containers. Therefore, # we have to be admin to do this action. @@ -77,18 +72,6 @@ class TestContainer(base.BaseZunTest): params={'stop': True, 'all_projects': True}) self.container_client.ensure_container_deleted(c['uuid']) - # cleanup the network resources - project_id = self.container_client.tenant_id - for host in hosts: - # NOTE(kiennt): Default docker remote url - # Remove networks in all hosts - docker_base_url = self._get_docker_url(host=host) - networks = self.docker_client.list_networks(project_id, - docker_base_url) - for network in networks: - self.docker_client.remove_network(network['Id'], - docker_base_url) - super(TestContainer, self).tearDown() @decorators.idempotent_id('b8946b8c-57d5-4fdc-a09a-001d6b552725') diff --git a/zun_tempest_plugin/tests/tempest/api/test_services.py b/zun_tempest_plugin/tests/tempest/api/test_services.py index 997a090..d62785b 100644 --- a/zun_tempest_plugin/tests/tempest/api/test_services.py +++ b/zun_tempest_plugin/tests/tempest/api/test_services.py @@ -19,6 +19,7 @@ from zun_tempest_plugin.tests.tempest import base class TestService(base.BaseZunTest): + credentials = ['primary', 'admin'] min_microversion = '1.7' @classmethod @@ -32,12 +33,6 @@ class TestService(base.BaseZunTest): ) return clients.Manager(manager.credentials) - @classmethod - def setup_clients(cls): - - super(TestService, cls).setup_clients() - cls.container_client = cls.os_primary.container_client - @classmethod def resource_setup(cls): diff --git a/zun_tempest_plugin/tests/tempest/base.py b/zun_tempest_plugin/tests/tempest/base.py index c550871..d6cb5c0 100644 --- a/zun_tempest_plugin/tests/tempest/base.py +++ b/zun_tempest_plugin/tests/tempest/base.py @@ -12,14 +12,18 @@ # See the License for the specific language governing permissions and # limitations under the License. +from oslo_log import log as logging from tempest import config from tempest.lib.common import api_version_utils from tempest.lib.common.utils import data_utils from tempest import test from zun_tempest_plugin.tests.tempest.api import api_microversion_fixture +from zun_tempest_plugin.tests.tempest.api import clients + CONF = config.CONF +LOG = logging.getLogger(__name__) class BaseZunTest(api_version_utils.BaseMicroversionTest, @@ -45,6 +49,8 @@ class BaseZunTest(api_version_utils.BaseMicroversionTest, super(BaseZunTest, cls).setup_clients() cls.networks_client = cls.os_primary.neutron_client cls.subnets_client = cls.os_primary.subnets_client + cls.docker_client = clients.DockerClient() + cls.container_client = cls.os_primary.container_client @classmethod def setup_credentials(cls): @@ -66,6 +72,26 @@ class BaseZunTest(api_version_utils.BaseMicroversionTest, CONF.container_service.min_microversion)) cls.wait_timeout = CONF.container_service.wait_timeout + @classmethod + def clear_credentials(cls): + cls.cleanup_network() + super(BaseZunTest, cls).clear_credentials() + + @classmethod + def cleanup_network(cls): + creds_provider = cls._get_credentials_provider() + creds = creds_provider.get_primary_creds() + network = getattr(creds, 'network', None) + if not network: + return + + docker_url = 'tcp://localhost:2375' + networks = cls.docker_client.list_networks( + network['id'], docker_auth_url=docker_url) + for network in networks: + cls.docker_client.remove_network( + network['Id'], docker_auth_url=docker_url) + def setUp(self): super(BaseZunTest, self).setUp() self.useFixture(api_microversion_fixture.APIMicroversionFixture(