Merge "No need to check if resource not created"

changes/20/435520/1
Jenkins 6 years ago committed by Gerrit Code Review
commit 7e6ba07a6a

@ -1401,6 +1401,13 @@ class Resource(object):
LOG.info(_LI('Checking %s'), self)
if hasattr(self, 'handle_%s' % action.lower()):
if self.state == (self.INIT, self.COMPLETE):
reason = _('Can not check %s, resource not '
'created yet.') % self.name
self.state_set(action, self.FAILED, reason)
exc = Exception(_('Resource %s not created yet.') % self.name)
failure = exception.ResourceFailure(exc, self, action)
raise failure
return self._do_action(action)
else:
reason = '%s not supported for %s' % (action, self.type())

@ -517,6 +517,7 @@ class VolumeTest(vt_base.BaseVolumeTest):
def test_volume_check(self):
stack = utils.parse_stack(self.t, stack_name='volume_check')
res = stack['DataVolume']
res.state_set(res.CREATE, res.COMPLETE)
fake_volume = vt_base.FakeVolume('available')
cinder = mock.Mock()
cinder.volumes.get.return_value = fake_volume
@ -533,6 +534,7 @@ class VolumeTest(vt_base.BaseVolumeTest):
def test_volume_check_not_available(self):
stack = utils.parse_stack(self.t, stack_name='volume_check_na')
res = stack['DataVolume']
res.state_set(res.CREATE, res.COMPLETE)
cinder = mock.Mock()
fake_volume = vt_base.FakeVolume('foobar')
cinder.volumes.get.return_value = fake_volume
@ -546,6 +548,7 @@ class VolumeTest(vt_base.BaseVolumeTest):
def test_volume_check_fail(self):
stack = utils.parse_stack(self.t, stack_name='volume_check_fail')
res = stack['DataVolume']
res.state_set(res.CREATE, res.COMPLETE)
cinder = mock.Mock()
cinder.volumes.get.side_effect = Exception('boom')
self.patchobject(res, 'client', return_value=cinder)

