File: TestCliRolePaths.py

package info (click to toggle)
ansible-lint 4.1.0%2Bdfsg.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,096 kB
  • sloc: python: 3,373; sh: 4; makefile: 2
file content (92 lines) | stat: -rw-r--r-- 3,390 bytes parent folder | download
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
import unittest
import subprocess
import os


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

    def run_ansible_lint(self, cwd, bin, role_path, env=None):
        command = '{} {}'.format(bin, role_path)

        result, err = subprocess.Popen(
            [command],
            cwd=cwd,
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            shell=True,
            env=env,
        ).communicate()

        self.assertFalse(err, 'Expected no error but was ' + str(err))

        return result

    def test_run_single_role_path_no_trailing_slash(self):
        cwd = self.local_test_dir
        bin = '../bin/ansible-lint'
        role_path = 'test-role'

        result = self.run_ansible_lint(cwd=cwd, bin=bin, role_path=role_path)
        self.assertIn('Use shell only when shell functionality is required',
                      str(result))

    def test_run_single_role_path_with_trailing_slash(self):
        cwd = self.local_test_dir
        bin = '../bin/ansible-lint'
        role_path = 'test-role/'

        result = self.run_ansible_lint(cwd=cwd, bin=bin, role_path=role_path)
        self.assertIn('Use shell only when shell functionality is required',
                      str(result))

    def test_run_multiple_role_path_no_trailing_slash(self):
        cwd = self.local_test_dir
        bin = '../bin/ansible-lint'
        role_path = 'roles/test-role'

        result = self.run_ansible_lint(cwd=cwd, bin=bin, role_path=role_path)
        self.assertIn('Use shell only when shell functionality is required',
                      str(result))

    def test_run_multiple_role_path_with_trailing_slash(self):
        cwd = self.local_test_dir
        bin = '../bin/ansible-lint'
        role_path = 'roles/test-role/'

        result = self.run_ansible_lint(cwd=cwd, bin=bin, role_path=role_path)
        self.assertIn('Use shell only when shell functionality is required',
                      str(result))

    def test_run_inside_role_dir(self):
        cwd = os.path.join(self.local_test_dir, 'test-role/')
        bin = '../../bin/ansible-lint'
        role_path = '.'

        result = self.run_ansible_lint(cwd=cwd, bin=bin, role_path=role_path)
        self.assertIn('Use shell only when shell functionality is required',
                      str(result))

    def test_run_role_three_dir_deep(self):
        cwd = self.local_test_dir
        bin = '../bin/ansible-lint'
        role_path = 'testproject/roles/test-role'

        result = self.run_ansible_lint(cwd=cwd, bin=bin, role_path=role_path)
        self.assertIn('Use shell only when shell functionality is required',
                      str(result))

    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')
        bin = top_src_dir + '/bin/ansible-lint'
        role_path = 'molecule/default/include-import-role.yml'

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

        result = self.run_ansible_lint(cwd=cwd, bin=bin, role_path=role_path, env=env)
        self.assertIn('Use shell only when shell functionality is required', str(result))