Esempio n. 1
0
class TestSudoRuleWithFile(unittest.TestCase):
    file1 = 'test/sudo.yml'

    def setUp(self):
        self.rule = SudoRule()

    def test_matchplay_sudo(self):
        yaml = ansiblelint.utils.parse_yaml_linenumbers(open(self.file1).read(), self.file1)

        self.assertTrue(yaml)
        for play in yaml:
            result = self.rule.matchplay(self.file1, play)
            self.assertEquals(2, len(result))

    def test_matchtask_sudo(self):
        yaml = ansiblelint.utils.parse_yaml_linenumbers(open(self.file1).read(), self.file1)
        results = []
        for task in ansiblelint.utils.get_normalized_tasks(yaml, dict(path=self.file1, type='playbook')):
            results.append(self.rule.matchtask(self.file1, task))
        self.assertEquals(1, len([result for result in results if result]))
Esempio n. 2
0
class TestSudoRule(unittest.TestCase):
    simple_dict_yaml = {
        'value1': '{foo}}',
        'value2': 2,
        'value3': ['foo', 'bar', '{baz}}'],
        'value4': '{bar}',
        'value5': '{{baz}',
    }

    def setUp(self):
        self.rule = SudoRule()

    def test_check_value_simple_matching(self):
        result = self.rule._check_value("sudo: yes")
        self.assertEquals(0, len(result))

    def test_check_value_shallow_dict(self):
        result = self.rule._check_value({
            'sudo': 'yes',
            'sudo_user': '******'
        })
        self.assertEquals(2, len(result))

    def test_check_value_nested(self):
        yaml = [{
            'hosts':
            'all',
            'sudo':
            'yes',
            'sudo_user':
            '******',
            'tasks': [{
                'name': 'test',
                'debug': 'msg=test',
                'sudo': 'yes',
                'sudo_user': '******'
            }]
        }]
        result = self.rule._check_value(yaml)
        self.assertEquals(2, len(result))
Esempio n. 3
0
class TestSudoRuleWithFile(unittest.TestCase):
    file1 = 'test/sudo.yml'

    def setUp(self):
        self.rule = SudoRule()

    def test_matchplay_sudo(self):
        yaml = ansiblelint.utils.parse_yaml_linenumbers(open(self.file1).read())

        self.assertTrue(yaml)
        for play in yaml:
            result = self.rule.matchplay(self.file1, play)
            self.assertEquals(2, len(result))
Esempio n. 4
0
class TestSudoRule(unittest.TestCase):
    simple_dict_yaml = {
        'value1': '{foo}}',
        'value2': 2,
        'value3': ['foo', 'bar', '{baz}}'],
        'value4': '{bar}',
        'value5': '{{baz}',
    }

    def setUp(self):
        self.rule = SudoRule()

    def test_check_value_simple_matching(self):
        result = self.rule._check_value("sudo: yes")
        self.assertEquals(0, len(result))

    def test_check_value_shallow_dict(self):
        result = self.rule._check_value({'sudo': 'yes', 'sudo_user': '******'})
        self.assertEquals(2, len(result))

    def test_check_value_nested(self):
        yaml = [
            {
                'hosts': 'all',
                'sudo': 'yes',
                'sudo_user': '******',
                'tasks': [
                    {
                        'name': 'test',
                        'debug': 'msg=test',
                        'sudo': 'yes',
                        'sudo_user': '******'
                    }
                ]
            }
        ]
        result = self.rule._check_value(yaml)
        self.assertEquals(2, len(result))
Esempio n. 5
0
class TestSudoRuleWithFile(unittest.TestCase):
    file1 = 'test/sudo.yml'

    def setUp(self):
        self.rule = SudoRule()

    def test_matchplay_sudo(self):
        yaml = ansiblelint.utils.parse_yaml_linenumbers(
            open(self.file1).read())

        self.assertTrue(yaml)
        for play in yaml:
            result = self.rule.matchplay(self.file1, play)
            self.assertEquals(2, len(result))
Esempio n. 6
0
class TestSudoRuleWithFile(unittest.TestCase):
    file1 = 'test/sudo.yml'

    def setUp(self):
        self.rule = SudoRule()

    def test_matchplay_sudo(self):
        yaml = ansiblelint.utils.parse_yaml_linenumbers(
            open(self.file1).read(), self.file1)

        self.assertTrue(yaml)
        for play in yaml:
            result = self.rule.matchplay(self.file1, play)
            self.assertEquals(2, len(result))

    def test_matchtask_sudo(self):
        yaml = ansiblelint.utils.parse_yaml_linenumbers(
            open(self.file1).read(), self.file1)
        results = []
        for task in ansiblelint.utils.get_normalized_tasks(
                yaml, dict(path=self.file1, type='playbook')):
            results.append(self.rule.matchtask(self.file1, task))
        self.assertEquals(1, len([result for result in results if result]))
Esempio n. 7
0
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))
Esempio n. 8
0
 def setUp(self):
     self.rule = SudoRule()
Esempio n. 9
0
 def setUp(self):
     self.rule = SudoRule()
def test_rule_linenumber(monkeypatch) -> None:
    """Check that SudoRule offense contains a line number."""
    rule = SudoRule()
    lintable = Lintable("", kind="tasks", content=TEST_TASKLIST)
    matches = rule.matchyaml(lintable)
    assert matches[0].linenumber == 5
Esempio n. 11
0
def test_rule_linenumber(monkeypatch):
    """Check that SudoRule offense contains a line number."""
    rule = SudoRule()
    matches = rule.matchyaml(dict(path="", type='tasklist'), TEST_TASKLIST)
    assert matches[0].linenumber == 5