From e5529a89e19fb2325c73c479e23962dbe9e5ef36 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Tue, 15 Mar 2016 12:43:37 -0400 Subject: [PATCH] Make down idempotent, continue to remove resources if one is missing. Signed-off-by: Daniel Nephin --- compose/network.py | 5 ++++- compose/volume.py | 5 ++++- tests/unit/project_test.py | 22 ++++++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/compose/network.py b/compose/network.py index 81e3b5bf3..affba7c2d 100644 --- a/compose/network.py +++ b/compose/network.py @@ -149,7 +149,10 @@ class ProjectNetworks(object): if not self.use_networking: return for network in self.networks.values(): - network.remove() + try: + network.remove() + except NotFound: + log.warn("Network %s not found.", network.full_name) def initialize(self): if not self.use_networking: diff --git a/compose/volume.py b/compose/volume.py index 17e900876..f440ba40c 100644 --- a/compose/volume.py +++ b/compose/volume.py @@ -76,7 +76,10 @@ class ProjectVolumes(object): def remove(self): for volume in self.volumes.values(): - volume.remove() + try: + volume.remove() + except NotFound: + log.warn("Volume %s not found.", volume.full_name) def initialize(self): try: diff --git a/tests/unit/project_test.py b/tests/unit/project_test.py index c28c21523..bc6421a55 100644 --- a/tests/unit/project_test.py +++ b/tests/unit/project_test.py @@ -4,6 +4,7 @@ from __future__ import unicode_literals import datetime import docker +from docker.errors import NotFound from .. import mock from .. import unittest @@ -12,6 +13,7 @@ from compose.config.types import VolumeFromSpec from compose.const import LABEL_SERVICE from compose.container import Container from compose.project import Project +from compose.service import ImageType from compose.service import Service @@ -476,3 +478,23 @@ class ProjectTest(unittest.TestCase): ), ) self.assertEqual([c.id for c in project.containers()], ['1']) + + def test_down_with_no_resources(self): + project = Project.from_config( + name='test', + client=self.mock_client, + config_data=Config( + version='2', + services=[{ + 'name': 'web', + 'image': 'busybox:latest', + }], + networks={'default': {}}, + volumes={'data': {}}, + ), + ) + self.mock_client.remove_network.side_effect = NotFound(None, None, 'oops') + self.mock_client.remove_volume.side_effect = NotFound(None, None, 'oops') + + project.down(ImageType.all, True) + self.mock_client.remove_image.assert_called_once_with("busybox:latest")