def test_kill_job(self): with JobRunner.temp_file() as script: script.write(b"sleep 30") script.close() with open(os.devnull, "wb") as devnull: r = self.create_runner() proc = r.create_process(script.name, {}, devnull) r.kill_job(proc) self.assertEqual(proc.poll(), -15) # SIGTERM proc.wait() # get some coverage when the proc is already dead r.kill_job(proc) # the kill path for windows is different, just get some # coverage because we don't currently have a windows box # to test on with patch.object(platform, 'system') as mock_system: mock_system.side_effect = ["linux", "Windows"] proc = r.create_process(script.name, {}, devnull) r.kill_job(proc) with patch.object(spawn, 'find_executable') as mock_find: mock_system.side_effect = ["Windows"] mock_find.return_value = True r.kill_job(proc) # mimic not being able to kill the job with patch.object(subprocess.Popen, 'poll') as mock_poll, patch.object( subprocess.Popen, 'kill') as mock_kill: mock_poll.side_effect = [True, None, None] mock_kill.return_value = False proc = r.create_process(script.name, {}, devnull) r.kill_job(proc)
def test_read_process_output(self): r = self.create_runner() r.client_info["update_step_time"] = 1 with JobRunner.temp_file() as script_file: script = b"for i in $(seq 5);do echo start $i; sleep 1; echo done $i; done" script_file.write(script) script_file.close() with open(os.devnull, "wb") as devnull: proc = r.create_process(script_file.name, {}, devnull) # standard run of the subprocess, just check we get all the output out = r.read_process_output(proc, r.job_data["steps"][0], {}) proc.wait() test_out = "" self.assertGreater(self.message_q.qsize(), 3) msg_list = [] try: while True: l = self.message_q.get(block=False) msg_list.append(l) except Empty: pass for i in range(1, 6): start = "start {}\n".format(i) done = "done {}\n".format(i) if i < 4: # only do this test for the first few # since there is no guarentee that update_step() # will get called for all of them before the # process terminates found_start = False found_done = False for msg in msg_list: if start.strip() in msg["payload"]["output"]: found_start = True if done.strip() in msg["payload"]["output"]: found_done = True self.assertTrue(found_start) self.assertTrue(found_done) test_out += start + done self.assertEqual(test_out, out["output"]) self.assertEqual(out["complete"], True) self.assertGreater(out["time"], 1) proc = r.create_process(script_file.name, {}, devnull) # Test cancel while reading output self.command_q.put({ "job_id": r.job_data["job_id"], "command": "cancel" }) self.assertEqual(r.canceled, False) r.read_process_output(proc, r.job_data["steps"][0], {}) proc.wait() self.assertEqual(r.canceled, True)
def test_max_step_time(self): with JobRunner.temp_file() as script: script.write(b"sleep 30") script.close() with open(os.devnull, "wb") as devnull: r = self.create_runner() r.max_step_time = 2 proc = r.create_process(script.name, {}, devnull) out = r.read_process_output(proc, r.job_data["steps"][0], {}) self.assertIn("taking longer than the max", out["output"]) self.assertLess(out["time"], 10) self.assertEqual(out["canceled"], True)
def create_runner(self): client_info = utils.default_client_info() job_info = utils.create_job_dict() runner = JobRunner.JobRunner(client_info, job_info, self.message_q, self.command_q) self.assertEqual(runner.canceled, False) self.assertEqual(runner.stopped, False) self.assertEqual(runner.global_env["var_with_root"], "%s/bar" % self.build_root) self.assertEqual( runner.job_data["steps"][0]["environment"]["step_var_with_root"], "%s/foo" % self.build_root) return runner