From 4320178066565608351c37a720922cbd36ac1f03 Mon Sep 17 00:00:00 2001 From: Steven Hardy Date: Mon, 16 Jun 2014 19:08:49 +0100 Subject: [PATCH] test_autoscaling refactor suspend/resume stubbing Rework to encapsulate the suspend/resume stubbing in functions and add a stub of the image validation, which is required because the properties get revalidated when we create the events associated with the state transition (ref bug #1324102). With the current keystoneclient mocking this is not visible in the tests because the error creating the client to do the validation is obscured by the fact that the Event constructor tolerates a validation error and the error creating the client is just treated as an unexpected validation error by the CustomConstraint code. Moving to a model where clients.keystone() is consistently mocked exposes the fact that we will try to connect to glance on suspend when the event is created if the validation is not correctly stubbed. Partial-Bug: #1306294 Change-Id: I001d3a432397d4cdfaa62145228aad7eaf051b98 --- heat/tests/test_autoscaling.py | 86 ++++++++++++++++++---------------- 1 file changed, 46 insertions(+), 40 deletions(-) diff --git a/heat/tests/test_autoscaling.py b/heat/tests/test_autoscaling.py index c1a024677..bc2fdafb2 100644 --- a/heat/tests/test_autoscaling.py +++ b/heat/tests/test_autoscaling.py @@ -183,6 +183,42 @@ class AutoScalingTest(HeatTestCase): instance.Instance.check_delete_complete( task).MultipleTimes().AndReturn(True) + def _stub_suspend(self, cookies=[], with_error=None): + self.m.StubOutWithMock(instance.Instance, 'handle_suspend') + self.m.StubOutWithMock(instance.Instance, 'check_suspend_complete') + self.m.StubOutWithMock(image.ImageConstraint, "validate") + image.ImageConstraint.validate( + mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(True) + if with_error: + instance.Instance.handle_suspend().AndRaise( + exception.Error(with_error)) + return + inst_cookies = cookies or [(object(), object(), object())] + for cookie in inst_cookies: + instance.Instance.handle_suspend().InAnyOrder().AndReturn(cookie) + instance.Instance.check_suspend_complete( + cookie).InAnyOrder().AndReturn(False) + instance.Instance.check_suspend_complete( + cookie).InAnyOrder().AndReturn(True) + + def _stub_resume(self, cookies=[], with_error=None): + self.m.StubOutWithMock(instance.Instance, 'handle_resume') + self.m.StubOutWithMock(instance.Instance, 'check_resume_complete') + self.m.StubOutWithMock(image.ImageConstraint, "validate") + image.ImageConstraint.validate( + mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(True) + if with_error: + instance.Instance.handle_resume().AndRaise( + exception.Error(with_error)) + return + inst_cookies = cookies or [(object(), object(), object())] + for cookie in inst_cookies: + instance.Instance.handle_resume().InAnyOrder().AndReturn(cookie) + instance.Instance.check_resume_complete( + cookie).InAnyOrder().AndReturn(False) + instance.Instance.check_resume_complete( + cookie).InAnyOrder().AndReturn(True) + def _stub_lb_reload(self, num, unset=True, nochange=False): expected_list = [self.dummy_instance_id] * num if unset: @@ -348,12 +384,7 @@ class AutoScalingTest(HeatTestCase): self.m.VerifyAll() self.m.UnsetStubs() - self.m.StubOutWithMock(instance.Instance, 'handle_suspend') - self.m.StubOutWithMock(instance.Instance, 'check_suspend_complete') - inst_cookie = (object(), object(), object()) - instance.Instance.handle_suspend().AndReturn(inst_cookie) - instance.Instance.check_suspend_complete(inst_cookie).AndReturn(False) - instance.Instance.check_suspend_complete(inst_cookie).AndReturn(True) + self._stub_suspend() self.m.ReplayAll() scheduler.TaskRunner(rsrc.suspend)() @@ -380,12 +411,7 @@ class AutoScalingTest(HeatTestCase): self.m.VerifyAll() self.m.UnsetStubs() - self.m.StubOutWithMock(instance.Instance, 'handle_resume') - self.m.StubOutWithMock(instance.Instance, 'check_resume_complete') - inst_cookie = (object(), object(), object()) - instance.Instance.handle_resume().AndReturn(inst_cookie) - instance.Instance.check_resume_complete(inst_cookie).AndReturn(False) - instance.Instance.check_resume_complete(inst_cookie).AndReturn(True) + self._stub_resume() self.m.ReplayAll() rsrc.state_set(rsrc.SUSPEND, rsrc.COMPLETE) @@ -418,16 +444,8 @@ class AutoScalingTest(HeatTestCase): self.m.VerifyAll() self.m.UnsetStubs() - self.m.StubOutWithMock(instance.Instance, 'handle_suspend') - self.m.StubOutWithMock(instance.Instance, 'check_suspend_complete') - inst_cookie1 = ('foo1', 'foo2', 'foo3') - inst_cookie2 = ('bar1', 'bar2', 'bar3') - instance.Instance.handle_suspend().InAnyOrder().AndReturn(inst_cookie1) - instance.Instance.handle_suspend().InAnyOrder().AndReturn(inst_cookie2) - instance.Instance.check_suspend_complete(inst_cookie1).InAnyOrder( - ).AndReturn(True) - instance.Instance.check_suspend_complete(inst_cookie2).InAnyOrder( - ).AndReturn(True) + self._stub_suspend(cookies=[('foo1', 'foo2', 'foo3'), + ('bar1', 'bar2', 'bar3')]) self.m.ReplayAll() scheduler.TaskRunner(rsrc.suspend)() @@ -456,16 +474,8 @@ class AutoScalingTest(HeatTestCase): self.m.VerifyAll() self.m.UnsetStubs() - self.m.StubOutWithMock(instance.Instance, 'handle_resume') - self.m.StubOutWithMock(instance.Instance, 'check_resume_complete') - inst_cookie1 = ('foo1', 'foo2', 'foo3') - inst_cookie2 = ('bar1', 'bar2', 'bar3') - instance.Instance.handle_resume().InAnyOrder().AndReturn(inst_cookie1) - instance.Instance.handle_resume().InAnyOrder().AndReturn(inst_cookie2) - instance.Instance.check_resume_complete(inst_cookie1).InAnyOrder( - ).AndReturn(True) - instance.Instance.check_resume_complete(inst_cookie2).InAnyOrder( - ).AndReturn(True) + self._stub_resume(cookies=[('foo1', 'foo2', 'foo3'), + ('bar1', 'bar2', 'bar3')]) self.m.ReplayAll() rsrc.state_set(rsrc.SUSPEND, rsrc.COMPLETE) @@ -496,15 +506,13 @@ class AutoScalingTest(HeatTestCase): self.m.VerifyAll() self.m.UnsetStubs() - self.m.StubOutWithMock(instance.Instance, 'handle_suspend') - self.m.StubOutWithMock(instance.Instance, 'check_suspend_complete') - instance.Instance.handle_suspend().AndRaise(Exception('oops')) + self._stub_suspend(with_error='oops') self.m.ReplayAll() sus_task = scheduler.TaskRunner(rsrc.suspend) self.assertRaises(exception.ResourceFailure, sus_task, ()) self.assertEqual((rsrc.SUSPEND, rsrc.FAILED), rsrc.state) - self.assertEqual('Error: Resource SUSPEND failed: Exception: oops', + self.assertEqual('Error: Resource SUSPEND failed: Error: oops', rsrc.status_reason) rsrc.delete() @@ -528,9 +536,7 @@ class AutoScalingTest(HeatTestCase): self.m.VerifyAll() self.m.UnsetStubs() - self.m.StubOutWithMock(instance.Instance, 'handle_resume') - self.m.StubOutWithMock(instance.Instance, 'check_resume_complete') - instance.Instance.handle_resume().AndRaise(Exception('oops')) + self._stub_resume(with_error='oops') self.m.ReplayAll() rsrc.state_set(rsrc.SUSPEND, rsrc.COMPLETE) @@ -540,7 +546,7 @@ class AutoScalingTest(HeatTestCase): sus_task = scheduler.TaskRunner(rsrc.resume) self.assertRaises(exception.ResourceFailure, sus_task, ()) self.assertEqual((rsrc.RESUME, rsrc.FAILED), rsrc.state) - self.assertEqual('Error: Resource RESUME failed: Exception: oops', + self.assertEqual('Error: Resource RESUME failed: Error: oops', rsrc.status_reason) rsrc.delete()