Merge "Add validation for mistral cron trigger"

This commit is contained in:
Jenkins 2016-03-28 04:46:34 +00:00 committed by Gerrit Code Review
commit 18a1f5a5e1
2 changed files with 46 additions and 6 deletions

View File

@ -13,6 +13,7 @@
from oslo_log import log as logging from oslo_log import log as logging
from heat.common import exception
from heat.common.i18n import _ from heat.common.i18n import _
from heat.common.i18n import _LW from heat.common.i18n import _LW
from heat.engine import attributes from heat.engine import attributes
@ -109,6 +110,13 @@ class CronTrigger(resource.Resource):
entity = 'cron_triggers' entity = 'cron_triggers'
def validate(self):
super(CronTrigger, self).validate()
if not (self.properties[self.PATTERN]
or self.properties[self.FIRST_TIME]):
raise exception.PropertyUnspecifiedError(self.PATTERN,
self.FIRST_TIME)
def _cron_trigger_name(self): def _cron_trigger_name(self):
return self.properties.get(self.NAME) or self.physical_resource_name() return self.properties.get(self.NAME) or self.physical_resource_name()

View File

@ -13,6 +13,7 @@
import mock import mock
from heat.common import exception
from heat.common import template_format from heat.common import template_format
from heat.engine import resources from heat.engine import resources
from heat.engine.resources.openstack.mistral import cron_trigger from heat.engine.resources.openstack.mistral import cron_trigger
@ -70,6 +71,12 @@ class MistralCronTriggerTest(common.HeatTestCase):
self.client.cron_triggers.get.return_value = FakeCronTrigger( self.client.cron_triggers.get.return_value = FakeCronTrigger(
'my_cron_trigger') 'my_cron_trigger')
scheduler.TaskRunner(ct.create)() scheduler.TaskRunner(ct.create)()
return ct
def test_create(self):
ct = self._create_resource('trigger', self.rsrc_defn, self.stack)
expected_state = (ct.CREATE, ct.COMPLETE)
self.assertEqual(expected_state, ct.state)
args = self.client.cron_triggers.create.call_args[1] args = self.client.cron_triggers.create.call_args[1]
self.assertEqual('* * 0 * *', args['pattern']) self.assertEqual('* * 0 * *', args['pattern'])
self.assertEqual('get_first_glance_image', args['workflow_name']) self.assertEqual('get_first_glance_image', args['workflow_name'])
@ -77,12 +84,6 @@ class MistralCronTriggerTest(common.HeatTestCase):
self.assertEqual('2015-04-08 06:20', args['first_time']) self.assertEqual('2015-04-08 06:20', args['first_time'])
self.assertEqual(3, args['count']) self.assertEqual(3, args['count'])
self.assertEqual('my_cron_trigger', ct.resource_id) self.assertEqual('my_cron_trigger', ct.resource_id)
return ct
def test_create(self):
ct = self._create_resource('trigger', self.rsrc_defn, self.stack)
expected_state = (ct.CREATE, ct.COMPLETE)
self.assertEqual(expected_state, ct.state)
def test_attributes(self): def test_attributes(self):
ct = self._create_resource('trigger', self.rsrc_defn, self.stack) ct = self._create_resource('trigger', self.rsrc_defn, self.stack)
@ -90,3 +91,34 @@ class MistralCronTriggerTest(common.HeatTestCase):
ct.FnGetAtt('next_execution_time')) ct.FnGetAtt('next_execution_time'))
self.assertEqual(3, ct.FnGetAtt('remaining_executions')) self.assertEqual(3, ct.FnGetAtt('remaining_executions'))
self.assertEqual({'trigger': 'info'}, ct.FnGetAtt('show')) self.assertEqual({'trigger': 'info'}, ct.FnGetAtt('show'))
def test_validate_fail(self):
t = template_format.parse(stack_template)
del t['resources']['cron_trigger']['properties']['first_time']
del t['resources']['cron_trigger']['properties']['pattern']
stack = utils.parse_stack(t)
resource_defns = stack.t.resource_definitions(stack)
self.rsrc_defn = resource_defns['cron_trigger']
ct = self._create_resource('trigger', self.rsrc_defn, self.stack)
msg = ("At least one of the following properties must be specified: "
"pattern, first_time")
self.assertRaisesRegexp(exception.PropertyUnspecifiedError, msg,
ct.validate)
def test_validate_ok_without_first_time(self):
t = template_format.parse(stack_template)
del t['resources']['cron_trigger']['properties']['first_time']
stack = utils.parse_stack(t)
resource_defns = stack.t.resource_definitions(stack)
self.rsrc_defn = resource_defns['cron_trigger']
ct = self._create_resource('trigger', self.rsrc_defn, self.stack)
ct.validate()
def test_validate_ok_without_pattern(self):
t = template_format.parse(stack_template)
del t['resources']['cron_trigger']['properties']['pattern']
stack = utils.parse_stack(t)
resource_defns = stack.t.resource_definitions(stack)
self.rsrc_defn = resource_defns['cron_trigger']
ct = self._create_resource('trigger', self.rsrc_defn, self.stack)
ct.validate()