summaryrefslogtreecommitdiffstats
path: root/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestRules.py
blob: 7083fff06de82f3b5609a655a5bb14856195de13 (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
import os
import sys
import lxml.etree
import Bcfg2.Server.Plugin
from mock import Mock, MagicMock, patch
from Bcfg2.Server.Plugins.Rules import *

# add all parent testsuite directories to sys.path to allow (most)
# relative imports in python 2.4
path = os.path.dirname(__file__)
while path != "/":
    if os.path.basename(path).lower().startswith("test"):
        sys.path.append(path)
    if os.path.basename(path) == "testsuite":
        break
    path = os.path.dirname(path)
from common import *
from TestPlugin import TestPrioDir


class TestRules(TestPrioDir):
    test_obj = Rules

    def test_HandlesEntry(self):
        r = self.get_obj()
        r.Entries = dict(Path={"/etc/foo.conf": Mock(),
                               "/etc/bar.conf": Mock()})
        r._matches = Mock()
        metadata = Mock()

        entry = lxml.etree.Element("Path", name="/etc/foo.conf")
        self.assertEqual(r.HandlesEntry(entry, metadata),
                         r._matches.return_value)
        r._matches.assert_called_with(entry, metadata,
                                      r.Entries['Path'].keys())

        r._matches.reset_mock()
        entry = lxml.etree.Element("Path", name="/etc/baz.conf")
        self.assertEqual(r.HandlesEntry(entry, metadata),
                         r._matches.return_value)
        r._matches.assert_called_with(entry, metadata,
                                      r.Entries['Path'].keys())

        r._matches.reset_mock()
        entry = lxml.etree.Element("Package", name="foo")
        self.assertFalse(r.HandlesEntry(entry, metadata))

    @patch("Bcfg2.Server.Plugin.PrioDir._matches")
    def test__matches(self, mock_matches):
        r = self.get_obj()
        metadata = Mock()

        # test parent _matches() returning True
        entry = lxml.etree.Element("Path", name="/etc/foo.conf")
        candidate = lxml.etree.Element("Path", name="/etc/bar.conf")
        mock_matches.return_value = True
        self.assertTrue(r._matches(entry, metadata, candidate))
        mock_matches.assert_called_with(r, entry, metadata, candidate)

        # test all conditions returning False
        mock_matches.reset_mock()
        mock_matches.return_value = False
        self.assertFalse(r._matches(entry, metadata, candidate))
        mock_matches.assert_called_with(r, entry, metadata, candidate)

        # test special Path cases -- adding and removing trailing slash
        mock_matches.reset_mock()
        withslash = lxml.etree.Element("Path", name="/etc/foo")
        withoutslash = lxml.etree.Element("Path", name="/etc/foo/")
        self.assertTrue(r._matches(withslash, metadata, withoutslash))
        self.assertTrue(r._matches(withoutslash, metadata, withslash))

        if r._regex_enabled:
            mock_matches.reset_mock()
            candidate = lxml.etree.Element("Path", name="/etc/.*\.conf")
            self.assertTrue(r._matches(entry, metadata, candidate))
            mock_matches.assert_called_with(r, entry, metadata, candidate)
            self.assertIn("/etc/.*\.conf", r._regex_cache.keys())

    def test__regex_enabled(self):
        r = self.get_obj()
        r.core.setup = MagicMock()
        self.assertEqual(r._regex_enabled,
                         r.core.setup.cfp.getboolean.return_value)
        r.core.setup.cfp.getboolean.assert_called_with("rules", "regex",
                                                       default=False)