From cbb7b1ab1d2d03d3f4c56bf49695c4c81a4fa08f Mon Sep 17 00:00:00 2001 From: WangChangyu Date: Fri, 16 Mar 2018 14:37:18 +0800 Subject: [PATCH] Delete container failed if container host is None Change-Id: Id1fe3d4f05d0714a57416097d017f908f0788ed8 Closes-Bug: #1756257 --- zun/api/controllers/v1/containers.py | 5 ++++- .../unit/api/controllers/v1/test_containers.py | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/zun/api/controllers/v1/containers.py b/zun/api/controllers/v1/containers.py index f9902a7df..16bc0379f 100644 --- a/zun/api/controllers/v1/containers.py +++ b/zun/api/controllers/v1/containers.py @@ -665,7 +665,10 @@ class ContainersController(base.Controller): req_version=req_version, min_version=min_version) container.status = consts.DELETING - compute_api.container_delete(context, container, force) + if container.host: + compute_api.container_delete(context, container, force) + else: + container.destroy(context) pecan.response.status = 204 @pecan.expose('json') diff --git a/zun/tests/unit/api/controllers/v1/test_containers.py b/zun/tests/unit/api/controllers/v1/test_containers.py index 5938cb1bb..57edac51d 100644 --- a/zun/tests/unit/api/controllers/v1/test_containers.py +++ b/zun/tests/unit/api/controllers/v1/test_containers.py @@ -1267,6 +1267,20 @@ class TestContainerController(api_base.FunctionalTest): self.assertRaises(AppError, self.delete, '/v1/containers/%s' % uuid) + @patch('zun.objects.Container.destroy') + @patch('zun.common.utils.validate_container_state') + @patch('zun.objects.Container.get_by_uuid') + def test_delete_container_without_host(self, mock_get_by_uuid, + mock_validate, mock_destroy): + test_container = utils.get_test_container(host="") + test_container_obj = objects.Container(self.context, **test_container) + mock_get_by_uuid.return_value = test_container_obj + container_uuid = test_container.get('uuid') + response = self.delete('/v1/containers/%s/' % container_uuid) + self.assertEqual(204, response.status_int) + mock_validate.assert_called_once() + mock_destroy.assert_called_once() + @patch('zun.common.utils.validate_container_state') @patch('zun.compute.api.API.container_kill') @patch('zun.objects.Container.get_by_uuid')