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)
Example #2
0
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)

    def test_file_positive(self):
        success = 'test/octalpermissions-success.yml'
        good_runner = Runner(self.collection, success, [], [], [])
        self.assertEqual([], good_runner.run())

    def test_file_negative(self):
        failure = 'test/octalpermissions-failure.yml'
        bad_runner = Runner(self.collection, failure, [], [], [])
        errs = bad_runner.run()
        self.assertEqual(5, len(errs))

    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)
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)
 def test_files(self):
     self.collection.register(OctalPermissionsRule())
     failure = 'test/octalpermissions-failure.yml'
     bad_runner = ansiblelint.Runner(self.collection, [failure], [], [], [])
     errs = bad_runner.run()
     # TODO: all errors are counted twice. Why?
     self.assertEqual(6, len(errs))
class TestOctalPermissionsRule(unittest.TestCase):
    rule = OctalPermissionsRule()
    one_to_seven = [str(digit) for digit in range(8)]
    # All possible modes are any permutation of three digits in 1-7
    bad_permutations = set(permutations(combinations(one_to_seven, 3), 3))
    # Join tuples to strings and flatten the list
    modes = ["".join(tup) for lst in bad_permutations for tup in lst]
    bad_modes = ["    mode: " + mode for mode in modes]
    # Valid modes are just the bad ones with a leading zero
    good_modes = ["    mode: 0" + mode for mode in modes]

    # Ensure that the given regex matches all octal numbers appropriately
    def test_regex_positives(self):
        for good in self.good_modes:
            self.assertRegexpMatches(good, self.rule.mode_regex)
            self.assertRegexpMatches(good, self.rule.valid_mode_regex)

    def test_regex_negatives(self):
        for bad in self.bad_modes:
            self.assertRegexpMatches(bad, self.rule.mode_regex)
            self.assertNotRegexpMatches(bad, self.rule.valid_mode_regex)
Example #6
0
 def setUp(self) -> None:
     self.rule = OctalPermissionsRule()
     self.collection.register(self.rule)
     self.runner = RunFromText(self.collection)
 def setUp(self):
     self.collection.register(OctalPermissionsRule())
 def setUp(self):
     self.rule = OctalPermissionsRule()
     self.collection.register(self.rule)
     self.runner = RunFromText(self.collection)
 def test_file_positive(self):
     self.collection.register(OctalPermissionsRule())
     success = 'test/octalpermissions-success.yml'
     good_runner = ansiblelint.Runner(self.collection, [success], [], [],
                                      [])
     self.assertEqual([], good_runner.run())
Example #10
0
 def test_file_negative(self):
     self.collection.register(OctalPermissionsRule())
     failure = 'test/octalpermissions-failure.yml'
     bad_runner = ansiblelint.Runner(self.collection, failure, [], [], [])
     errs = bad_runner.run()
     self.assertEqual(4, len(errs))