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

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

SUCCESS_TASKS = '''
---
- hosts: hosts
  vars:
    varset: varset
  tasks:
    - name: octal permissions test success (0600)
      file:
        path: foo
        mode: 0600

    - name: octal permissions test success (0000)
      file:
        path: foo
        mode: 0000

    - name: octal permissions test success (02000)
      file:
        path: bar
        mode: 02000

    - name: octal permissions test success (02751)
      file:
        path: bar
        mode: 02751

    - name: octal permissions test success (0777)
      file: path=baz mode=0777

    - name: octal permissions test success (0711)
      file: path=baz mode=0711

    - name:  permissions test success (0777)
      file: path=baz mode=u+rwx

    - name: octal permissions test success (777)
      file: path=baz mode=777

    - name: octal permissions test success (733)
      file: path=baz mode=733
'''

FAIL_TASKS = '''
---
- hosts: hosts
  vars:
    varset: varset
  tasks:
    - name: octal permissions test fail (600)
      file:
        path: foo
        mode: 600

    - name: octal permissions test fail (710)
      file:
        path: foo
        mode: 710

    - name: octal permissions test fail (123)
      file:
        path: foo
        mode: 123

    - name: octal permissions test fail (2000)
      file:
        path: bar
        mode: 2000
'''


class TestOctalPermissionsRuleWithFile(unittest.TestCase):

    collection = RulesCollection()
    VALID_MODES = [0o777, 0o775, 0o770, 0o755, 0o750, 0o711, 0o710, 0o700,
                   0o666, 0o664, 0o660, 0o644, 0o640, 0o600,
                   0o555, 0o551, 0o550, 0o511, 0o510, 0o500,
                   0o444, 0o440, 0o400]

    INVALID_MODES = [777, 775, 770, 755, 750, 711, 710, 700,
                     666, 664, 660, 644, 640, 622, 620, 600,
                     555, 551, 550,  # 511 == 0o777, 510 == 0o776, 500 == 0o764
                     444, 440, 400]

    def setUp(self):
        self.rule = OctalPermissionsRule()
        self.collection.register(self.rule)
        self.runner = RunFromText(self.collection)

    def test_success(self):
        results = self.runner.run_playbook(SUCCESS_TASKS)
        self.assertEqual(0, len(results))

    def test_fail(self):
        results = self.runner.run_playbook(FAIL_TASKS)
        self.assertEqual(4, len(results))

    def test_valid_modes(self):
        for mode in self.VALID_MODES:
            self.assertFalse(self.rule.is_invalid_permission(mode),
                             msg="0o%o should be a valid mode" % mode)

    def test_invalid_modes(self):
        for mode in self.INVALID_MODES:
            self.assertTrue(self.rule.is_invalid_permission(mode),
                            msg="%d should be an invalid mode" % mode)