deb-heat/heat/tests/test_restarter.py
Zane Bitter c4dd7d70b5 Make HARestarter do a proper lookup of the RefID
HARestarter is a primitive prototype that is barely maintained.  Its
implementation still dates to the era when the resource name, physical
resource ID and RefId were regularly used interchangably.

This change substitutes Stack.resource_by_refid() for the previous
(incorrect) home-grown hack, so that at least the giant red flags will be
easy to grep for.

Change-Id: I9d81dfcd5d34282c8bd83bbd98350e75c1ec0eb2
Closes-Bug: #1381136
2014-10-21 13:04:52 -04:00

111 lines
3.5 KiB
Python

#
# 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.
import mock
from heat.common import template_format
from heat.engine.resources import instance
from heat.engine import scheduler
from heat.tests import common
from heat.tests import utils
restarter_template = '''
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Description" : "Template to test HARestarter",
"Parameters" : {},
"Resources" : {
"restarter": {
"Type": "OS::Heat::HARestarter",
"Properties": {
"InstanceId": "1234"
}
}
}
}
'''
class RestarterTest(common.HeatTestCase):
def setUp(self):
super(RestarterTest, self).setUp()
def create_restarter(self):
snippet = template_format.parse(restarter_template)
stack = utils.parse_stack(snippet)
resource_defns = stack.t.resource_definitions(stack)
restarter = instance.Restarter(
'restarter', resource_defns['restarter'], stack)
restarter.handle_create = mock.Mock(return_value=None)
return restarter
def create_mock_instance(self, stack):
inst = mock.Mock(spec=instance.Instance)
inst.resource_id = '1234'
inst.name = 'instance'
inst.action = inst.CREATE
inst.status = inst.COMPLETE
inst.state = (inst.action, inst.status)
inst.FnGetRefId = lambda: inst.resource_id
stack.resources['instance'] = inst
def test_create(self):
rsrc = self.create_restarter()
scheduler.TaskRunner(rsrc.create)()
self.assertEqual((rsrc.CREATE, rsrc.COMPLETE), rsrc.state)
rsrc.handle_create.assert_called_once_with()
def test_handle_signal(self):
rsrc = self.create_restarter()
scheduler.TaskRunner(rsrc.create)()
self.create_mock_instance(rsrc.stack)
rsrc.stack.restart_resource = mock.Mock(return_value=None)
self.assertIsNone(rsrc.handle_signal())
rsrc.stack.restart_resource.assert_called_once_with('instance')
def test_handle_signal_alarm(self):
rsrc = self.create_restarter()
scheduler.TaskRunner(rsrc.create)()
self.create_mock_instance(rsrc.stack)
rsrc.stack.restart_resource = mock.Mock(return_value=None)
self.assertIsNone(rsrc.handle_signal({'state': 'Alarm'}))
rsrc.stack.restart_resource.assert_called_once_with('instance')
def test_handle_signal_not_alarm(self):
rsrc = self.create_restarter()
scheduler.TaskRunner(rsrc.create)()
self.create_mock_instance(rsrc.stack)
rsrc.stack.restart_resource = mock.Mock(return_value=None)
self.assertIsNone(rsrc.handle_signal({'state': 'spam'}))
self.assertEqual([], rsrc.stack.restart_resource.mock_calls)
def test_handle_signal_no_instance(self):
rsrc = self.create_restarter()
scheduler.TaskRunner(rsrc.create)()
rsrc.stack.restart_resource = mock.Mock(return_value=None)
self.assertIsNone(rsrc.handle_signal())
self.assertEqual([], rsrc.stack.restart_resource.mock_calls)