Update tests to properly dispose of client instances in tearDown

Signed-off-by: Joffrey F <joffrey@docker.com>
This commit is contained in:
Joffrey F 2018-10-31 17:09:33 -07:00
parent 1df021ee24
commit 94aa9a89f7
4 changed files with 45 additions and 43 deletions

View File

@ -8,8 +8,8 @@ from .base import BaseAPIIntegrationTest, BUSYBOX
class TestNetworks(BaseAPIIntegrationTest):
def tearDown(self):
super(TestNetworks, self).tearDown()
self.client.leave_swarm(force=True)
super(TestNetworks, self).tearDown()
def create_network(self, *args, **kwargs):
net_name = random_name()

View File

@ -3,7 +3,7 @@ import os
import docker
import pytest
from .base import BaseAPIIntegrationTest, TEST_API_VERSION
from .base import BaseAPIIntegrationTest
from ..helpers import requires_api_version
SSHFS = 'vieux/sshfs:latest'
@ -13,27 +13,27 @@ SSHFS = 'vieux/sshfs:latest'
class PluginTest(BaseAPIIntegrationTest):
@classmethod
def teardown_class(cls):
c = docker.APIClient(
version=TEST_API_VERSION, timeout=60,
**docker.utils.kwargs_from_env()
)
client = cls.get_client_instance()
try:
c.remove_plugin(SSHFS, force=True)
client.remove_plugin(SSHFS, force=True)
except docker.errors.APIError:
pass
def teardown_method(self, method):
client = self.get_client_instance()
try:
self.client.disable_plugin(SSHFS)
client.disable_plugin(SSHFS)
except docker.errors.APIError:
pass
for p in self.tmp_plugins:
try:
self.client.remove_plugin(p, force=True)
client.remove_plugin(p, force=True)
except docker.errors.APIError:
pass
client.close()
def ensure_plugin_installed(self, plugin_name):
try:
return self.client.inspect_plugin(plugin_name)

View File

@ -13,14 +13,13 @@ class SwarmTest(BaseAPIIntegrationTest):
self._unlock_key = None
def tearDown(self):
super(SwarmTest, self).tearDown()
try:
if self._unlock_key:
self.client.unlock_swarm(self._unlock_key)
except docker.errors.APIError:
pass
force_leave_swarm(self.client)
super(SwarmTest, self).tearDown()
@requires_api_version('1.24')
def test_init_swarm_simple(self):

View File

@ -29,41 +29,44 @@ class BaseIntegrationTest(unittest.TestCase):
def tearDown(self):
client = docker.from_env(version=TEST_API_VERSION)
for img in self.tmp_imgs:
try:
client.api.remove_image(img)
except docker.errors.APIError:
pass
for container in self.tmp_containers:
try:
client.api.remove_container(container, force=True, v=True)
except docker.errors.APIError:
pass
for network in self.tmp_networks:
try:
client.api.remove_network(network)
except docker.errors.APIError:
pass
for volume in self.tmp_volumes:
try:
client.api.remove_volume(volume)
except docker.errors.APIError:
pass
try:
for img in self.tmp_imgs:
try:
client.api.remove_image(img)
except docker.errors.APIError:
pass
for container in self.tmp_containers:
try:
client.api.remove_container(container, force=True, v=True)
except docker.errors.APIError:
pass
for network in self.tmp_networks:
try:
client.api.remove_network(network)
except docker.errors.APIError:
pass
for volume in self.tmp_volumes:
try:
client.api.remove_volume(volume)
except docker.errors.APIError:
pass
for secret in self.tmp_secrets:
try:
client.api.remove_secret(secret)
except docker.errors.APIError:
pass
for secret in self.tmp_secrets:
try:
client.api.remove_secret(secret)
except docker.errors.APIError:
pass
for config in self.tmp_configs:
try:
client.api.remove_config(config)
except docker.errors.APIError:
pass
for config in self.tmp_configs:
try:
client.api.remove_config(config)
except docker.errors.APIError:
pass
for folder in self.tmp_folders:
shutil.rmtree(folder)
for folder in self.tmp_folders:
shutil.rmtree(folder)
finally:
client.close()
class BaseAPIIntegrationTest(BaseIntegrationTest):