def _non_idempotent_tasks(self, output): """ Parse the output to identify the non idempotent tasks. :param (str) output: A string containing the output of the ansible run. :return: A list containing the names of the non idempotent tasks. """ # Remove blank lines to make regex matches easier. output = re.sub(r'\n\s*\n*', '\n', output) # Remove ansi escape sequences. output = util.strip_ansi_escape(output) # Split the output into a list and go through it. output_lines = output.split('\n') res = [] task_line = '' for _, line in enumerate(output_lines): if line.startswith('TASK'): task_line = line elif line.startswith('changed'): host_name = re.search(r'\[(.*)\]', line).groups()[0] task_name = re.search(r'\[(.*)\]', task_line).groups()[0] res.append(u'* [{}] => {}'.format(host_name, task_name)) return res
def _non_idempotent_tasks(self, output): """ Parses the output to identify the non idempotent tasks. :param (str) output: A string containing the output of the ansible run. :return: A list containing the names of the non idempotent tasks. """ # Remove blank lines to make regex matches easier. output = re.sub(r'\n\s*\n*', '\n', output) # Remove ansi escape sequences. output = util.strip_ansi_escape(output) # Split the output into a list and go through it. output_lines = output.split('\n') res = [] task_line = '' for _, line in enumerate(output_lines): if line.startswith('TASK'): task_line = line elif line.startswith('changed'): host_name = re.search(r'\[(.*)\]', line).groups()[0] task_name = re.search(r'\[(.*)\]', task_line).groups()[0] res.append('* [{}] => {}'.format(host_name, task_name)) return res
def test_print_matrix(capsys, _instance): with console.capture() as capture: _instance.print_matrix() result = util.chomp(util.strip_ansi_escape(capture.get())) matrix_out = u"""--- default: - dependency - lint - cleanup - destroy - syntax - create - prepare - converge - idempotence - side_effect - verify - cleanup - destroy foo: - dependency - lint - cleanup - destroy - syntax - create - prepare - converge - idempotence - side_effect - verify - cleanup - destroy""" assert matrix_out in result
def test_print_environment_vars(capsys): env = { "ANSIBLE_FOO": "foo", "ANSIBLE_BAR": "bar", "ANSIBLE": None, "MOLECULE_FOO": "foo", "MOLECULE_BAR": "bar", "MOLECULE": None, } expected = """DEBUG: ANSIBLE ENVIRONMENT: ANSIBLE_BAR: bar ANSIBLE_FOO: foo DEBUG: MOLECULE ENVIRONMENT: MOLECULE_BAR: bar MOLECULE_FOO: foo DEBUG: SHELL REPLAY: ANSIBLE_BAR=bar ANSIBLE_FOO=foo MOLECULE_BAR=bar MOLECULE_FOO=foo """ with console.capture() as capture: util.print_environment_vars(env) result = util.strip_ansi_escape(capture.get()) assert result == expected
def test_print_debug(): expected = "DEBUG: test_title:\ntest_data\n" with console.capture() as capture: util.print_debug("test_title", "test_data") result = util.strip_ansi_escape(capture.get()) assert result == expected
def test_host_group_vars(scenario_to_test, with_scenario, scenario_name): options = {'all': True} cmd = sh.molecule.bake('test', **options) out = pytest.helpers.run_command(cmd, log=False) out = util.strip_ansi_escape(out.stdout.decode('utf-8')) assert re.search(r'\[instance\].*?ok: \[instance\]', out, re.DOTALL) assert re.search(r'\[example\].*?ok: \[instance\]', out, re.DOTALL) assert re.search(r'\[example_1\].*?ok: \[instance\]', out, re.DOTALL)
def test_host_group_vars(scenario_to_test, with_scenario, scenario_name): cmd = sh.molecule.bake('test') out = pytest.helpers.run_command(cmd, log=False) out = util.strip_ansi_escape(out.stdout) assert re.search('\[all\].*?ok: \[instance-1-default\]', out, re.DOTALL) assert re.search('\[example\].*?ok: \[instance-1-default\]', out, re.DOTALL) assert re.search('\[example_1\].*?ok: \[instance-1-default\]', out, re.DOTALL)
def test_host_group_vars(scenario_to_test, with_scenario, scenario_name): options = { 'all': True, } cmd = sh.molecule.bake('test', **options) out = pytest.helpers.run_command(cmd, log=False) out = util.strip_ansi_escape(out.stdout) assert re.search('\[all\].*?ok: \[instance\]', out, re.DOTALL) assert re.search('\[example\].*?ok: \[instance\]', out, re.DOTALL) assert re.search('\[example_1\].*?ok: \[instance\]', out, re.DOTALL)
def test_command_test_destroy_strategy_always(scenario_to_test, with_scenario, scenario_name, driver_name): options = {"destroy": "always"} with pytest.raises(sh.ErrorReturnCode) as e: cmd = sh.molecule.bake("test", **options) pytest.helpers.run_command(cmd, log=False) stdout = util.strip_ansi_escape(e.value.stdout) assert "Action: 'cleanup'" in stdout assert "PLAY [Destroy]" in stdout assert 0 != e.value.exit_code
def test_strip_ansi_escape(): string = "ls\r\n\x1b[00m\x1b[01;31mfoo\x1b[00m\r\n\x1b[01;31m" assert "ls\r\nfoo\r\n" == util.strip_ansi_escape(string)
def test_strip_ansi_escape(): string = 'ls\r\n\x1b[00m\x1b[01;31mfoo\x1b[00m\r\n\x1b[01;31m' assert 'ls\r\nfoo\r\n' == util.strip_ansi_escape(string)