summaryrefslogtreecommitdiffstats
path: root/testsuite/Testsrc/Testlib/TestServer/TestPlugin/Testbase.py
blob: f135a0197b7456fed19bcdd5f5dc4785654b5044 (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
import os
import sys
from mock import Mock, MagicMock, patch
from Bcfg2.Server.Plugin.base 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 TestLogger import TestDebuggable


class TestPlugin(TestDebuggable):
    test_obj = Plugin

    def setUp(self):
        TestDebuggable.setUp(self)
        set_setup_default("filemonitor", MagicMock())

    def get_obj(self, core=None):
        if core is None:
            core = Mock()

        @patchIf(not isinstance(os.makedirs, Mock), "os.makedirs", Mock())
        def inner():
            return self.test_obj(core)
        return inner()

    @patch("os.makedirs")
    @patch("os.path.exists")
    def test__init(self, mock_exists, mock_makedirs):
        if self.test_obj.create:
            core = Mock()
            core.setup = MagicMock()

            mock_exists.return_value = True
            p = self.get_obj(core=core)
            self.assertEqual(p.data, os.path.join(datastore, p.name))
            self.assertEqual(p.core, core)
            mock_exists.assert_any_call(p.data)
            self.assertFalse(mock_makedirs.called)

            mock_exists.reset_mock()
            mock_makedirs.reset_mock()
            mock_exists.return_value = False
            p = self.get_obj(core=core)
            self.assertEqual(p.data, os.path.join(datastore, p.name))
            self.assertEqual(p.core, core)
            mock_exists.assert_any_call(p.data)
            mock_makedirs.assert_any_call(p.data)

    @patch("os.makedirs")
    def test_init_repo(self, mock_makedirs):
        self.test_obj.init_repo(datastore)
        mock_makedirs.assert_called_with(os.path.join(datastore,
                                                      self.test_obj.name))