From 7f1bb4a3f5b148f0894f7f22a8fa6abe65e000b1 Mon Sep 17 00:00:00 2001 From: Zane Bitter Date: Fri, 1 Mar 2013 15:24:19 +0100 Subject: [PATCH] Fix flaky unit test This was failing intermittently during the gate tests because there was no dependency in the template for the Instance on the WaitConditionHandle, nor for the WaitCondition on the Instance, so the order in which resources would be created was somewhat random. This patch fixes the test template, and tests updating the metadata should occur *during* creation of the stack, as well after, to replicate a real wait condition (which won't finish creation until the metadata update occurs). Change-Id: I26b5590a9ee90d205bd9f2e31bcb74e68065ac25 Signed-off-by: Zane Bitter --- heat/tests/test_metadata_refresh.py | 52 +++++++++++++++++------------ 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/heat/tests/test_metadata_refresh.py b/heat/tests/test_metadata_refresh.py index fff98ff3..6834ecb9 100644 --- a/heat/tests/test_metadata_refresh.py +++ b/heat/tests/test_metadata_refresh.py @@ -95,7 +95,10 @@ test_template_waitcondition = ''' "ImageId" : "a", "InstanceType" : "m1.large", "KeyName" : { "Ref" : "KeyName" }, - "UserData" : "#!/bin/bash -v\n" + "UserData" : { "Fn::Join" : [ "", [ "#!/bin/bash -v\n", + "echo ", + { "Ref" : "WH" }, + "\n" ] ] } } }, "WH" : { @@ -103,6 +106,7 @@ test_template_waitcondition = ''' }, "WC" : { "Type" : "AWS::CloudFormation::WaitCondition", + "DependsOn": "S1", "Properties" : { "Handle" : {"Ref" : "WH"}, "Timeout" : "5" @@ -216,8 +220,6 @@ class WaitCondMetadataUpdateTest(unittest.TestCase): self.m.StubOutWithMock(wc.WaitConditionHandle, 'identifier') wc.WaitConditionHandle.identifier().MultipleTimes().AndReturn(id) - self.m.StubOutWithMock(wc.WaitConditionHandle, 'get_status') - self.m.StubOutWithMock(wc.WaitCondition, '_create_timeout') self.m.StubOutWithMock(eventlet, 'sleep') return stack @@ -234,30 +236,36 @@ class WaitCondMetadataUpdateTest(unittest.TestCase): self.stack = self.create_stack(template=test_template_waitcondition) - wc.WaitCondition._create_timeout().AndReturn(eventlet.Timeout(5)) - wc.WaitConditionHandle.get_status().AndReturn([]) - eventlet.sleep(1).AndReturn(None) - wc.WaitConditionHandle.get_status().AndReturn([]) - eventlet.sleep(1).AndReturn(None) - wc.WaitConditionHandle.get_status().AndReturn(['SUCCESS']) + watch = self.stack['WC'] + inst = self.stack['S1'] - self.m.ReplayAll() - self.stack.create() + def check_empty(sleep_time): + self.assertEqual(watch.FnGetAtt('Data'), '{}') + self.assertEqual(inst.metadata['test'], '{}') - s1 = self.stack.resources['S1'] - s1._store() - watch = self.stack.resources['WC'] + def update_metadata(id, data, reason): + self.man.metadata_update(self.ctx, + dict(self.stack.identifier()), + 'WH', + {'Data': data, 'Reason': reason, + 'Status': 'SUCCESS', 'UniqueId': id}) - self.assertEqual(watch.FnGetAtt('Data'), '{}') - self.assertEqual(s1.metadata['test'], '{}') + def post_success(sleep_time): + update_metadata('123', 'foo', 'bar') - test_metadata = {'Data': 'foo', 'Reason': 'bar', - 'Status': 'SUCCESS', 'UniqueId': '123'} - self.man.metadata_update(self.ctx, - dict(self.stack.identifier()), - 'WH', test_metadata) + eventlet.sleep(mox.IsA(int)).WithSideEffects(check_empty) + eventlet.sleep(mox.IsA(int)).WithSideEffects(post_success) + + self.m.ReplayAll() + self.stack.create() self.assertEqual(watch.FnGetAtt('Data'), '{"123": "foo"}') - self.assertEqual(s1.metadata['test'], '{"123": "foo"}') + self.assertEqual(inst.metadata['test'], '{"123": "foo"}') + + update_metadata('456', 'blarg', 'wibble') + self.assertEqual(watch.FnGetAtt('Data'), + '{"123": "foo", "456": "blarg"}') + self.assertEqual(inst.metadata['test'], + '{"123": "foo", "456": "blarg"}') self.m.VerifyAll() -- 2.45.2