def test_one_failed_step_is_preventing_next_steps_from_execution_and_result_is_marked_as_failure(self): """Check the correctness of error handling""" io = IO() str_io = StringIO() buffered = BufferedSystemIO() task_declaration = get_test_declaration() BasicTestingCase.satisfy_task_dependencies(task_declaration.get_task_to_execute(), io=buffered) ctx = ExecutionContext(task_declaration) executor = DeclarativeExecutor() executor.add_step('python', 'this.io().outln("Peter Kropotkin"); return True', task_name=':first', rkd_path='', envs={}) executor.add_step('bash', 'echo "Buenaventura Durruti"; exit 1', task_name=':second', rkd_path='', envs={}) executor.add_step('python', 'this.io().outln("This one will not show"); return True', task_name=':third', rkd_path='', envs={}) with io.capture_descriptors(target_files=[], stream=str_io, enable_standard_out=False): final_result = executor.execute_steps_one_by_one(ctx, task_declaration.get_task_to_execute()) output = str_io.getvalue() + buffered.get_value() self.assertIn('Peter Kropotkin', output) self.assertIn('Buenaventura Durruti', output) self.assertNotIn('This one will not show', output) self.assertEqual(False, final_result)
def test_add_env_variables_to_argparse(self): parser = ArgumentParser(':test') task = get_test_declaration() CommandlineParsingHelper.add_env_variables_to_argparse_description( parser, task) self.assertIn('ORG_NAME (default: International Workers Association)', parser.description)
def test_get_full_description_allows_empty_doc(self): """Test that description can be not defined at all""" declaration = get_test_declaration() declaration.get_task_to_execute().get_description = lambda: None declaration.get_task_to_execute().__doc__ = None self.assertEqual('', declaration.get_full_description())
def test_ljust_task_name(self): """Assert that the formatting is not breaking the description alignment The formatting instructions should not be considered by ljust. Only visible characters should be considered in padding with spaces""" # colored colored_declaration = get_test_declaration() colored_declaration.format_task_name = lambda text: "\x1B[93m" + text + "\x1B[0m" ljusted_colored = TasksListingTask.ljust_task_name(colored_declaration, ':general-strike') # not colored regular_declaration = get_test_declaration() regular_declaration.format_task_name = lambda text: text ljusted_regular = TasksListingTask.ljust_task_name(regular_declaration, ':general-strike') # assert: the coloring should not impact on the filling up size self.assertTrue(ljusted_colored.endswith((' ' * 35)), msg='Expected 35 spaces at the end (fill up)') self.assertTrue(ljusted_regular.endswith((' ' * 35)), msg='Expected 35 spaces at the end (fill up)')
def test_add_env_variables_to_argparse__no_envs(self): parser = ArgumentParser(':test') task = get_test_declaration() # empty the values task.get_task_to_execute().get_declared_envs = lambda: {} CommandlineParsingHelper.add_env_variables_to_argparse_description(parser, task) self.assertNotIn('ORG_NAME (default: International Workers Association)', parser.description) self.assertIn('-- No environment variables declared --', parser.description)
def test_get_full_description_returns_result_of_tasks_get_description_if_defined( self): """TaskDeclaration.get_full_description() returns get_task_to_execute().get_description() if response is not empty """ declaration = get_test_declaration() declaration.get_task_to_execute().get_description = lambda: '10 June 1942 Polish prisoners on work detail ' + \ 'at Auschwitz Birkenau managed to escape while ' + \ 'digging a drainage ditch.' self.assertIn('Polish prisoners', declaration.get_full_description()) self.assertIn('Auschwitz Birkenau', declaration.get_full_description()) self.assertIn('managed to escape', declaration.get_full_description())
def test_get_full_description_returns_docstring_when_description_method_does_not_return_anything( self): """TaskDeclaration.get_full_description() returns docstring of task to execute, when the task does not implement get_description() by itself """ declaration = get_test_declaration() declaration.get_task_to_execute().get_description = lambda: '' declaration.get_task_to_execute().__doc__ = ''' 10 Jun 1973 a strike of gravediggers at 3 cemeteries in the New York metropolitan area was expanded to include 44 others. Strikers defied a court order, the jailing of their union leader and after 27 days they won a pension scheme and pay increase ''' self.assertIn('27 days they won a pension scheme and pay increase', declaration.get_full_description())
def _prepare_test_for_forking_process(task: TaskInterface = None): if not task: task = TestTask() io = IO() string_io = StringIO() temp = TempManager(chdir='/tmp/') container = ApplicationContext([], [], '') container.io = BufferedSystemIO() executor = OneByOneTaskExecutor(container) declaration = get_test_declaration(task) task._io = io task._io.set_log_level('debug') ctx = ExecutionContext(declaration) return string_io, task, executor, io, ctx, temp
def test_executing_multiple_steps_one_by_one_the_order_is_preserved(self): """Assert that execution order is preserved - as we register steps""" io = IO() str_io = StringIO() task_declaration = get_test_declaration() BasicTestingCase.satisfy_task_dependencies(task_declaration.get_task_to_execute(), io=io) ctx = ExecutionContext(task_declaration) executor = DeclarativeExecutor() executor.add_step('python', 'this.io().outln("First"); return True', task_name=':first', rkd_path='', envs={}) executor.add_step('bash', 'echo "Second"; exit 0', task_name=':second', rkd_path='', envs={}) executor.add_step('python', 'this.io().outln("Third"); return True', task_name=':third', rkd_path='', envs={}) with io.capture_descriptors(target_files=[], stream=str_io, enable_standard_out=False): executor.execute_steps_one_by_one(ctx, task_declaration.get_task_to_execute()) self.assertEqual("First\nSecond\r\nThird\n", str_io.getvalue())
def test_requirements_are_written(self): """Verify that basic requirements list is written into requirements.txt file""" backup_dir_path = os.getcwd() try: with tempfile.TemporaryDirectory() as dir_path: os.chdir(dir_path) task = CreateHarborStructureTask() task._io = IO() task.on_requirements_txt_write(ExecutionContext(get_test_declaration())) with open(dir_path + '/requirements.txt', 'rb') as requirements_txt: content = requirements_txt.read().decode('utf-8') self.assertIn('ansible>=2.8', content, msg='Expected Ansible defined in requirements.txt') self.assertIn('rkd-harbor==', content, msg='Expected rkd-harbor to be defined in requirements.txt') finally: os.chdir(backup_dir_path)
def _create_callable_tester(code: str, language: str, io: IO = None) -> bool: if not io: io = IO() executor = DeclarativeExecutor() declaration = get_test_declaration() declaration.get_task_to_execute()._io = io ctx = ExecutionContext(declaration) step = Step( language=language, task_name=':test', code=code, envs={}, task_num=1, rkd_path='' ) if language == 'python': return executor.execute_python_step(ctx, declaration.get_task_to_execute(), step) else: return executor.execute_bash_step(ctx, declaration.get_task_to_execute(), step)