summaryrefslogtreecommitdiffstats
path: root/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestDecisions.py
blob: 537ceb4ff31e6c3da6a028d8e22fb3f30096c97a (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
import os
import sys
import lxml.etree
from mock import Mock, MagicMock, patch
from Bcfg2.Server.Plugins.Decisions 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 TestStructFile, TestPlugin, TestDecision


class TestDecisionFile(TestStructFile):
    test_obj = DecisionFile

    def test_get_decisions(self):
        df = self.get_obj()
        metadata = Mock()

        df.xdata = None
        self.assertItemsEqual(df.get_decisions(metadata), [])

        df.xdata = lxml.etree.Element("Decisions")
        df.XMLMatch = Mock()
        df.XMLMatch.return_value = lxml.etree.Element("Decisions")
        lxml.etree.SubElement(df.XMLMatch.return_value,
                              "Decision", type="Service", name='*')
        lxml.etree.SubElement(df.XMLMatch.return_value,
                              "Decision", type="Path",
                              name='/etc/apt/apt.conf')

        self.assertItemsEqual(df.get_decisions(metadata),
                              [("Service", '*'),
                               ("Path", '/etc/apt/apt.conf')])
        df.XMLMatch.assert_called_with(metadata)


class TestDecisions(TestPlugin, TestDecision):
    test_obj = Decisions

    def test_GetDecisions(self):
        d = self.get_obj()
        d.whitelist = Mock()
        d.blacklist = Mock()
        metadata = Mock()

        self.assertEqual(d.GetDecisions(metadata, "whitelist"),
                         d.whitelist.get_decision.return_value)
        d.whitelist.get_decision.assert_called_with(metadata)

        self.assertEqual(d.GetDecisions(metadata, "blacklist"),
                         d.blacklist.get_decision.return_value)
        d.blacklist.get_decision.assert_called_with(metadata)