def _execute_mocked_task( params: dict, envs: dict = {}, task: CreateStructureTask = None) -> BufferedSystemIO: io = BufferedSystemIO() if not task: task: CreateStructureTask = CreateStructureTask() BasicTestingCase.satisfy_task_dependencies(task, io=io) task.execute( BasicTestingCase.mock_execution_context(task, params, envs)) return io
def test_input_is_retried_when_validation_fails(self): wizard = Wizard(TestTask()) wizard.sleep_time = 0 wizard.io = BufferedSystemIO() self.retry_num = 0 def mocked_input(secret: bool = False): self.retry_num = self.retry_num + 1 return ['...', '...', 'Buenaventura'][self.retry_num] wizard.input = mocked_input wizard.ask('What\'s your name?', 'name', regexp='([A-Za-z]+)') self.assertEqual({'name': 'Buenaventura'}, wizard.answers)
def test_regexp_validation(self): wizard = Wizard(TaskForTesting()) wizard.sleep_time = 0 wizard.io = BufferedSystemIO() with self.subTest('Input is matching regexp'): wizard.input = lambda secret: 'Buenaventura' wizard.ask('What\'s your name?', 'name', regexp='([A-Za-z]+)') self.assertIn('name', wizard.answers) with self.subTest('Input is not matching regexp'): wizard.input = lambda secret: '...' self.assertRaises( InterruptExecution, lambda: wizard.ask( 'What\'s your name?', 'name', regexp='([A-Za-z]+)'))
def _execute_mocked_task(params: dict, envs: dict = None) -> BufferedSystemIO: if envs is None: envs = {} io = BufferedSystemIO() task: FileRendererTask = FileRendererTask() BasicTestingCase.satisfy_task_dependencies(task, io=io) execution_context = BasicTestingCase.mock_execution_context( task, params, envs) task.execute(execution_context) return io
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_functional_execute_hooks_executes_post_upgrade_hooks(self): """Assert that post-upgrade/history.sh is executed""" self._prepare_test_data() buffer = StringIO() hooks_capturing_io = IO() task = TestTask() task._io = BufferedSystemIO() ctx = ExecutionContext(TaskDeclaration(task), args={}, env={}) with hooks_capturing_io.capture_descriptors(stream=buffer, enable_standard_out=True): task.execute_hooks(ctx, 'post-upgrade') self.assertIn('25 June 1978 the rainbow flag was first flown', buffer.getvalue(), msg='Expected post-upgrade hook to be ran')
def test_is_information_written_through_stderr_methods(self): """To check if information is written to the proper methods we will mock stdout methods""" io = BufferedSystemIO() io._stdout = lambda *args, **kwargs: None try: raise IndexError('Invalid index 5') except Exception as exc: output_formatted_exception(exc, ':my-test-task', io) self.assertIn('IndexError', io.get_value()) self.assertIn('Invalid index 5', io.get_value()) self.assertIn( 'Retry with "-rl debug" switch before failed task to see stacktrace', io.get_value())
def test_finish_is_writing_values(self): tmp_wizard_file = mock_open() rkd_shell_calls = [] with patch('rkd.api.inputoutput.open', tmp_wizard_file, create=True): task = TestTask() task.rkd = lambda *args, **kwargs: rkd_shell_calls.append(args) wizard = Wizard(task) wizard.io = BufferedSystemIO() wizard.input = lambda secret: '1936' wizard.ask('In which year the Spanish social revolution has begun?', attribute='year', regexp='([0-9]{4})', default='1936') wizard.input = lambda secret: 'ait' wizard.ask('Enter new value for COMPOSE_PROJECT_NAME', attribute='COMPOSE_PROJECT_NAME', to_env=True) wizard.finish() # assertions tmp_wizard_file.assert_called_once_with('.rkd/tmp-wizard.json', 'wb') self.assertEqual([':env:set', '--name="COMPOSE_PROJECT_NAME"', '--value="ait"'], rkd_shell_calls[0][0])
def test_formatting_methods_are_clearing_formatting_at_the_end(self): """Check that formatting methods are clearing the formatting at the end""" io = BufferedSystemIO() methods = [ io.h1, io.h2, io.h3, io.h4, io.success_msg, io.error_msg, io.info_msg, io.print_separator, io.print_group ] for method in methods: try: method('test') except TypeError: method() self.assertEqual("\x1B[", io.get_value()[0:2], msg='Expected beginning of formatting') self.assertEqual('[0m', io.get_value().strip()[-3:], msg='Expected formatting clearing at the end') io.clear_buffer()
def test_python_case_syntax_error(self): result = self._create_callable_tester('''impppport os''', language='python', io=BufferedSystemIO()) self.assertFalse(result)
def test_bash_failure_case_on_invalid_exit_code(self): """ Bash callable test: Check if failures are correctly catched """ result = self._create_callable_tester('exit 161', language='bash', io=BufferedSystemIO()) self.assertFalse(result)