def test_launch_task_handle_exception(self): task_id = tu.get_random_task_id() task = {'task_id': {'value': task_id}} process = ce.launch_task(task, os.environ) self.assertIsNone(process)
def test_launch_task_handle_exception(self): task_id = get_random_task_id() task = {'task_id': {'value': task_id}} stdout_name = '' stderr_name = '' result = ce.launch_task(task, stdout_name, stderr_name) self.assertIsNone(result)
def test_launch_task_no_command(self): task_id = get_random_task_id() task = {'task_id': {'value': task_id}, 'data': encode_data(json.dumps({'command': ''}).encode('utf8'))} stdout_name = '' stderr_name = '' result = ce.launch_task(task, stdout_name, stderr_name) self.assertIsNone(result)
def test_launch_task_no_command(self): task_id = tu.get_random_task_id() task = { 'task_id': { 'value': task_id }, 'data': pm.encode_data(json.dumps({ 'command': '' }).encode('utf8')) } process = ce.launch_task(task, os.environ) self.assertIsNone(process)
def test_launch_task_interactive_output(self): task_id = tu.get_random_task_id() command = 'echo "Start"; echo "Hello"; sleep 100; echo "World"; echo "Done"; ' task = { 'task_id': { 'value': task_id }, 'data': pm.encode_data(json.dumps({ 'command': command }).encode('utf8')) } stdout_name = tu.ensure_directory('build/stdout.{}'.format(task_id)) stderr_name = tu.ensure_directory('build/stderr.{}'.format(task_id)) tu.redirect_stdout_to_file(stdout_name) tu.redirect_stderr_to_file(stderr_name) try: process = ce.launch_task(task, os.environ) self.assertIsNotNone(process) # let the process run for up to 50 seconds for _ in range(5000): if cs.is_process_running(process): time.sleep(0.01) with open(stdout_name) as f: stdout_content = f.read() if 'Start' in stdout_content and 'Hello' in stdout_content: break try: with open(stdout_name) as f: stdout_content = f.read() logging.info( 'Contents of stdout: {}'.format(stdout_content)) self.assertTrue("Start" in stdout_content) self.assertTrue("Hello" in stdout_content) self.assertFalse("World" in stdout_content) self.assertFalse("Done" in stdout_content) finally: if process.poll() is None: logging.info('Killing launched process') process.kill() finally: tu.cleanup_output(stdout_name, stderr_name)
def test_launch_task(self): task_id = get_random_task_id() command = 'echo "Hello World"; echo "Error Message" >&2' task = { 'task_id': { 'value': task_id }, 'data': encode_data(json.dumps({ 'command': command }).encode('utf8')) } stdout_name = ensure_directory('build/stdout.' + str(task_id)) stderr_name = ensure_directory('build/stderr.' + str(task_id)) if not os.path.isdir("build"): os.mkdir("build") try: process, stdout, stderr = ce.launch_task(task, stdout_name, stderr_name) self.assertIsNotNone(process) for i in range(100): if process.poll() is None: time.sleep(0.01) stdout.close() stderr.close() if process.poll() is None: process.kill() self.assertEqual(0, process.poll()) with open(stdout_name) as f: stdout_content = f.read() self.assertEqual("Hello World\n", stdout_content) with open(stderr_name) as f: stderr_content = f.read() self.assertEqual("Error Message\n", stderr_content) finally: cleanup_output(stdout_name, stderr_name)
def test_launch_task(self): task_id = tu.get_random_task_id() command = 'echo "Hello World"; echo "Error Message" >&2' task = { 'task_id': { 'value': task_id }, 'data': pm.encode_data(json.dumps({ 'command': command }).encode('utf8')) } stdout_name = tu.ensure_directory('build/stdout.{}'.format(task_id)) stderr_name = tu.ensure_directory('build/stderr.{}'.format(task_id)) tu.redirect_stdout_to_file(stdout_name) tu.redirect_stderr_to_file(stderr_name) try: process = ce.launch_task(task, os.environ) self.assertIsNotNone(process) for _ in range(100): if cs.is_process_running(process): time.sleep(0.01) if process.poll() is None: process.kill() tu.close_sys_outputs() self.assertEqual(0, process.poll()) with open(stdout_name) as f: stdout_content = f.read() self.assertTrue("Hello World\n" in stdout_content) with open(stderr_name) as f: stderr_content = f.read() self.assertTrue("Error Message\n" in stderr_content) finally: tu.cleanup_output(stdout_name, stderr_name)