Reduce complexity of poll_and_check method

This is method is overly complex and should be broken down. When
reducing the max complexity allowed by flake8 this method was flagged.
Reducing the complexity of this method will allow better testing and
maintenance.

Partial-Bug: #1501331
Change-Id: Id0bdb2d03614ba31de0d96ed09c33695d933b9f7
This commit is contained in:
Tom Cammann 2015-09-30 14:09:58 +01:00
parent e5dd25cf22
commit 64cc141379
3 changed files with 78 additions and 41 deletions

View File

@ -227,50 +227,20 @@ class HeatPoller(object):
# poll_and_check is detached and polling long time to check status,
# so another user/client can call delete bay/stack.
if stack.stack_status == bay_status.DELETE_COMPLETE:
LOG.info(_LI('Bay has been deleted, stack_id: %s')
% self.bay.stack_id)
try:
cert_manager.delete_certificates_from_bay(self.bay)
self.bay.destroy()
except exception.BayNotFound:
LOG.info(_LI('The bay %s has been deleted by others.')
% self.bay.uuid)
self._delete_complete()
raise loopingcall.LoopingCallDone()
if (stack.stack_status in [bay_status.CREATE_COMPLETE,
bay_status.UPDATE_COMPLETE]):
self.template_def.update_outputs(stack, self.baymodel, self.bay)
self.bay.status = stack.stack_status
self.bay.status_reason = stack.stack_status_reason
stack_nc_param = self.template_def.get_heat_param(
bay_attr='node_count')
self.bay.node_count = stack.parameters[stack_nc_param]
self.bay.save()
if stack.stack_status in (bay_status.CREATE_COMPLETE,
bay_status.UPDATE_COMPLETE):
self._create_or_update_complete(stack)
raise loopingcall.LoopingCallDone()
elif stack.stack_status != self.bay.status:
self.bay.status = stack.stack_status
self.bay.status_reason = stack.stack_status_reason
stack_nc_param = self.template_def.get_heat_param(
bay_attr='node_count')
self.bay.node_count = stack.parameters[stack_nc_param]
self.bay.save()
if stack.stack_status == bay_status.CREATE_FAILED:
LOG.error(_LE('Unable to create bay, stack_id: %(stack_id)s, '
'reason: %(reason)s') %
{'stack_id': self.bay.stack_id,
'reason': stack.stack_status_reason})
raise loopingcall.LoopingCallDone()
if stack.stack_status == bay_status.DELETE_FAILED:
LOG.error(_LE('Unable to delete bay, stack_id: %(stack_id)s, '
'reason: %(reason)s') %
{'stack_id': self.bay.stack_id,
'reason': stack.stack_status_reason})
raise loopingcall.LoopingCallDone()
if stack.stack_status == bay_status.UPDATE_FAILED:
LOG.error(_LE('Unable to update bay, stack_id: %(stack_id)s, '
'reason: %(reason)s') %
{'stack_id': self.bay.stack_id,
'reason': stack.stack_status_reason})
self._sync_bay_status(stack)
if stack.stack_status in (bay_status.CREATE_FAILED,
bay_status.DELETE_FAILED,
bay_status.UPDATE_FAILED):
self._bay_failed()
raise loopingcall.LoopingCallDone()
# only check max attempts when the stack is being created when
# the timeout hasn't been set. If the timeout has been set then
@ -292,3 +262,39 @@ class HeatPoller(object):
'id': self.bay.stack_id,
'status': stack.stack_status})
raise loopingcall.LoopingCallDone()
def _delete_complete(self):
LOG.info(_LI('Bay has been deleted, stack_id: %s')
% self.bay.stack_id)
try:
cert_manager.delete_certificates_from_bay(self.bay)
self.bay.destroy()
except exception.BayNotFound:
LOG.info(_LI('The bay %s has been deleted by others.')
% self.bay.uuid)
def _create_or_update_complete(self, stack):
self.template_def.update_outputs(stack, self.baymodel, self.bay)
self.bay.status = stack.stack_status
self.bay.status_reason = stack.stack_status_reason
stack_nc_param = self.template_def.get_heat_param(
bay_attr='node_count')
self.bay.node_count = stack.parameters[stack_nc_param]
self.bay.save()
def _sync_bay_status(self, stack):
self.bay.status = stack.stack_status
self.bay.status_reason = stack.stack_status_reason
stack_nc_param = self.template_def.get_heat_param(
bay_attr='node_count')
self.bay.node_count = stack.parameters[stack_nc_param]
self.bay.save()
def _bay_failed(self):
LOG.error(_LE('Bay error, stack status: %(bay_status)s, '
'stack_id: %(stack_id)s, '
'reason: %(reason)s') %
{'bay_status': self.bay.stack_status,
'stack_id': self.bay.stack_id,
'reason': self.bay.status_reason})

View File

@ -347,3 +347,34 @@ class TestHeatPoller(base.TestCase):
self.assertRaises(loopingcall.LoopingCallDone, poller.poll_and_check)
self.assertEqual(2, bay.node_count)
@patch('magnum.conductor.handlers.bay_conductor.cert_manager')
def test_delete_complete(self, cert_manager):
mock_heat_stack, bay, poller = self.setup_poll_test()
poller._delete_complete()
self.assertEqual(1, bay.destroy.call_count)
self.assertEqual(1,
cert_manager.delete_certificates_from_bay.call_count)
def test_create_or_complete(self):
mock_heat_stack, bay, poller = self.setup_poll_test()
mock_heat_stack.stack_status = bay_status.CREATE_COMPLETE
mock_heat_stack.stack_status_reason = 'stack complete'
poller._create_or_update_complete(mock_heat_stack)
self.assertEqual('stack complete', bay.status_reason)
self.assertEqual(bay_status.CREATE_COMPLETE, bay.status)
self.assertEqual(1, bay.save.call_count)
def test_sync_bay_status(self):
mock_heat_stack, bay, poller = self.setup_poll_test()
mock_heat_stack.stack_status = bay_status.CREATE_IN_PROGRESS
mock_heat_stack.stack_status_reason = 'stack incomplete'
poller._sync_bay_status(mock_heat_stack)
self.assertEqual('stack incomplete', bay.status_reason)
self.assertEqual(bay_status.CREATE_IN_PROGRESS, bay.status)
@patch('magnum.conductor.handlers.bay_conductor.LOG')
def test_bay_failed(self, logger):
mock_heat_stack, bay, poller = self.setup_poll_test()
poller._bay_failed()
self.assertEqual(1, logger.error.call_count)

View File

@ -26,7 +26,7 @@ deps =
[testenv:pep8]
commands =
doc8 -e .rst specs/ doc/source/ contrib/ CONTRIBUTING.rst HACKING.rst README.rst
flake8 --max-complexity 13 {posargs}
flake8 --max-complexity 12 {posargs}
[testenv:venv]
commands = {posargs}