summaryrefslogtreecommitdiffstats
path: root/test/TestSudoRule.py
blob: a1058548997b4504af75dab3ff29e19c9968c30d (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
# pylint: disable=preferred-module  # FIXME: remove once migrated per GH-725
import unittest

from ansiblelint.rules import RulesCollection
from ansiblelint.rules.SudoRule import SudoRule
from ansiblelint.testing import RunFromText

ROLE_2_ERRORS = '''
- name: test
  debug:
    msg: 'test message'
  sudo: yes
  sudo_user: nobody
'''

ROLE_0_ERRORS = '''
- name: test
  debug:
    msg: 'test message'
  become: yes
  become_user: somebody
'''

PLAY_4_ERRORS = '''
- hosts: all
  sudo: yes
  sudo_user: somebody
  tasks:
  - name: test
    debug:
      msg: 'test message'
    sudo: yes
    sudo_user: nobody
'''

PLAY_1_ERROR = '''
- hosts: all
  tasks:
  - name: test
    debug:
      msg: 'test message'
    sudo: yes
'''


class TestSudoRule(unittest.TestCase):
    collection = RulesCollection()
    collection.register(SudoRule())

    def setUp(self):
        self.runner = RunFromText(self.collection)

    def test_run_role_fail(self):
        results = self.runner.run_role_tasks_main(ROLE_2_ERRORS)
        self.assertEqual(2, len(results))

    def test_run_role_pass(self):
        results = self.runner.run_role_tasks_main(ROLE_0_ERRORS)
        self.assertEqual(0, len(results))

    def test_play_root_and_task_fail(self):
        results = self.runner.run_playbook(PLAY_4_ERRORS)
        self.assertEqual(4, len(results))

    def test_play_task_fail(self):
        results = self.runner.run_playbook(PLAY_1_ERROR)
        self.assertEqual(1, len(results))