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):