]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
Wrap the watch rule start in a method
authorAngus Salkeld <asalkeld@redhat.com>
Fri, 19 Jul 2013 04:58:37 +0000 (14:58 +1000)
committerAngus Salkeld <asalkeld@redhat.com>
Fri, 19 Jul 2013 04:58:37 +0000 (14:58 +1000)
This is the first step in making the watch task dependant on:
1) whether we even have an alarm in the stack
2) whether the alarm is a ceilometer alarm

The test confirms the current behaviour, which is a periodic task
is created whether or not a watch is in the stack.

bug 1202031
Change-Id: Iebb948f788270fca0dbef61a2e122fe3900d84b2

heat/engine/service.py
heat/tests/test_engine_service.py

index 193eed3fa11674d95266e6b598e79a81776a2ebe..0a065d107ca724ed14df762dae0f23a2ab2bb2c0 100644 (file)
@@ -95,6 +95,10 @@ class EngineService(service.Service):
         """
         pass
 
+    def _start_watch_task(self, stack_id):
+        self._timer_in_thread(stack_id, self._periodic_watcher_task,
+                              sid=stack_id)
+
     def start(self):
         super(EngineService, self).start()
 
@@ -107,7 +111,7 @@ class EngineService(service.Service):
         admin_context = context.get_admin_context()
         stacks = db_api.stack_get_all(admin_context)
         for s in stacks:
-            self._timer_in_thread(s.id, self._periodic_watcher_task, sid=s.id)
+            self._start_watch_task(s.id)
 
     @request_context
     def identify_stack(self, cnxt, stack_name):
@@ -214,8 +218,7 @@ class EngineService(service.Service):
             stack.create()
             if stack.action == stack.CREATE and stack.status == stack.COMPLETE:
                 # Schedule a periodic watcher task for this stack
-                self._timer_in_thread(stack.id, self._periodic_watcher_task,
-                                      sid=stack.id)
+                self._start_watch_task(stack.id)
             else:
                 logger.warning("Stack create failed, status %s" % stack.status)
 
index 652daf1a6f22f1301e9342598e40e38887608793..2ddf4b3809a252fe84a3007dbf2dff0f6b96b2c9 100644 (file)
@@ -66,6 +66,29 @@ wp_template = '''
 }
 '''
 
+alarm_template = '''
+{
+  "AWSTemplateFormatVersion" : "2010-09-09",
+  "Description" : "alarming",
+  "Resources" : {
+    "service_alarm": {
+      "Type": "AWS::CloudWatch::Alarm",
+      "Properties": {
+        "EvaluationPeriods": "1",
+        "AlarmActions": [],
+        "AlarmDescription": "do the thing",
+        "Namespace": "dev/null",
+        "Period": "300",
+        "ComparisonOperator": "GreaterThanThreshold",
+        "Statistic": "SampleCount",
+        "Threshold": "2",
+        "MetricName": "ServiceFailure"
+      }
+    }
+  }
+}
+'''
+
 
 def create_context(mocks, user='stacks_test_user',
                    tenant='test_admin', password='stacks_test_password'):
@@ -87,6 +110,13 @@ def get_wordpress_stack(stack_name, ctx):
     return stack
 
 
+def get_alarm_stack(stack_name, ctx):
+    t = template_format.parse(alarm_template)
+    template = parser.Template(t)
+    stack = parser.Stack(ctx, stack_name, template)
+    return stack
+
+
 def setup_mocks(mocks, stack):
     fc = fakes.FakeClient()
     mocks.StubOutWithMock(instances.Instance, 'nova')
@@ -172,11 +202,10 @@ class DummyThreadGroup(object):
 
     def add_timer(self, interval, callback, initial_delay=None,
                   *args, **kwargs):
-        pass
+        self.threads.append(callback)
 
     def add_thread(self, callback, *args, **kwargs):
         self.threads.append(callback)
-        pass
 
     def stop(self):
         pass
@@ -185,9 +214,9 @@ class DummyThreadGroup(object):
         pass
 
 
-class stackCreateTest(HeatTestCase):
+class StackCreateTest(HeatTestCase):
     def setUp(self):
-        super(stackCreateTest, self).setUp()
+        super(StackCreateTest, self).setUp()
         setup_dummy_db()
 
     def test_wordpress_single_instance_stack_create(self):
@@ -229,10 +258,10 @@ class stackCreateTest(HeatTestCase):
         self.assertEqual('COMPLETE', db_s.status, )
 
 
-class stackServiceCreateUpdateDeleteTest(HeatTestCase):
+class StackServiceCreateUpdateDeleteTest(HeatTestCase):
 
     def setUp(self):
-        super(stackServiceCreateUpdateDeleteTest, self).setUp()
+        super(StackServiceCreateUpdateDeleteTest, self).setUp()
         self.username = 'stack_service_create_test_user'
         self.tenant = 'stack_service_create_test_tenant'
         setup_dummy_db()
@@ -502,10 +531,10 @@ class stackServiceCreateUpdateDeleteTest(HeatTestCase):
                           None, {})
 
 
-class stackServiceSuspendResumeTest(HeatTestCase):
+class StackServiceSuspendResumeTest(HeatTestCase):
 
     def setUp(self):
-        super(stackServiceSuspendResumeTest, self).setUp()
+        super(StackServiceSuspendResumeTest, self).setUp()
         self.username = 'stack_service_suspend_test_user'
         self.tenant = 'stack_service_suspend_test_tenant'
         setup_dummy_db()
@@ -570,10 +599,10 @@ class stackServiceSuspendResumeTest(HeatTestCase):
         self.m.VerifyAll()
 
 
-class stackServiceTest(HeatTestCase):
+class StackServiceTest(HeatTestCase):
 
     def setUp(self):
-        super(stackServiceTest, self).setUp()
+        super(StackServiceTest, self).setUp()
 
         self.username = 'stack_service_test_user'
         self.tenant = 'stack_service_test_tenant'
@@ -1104,6 +1133,26 @@ class stackServiceTest(HeatTestCase):
 
         self.m.VerifyAll()
 
+    @stack_context('periodic_watch_task_not_created')
+    def test_periodic_watch_task_not_created(self):
+        self.eng.stg[self.stack.id] = DummyThreadGroup()
+        self.eng._start_watch_task(self.stack.id)
+        self.assertEqual([self.eng._periodic_watcher_task],
+                         self.eng.stg[self.stack.id].threads)
+
+    def test_periodic_watch_task_created(self):
+        stack = get_alarm_stack('period_watch_task_created',
+                                create_context(self.m))
+        self.stack = stack
+        self.m.ReplayAll()
+        stack.store()
+        stack.create()
+        self.eng.stg[stack.id] = DummyThreadGroup()
+        self.eng._start_watch_task(stack.id)
+        self.assertEqual([self.eng._periodic_watcher_task],
+                         self.eng.stg[stack.id].threads)
+        self.stack.delete()
+
     @stack_context('service_show_watch_test_stack', False)
     @utils.wr_delete_after
     def test_show_watch(self):