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
This commit is contained in:
parent
17384af072
commit
50ec15fd93
@ -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
|
||||
|
@ -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')
|
||||
|
@ -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):
|
||||
|
||||
|
@ -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(
|
||||
|
Loading…
Reference in New Issue
Block a user