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

# 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(foo=Mock(), bar=Mock(), baz=Mock())
        metadata = Mock()
        metadata.bundles = ["foo", "baz"]

        self.assertItemsEqual(b.BuildStructures(metadata),
                              [b.bundles[n].XMLMatch.return_value
                               for n in metadata.bundles])
        for bname in metadata.bundles:
            b.bundles[bname].XMLMatch.assert_called_with(metadata)