summaryrefslogtreecommitdiffstats
path: root/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestBundler.py
blob: cfb379c40463eae99f83fb80055bbd008b70ae93 (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
import os
import sys
import lxml.etree
from mock import Mock, MagicMock, patch
from Bcfg2.Server.Plugins.Bundler 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, TestStructure, \
    TestXMLDirectoryBacked


class TestBundleFile(TestStructFile):
    test_obj = BundleFile
    path = os.path.join(datastore, "test", "test1.xml")

    def test_bundle_name(self):
        cases = [("foo.xml", "foo"),
                 ("foo.bar.xml", "foo.bar"),
                 ("foo-bar-baz.xml", "foo-bar-baz"),
                 ("foo....xml", "foo..."),
                 ("foo.genshi", "foo")]
        bf = self.get_obj()
        for fname, bname in cases:
            bf.name = fname
            self.assertEqual(bf.bundle_name, bname)


class TestBundler(TestPlugin, TestStructure, TestXMLDirectoryBacked):
    test_obj = Bundler

    def get_obj(self, core=None):
        @patch("%s.%s.add_directory_monitor" % (self.test_obj.__module__,
                                                self.test_obj.__name__),
               Mock())
        def inner():
            return TestPlugin.get_obj(self, core=core)
        return inner()

    @patch("Bcfg2.Server.Plugin.XMLDirectoryBacked.HandleEvent")
    def test_HandleEvent(self, mock_HandleEvent):
        b = self.get_obj()
        b.bundles = dict(foo=Mock(), bar=Mock())
        b.entries = {"foo.xml": BundleFile("foo.xml"),
                     "baz.xml": BundleFile("baz.xml")}
        event = Mock()
        b.HandleEvent(event)
        mock_HandleEvent.assert_called_with(b, event)
        self.assertItemsEqual(b.bundles,
                              dict(foo=b.entries['foo.xml'],
                                   baz=b.entries['baz.xml']))

    def test_BuildStructures(self):
        b = self.get_obj()
        b.bundles = dict(error=Mock(), skip=Mock(), xinclude=Mock(),
                         has_dep=Mock(), is_dep=Mock(), indep=Mock())
        expected = dict()

        b.bundles['error'].XMLMatch.side_effect = TemplateError(None)

        xinclude = lxml.etree.Element("Bundle")
        lxml.etree.SubElement(lxml.etree.SubElement(xinclude, "Bundle"),
                              "Path", name="/test")
        b.bundles['xinclude'].XMLMatch.return_value = xinclude
        expected['xinclude'] = lxml.etree.Element("Bundle", name="xinclude")
        lxml.etree.SubElement(expected['xinclude'], "Path", name="/test")

        has_dep = lxml.etree.Element("Bundle")
        lxml.etree.SubElement(has_dep, "Bundle", name="is_dep")
        lxml.etree.SubElement(has_dep, "Package", name="foo")
        b.bundles['has_dep'].XMLMatch.return_value = has_dep
        expected['has_dep'] = lxml.etree.Element("Bundle", name="has_dep")
        lxml.etree.SubElement(expected['has_dep'], "Package", name="foo")

        is_dep = lxml.etree.Element("Bundle")
        lxml.etree.SubElement(is_dep, "Package", name="bar")
        b.bundles['is_dep'].XMLMatch.return_value = is_dep
        expected['is_dep'] = lxml.etree.Element("Bundle", name="is_dep")
        lxml.etree.SubElement(expected['is_dep'], "Package", name="bar")

        indep = lxml.etree.Element("Bundle", independent="true")
        lxml.etree.SubElement(indep, "Service", name="baz")
        b.bundles['indep'].XMLMatch.return_value = indep
        expected['indep'] = lxml.etree.Element("Independent", name="indep")
        lxml.etree.SubElement(expected['indep'], "Service", name="baz")

        metadata = Mock()
        metadata.bundles = ["error", "xinclude", "has_dep", "indep"]

        rv = b.BuildStructures(metadata)
        self.assertEqual(len(rv), 4)
        for bundle in rv:
            name = bundle.get("name")
            self.assertIsNotNone(name,
                                "Bundle %s was not built" % name)
            self.assertIn(name, expected,
                          "Unexpected bundle %s was built" % name)
            self.assertXMLEqual(bundle, expected[name],
                                "Bundle %s was not built correctly" % name)
            b.bundles[name].XMLMatch.assert_called_with(metadata)

        b.bundles['error'].XMLMatch.assert_called_with(metadata)
        self.assertFalse(b.bundles['skip'].XMLMatch.called)