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'