summaryrefslogtreecommitdiff
path: root/ceilometer/storage/impl_test.py
blob: c6e2d7e89e30519403e6ea54719af30d47fdb946 (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
# -*- encoding: utf-8 -*-
#
# Copyright © 2012 New Dream Network, LLC (DreamHost)
#
# Author: Doug Hellmann <doug.hellmann@dreamhost.com>
#
# 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.
"""In-memory storage driver for use with tests.

This driver is based on MIM, an in-memory version of MongoDB.
"""

import os
import nose

try:
    from ming import mim
except ImportError:
    mim = None

from ceilometer.openstack.common import log as logging

from ceilometer.storage import base
from ceilometer.storage import impl_mongodb


LOG = logging.getLogger(__name__)


class TestDBStorage(base.StorageEngine):
    """Put the data into an in-memory database for testing

    This driver is based on MIM, an in-memory version of MongoDB.

    Collections::

        - user
          - { _id: user id
              source: [ array of source ids reporting for the user ]
              }
        - project
          - { _id: project id
              source: [ array of source ids reporting for the project ]
              }
        - meter
          - the raw incoming data
        - resource
          - the metadata for resources
          - { _id: uuid of resource,
              metadata: metadata dictionaries
              timestamp: datetime of last update
              user_id: uuid
              project_id: uuid
              meter: [ array of {counter_name: string, counter_type: string,
                                 counter_unit: string} ]
            }
    """

    OPTIONS = []

    def register_opts(self, conf):
        """Register any configuration options used by this engine.
        """
        conf.register_opts(self.OPTIONS)

    def get_connection(self, conf):
        """Return a Connection instance based on the configuration settings.
        """
        return TestConnection(conf)


class TestConnection(impl_mongodb.Connection):

    _mim_instance = None
    FORCE_MONGO = bool(int(os.environ.get('CEILOMETER_TEST_LIVE', 0)))

    def clear(self):
        if TestConnection._mim_instance is not None:
            # Don't want to use drop_database() because
            # may end up running out of spidermonkey instances.
            # http://davisp.lighthouseapp.com/projects/26898/tickets/22
            self.db.clear()
        else:
            super(TestConnection, self).clear()

    def _get_connection(self, conf):
        # Use a real MongoDB server if we can connect, but fall back
        # to a Mongo-in-memory connection if we cannot.
        if self.FORCE_MONGO:
            try:
                return super(TestConnection, self)._get_connection(conf)
            except:
                LOG.debug('Unable to connect to mongodb')
                raise
        else:
            LOG.debug('Using MIM for test connection')

            # MIM will die if we have too many connections, so use a
            # Singleton
            if TestConnection._mim_instance is None:
                if mim:
                    LOG.debug('Creating a new MIM Connection object')
                    TestConnection._mim_instance = mim.Connection()
                else:
                    raise nose.SkipTest("Ming not found")
            return TestConnection._mim_instance


def require_map_reduce(conn):
    """Raises SkipTest if the connection is using mim.
    """
    # NOTE(dhellmann): mim requires spidermonkey to implement the
    # map-reduce functions, so if we can't import it then just
    # skip these tests unless we aren't using mim.
    try:
        import spidermonkey
    except BaseException:
        if isinstance(conn.conn, mim.Connection):
            raise skip.SkipTest('requires spidermonkey')