summaryrefslogtreecommitdiff
path: root/moniker/tests/__init__.py
blob: 06f78e8b349bd20baa305ce8f1608063a7bfba81 (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
# Copyright 2012 Managed I.T.
#
# Author: Kiall Mac Innes <kiall@managedit.ie>
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import sys
import unittest
import mox
from moniker.openstack.common import cfg
from moniker.openstack.common import log as logging
from moniker.openstack.common.context import RequestContext, get_admin_context
from moniker import storage

LOG = logging.getLogger(__name__)


class TestCase(unittest.TestCase):
    def setUp(self):
        super(TestCase, self).setUp()

        self.mox = mox.Mox()
        self.config(database_connection='sqlite://',
                    rpc_backend='moniker.openstack.common.rpc.impl_fake',
                    notification_driver=[])
        storage.setup_schema()

    def tearDown(self):
        storage.teardown_schema()
        cfg.CONF.reset()
        self.mox.UnsetStubs()

        super(TestCase, self).tearDown()

    def config(self, **kwargs):
        group = kwargs.pop('group', None)
        for k, v in kwargs.iteritems():
            cfg.CONF.set_override(k, v, group)

    def get_context(self, **kwargs):
        return RequestContext(**kwargs)

    def get_admin_context(self):
        return get_admin_context()


if sys.version_info < (2, 7):
    # Add in some of the nicer methods not present in 2.6
    from contextlib import contextmanager

    def assertIsNone(self, expr, msg=None):
        return self.assertEqual(expr, None, msg)

    TestCase.assertIsNone = assertIsNone

    def assertIsNotNone(self, expr, msg=None):
        return self.assertNotEqual(expr, None, msg)

    TestCase.assertIsNotNone = assertIsNotNone

    def assertRaises(self, excClass, callableObj=None, *args, **kwargs):
        @contextmanager
        def context():
            raised = None
            try:
                yield
            except Exception, e:
                raised = e
            finally:
                if not isinstance(raised, excClass):
                    raise self.failureException(
                        "%s not raised" % str(excClass))

        if callableObj is None:
            return context()
        with context:
            callableObj(*args, **kwargs)

    TestCase.assertRaises = assertRaises