Beispiel #1
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))
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)
    def test_run_multiple_role_path_with_trailing_slash(self):
        cwd = self.local_test_dir
        role_path = 'roles/test-role/'

        result = run_ansible_lint(role_path, cwd=cwd)
        self.assertIn('Use shell only when shell functionality is required',
                      result.stdout)
    def test_run_single_role_path_no_trailing_slash_script(self):
        cwd = self.local_test_dir
        role_path = 'test-role'

        result = run_ansible_lint(role_path, cwd=cwd, bin="ansible-lint")
        self.assertIn('Use shell only when shell functionality is required',
                      result.stdout)
    def test_run_inside_role_dir(self):
        cwd = os.path.join(self.local_test_dir, 'test-role/')
        role_path = '.'

        result = run_ansible_lint(role_path, cwd=cwd)
        self.assertIn('Use shell only when shell functionality is required',
                      result.stdout)
    def test_run_role_three_dir_deep(self):
        cwd = self.local_test_dir
        role_path = 'testproject/roles/test-role'

        result = run_ansible_lint(role_path, cwd=cwd)
        self.assertIn('Use shell only when shell functionality is required',
                      result.stdout)
Beispiel #7
0
    def test_run_single_role_path_no_trailing_slash_module(self) -> None:
        cwd = self.local_test_dir
        role_path = 'roles/test-role'

        result = run_ansible_lint(role_path, cwd=cwd)
        self.assertIn('Use shell only when shell functionality is required',
                      result.stdout)
Beispiel #8
0
def test_custom_kinds():
    """Check if user defined kinds are used."""
    result = run_ansible_lint('-vv', '--offline', 'examples/other/')
    assert result.returncode == 0
    # .yaml-too is not a recognized extension and unless is manually defined
    # in our .ansible-lint config, the test would not identify it as yaml file.
    assert "Examining examples/other/some.yaml-too of type yaml" in result.stderr
Beispiel #9
0
    def test_run_role_name_with_prefix(self):
        cwd = self.local_test_dir
        role_path = 'roles/ansible-role-foo'

        result = run_ansible_lint(role_path, cwd=cwd)
        assert len(result.stdout) == 0
        assert "Added ANSIBLE_ROLES_PATH=roles:.cache/roles" in result.stderr
        assert result.returncode == 0
    def test_run_role_name_from_meta(self):
        cwd = self.local_test_dir
        role_path = 'roles/valid-due-to-meta'

        result = run_ansible_lint(role_path, cwd=cwd)
        assert len(result.stdout) == 0
        assert len(result.stderr) == 0
        assert result.returncode == 0
Beispiel #11
0
    def test_run_role_name_from_meta(self):
        cwd = self.local_test_dir
        role_path = 'roles/valid-due-to-meta'

        result = run_ansible_lint(role_path, cwd=cwd)
        assert len(result.stdout) == 0
        assert "Added ANSIBLE_ROLES_PATH=roles:.cache/roles" in result.stderr
        assert result.returncode == 0
    def test_run_role_name_with_prefix(self):
        cwd = self.local_test_dir
        role_path = 'roles/ansible-role-foo'

        result = run_ansible_lint(role_path, cwd=cwd)
        assert len(result.stdout) == 0
        assert len(result.stderr) == 0
        assert result.returncode == 0
Beispiel #13
0
def test_prerun_reqs_v2() -> None:
    """Checks that the linter can auto-install requirements v2 when found."""
    cwd = os.path.realpath(
        os.path.join(os.path.dirname(os.path.realpath(__file__)), "..",
                     "examples", "reqs_v2"))
    result = run_ansible_lint("-v", ".", cwd=cwd)
    assert "Running ansible-galaxy role install" in result.stderr, result.stderr
    assert "Running ansible-galaxy collection install" in result.stderr, result.stderr
    assert result.returncode == 0, result
Beispiel #14
0
    def test_run_role_name_with_prefix(self) -> None:
        cwd = self.local_test_dir
        role_path = 'roles/ansible-role-foo'

        result = run_ansible_lint("-v", role_path, cwd=cwd)
        assert len(result.stdout) == 0
        assert (
            "Added ANSIBLE_ROLES_PATH=~/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles:roles"
            in result.stderr)
        assert result.returncode == 0
Beispiel #15
0
def test_run_playbook_github(result, env):
    """Call ansible-lint simulating GitHub Actions environment."""
    cwd = str(Path(__file__).parent.parent.resolve())
    role_path = 'examples/example.yml'

    result_gh = run_ansible_lint(role_path, cwd=cwd, env=env)

    expected = ('::error file=examples/example.yml,line=47::[E101] '
                'Deprecated always_run')
    assert (expected in result_gh.stdout) is result
