def test_normal_machine_state_flow_for_received(self): self.aws_mock.start_machine = mock.MagicMock() self.aws_mock.start_machine.return_value = 'jm-', 'res0' from machine_midwife import MachineMidwife from job import Job from worker import Worker midwife = MachineMidwife() midwife.apprentice = mock.MagicMock() midwife.settings = mock.MagicMock() midwife.client = mock.MagicMock() midwife.job_pub_sub = mock.MagicMock() midwife.job_pub_sub.listen.return_value = [{'data': 'test'}] midwife.client.exists.return_value = True midwife.client.keys.return_value = ['job-', 'jm-'] job = Job('received', 'something') worker = Worker('job-', None) midwife.client.get.side_effect = [ pickle.dumps(job), pickle.dumps(worker) ] midwife.client.set = mock.MagicMock() midwife.client.publish = mock.MagicMock() midwife.run() assert midwife.client.exists.call_count == 2 assert len(midwife.client.set.call_args_list) == 2 assert midwife.client.set.call_args_list[0][0][0] == 'jm-' assert midwife.client.set.call_args_list[1][0][0] == 'test' assert self.aws_mock.start_machine.call_count == 1 assert pickle.loads( midwife.client.set.call_args_list[1][0][1]).state == 'requested'
def test_delayed_machine_state_flow_for_requested_with_recycle(self): from machine_midwife import MachineMidwife from job import Job from worker import Worker midwife = MachineMidwife() midwife.apprentice = mock.MagicMock() midwife.settings = mock.MagicMock() midwife.client = mock.MagicMock() midwife.job_pub_sub = mock.MagicMock() midwife.job_pub_sub.listen.return_value = [{'data': 'test'}] midwife.client.exists.return_value = True job = Job('delayed', 'batch-') worker = Worker(None, 'batch-') worker.reservation = 'reservation' worker.request_time = datetime.now() midwife.client.keys.return_value = ['jm-'] midwife.client.get.side_effect = [ pickle.dumps(job), pickle.dumps(worker) ] midwife.client.set = mock.MagicMock() midwife.client.publish = mock.MagicMock() midwife.run() assert midwife.client.exists.call_count == 2 assert len(midwife.client.set.call_args_list) == 2 assert pickle.loads( midwife.client.set.call_args_list[0][0][1]).job_id == 'test' assert pickle.loads( midwife.client.set.call_args_list[1][0][1]).state == 'booted'
def test_requestfailed_machine_state_flow_for_requested(self): self.aws_mock.start_machine = mock.MagicMock() self.aws_mock.start_machine.return_value = None, None from machine_midwife import MachineMidwife from job import Job from worker import Worker midwife = MachineMidwife() midwife.apprentice = mock.MagicMock() midwife.settings = mock.MagicMock() midwife.client = mock.MagicMock() midwife.job_pub_sub = mock.MagicMock() midwife.job_pub_sub.listen.return_value = [{'data': 'test'}] midwife.client.exists.return_value = True midwife.client.keys.return_value = ['job-', 'jm-'] job = Job('received', 'something') worker = Worker('job-', None) midwife.client.get.side_effect = [pickle.dumps(job), pickle.dumps(worker)] midwife.client.set = mock.MagicMock() midwife.client.publish = mock.MagicMock() midwife.run() assert midwife.client.exists.call_count == 2 assert len(midwife.client.set.call_args_list) == 1 assert pickle.loads(midwife.client.set.call_args_list[0][0][1]).state == 'delayed'
def test_delayed_machine_state_flow_for_requested_with_recycle(self): from machine_midwife import MachineMidwife from job import Job from worker import Worker midwife = MachineMidwife() midwife.apprentice = mock.MagicMock() midwife.settings = mock.MagicMock() midwife.client = mock.MagicMock() midwife.job_pub_sub = mock.MagicMock() midwife.job_pub_sub.listen.return_value = [{'data': 'test'}] midwife.client.exists.return_value = True job = Job('delayed', 'batch-') worker = Worker(None, 'batch-') worker.reservation = 'reservation' worker.request_time = datetime.now() midwife.client.keys.return_value = ['jm-'] midwife.client.get.side_effect = [pickle.dumps(job), pickle.dumps(worker)] midwife.client.set = mock.MagicMock() midwife.client.publish = mock.MagicMock() midwife.run() assert midwife.client.exists.call_count == 2 assert len(midwife.client.set.call_args_list) == 2 assert pickle.loads(midwife.client.set.call_args_list[0][0][1]).job_id == 'test' assert pickle.loads(midwife.client.set.call_args_list[1][0][1]).state == 'booted'
def test_delay_machine_state_flow_for_requested(self): from machine_midwife import MachineMidwife from job import Job midwife = MachineMidwife() midwife.apprentice = mock.MagicMock() midwife.settings = mock.MagicMock() midwife.client = mock.MagicMock() midwife.job_pub_sub = mock.MagicMock() midwife.job_pub_sub.listen.return_value = [{'data': 'test'}] midwife.client.exists.return_value = True job = Job('received', 'something') midwife.client.get.side_effect = [pickle.dumps(job), pickle.dumps(job)] midwife.client.set = mock.MagicMock() midwife.client.publish = mock.MagicMock() midwife.run() assert midwife.client.exists.call_count == 1 assert len(midwife.client.set.call_args_list) == 1 assert midwife.client.set.call_args_list[0][0][0] == 'test' assert pickle.loads( midwife.client.set.call_args_list[0][0][1]).state == 'delayed'
def test_delay_machine_state_flow_for_requested(self): from machine_midwife import MachineMidwife from job import Job midwife = MachineMidwife() midwife.apprentice = mock.MagicMock() midwife.settings = mock.MagicMock() midwife.client = mock.MagicMock() midwife.job_pub_sub = mock.MagicMock() midwife.job_pub_sub.listen.return_value = [{'data': 'test'}] midwife.client.exists.return_value = True job = Job('received', 'something') midwife.client.get.side_effect = [pickle.dumps(job), pickle.dumps(job)] midwife.client.set = mock.MagicMock() midwife.client.publish = mock.MagicMock() midwife.run() assert midwife.client.exists.call_count == 1 assert len(midwife.client.set.call_args_list) == 1 assert midwife.client.set.call_args_list[0][0][0] == 'test' assert pickle.loads(midwife.client.set.call_args_list[0][0][1]).state == 'delayed'
from flask import Flask, request, jsonify, Response from flask.json import dumps from flask_autodoc import Autodoc from repository import AmiRepository from machine_midwife import MachineMidwife from consuela import Consuela app = Flask(__name__) auto = Autodoc(app) app.config['REPOSITORY'] = AmiRepository() if not os.path.exists('/tmp/store'): os.mkdir('/tmp/store') app.config['MM'] = MachineMidwife() app.config['MM'].start() app.config['CSU'] = Consuela() app.config['CSU'].start() def __get_amis__(): repository = app.config['REPOSITORY'] return Response(dumps(repository.get_all_amis()), mimetype='application/json') def __get_ami_status__(aid): status = aws.get_status(aid) if status: return Response(dumps(str(status)), mimetype='application/json')