summaryrefslogtreecommitdiffstats
path: root/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestBundler.py
blob: 5a8c44cd529b9cb8f197e2b9680d51fa4cf078f8 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import os
import sys
import lxml.etree
from mock import Mock, MagicMock, patch
from Bcfg2.Server.Plugins.Bundler import *
from Bcfg2.version import Bcfg2VersionInfo

# 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, "RequiredBundle", name="is_dep")
        lxml.etree.SubElement(has_dep, "RequiredBundle", name="is_mod_dep",
                              inherit_modification="true")
        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")
        lxml.etree.SubElement(expected['has_dep'], "Bundle",
                              name="is_mod_dep")

        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 = set(["error", "xinclude", "has_dep", "indep"])
        metadata.version_info = Bcfg2VersionInfo('1.4.0')

        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)

    def test_BuildStructuresOldClient(self):
        b = self.get_obj()
        b.bundles = dict(has_dep=Mock())
        expected = dict()

        has_dep = lxml.etree.Element("Bundle")
        lxml.etree.SubElement(has_dep, "RequiredBundle", name="is_dep")
        lxml.etree.SubElement(has_dep, "RequiredBundle", name="is_mod_dep",
                              inherit_modification="true")
        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")

        metadata = Mock()
        metadata.bundles = set(["has_dep"])
        metadata.version_info = Bcfg2VersionInfo('1.3.0')

        rv = b.BuildStructures(metadata)
        self.assertEqual(len(rv), len(metadata.bundles))
        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)