From 464731b91f3ad179983d8b66d2049ab50c694834 Mon Sep 17 00:00:00 2001 From: Ethan Lynn Date: Wed, 20 Jan 2016 17:36:42 +0800 Subject: [PATCH] Raise NotFound exception when get a deleted stack Heat will always return a stack record if query with an id, so if a stack is deleted, we can still get it from heat, it will break wait_for_delete function. Change-Id: I44367fd053201dd2a20dc14dd9067e29a3a7d2a6 Closes-Bug: #1528996 --- openstack/orchestration/v1/stack.py | 7 +++++++ .../tests/unit/orchestration/v1/test_stack.py | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/openstack/orchestration/v1/stack.py b/openstack/orchestration/v1/stack.py index 211852c6..0c47cc1d 100644 --- a/openstack/orchestration/v1/stack.py +++ b/openstack/orchestration/v1/stack.py @@ -108,6 +108,13 @@ class Stack(resource.Resource): "No stack found for %s" % name_or_id) return stk + def get(self, session, include_headers=False, args=None): + stk = super(Stack, self).get(session, include_headers, args) + if stk and stk.status in ['DELETE_COMPLETE', 'ADOPT_COMPLETE']: + raise exceptions.NotFoundException( + "No stack found for %s" % stk.id) + return stk + class StackPreview(Stack): base_path = '/stacks/preview' diff --git a/openstack/tests/unit/orchestration/v1/test_stack.py b/openstack/tests/unit/orchestration/v1/test_stack.py index e8332ac8..d7f408b8 100644 --- a/openstack/tests/unit/orchestration/v1/test_stack.py +++ b/openstack/tests/unit/orchestration/v1/test_stack.py @@ -178,3 +178,22 @@ class TestStack(testtools.TestCase): sess, 'fake_name', ignore_missing=False) self.assertEqual('ResourceNotFound: No stack found for fake_name', six.text_type(ex)) + + @mock.patch.object(resource.Resource, 'get') + def test_get(self, mock_get): + sess = mock.Mock() + sot = stack.Stack(FAKE) + deleted_stack = mock.Mock(id=FAKE_ID, status='DELETE_COMPLETE') + normal_stack = mock.Mock(status='CREATE_COMPLETE') + mock_get.side_effect = [ + normal_stack, + exceptions.NotFoundException(message='oops'), + deleted_stack, + ] + + self.assertEqual(normal_stack, sot.get(sess)) + ex = self.assertRaises(exceptions.NotFoundException, sot.get, sess) + self.assertEqual('NotFoundException: oops', six.text_type(ex)) + ex = self.assertRaises(exceptions.NotFoundException, sot.get, sess) + self.assertEqual('NotFoundException: No stack found for %s' % FAKE_ID, + six.text_type(ex))