From 1b29058852e2a6ff78a420928ebc59c48f3aea12 Mon Sep 17 00:00:00 2001 From: Sergio Schvezov Date: Tue, 9 Apr 2019 16:01:17 -0300 Subject: [PATCH] build providers: idempotent destroy for LXD (#2529) Fixes SNAPCRAFT-MB Signed-off-by: Sergio Schvezov --- snapcraft/internal/build_providers/_lxd/_lxd.py | 5 +++++ tests/unit/build_providers/lxd/test_lxd.py | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/snapcraft/internal/build_providers/_lxd/_lxd.py b/snapcraft/internal/build_providers/_lxd/_lxd.py index 73f9364571..66f2b80365 100644 --- a/snapcraft/internal/build_providers/_lxd/_lxd.py +++ b/snapcraft/internal/build_providers/_lxd/_lxd.py @@ -193,6 +193,11 @@ def _start(self): self._run(command=["cloud-init", "status", "--wait"]) def _stop(self): + # If _container is still None here it means creation/starting was not + # successful. + if self._container is None: + return + self._container.sync() if self._container.status.lower() != "stopped": diff --git a/tests/unit/build_providers/lxd/test_lxd.py b/tests/unit/build_providers/lxd/test_lxd.py index 41f2c69f0c..bf02fef85d 100644 --- a/tests/unit/build_providers/lxd/test_lxd.py +++ b/tests/unit/build_providers/lxd/test_lxd.py @@ -221,6 +221,11 @@ def test_clean_project_new_instance(self): container.delete_mock.assert_called_once_with(wait=True) self.assertThat(instance.clean_project(), Equals(True)) + def test_destroy_when_not_created(self): + instance = LXD(project=self.project, echoer=self.echoer_mock) + # This call should not fail + instance.destroy() + class LXDLaunchedTest(LXDBaseTest): def setUp(self):