Beispiel #1
0
    def _get_ansible_syntax_check_matches(lintable: Lintable) -> List[MatchError]:
        """Run ansible syntax check and return a list of MatchError(s)."""
        if lintable.kind != 'playbook':
            return []

        with timed_info("Executing syntax check on %s", lintable.path):
            cmd = ['ansible-playbook', '--syntax-check', str(lintable.path)]
            run = subprocess.run(
                cmd,
                stdin=subprocess.PIPE,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE,
                shell=False,  # needed when command is a list
                universal_newlines=True,
                check=False
            )
            result = []
        if run.returncode != 0:
            message = None
            filename = str(lintable.path)
            linenumber = 0
            column = None

            stderr = strip_ansi_escape(run.stderr)
            stdout = strip_ansi_escape(run.stdout)
            if stderr:
                details = stderr
                if stdout:
                    details += "\n" + stdout
            else:
                details = stdout

            m = _ansible_syntax_check_re.search(stderr)
            if m:
                message = m.groupdict()['title']
                # Ansible returns absolute paths
                filename = m.groupdict()['filename']
                linenumber = int(m.groupdict()['line'])
                column = int(m.groupdict()['column'])

            if run.returncode == 4:
                rule: BaseRule = AnsibleSyntaxCheckRule()
            else:
                rule = RuntimeErrorRule()
                if not message:
                    message = (
                        f"Unexpected error code {run.returncode} from "
                        f"execution of: {' '.join(cmd)}")

            result.append(MatchError(
                message=message,
                filename=filename,
                linenumber=linenumber,
                column=column,
                rule=rule,
                details=details
                ))
        return result
Beispiel #2
0
    def test_run_role_name_invalid(self) -> None:
        cwd = self.local_test_dir
        role_path = 'roles/invalid-name'

        result = run_ansible_lint(role_path, cwd=cwd)
        assert 'role-name: Role name invalid-name does not match' in strip_ansi_escape(
            result.stdout)
Beispiel #3
0
    def test_run_invalid_role_name_from_meta(self) -> None:
        cwd = self.local_test_dir
        role_path = 'roles/invalid_due_to_meta'

        result = run_ansible_lint(role_path, cwd=cwd)
        assert ('role-name: Role name invalid-due-to-meta does not match'
                in strip_ansi_escape(result.stdout))