-
Notifications
You must be signed in to change notification settings - Fork 325
/
test_impl_mongodb.py
232 lines (174 loc) · 7.14 KB
/
test_impl_mongodb.py
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# -*- 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.
"""Tests for ceilometer/storage/impl_mongodb.py
.. note::
(dhellmann) These tests have some dependencies which cannot be
installed in the CI environment right now.
Ming is necessary to provide the Mongo-in-memory implementation for
of MongoDB. The original source for Ming is at
http://sourceforge.net/project/merciless but there does not seem to
be a way to point to a "zipball" of the latest HEAD there, and we
need features present only in that version. I forked the project to
github to make it easier to install, and put the URL into the
test-requires file. Then I ended up making some changes to it so it
would be compatible with PyMongo's API.
https://github.com/dreamhost/Ming/zipball/master#egg=Ming
In order to run the tests that use map-reduce with MIM, some
additional system-level packages are required::
apt-get install nspr-config
apt-get install libnspr4-dev
apt-get install pkg-config
pip install python-spidermonkey
To run the tests *without* mim, set the environment variable
CEILOMETER_TEST_LIVE=1 before running tox.
"""
import copy
import datetime
import mox
from tests.storage import base
from ceilometer.collector import meter
from ceilometer import counter
from ceilometer.storage.impl_test import TestConnection
from ceilometer.tests.db import require_map_reduce
class MongoDBEngine(base.DBEngineBase):
DBNAME = 'testdb'
def tearDown(self):
self.conn.drop_database(self.DBNAME)
super(MongoDBEngine, self).tearDown()
def get_connection(self):
conf = mox.Mox().CreateMockAnything()
conf.database_connection = 'mongodb://localhost/%s' % self.DBNAME
self.conn = TestConnection(conf)
self.db = self.conn.conn[self.DBNAME]
return self.conn
def clean_up(self):
self.conn.clear()
def get_sources_by_project_id(self, id):
project = self.db.project.find_one({'_id': id})
return list(project['source'])
def get_sources_by_user_id(self, id):
user = self.db.user.find_one({'_id': id})
return list(user['source'])
class MongoDBEngineTestBase(base.DBTestBase):
def get_engine(cls):
return MongoDBEngine()
class IndexTest(MongoDBEngineTestBase):
def test_indexes_exist(self):
# ensure_index returns none if index already exists
assert self.engine is not None
assert not self.engine.db.resource.ensure_index('foo',
name='resource_idx')
assert not self.engine.db.meter.ensure_index('foo',
name='meter_idx')
class UserTest(base.UserTest, MongoDBEngineTestBase):
pass
class ProjectTest(base.ProjectTest, MongoDBEngineTestBase):
pass
class ResourceTest(base.ResourceTest, MongoDBEngineTestBase):
pass
class MeterTest(base.MeterTest, MongoDBEngineTestBase):
pass
class RawEventTest(base.RawEventTest, MongoDBEngineTestBase):
pass
class SumTest(base.SumTest, MongoDBEngineTestBase):
def setUp(self):
super(SumTest, self).setUp()
require_map_reduce(self.conn)
class TestGetEventInterval(base.TestGetEventInterval, MongoDBEngineTestBase):
def setUp(self):
super(TestGetEventInterval, self).setUp()
require_map_reduce(self.conn)
class MaxProjectTest(base.MaxProjectTest, MongoDBEngineTestBase):
def setUp(self):
super(MaxProjectTest, self).setUp()
require_map_reduce(self.conn)
class MaxResourceTest(base.MaxResourceTest, MongoDBEngineTestBase):
def setUp(self):
super(MaxResourceTest, self).setUp()
require_map_reduce(self.conn)
class StatisticsTest(base.StatisticsTest, MongoDBEngineTestBase):
def setUp(self):
super(StatisticsTest, self).setUp()
require_map_reduce(self.conn)
class CompatibilityTest(MongoDBEngineTestBase):
def prepare_data(self):
def old_record_metering_data(self, data):
self.db.user.update(
{'_id': data['user_id']},
{'$addToSet': {'source': data['source'],
},
},
upsert=True,
)
self.db.project.update(
{'_id': data['project_id']},
{'$addToSet': {'source': data['source'],
},
},
upsert=True,
)
received_timestamp = datetime.datetime.utcnow()
self.db.resource.update(
{'_id': data['resource_id']},
{'$set': {'project_id': data['project_id'],
'user_id': data['user_id'],
# Current metadata being used and when it was
# last updated.
'timestamp': data['timestamp'],
'received_timestamp': received_timestamp,
'metadata': data['resource_metadata'],
'source': data['source'],
},
'$addToSet': {'meter': {'counter_name': data['counter_name'],
'counter_type': data['counter_type'],
},
},
},
upsert=True,
)
record = copy.copy(data)
self.db.meter.insert(record)
return
# Stubout with the old version DB schema, the one w/o 'counter_unit'
self.stubs.Set(self.conn,
'record_metering_data',
old_record_metering_data)
self.counters = []
c = counter.Counter(
'volume.size',
'gauge',
'GiB',
5,
'user-id',
'project1',
'resource-id',
timestamp=datetime.datetime(2012, 9, 25, 10, 30),
resource_metadata={'display_name': 'test-volume',
'tag': 'self.counter',
}
)
self.counters.append(c)
msg = meter.meter_message_from_counter(c,
secret='not-so-secret',
source='test')
self.conn.record_metering_data(self.conn, msg)
def test_counter_unit(self):
meters = list(self.conn.get_meters())
self.assertEqual(len(meters), 1)
class CounterDataTypeTest(base.CounterDataTypeTest, MongoDBEngineTestBase):
pass