示例#1
0
def test_execute_exits_when_ansible_lint_fails(
        mocker, patched_ansible_lint, patched_trailing, molecule_instance):
    patched_ansible_lint.side_effect = sh.ErrorReturnCode_2(sh.ls, None, None)

    v = verify.Verify({}, {}, molecule_instance)
    with pytest.raises(SystemExit):
        v.execute()

    patched_ansible_lint.assert_called_once_with(molecule_instance)
 def test_run_step_fail_sonar_scanner_tests_failed(self, sonar_mock):
     self.__run__run_step_fail_sonar_scanner_error_test(
         sonar_scanner_error=sh.ErrorReturnCode_2('sonar-scanner',
                                                  b'mock out',
                                                  b'mock user error'),
         expected_result_message_regex=re.compile(
             r"Static code analysis failed."
             r" See 'sonarqube-result-set' result artifact for details.",
             re.DOTALL),
         sonar_mock=sonar_mock)
示例#3
0
    def __run_test___run_oscap_scan_xccdf_do_not_fetch_remote_with_profile_all_pass(
            self,
            buildah_mock,
            oscap_eval_type,
            oscap_fetch_remote_resources,
            oscap_stdout,
            oscap_stdout_expected,
            oscap_profile=None,
            oscap_tailoring_file=None,
            oscap_eval_success_expected=True,
            exit_code=0,
            oscap_eval_fails_expected=None):
        with TempDirectory() as temp_dir:
            buildah_unshare_comand = sh.buildah.bake('unshare')
            oscap_input_file = '/does/not/matter/input.xml'
            oscap_out_file_path = os.path.join(temp_dir.path, 'out')
            oscap_xml_results_file_path = '/does/not/matter/results.xml'
            oscap_html_report_path = '/does/not/matter/results.html'
            container_mount_path = '/does/not/matter/coutainer_mount'

            exception = None
            if exit_code == 2:
                exception = sh.ErrorReturnCode_2(
                    'oscap-chroot eval', bytes(oscap_stdout, 'utf-8'),
                    bytes(f'mock error - exit code {exit_code}', 'utf-8'))
            elif exit_code == 1:
                exception = sh.ErrorReturnCode_1(
                    'oscap-chroot eval', bytes(oscap_stdout, 'utf-8'),
                    bytes(f'mock error - exit code {exit_code}', 'utf-8'))
            elif exit_code:
                exception = sh.ErrorReturnCode(
                    'oscap-chroot eval', bytes(oscap_stdout, 'utf-8'),
                    bytes(f'mock error - exit code {exit_code}', 'utf-8'))

            buildah_mock.bake('unshare').bake(
                'oscap-chroot').side_effect = create_sh_side_effect(
                    mock_stdout=oscap_stdout, exception=exception)

            stdout_buff = StringIO()
            with redirect_stdout(stdout_buff):
                oscap_eval_success, oscap_eval_fails = OpenSCAPGeneric._OpenSCAPGeneric__run_oscap_scan(
                    buildah_unshare_comand=buildah_unshare_comand,
                    oscap_eval_type=oscap_eval_type,
                    oscap_input_file=oscap_input_file,
                    oscap_out_file_path=oscap_out_file_path,
                    oscap_xml_results_file_path=oscap_xml_results_file_path,
                    oscap_html_report_path=oscap_html_report_path,
                    container_mount_path=container_mount_path,
                    oscap_profile=oscap_profile,
                    oscap_tailoring_file=oscap_tailoring_file,
                    oscap_fetch_remote_resources=oscap_fetch_remote_resources)

            if oscap_profile:
                oscap_profile_flag = f"--profile={oscap_profile}"
            else:
                oscap_profile_flag = None

            if oscap_fetch_remote_resources:
                oscap_fetch_remote_resources_flag = "--fetch-remote-resources"
            else:
                oscap_fetch_remote_resources_flag = None

            if oscap_tailoring_file:
                oscap_tailoring_file_flag = f"--tailoring-file={oscap_tailoring_file}"
            else:
                oscap_tailoring_file_flag = None

            buildah_mock.bake('unshare').bake.assert_called_with(
                'oscap-chroot')
            buildah_mock.bake('unshare').bake(
                'oscap-chroot').assert_called_once_with(
                    container_mount_path,
                    oscap_eval_type,
                    'eval',
                    oscap_profile_flag,
                    oscap_fetch_remote_resources_flag,
                    oscap_tailoring_file_flag,
                    f'--results={oscap_xml_results_file_path}',
                    f'--report={oscap_html_report_path}',
                    oscap_input_file,
                    _out=Any(IOBase),
                    _err=Any(IOBase),
                    _tee='err')

            self.assertEqual(oscap_eval_success, oscap_eval_success_expected)

            if oscap_eval_fails_expected:
                self.assertEqual(oscap_eval_fails, oscap_eval_fails_expected)

            stdout = stdout_buff.getvalue()
            self.assertEqual(stdout, oscap_stdout_expected)