def test__run(self, setup, dequeue, on_started): """The _run() method can send a proper message to a worker""" f = FSM(state_id) f.reply_queue = temp_queue f.project = "mock tests" f.dynamic = {} f.active = { 'plugin': 'fake', 'parameters': {'no': 'parameters'} } consume_iter = [ (mock.Mock(name="method_mocked"), mock.Mock(name="properties_mocked"), json.dumps(msg_completed)) ] publish = mock.Mock() channel = mock.Mock() channel.consume.return_value = iter(consume_iter) channel.basic_publish = publish f.ch = channel f._run() setup.assert_called_once_with() dequeue.assert_called_once_with() f.ch.basic_ack.assert_called_once_with(consume_iter[0][0].delivery_tag) f.ch.cancel.assert_called_once_with() on_started.assert_called_once_with(f.ch, *consume_iter[0])
def test__run(self, setup, dequeue, on_started): """The _run() method can send a proper message to a worker""" f = FSM(state_id) f.reply_queue = temp_queue f.group = "mock tests" f.dynamic = {} f.active_step = _active_step_string f.active_sequence = new_active_sequence() f.execution = new_playbook()['execution'] consume_iter = [( mock.Mock(name="method_mocked"), mock.Mock(name="properties_mocked"), json.dumps(msg_completed), )] publish = mock.Mock() channel = mock.Mock() channel.consume.return_value = iter(consume_iter) channel.basic_publish = publish f.ch = channel with mock.patch('recore.amqp.MQ_CONF') as mq_conf: mq_conf = MQ_CONF f._run() setup.assert_called_once_with() dequeue.assert_called_once_with() f.ch.basic_ack.assert_called_once_with(consume_iter[0][0].delivery_tag) f.ch.cancel.assert_called_once_with() on_started.assert_called_once_with(f.ch, *consume_iter[0])
def test__cleanup_post_failed(self, send_notification, post_deploy): """Cleanup marks release as failed if post deploy fails""" post_deploy.return_value = False f = FSM(state_id) f.ch = mock.Mock(pika.channel.Channel) f.conn = mock.Mock(pika.connection.Connection) f.reply_queue = temp_queue _update_state = { '$set': { 'ended': UTCNOW, 'failed': True } } with mock.patch.object(f, 'update_state', mock.Mock()) as ( us): with mock.patch('recore.fsm.dt') as ( dt): with mock.patch('recore.amqp.CONF') as notif_conf: notif_conf = NOTIFICATION_CONF dt.now.return_value = UTCNOW f._cleanup() # update state set the ended item in the state doc. us.assert_called_with(_update_state) f.conn.close.assert_called_once_with() f.ch.queue_delete.assert_called_once_with(queue=temp_queue) # At the very end a notification should go out no matter what self.assertEqual(send_notification.call_count, 1) assert send_notification.call_args[0][4] == 'failed' post_deploy.assert_called_once()
def test_on_started(self, ended, pdc): """Once started, the FSM waits for a response, and then calls on_ended""" pdc.return_value = True with nested( mock.patch('recore.mongo.lookup_state'), mock.patch('recore.mongo.database') ) as (lookup_state, database): lookup_state.return_value = _state.copy() # { # 'group': 'PROJECT', # 'dynamic': {}, # 'completed_steps': [], # 'active_step': 'active_step', # 'remaining_steps': [], # } f = FSM(TEST_PBID, state_id) f.reply_queue = temp_queue f.group = 'GROUP' consume_iter = [ (mock.Mock(name="method_mocked"), mock.Mock(name="properties_mocked"), json.dumps(msg_started)) ] publish = mock.Mock() channel = mock.Mock() channel.consume.return_value = iter(consume_iter) channel.basic_publish = publish f.ch = channel f.conn = mock.Mock(pika.connection.Connection) set_field = mock.MagicMock() filter = mock.MagicMock(return_value=set_field) f.filter = filter f._setup() f.on_started(f.ch, *consume_iter[0]) f.ch.basic_ack.assert_called_once_with(consume_iter[0][0].delivery_tag) f.ch.cancel.assert_called_once_with() f.on_ended.assert_called_once_with(f.ch, *consume_iter[0])
def test_on_started(self, ended): """Once started, the FSM waits for a response, and then calls on_ended""" f = FSM(state_id) f.reply_queue = temp_queue consume_iter = [ (mock.Mock(name="method_mocked"), mock.Mock(name="properties_mocked"), json.dumps(msg_completed)) ] publish = mock.Mock() channel = mock.Mock() channel.consume.return_value = iter(consume_iter) channel.basic_publish = publish f.ch = channel f.on_started(f.ch, *consume_iter[0]) f.ch.basic_ack.assert_called_once_with(consume_iter[0][0].delivery_tag) f.ch.cancel.assert_called_once_with() ended.assert_called_once_with(f.ch, *consume_iter[0])
def test__cleanup(self): """Cleanup erases the needful""" f = FSM(state_id) f.ch = mock.Mock(pika.channel.Channel) f.conn = mock.Mock(pika.connection.Connection) f.reply_queue = temp_queue _update_state = { '$set': { 'ended': UTCNOW } } with mock.patch.object(f, 'update_state', mock.Mock()) as ( us): with mock.patch('recore.fsm.dt') as ( dt): dt.now.return_value = UTCNOW f._cleanup() # update state set the ended item in the state doc. us.assert_called_with(_update_state) f.conn.close.assert_called_once_with() f.ch.queue_delete.assert_called_once_with(queue=temp_queue)
def test__cleanup(self, send_notification, post_deploy): """Cleanup erases the needful""" f = FSM(TEST_PBID, state_id) f.ch = mock.Mock(pika.channel.Channel) f.conn = mock.Mock(pika.connection.Connection) f.reply_queue = temp_queue _update_state = { '$set': { 'ended': UTCNOW, 'failed': False } } with mock.patch.object(f, 'update_state', mock.Mock()) as ( us): with mock.patch('recore.fsm.dt') as ( dt): dt.utcnow.return_value = UTCNOW with mock.patch('recore.amqp.CONF') as notif_conf: notif_conf = NOTIFICATION_CONF set_field = mock.MagicMock() filter = mock.MagicMock(return_value=set_field) f.filter = filter f._cleanup() # update state set the ended item in the state doc. us.assert_called_with(_update_state) f.conn.close.assert_called_once_with() f.ch.queue_delete.assert_called_once_with(queue=temp_queue) # At the very end a notification should go out no matter what self.assertEqual(send_notification.call_count, 1) assert send_notification.call_args[0][4] == 'completed' post_deploy.assert_called_once()