Beispiel #16
0
    def test_run_role_name_from_meta(self) -> None:
        cwd = self.local_test_dir
        role_path = 'roles/valid-due-to-meta'

        result = run_ansible_lint("-v", role_path, cwd=cwd)
        assert len(result.stdout) == 0
        assert (
            "Added ANSIBLE_ROLES_PATH=~/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles:roles"
            in result.stderr)
        assert result.returncode == 0
    def test_run_playbook(self):
        """Call ansible-lint the way molecule does."""
        top_src_dir = os.path.dirname(self.local_test_dir)
        cwd = os.path.join(top_src_dir, 'test/roles/test-role')
        role_path = 'molecule/default/include-import-role.yml'

        env = os.environ.copy()
        env['ANSIBLE_ROLES_PATH'] = os.path.dirname(cwd)

        result = run_ansible_lint(role_path, cwd=cwd, env=env)
        self.assertIn('Use shell only when shell functionality is required', result.stdout)
Beispiel #18
0
    def test_run_playbook(self) -> None:
        """Call ansible-lint the way molecule does."""
        cwd = os.path.abspath(
            os.path.join(self.local_test_dir, 'roles/test-role'))
        lintable = 'molecule/default/include-import-role.yml'
        role_path = str(Path(cwd).parent.resolve())

        env = os.environ.copy()
        env['ANSIBLE_ROLES_PATH'] = role_path

        result = run_ansible_lint(lintable, cwd=cwd, env=env)
        self.assertIn('Use shell only when shell functionality is required',
                      result.stdout)
Beispiel #19
0
def test_template_lookup(role: str, expect_warning: bool) -> None:
    """Assure lookup plugins used in templates does not trigger Ansible warnings."""
    task_path = os.path.realpath(
        os.path.join(
            os.path.dirname(os.path.realpath(__file__)),
            "..",
            "examples",
            "roles",
            role,
            "tasks",
            "main.yml",
        ))
    result = run_ansible_lint("-v", task_path)
    assert ("Unable to find" in result.stderr) == expect_warning
    def test_run_single_role_path_with_roles_path_env(self):
        """Test for role name collision with ANSIBLE_ROLES_PATH.

        Test if ansible-lint chooses the role in the current directory when the role
        specified as parameter exists in the current directory and the ANSIBLE_ROLES_PATH.
        """
        cwd = self.local_test_dir
        role_path = 'test-role'

        env = os.environ.copy()
        env['ANSIBLE_ROLES_PATH'] = os.path.join(cwd, 'use-as-default-roles-path')

        result = run_ansible_lint(role_path, cwd=cwd, env=env)
        assert 'Use shell only when shell functionality is required' in result.stdout
Beispiel #21
0
def test_run_playbook_github(result: bool, env: Dict[str, str]) -> None:
    """Call ansible-lint simulating GitHub Actions environment."""
    cwd = str(Path(__file__).parent.parent.resolve())
    role_path = 'examples/playbooks/example.yml'

    if env is None:
        env = {}
    env['PATH'] = os.environ['PATH']
    result_gh = run_ansible_lint(role_path, cwd=cwd, env=env)

    expected = (
        '::warning file=examples/playbooks/example.yml,line=44,severity=VERY_LOW::package-latest '
        'Package installs should not use latest')
    assert (expected in result_gh.stdout) is result
Beispiel #22
0
def test_rich_rule_listing():
    """Test that rich list format output is rendered as a table.

    This check also offers the contract of having rule id, short and long
    descriptions in the console output.
    """
    rules_path = os.path.abspath('./test/rules')
    result = run_ansible_lint("-r", rules_path, "-f", "rich", "-L")
    assert result.returncode == 0

    for rule in RulesCollection([rules_path]):
        assert rule.id in result.stdout
        assert rule.shortdesc in result.stdout
        # description could wrap inside table, so we do not check full length
        assert rule.description[:30] in result.stdout
    def test_run_role_name_invalid(self):
        cwd = self.local_test_dir
        role_path = 'roles/invalid-name'

        result = run_ansible_lint(role_path, cwd=cwd)
        assert '106: Role name invalid-name does not match' in result.stdout
    def test_run_invalid_role_name_from_meta(self):
        cwd = self.local_test_dir
        role_path = 'roles/invalid_due_to_meta'

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