Source gnocchi alarms resources in the right way

Previously we missed typo in resource mapping name. It happend due to
using hardcoded name in registring new resource in environment.
Other resources in contrib use alternative approach, i.e. they register
classes using mapping function from native module.

This patch adds same approach for Gnocchi alarms. Also small refactoring
for re-using existing code in test.

Change-Id: Ibe8dfc54f9502fb6e97e496fb51b4e46e7f4d8a9
Related-Bug: #1431873
This commit is contained in:
Sergey Kraynev 2015-03-16 03:55:27 -04:00
parent 9727d00f9b
commit bde1dbcf93
2 changed files with 33 additions and 139 deletions

View File

@ -16,6 +16,7 @@ import copy
from ceilometerclient import exc as ceilometerclient_exc
import mock
import mox
import six
from heat.common import exception
from heat.common import template_format
@ -25,7 +26,7 @@ from heat.engine import scheduler
from heat.tests import common
from heat.tests import utils
from ..resources import gnocchi_alarm # noqa
from ..resources import gnocchi_alarm as gnocchi # noqa
gnocchi_resources_alarm_template = '''
heat_template_version: 2013-05-23
@ -70,31 +71,18 @@ class FakeCeilometerAlarm(object):
alarm_id = 'foo'
class FakeCeilometerAlarms(object):
def create(self, **kwargs):
pass
def update(self, **kwargs):
pass
def delete(self, alarm_id):
pass
class FakeCeilometerClient(object):
alarms = FakeCeilometerAlarms()
class GnocchiResourcesAlarmTest(common.HeatTestCase):
def setUp(self):
super(GnocchiResourcesAlarmTest, self).setUp()
self.fc = FakeCeilometerClient()
resource._register_class("OS::Ceilometer::GnocchiResourcesAlarm",
gnocchi_alarm.CeilometerGnocchiResourcesAlarm)
self.m.StubOutWithMock(ceilometer.CeilometerClientPlugin, '_create')
self.fc = mock.Mock()
self._register_resources()
def _register_resources(self):
for res_name, res_class in six.iteritems(gnocchi.resource_mapping()):
resource._register_class(res_name, res_class)
def create_alarm(self):
self.m.StubOutWithMock(ceilometer.CeilometerClientPlugin, '_create')
ceilometer.CeilometerClientPlugin._create().AndReturn(
self.fc)
self.m.StubOutWithMock(self.fc.alarms, 'create')
@ -120,18 +108,9 @@ class GnocchiResourcesAlarmTest(common.HeatTestCase):
snippet = template_format.parse(gnocchi_resources_alarm_template)
stack = utils.parse_stack(snippet)
resource_defns = stack.t.resource_definitions(stack)
return gnocchi_alarm.CeilometerGnocchiResourcesAlarm(
return gnocchi.CeilometerGnocchiResourcesAlarm(
'GnoResAlarm', resource_defns['GnoResAlarm'], stack)
def test_create(self):
rsrc = self.create_alarm()
self.m.ReplayAll()
scheduler.TaskRunner(rsrc.create)()
self.assertEqual((rsrc.CREATE, rsrc.COMPLETE), rsrc.state)
self.assertEqual('foo', rsrc.resource_id)
self.m.VerifyAll()
def test_update(self):
rsrc = self.create_alarm()
self.m.StubOutWithMock(self.fc.alarms, 'update')
@ -151,6 +130,24 @@ class GnocchiResourcesAlarmTest(common.HeatTestCase):
self.m.VerifyAll()
def _prepare_check_resource(self):
snippet = template_format.parse(gnocchi_resources_alarm_template)
stack = utils.parse_stack(snippet)
res = stack['GnoResAlarm']
res.ceilometer = mock.Mock()
mock_alarm = mock.Mock(enabled=True, state='ok')
res.ceilometer().alarms.get.return_value = mock_alarm
return res
def test_create(self):
rsrc = self.create_alarm()
self.m.ReplayAll()
scheduler.TaskRunner(rsrc.create)()
self.assertEqual((rsrc.CREATE, rsrc.COMPLETE), rsrc.state)
self.assertEqual('foo', rsrc.resource_id)
self.m.VerifyAll()
def test_suspend(self):
rsrc = self.create_alarm()
self.m.StubOutWithMock(self.fc.alarms, 'update')
@ -201,15 +198,6 @@ class GnocchiResourcesAlarmTest(common.HeatTestCase):
self.m.VerifyAll()
def _prepare_check_resource(self):
snippet = template_format.parse(gnocchi_resources_alarm_template)
stack = utils.parse_stack(snippet)
res = stack['GnoResAlarm']
res.ceilometer = mock.Mock()
mock_alarm = mock.Mock(enabled=True, state='ok')
res.ceilometer().alarms.get.return_value = mock_alarm
return res
def test_check(self):
res = self._prepare_check_resource()
scheduler.TaskRunner(res.check)()
@ -225,16 +213,10 @@ class GnocchiResourcesAlarmTest(common.HeatTestCase):
self.assertIn('Boom', res.status_reason)
class GnocchiMetricsAlarmTest(common.HeatTestCase):
def setUp(self):
super(GnocchiMetricsAlarmTest, self).setUp()
self.fc = FakeCeilometerClient()
resource._register_class("OS::Ceilometer::GnocchiMetricsAlarm",
gnocchi_alarm.CeilometerGnocchiMetricsAlarm)
self.m.StubOutWithMock(ceilometer.CeilometerClientPlugin, '_create')
class GnocchiMetricsAlarmTest(GnocchiResourcesAlarmTest):
def create_alarm(self):
self.m.StubOutWithMock(ceilometer.CeilometerClientPlugin, '_create')
ceilometer.CeilometerClientPlugin._create().AndReturn(
self.fc)
self.m.StubOutWithMock(self.fc.alarms, 'create')
@ -259,18 +241,9 @@ class GnocchiMetricsAlarmTest(common.HeatTestCase):
snippet = template_format.parse(gnocchi_metrics_alarm_template)
stack = utils.parse_stack(snippet)
resource_defns = stack.t.resource_definitions(stack)
return gnocchi_alarm.CeilometerGnocchiMetricsAlarm(
return gnocchi.CeilometerGnocchiMetricsAlarm(
'GnoMetricsAlarm', resource_defns['GnoMetricsAlarm'], stack)
def test_create(self):
rsrc = self.create_alarm()
self.m.ReplayAll()
scheduler.TaskRunner(rsrc.create)()
self.assertEqual((rsrc.CREATE, rsrc.COMPLETE), rsrc.state)
self.assertEqual('foo', rsrc.resource_id)
self.m.VerifyAll()
def test_update(self):
rsrc = self.create_alarm()
self.m.StubOutWithMock(self.fc.alarms, 'update')
@ -292,56 +265,6 @@ class GnocchiMetricsAlarmTest(common.HeatTestCase):
self.m.VerifyAll()
def test_suspend(self):
rsrc = self.create_alarm()
self.m.StubOutWithMock(self.fc.alarms, 'update')
self.fc.alarms.update(alarm_id='foo', enabled=False)
self.m.ReplayAll()
scheduler.TaskRunner(rsrc.create)()
scheduler.TaskRunner(rsrc.suspend)()
self.assertEqual((rsrc.SUSPEND, rsrc.COMPLETE), rsrc.state)
self.m.VerifyAll()
def test_resume(self):
rsrc = self.create_alarm()
self.m.StubOutWithMock(self.fc.alarms, 'update')
self.fc.alarms.update(alarm_id='foo', enabled=True)
self.m.ReplayAll()
scheduler.TaskRunner(rsrc.create)()
rsrc.state_set(rsrc.SUSPEND, rsrc.COMPLETE)
scheduler.TaskRunner(rsrc.resume)()
self.assertEqual((rsrc.RESUME, rsrc.COMPLETE), rsrc.state)
self.m.VerifyAll()
def test_delete(self):
rsrc = self.create_alarm()
self.m.StubOutWithMock(self.fc.alarms, 'delete')
self.fc.alarms.delete('foo')
self.m.ReplayAll()
scheduler.TaskRunner(rsrc.create)()
scheduler.TaskRunner(rsrc.delete)()
self.assertEqual((rsrc.DELETE, rsrc.COMPLETE), rsrc.state)
self.m.VerifyAll()
def test_delete_not_found(self):
rsrc = self.create_alarm()
self.m.StubOutWithMock(self.fc.alarms, 'delete')
self.fc.alarms.delete('foo').AndRaise(
ceilometerclient_exc.HTTPNotFound())
self.m.ReplayAll()
scheduler.TaskRunner(rsrc.create)()
scheduler.TaskRunner(rsrc.delete)()
self.assertEqual((rsrc.DELETE, rsrc.COMPLETE), rsrc.state)
self.m.VerifyAll()
def _prepare_check_resource(self):
snippet = template_format.parse(gnocchi_metrics_alarm_template)
stack = utils.parse_stack(snippet)
@ -350,17 +273,3 @@ class GnocchiMetricsAlarmTest(common.HeatTestCase):
mock_alarm = mock.Mock(enabled=True, state='ok')
res.ceilometer().alarms.get.return_value = mock_alarm
return res
def test_check(self):
res = self._prepare_check_resource()
scheduler.TaskRunner(res.check)()
self.assertEqual((res.CHECK, res.COMPLETE), res.state)
def test_check_failure(self):
res = self._prepare_check_resource()
res.ceilometer().alarms.get.side_effect = Exception('Boom')
self.assertRaises(exception.ResourceFailure,
scheduler.TaskRunner(res.check))
self.assertEqual((res.CHECK, res.FAILED), res.state)
self.assertIn('Boom', res.status_reason)

View File

@ -111,21 +111,6 @@ class FakeCeilometerAlarm(object):
alarm_id = 'foo'
class FakeCeilometerAlarms(object):
def create(self, **kwargs):
pass
def update(self, **kwargs):
pass
def delete(self, alarm_id):
pass
class FakeCeilometerClient(object):
alarms = FakeCeilometerAlarms()
class CeilometerAlarmTest(common.HeatTestCase):
def setUp(self):
super(CeilometerAlarmTest, self).setUp()
@ -137,7 +122,7 @@ class CeilometerAlarmTest(common.HeatTestCase):
'http://server.test:8000/v1/waitcondition')
self.stub_keystoneclient()
self.fa = FakeCeilometerClient()
self.fa = mock.Mock()
def create_stack(self, template=None):
if template is None:
@ -493,7 +478,7 @@ class CombinationAlarmTest(common.HeatTestCase):
def setUp(self):
super(CombinationAlarmTest, self).setUp()
self.fc = FakeCeilometerClient()
self.fc = mock.Mock()
self.m.StubOutWithMock(ceilometer.CeilometerClientPlugin, '_create')
def create_alarm(self):