def test_update_missing_state(self): """We notice if no document was found to update""" f = FSM(state_id) f.state_coll = mock.MagicMock(spec=pymongo.collection.Collection, return_value=True) f.state_coll.update.return_value = None _update_state = { '$set': { 'ended': UTCNOW } } with self.assertRaises(Exception): f.update_state(_update_state)
def test_update_state_mongo_failed(self): """We notice if mongo failed while updating state""" f = FSM(state_id) f.state_coll = mock.MagicMock(spec=pymongo.collection.Collection, return_value=True) mocked_update = mock.MagicMock(side_effect=pymongo.errors.PyMongoError) f.state_coll.update = mocked_update _update_state = { '$set': { 'ended': UTCNOW } } with self.assertRaises(pymongo.errors.PyMongoError): f.update_state(_update_state)
def test_step_notification_failed(self, send_notification, move_remaining, setup, updatestate): """Per-step notifications work when a step fails Tests for the case where only one notification transport (irc, email, etc) is defined""" f = FSM(TEST_PBID, state_id) msg_failed = {'status': 'failed'} consume_iter = [ (mock.Mock(name="method_mocked"), mock.Mock(name="properties_mocked"), json.dumps(msg_failed)) ] # Pre-test scaffolding. Hard-code some mocked out # attributes/variables because we're skipping the usual # initialization steps. f.conn = mock.Mock(pika.connection.Connection) f.executed = [] f.execution = [] f.state_coll = {} f.post_deploy_action = [] publish = mock.Mock() channel = mock.Mock() channel.consume.return_value = iter(consume_iter) channel.basic_publish = publish f.ch = channel f.active_sequence = {'hosts': ['localhost']} f.group = 'testgroup' f.dynamic = {} f.active_step = new_notify_step('failed') set_field = mock.MagicMock() filter = mock.MagicMock(return_value=set_field) f.filter = filter # Run the ended method with a body having 'status' as failed f.on_ended(channel, mock.Mock(name="method_mocked"), mock.Mock(name="header_mocked"), json.dumps(msg_failed)) self.assertEqual(send_notification.call_count, 1) self.assertEqual(send_notification.call_args[0][1], 'notify.irc') self.assertEqual(send_notification.call_args[0][2], state_id) self.assertEqual(send_notification.call_args[0][3], ['#achannel']) self.assertEqual(send_notification.call_args[0][4], 'failed')
def test_update_state(self): """State updating does the needful""" f = FSM(state_id) f.state_coll = mock.MagicMock(spec=pymongo.collection.Collection, return_value=True) _update_state = { '$set': { 'ended': UTCNOW } } # FSM Sets its state document ID attr properly self.assertEqual(f._id, fsm__id) f.update_state(_update_state) # FSM passes the needful to the update method f.state_coll.update.assert_called_once_with(fsm__id, _update_state)