]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
Fix flaky unit test
authorZane Bitter <zbitter@redhat.com>
Fri, 1 Mar 2013 14:24:19 +0000 (15:24 +0100)
committerZane Bitter <zbitter@redhat.com>
Fri, 1 Mar 2013 14:24:19 +0000 (15:24 +0100)
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 <zbitter@redhat.com>
heat/tests/test_metadata_refresh.py

index fff98ff39d6aa2e3bdb708a9b857c8d79c8b1836..6834ecb9a69d6d8111871c868f2f5f3b8143a7ff 100644 (file)
@@ -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()