def test_job_create(self): """ Verify when topic job.create is received the FSM handles it properly """ group = 'testgroup' release_id = '000000000000000007654321' playbook_id = "555544443333222211110000" body = '{"group": "%s", "dynamic": {}, "playbook_id": "%s"}' % ( group, playbook_id) method = mock.MagicMock(routing_key='job.create') with mock.patch('recore.job.create') as amqp.recore.job.create: amqp.recore.job.create.release.return_value = release_id with mock.patch('recore.fsm') as amqp.recore.fsm: with mock.patch( 'recore.amqp.recore.job.create.recore.mongo'): amqp.recore.job.create.recore.mongo.lookup_playbook = ( mock.MagicMock(return_value={''})) amqp.recore.mongo.create_state_document = ( mock.MagicMock(return_value="000000000000000007654321")) # Make the call amqp.receive(channel, method, PROPERTIES, body) # Verify the items which should have triggered amqp.recore.job.create.release.assert_called_once_with( channel, playbook_id, REPLY_TO, {}, "000000000000000007654321") # Verify a new thread of the FSM is started for # this one specific release amqp.recore.fsm.FSM.call_count == 1
def test_job_create(self): """ Verify when topic job.create is received the FSM handles it properly """ project = 'testproject' body = '{"project": "%s", "dynamic": {}}' % project release_id = 12345 method = mock.MagicMock(routing_key='job.create') with mock.patch('recore.job.create') as amqp.recore.job.create: amqp.recore.job.create.release.return_value = release_id with mock.patch('recore.fsm') as amqp.recore.fsm: with mock.patch( 'recore.amqp.recore.job.create.recore.mongo'): amqp.recore.job.create.recore.mongo.lookup_project = ( mock.MagicMock(return_value={''})) # Make the call amqp.receive(channel, method, PROPERTIES, body) # Verify the items which should have triggered amqp.recore.job.create.release.assert_called_once_with( channel, project, REPLY_TO, {}) # Verify a new thread of the FSM is started for # this one specific release amqp.recore.fsm.FSM.call_count == 1
def test_job_create_failure(self): """ Verify when topic job.create is received with bad data it's handled properly """ group = 'testgroup' body = '{"bad": "data", "playbook_id": "foo", "source_ip": "bar", "user_id": "joeuser"}' release_id = 12345 method = mock.MagicMock(routing_key='job.create') with mock.patch('recore.job.create') as amqp.recore.job.create: amqp.recore.job.create.release.return_value = release_id with mock.patch('recore.fsm') as amqp.recore.fsm: with mock.patch( 'recore.amqp.recore.job.create.recore.mongo'): amqp.recore.job.create.recore.mongo.lookup_playbook = ( mock.MagicMock(return_value={''})) # Make the call assert amqp.receive( channel, method, PROPERTIES, body) is None # Verify create release wasn't triggered assert amqp.recore.job.create.release.call_count == 0 # Verify a new thread of the FSM is started for # this one specific release amqp.recore.fsm.FSM.call_count == 0
def test_unknown_topic(self): """ When the FSM gets an unknown topic, verify the message is ignored """ project = 'testproject' release_id = 12345 body = {"project": project, "status": "completed"} method = mock.MagicMock(routing_key='unknown') with mock.patch('recore.job.create') as amqp.recore.job.create: amqp.recore.job.create.release.return_value = release_id with mock.patch('recore.fsm') as amqp.recore.fsm: # Make the call amqp.receive(channel, method, PROPERTIES, json.dumps(body)) # No release calls should be made assert amqp.recore.job.create.release.call_count == 0 assert amqp.recore.fsm.FSM.call_count == 0
def test_unknown_topic(self): """ When the FSM gets an unknown topic, verify the message is ignored """ group = 'testgroup' release_id = 12345 body = {"group": group, "status": "completed", "playbook_id": "foo", "source_ip": "bar", "user_id": "joeuser"} method = mock.MagicMock(routing_key='unknown') with mock.patch('recore.job.create') as amqp.recore.job.create: amqp.recore.job.create.release.return_value = release_id with mock.patch('recore.fsm') as amqp.recore.fsm: # Make the call amqp.receive(channel, method, PROPERTIES, json.dumps(body)) # No release calls should be made assert amqp.recore.job.create.release.call_count == 0 assert amqp.recore.fsm.FSM.call_count == 0
def test_job_create_with_invalid_json(self): """ New job requests with invalid json don't crash the FSM """ group = 'testgroup' release_id = 12345 body = '{"group": %s, "status": "completed"' % group method = mock.MagicMock(routing_key='job.create') with mock.patch('recore.job.create') as amqp.recore.job.create: amqp.recore.job.create.release.return_value = release_id with mock.patch('recore.fsm') as amqp.recore.fsm: with mock.patch('recore.amqp.reject') as amqp.reject: # Make the call amqp.receive(channel, method, PROPERTIES, body) # No release calls should be made assert amqp.recore.job.create.release.call_count == 0 assert amqp.recore.fsm.FSM.call_count == 0 amqp.reject.assert_called_once_with( channel, method, False)