@ -472,10 +472,12 @@ class AodhAlarmTest(common.HeatTestCase):
# check that super method has been called and execute deleting
self.assertEqual(1, self.fa.alarm.delete.call_count)
def _prepare_check_resource(self):
def _prepare_resource(self, for_check=True):
snippet = template_format.parse(not_string_alarm_template)
self.stack = utils.parse_stack(snippet)
res = self.stack['MEMAlarmHigh']
if for_check:
res.state_set(res.CREATE, res.COMPLETE)
res.client = mock.Mock()
mock_alarm = mock.Mock(enabled=True, state='ok')
res.client().alarm.get.return_value = mock_alarm
@ -483,13 +485,13 @@ class AodhAlarmTest(common.HeatTestCase):
@mock.patch.object(alarm.watchrule.WatchRule, 'load')
def test_check(self, mock_load):
res = self._prepare_check_resource()
res = self._prepare_resource()
scheduler.TaskRunner(res.check)()
self.assertEqual((res.CHECK, res.COMPLETE), res.state)
@mock.patch.object(alarm.watchrule.WatchRule, 'load')
def test_check_watchrule_failure(self, mock_load):
res = self._prepare_check_resource()
res = self._prepare_resource()
exc = alarm.exception.EntityNotFound(entity='Watch Rule', name='Boom')
mock_load.side_effect = exc
@ -500,7 +502,7 @@ class AodhAlarmTest(common.HeatTestCase):
@mock.patch.object(alarm.watchrule.WatchRule, 'load')
def test_check_alarm_failure(self, mock_load):
res = self._prepare_check_resource()
res = self._prepare_resource()
res.client().alarm.get.side_effect = Exception('Boom')
self.assertRaises(exception.ResourceFailure,
@ -509,7 +511,7 @@ class AodhAlarmTest(common.HeatTestCase):
self.assertIn('Boom', res.status_reason)
def test_show_resource(self):
res = self._prepare_check_resource()
res = self._prepare_resource(for_check=False)
res.client().alarm.create.return_value = FakeAodhAlarm
res.client().alarm.get.return_value = FakeAodhAlarm
scheduler.TaskRunner(res.create)()
@ -804,22 +806,24 @@ class CombinationAlarmTest(common.HeatTestCase):
self.m.VerifyAll()
def _prepare_check_resource(self):
def _prepare_resource(self, for_check=True):
snippet = template_format.parse(combination_alarm_template)
self.stack = utils.parse_stack(snippet)
res = self.stack['CombinAlarm']
if for_check:
res.state_set(res.CREATE, res.COMPLETE)
res.client = mock.Mock()
mock_alarm = mock.Mock(enabled=True, state='ok')
res.client().alarms.get.return_value = mock_alarm
return res
def test_check(self):
res = self._prepare_check_resource()
res = self._prepare_resource()
scheduler.TaskRunner(res.check)()
self.assertEqual((res.CHECK, res.COMPLETE), res.state)
def test_check_failure(self):
res = self._prepare_check_resource()
res = self._prepare_resource()
res.client().alarms.get.side_effect = Exception('Boom')
self.assertRaises(exception.ResourceFailure,
@ -828,7 +832,7 @@ class CombinationAlarmTest(common.HeatTestCase):
self.assertIn('Boom', res.status_reason)
def test_show_resource(self):
res = self._prepare_check_resource()
res = self._prepare_resource(for_check=False)
res.client().alarms.create.return_value = mock.MagicMock(
alarm_id='2')
res.client().alarms.get.return_value = FakeCombinationAlarm()
@ -898,22 +902,24 @@ class EventAlarmTest(common.HeatTestCase):
self.assertEqual('12345', rsrc.handle_delete())
self.assertEqual(1, self.fa.alarm.delete.call_count)
def _prepare_check_resource(self):
def _prepare_resource(self, for_check=True):
snippet = template_format.parse(event_alarm_template)
self.stack = utils.parse_stack(snippet)
res = self.stack['test_event_alarm']
if for_check:
res.state_set(res.CREATE, res.COMPLETE)
res.client = mock.Mock()
mock_alarm = mock.Mock(enabled=True, state='ok')
res.client().alarm.get.return_value = mock_alarm
return res
def test_check(self):
res = self._prepare_check_resource()
res = self._prepare_resource()
scheduler.TaskRunner(res.check)()
self.assertEqual((res.CHECK, res.COMPLETE), res.state)
def test_check_alarm_failure(self):
res = self._prepare_check_resource()
res = self._prepare_resource()
res.client().alarm.get.side_effect = Exception('Boom')
self.assertRaises(exception.ResourceFailure,
@ -922,7 +928,7 @@ class EventAlarmTest(common.HeatTestCase):
self.assertIn('Boom', res.status_reason)
def test_show_resource(self):
res = self._prepare_check_resource()
res = self._prepare_resource(for_check=False)
res.client().alarm.create.return_value = FakeAodhAlarm
res.client().alarm.get.return_value = FakeAodhAlarm
scheduler.TaskRunner(res.create)()

@ -160,10 +160,12 @@ class GnocchiResourcesAlarmTest(common.HeatTestCase):
self.m.VerifyAll()
def _prepare_check_resource(self):
def _prepare_resource(self, for_check=True):
snippet = template_format.parse(gnocchi_resources_alarm_template)
self.stack = utils.parse_stack(snippet)
res = self.stack['GnoResAlarm']
if for_check:
res.state_set(res.CREATE, res.COMPLETE)
res.client = mock.Mock()
mock_alarm = mock.Mock(enabled=True, state='ok')
res.client().alarm.get.return_value = mock_alarm
@ -206,12 +208,12 @@ class GnocchiResourcesAlarmTest(common.HeatTestCase):
self.m.VerifyAll()
def test_check(self):
res = self._prepare_check_resource()
res = self._prepare_resource()
scheduler.TaskRunner(res.check)()
self.assertEqual((res.CHECK, res.COMPLETE), res.state)
def test_check_failure(self):
res = self._prepare_check_resource()
res = self._prepare_resource()
res.client().alarm.get.side_effect = Exception('Boom')
self.assertRaises(exception.ResourceFailure,
@ -220,7 +222,7 @@ class GnocchiResourcesAlarmTest(common.HeatTestCase):
self.assertIn('Boom', res.status_reason)
def test_show_resource(self):
res = self._prepare_check_resource()
res = self._prepare_resource(for_check=False)
res.client().alarm.create.return_value = FakeAodhAlarm
res.client().alarm.get.return_value = FakeAodhAlarm
scheduler.TaskRunner(res.create)()
@ -350,18 +352,20 @@ class GnocchiAggregationByMetricsAlarmTest(GnocchiResourcesAlarmTest):
self.m.VerifyAll()
def _prepare_check_resource(self):
def _prepare_resource(self, for_check=True):
snippet = template_format.parse(
gnocchi_aggregation_by_metrics_alarm_template)
self.stack = utils.parse_stack(snippet)
res = self.stack['GnoAggregationByMetricsAlarm']
if for_check:
res.state_set(res.CREATE, res.COMPLETE)
res.client = mock.Mock()
mock_alarm = mock.Mock(enabled=True, state='ok')
res.client().alarm.get.return_value = mock_alarm
return res
def test_show_resource(self):
res = self._prepare_check_resource()
res = self._prepare_resource(for_check=False)
res.client().alarm.create.return_value = FakeAodhAlarm
res.client().alarm.get.return_value = FakeAodhAlarm
scheduler.TaskRunner(res.create)()
@ -492,18 +496,20 @@ class GnocchiAggregationByResourcesAlarmTest(GnocchiResourcesAlarmTest):
self.m.VerifyAll()
def _prepare_check_resource(self):
def _prepare_resource(self, for_check=True):
snippet = template_format.parse(
gnocchi_aggregation_by_resources_alarm_template)
self.stack = utils.parse_stack(snippet)
res = self.stack['GnoAggregationByResourcesAlarm']
if for_check:
res.state_set(res.CREATE, res.COMPLETE)
res.client = mock.Mock()
mock_alarm = mock.Mock(enabled=True, state='ok')
res.client().alarm.get.return_value = mock_alarm
return res
def test_show_resource(self):
res = self._prepare_check_resource()
res = self._prepare_resource(for_check=False)
res.client().alarm.create.return_value = FakeAodhAlarm
res.client().alarm.get.return_value = FakeAodhAlarm
scheduler.TaskRunner(res.create)()

@ -96,7 +96,9 @@ class CloudWatchAlarmTest(common.HeatTestCase):
def _get_watch_rule(self):
stack = self.parse_stack()
return stack['test_me']
res = stack['test_me']
res.state_set(res.CREATE, res.COMPLETE)
return res
@mock.patch.object(cloud_watch.watchrule.WatchRule, 'load')
def test_check(self, mock_lock):

@ -162,12 +162,14 @@ class NovaKeyPairTest(common.HeatTestCase):
def test_check_key(self):
res = self._get_test_resource(self.kp_template)
res.state_set(res.CREATE, res.COMPLETE, 'for test')
res.client = mock.Mock()
scheduler.TaskRunner(res.check)()
self.assertEqual((res.CHECK, res.COMPLETE), res.state)
def test_check_key_fail(self):
res = self._get_test_resource(self.kp_template)
res.state_set(res.CREATE, res.COMPLETE, 'for test')
res.client = mock.Mock()
res.client().keypairs.get.side_effect = Exception("boom")
exc = self.assertRaises(exception.ResourceFailure,

@ -267,6 +267,7 @@ class ServersTest(common.HeatTestCase):
server = self.fc.servers.list()[1]
server.status = status
res = self.stack['WebServer']
res.state_set(res.CREATE, res.COMPLETE)
res.client = mock.Mock()
res.client().servers.get.return_value = server
return res

@ -274,6 +274,7 @@ class InstanceTest(common.HeatTestCase):
def _get_db_instance(self):
t = template_format.parse(db_template)
res = self._setup_test_instance('trove_check', t)
res.state_set(res.CREATE, res.COMPLETE)
res.flavor = 'Foo Flavor'
res.volume = 'Foo Volume'
res.datastore_type = 'Foo Type'

@ -1320,10 +1320,18 @@ class ResourceTest(common.HeatTestCase):
self.m.VerifyAll()
def test_check_supported(self):
def _mock_check_res(self, mock_check=True):
tmpl = rsrc_defn.ResourceDefinition('test_res', 'GenericResourceType')
res = generic_rsrc.ResourceWithProps('test_res', tmpl, self.stack)
res.handle_check = mock.Mock()
res.state_set(res.CREATE, res.COMPLETE)
if mock_check:
res.handle_check = mock.Mock()
return res
def test_check_supported(self):
res = self._mock_check_res()
scheduler.TaskRunner(res.check)()
self.assertTrue(res.handle_check.called)
@ -1332,8 +1340,7 @@ class ResourceTest(common.HeatTestCase):
self.assertNotIn('not supported', res.status_reason)
def test_check_not_supported(self):
tmpl = rsrc_defn.ResourceDefinition('test_res', 'GenericResourceType')
res = generic_rsrc.ResourceWithProps('test_res', tmpl, self.stack)
res = self._mock_check_res(mock_check=False)
scheduler.TaskRunner(res.check)()
self.assertIn('not supported', res.status_reason)
@ -1341,9 +1348,7 @@ class ResourceTest(common.HeatTestCase):
self.assertEqual(res.COMPLETE, res.status)
def test_check_failed(self):
tmpl = rsrc_defn.ResourceDefinition('test_res', 'GenericResourceType')
res = generic_rsrc.ResourceWithProps('test_res', tmpl, self.stack)
res.handle_check = mock.Mock()
res = self._mock_check_res()
res.handle_check.side_effect = Exception('boom')
self.assertRaises(exception.ResourceFailure,

@ -922,6 +922,8 @@ class StackTest(common.HeatTestCase):
def test_check_supported(self):
stack1 = self._get_stack_to_check('check-supported')
stack1['A'].state_set(stack1['A'].CREATE, stack1['A'].COMPLETE)
stack1['B'].state_set(stack1['B'].CREATE, stack1['B'].COMPLETE)
stack1.check()
self.assertEqual(stack1.COMPLETE, stack1.status)
@ -933,6 +935,7 @@ class StackTest(common.HeatTestCase):
def test_check_not_supported(self):
stack1 = self._get_stack_to_check('check-not-supported')
del stack1['B'].handle_check
stack1['A'].state_set(stack1['A'].CREATE, stack1['A'].COMPLETE)
stack1.check()
self.assertEqual(stack1.COMPLETE, stack1.status)
@ -942,8 +945,21 @@ class StackTest(common.HeatTestCase):
def test_check_fail(self):
stk = self._get_stack_to_check('check-fail')
# if resource not created, check fail
stk.check()
self.assertEqual(stk.FAILED, stk.status)
self.assertEqual(stk.CHECK, stk.action)
self.assertFalse(stk['A'].handle_check.called)
self.assertFalse(stk['B'].handle_check.called)
self.assertIn('Resource A not created yet',
stk.status_reason)
self.assertIn('Resource B not created yet',
stk.status_reason)
# check if resource created
stk['A'].handle_check.side_effect = Exception('fail-A')
stk['B'].handle_check.side_effect = Exception('fail-B')
stk['A'].state_set(stk['A'].CREATE, stk['A'].COMPLETE)
stk['B'].state_set(stk['B'].CREATE, stk['B'].COMPLETE)
stk.check()
self.assertEqual(stk.FAILED, stk.status)

Loading…
Cancel
Save