summaryrefslogtreecommitdiffstats
path: root/test/TestCliRolePaths.py
blob: a459df20a41359dff79bf86046cd1bd0f117640d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# pylint: disable=preferred-module  # FIXME: remove once migrated per GH-725
import os
import unittest
from pathlib import Path

import pytest

from ansiblelint.testing import run_ansible_lint


class TestCliRolePaths(unittest.TestCase):
    def setUp(self):
        self.local_test_dir = os.path.dirname(os.path.realpath(__file__))

    def test_run_single_role_path_no_trailing_slash_module(self):
        cwd = self.local_test_dir
        role_path = '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_single_role_path_with_trailing_slash(self):
        cwd = self.local_test_dir
        role_path = '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_multiple_role_path_no_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_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_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)

    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)

    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_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

    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

    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


@pytest.mark.parametrize(('result', 'env'), (
    (True, {
        "GITHUB_ACTIONS": "true",
        "GITHUB_WORKFLOW": "foo"
    }),
    (False, None)),
    ids=("on", "off"))
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,severity=MEDIUM::[E101] '
        'Deprecated always_run'
    )
    assert (expected in result_gh.stdout) is result