]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
Fix cloud_watch delete when the watchrule is not found.
authorAngus Salkeld <asalkeld@redhat.com>
Thu, 25 Jul 2013 04:30:39 +0000 (14:30 +1000)
committerAngus Salkeld <asalkeld@redhat.com>
Thu, 25 Jul 2013 04:30:39 +0000 (14:30 +1000)
Also start using mock instead of mox.

Change-Id: Ia3baf510ffeafc73c42da7e2830819055c244c51

heat/engine/resources/cloud_watch.py
heat/tests/test_cloudwatch.py [new file with mode: 0644]
test-requirements.txt

index d4d787efa3ff1d11615056ea7f1047554da19fb1..c5cab169c01f8694a2544c8a09187add6c69af47 100644 (file)
@@ -109,7 +109,7 @@ class CloudWatchAlarm(resource.Resource):
             wr = watchrule.WatchRule.load(
                 self.context, watch_name=self.physical_resource_name())
             wr.destroy()
-        except exception.NotFound:
+        except exception.WatchRuleNotFound:
             pass
 
     def handle_suspend(self):
diff --git a/heat/tests/test_cloudwatch.py b/heat/tests/test_cloudwatch.py
new file mode 100644 (file)
index 0000000..3a877e4
--- /dev/null
@@ -0,0 +1,79 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+#    Licensed under the Apache License, Version 2.0 (the "License"); you may
+#    not use this file except in compliance with the License. You may obtain
+#    a copy of the License at
+#
+#         http://www.apache.org/licenses/LICENSE-2.0
+#
+#    Unless required by applicable law or agreed to in writing, software
+#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+#    License for the specific language governing permissions and limitations
+#    under the License.
+
+from mock import patch
+
+from heat.common import exception
+from heat.common import template_format
+from heat.tests import common
+from heat.tests import utils
+from heat.engine import scheduler
+from heat.engine import watchrule
+
+
+AWS_CloudWatch_Alarm = '''
+HeatTemplateFormatVersion: '2012-12-12'
+Description: Template which tests alarms
+Resources:
+  test_me:
+    Type: AWS::CloudWatch::Alarm
+    Properties:
+      MetricName: cpu_util
+      Namespace: AWS/EC2
+      Statistic: Average
+      Period: '60'
+      EvaluationPeriods: '1'
+      Threshold: '50'
+      ComparisonOperator: GreaterThanThreshold
+'''
+
+
+class CloudWatchAlarmTest(common.HeatTestCase):
+
+    def setUp(self):
+        super(CloudWatchAlarmTest, self).setUp()
+        utils.setup_dummy_db()
+
+    def parse_stack(self):
+        t = template_format.parse(AWS_CloudWatch_Alarm)
+        return utils.parse_stack(t)
+
+    @utils.wr_delete_after
+    def test_resource_create_good(self):
+        s = self.parse_stack()
+        self.wr = s['test_me']
+        self.assertEqual(None, scheduler.TaskRunner(s['test_me'].create)())
+
+    def test_resource_create_failed(self):
+        s = self.parse_stack()
+        with patch.object(watchrule.WatchRule, 'store') as bad_store:
+            bad_store.side_effect = KeyError('any random failure')
+            task_func = scheduler.TaskRunner(s['test_me'].create)
+            self.assertRaises(exception.ResourceFailure, task_func)
+
+    def test_resource_delete_good(self):
+        s = self.parse_stack()
+        self.assertEqual(None, scheduler.TaskRunner(s['test_me'].create)())
+        self.assertEqual(None, scheduler.TaskRunner(s['test_me'].delete)())
+
+    @utils.wr_delete_after
+    def test_resource_delete_notfound(self):
+        # if a resource is not found, handle_delete() should not raise
+        # an exception.
+        s = self.parse_stack()
+        self.wr = s['test_me']
+        self.assertEqual(None, scheduler.TaskRunner(s['test_me'].create)())
+        with patch.object(watchrule.WatchRule, 'destroy') as bad_destroy:
+            bad_destroy.side_effect = exception.WatchRuleNotFound
+            self.assertEqual(None, scheduler.TaskRunner(s['test_me'].delete)())
index 6d84efdadd37d588329eabab4f72b179364ce9f9..681da5fafefa3478b7c21551b9d5d4a6667ac507 100644 (file)
@@ -6,6 +6,7 @@ hacking>=0.5.3,<0.6
 
 coverage
 discover
+mock>=0.8.0
 mox==0.5.3
 testtools>=0.9.29
 testrepository>=0.0.13