Added unit tests for storage drivers

Change-Id: I2a31fc483e0d7b96eb73d322803724ac3ecd12a7
This commit is contained in:
Stéphane Albert 2015-11-19 17:53:44 +01:00 committed by Gauvain Pocentek
parent f7d272fcb5
commit e1d574cb73
3 changed files with 504 additions and 0 deletions

128
cloudkitty/tests/samples.py Normal file
View File

@ -0,0 +1,128 @@
# -*- coding: utf-8 -*-
# Copyright 2015 Objectif Libre
#
# 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.
#
# @author: Stéphane Albert
#
import copy
import decimal
from cloudkitty import utils as ck_utils
TENANT = 'f266f30b11f246b589fd266f85eeec39'
INITIAL_TIMESTAMP = 1420070400
FIRST_PERIOD_BEGIN = INITIAL_TIMESTAMP
FIRST_PERIOD_BEGIN_ISO = ck_utils.ts2iso(FIRST_PERIOD_BEGIN)
FIRST_PERIOD_END = FIRST_PERIOD_BEGIN + 3600
FIRST_PERIOD_END_ISO = ck_utils.ts2iso(FIRST_PERIOD_END)
SECOND_PERIOD_BEGIN = FIRST_PERIOD_END
SECOND_PERIOD_BEGIN_ISO = ck_utils.ts2iso(SECOND_PERIOD_BEGIN)
SECOND_PERIOD_END = SECOND_PERIOD_BEGIN + 3600
SECOND_PERIOD_END_ISO = ck_utils.ts2iso(SECOND_PERIOD_END)
COMPUTE_METADATA = {
'availability_zone': 'nova',
'flavor': 'm1.nano',
'image_id': 'f5600101-8fa2-4864-899e-ebcb7ed6b568',
'instance_id': '26c084e1-b8f1-4cbc-a7ec-e8b356788a17',
'memory': '64',
'metadata': {
'farm': 'prod'
},
'name': 'prod1',
'project_id': 'f266f30b11f246b589fd266f85eeec39',
'user_id': '55b3379b949243009ee96972fbf51ed1',
'vcpus': '1'}
IMAGE_METADATA = {
'checksum': '836c69cbcd1dc4f225daedbab6edc7c7',
'container_format': 'aki',
'created_at': '2014-06-04T16:26:01',
'deleted': 'False',
'deleted_at': 'None',
'disk_format': 'aki',
'is_public': 'True',
'min_disk': '0',
'min_ram': '0',
'name': 'cirros-0.3.2-x86_64-uec-kernel',
'protected': 'False',
'size': '4969360',
'status': 'active',
'updated_at': '2014-06-04T16:26:02'}
FIRST_PERIOD = {
'begin': FIRST_PERIOD_BEGIN,
'end': FIRST_PERIOD_END}
SECOND_PERIOD = {
'begin': SECOND_PERIOD_BEGIN,
'end': SECOND_PERIOD_END}
COLLECTED_DATA = [{
'period': FIRST_PERIOD,
'usage': {
'compute': [{
'desc': COMPUTE_METADATA,
'vol': {
'qty': decimal.Decimal(1.0),
'unit': 'instance'}}],
'image': [{
'desc': IMAGE_METADATA,
'vol': {
'qty': decimal.Decimal(1.0),
'unit': 'image'}}]
}}, {
'period': SECOND_PERIOD,
'usage': {
'compute': [{
'desc': COMPUTE_METADATA,
'vol': {
'qty': decimal.Decimal(1.0),
'unit': 'instance'}}]
}}]
RATED_DATA = copy.deepcopy(COLLECTED_DATA)
RATED_DATA[0]['usage']['compute'][0]['rating'] = {
'price': decimal.Decimal('0.42')}
RATED_DATA[0]['usage']['image'][0]['rating'] = {
'price': decimal.Decimal('0.1337')}
RATED_DATA[1]['usage']['compute'][0]['rating'] = {
'price': decimal.Decimal('0.42')}
def split_storage_data(raw_data):
final_data = []
for frame in raw_data:
frame['period']['begin'] = ck_utils.ts2iso(frame['period']['begin'])
frame['period']['end'] = ck_utils.ts2iso(frame['period']['end'])
usage_buffer = frame.pop('usage')
for service, data in usage_buffer.items():
new_frame = copy.deepcopy(frame)
new_frame['usage'] = {service: data}
new_frame['usage'][service][0]['tenant_id'] = TENANT
final_data.append(new_frame)
return final_data
# FIXME(sheeprine): storage is not using decimal for rates, we need to
# transition to decimal.
STORED_DATA = copy.deepcopy(COLLECTED_DATA)
STORED_DATA[0]['usage']['compute'][0]['rating'] = {
'price': 0.42}
STORED_DATA[0]['usage']['image'][0]['rating'] = {
'price': 0.1337}
STORED_DATA[1]['usage']['compute'][0]['rating'] = {
'price': 0.42}
STORED_DATA = split_storage_data(STORED_DATA)

View File

View File

@ -0,0 +1,376 @@
# -*- coding: utf-8 -*-
# Copyright 2015 Objectif Libre
#
# 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.
#
# @author: Stéphane Albert
#
import copy
import mock
import sqlalchemy
import testscenarios
from cloudkitty import storage
from cloudkitty import tests
from cloudkitty.tests import samples
from cloudkitty import utils as ck_utils
class StorageTest(tests.TestCase):
storage_scenarios = [
('sqlalchemy', dict(storage_backend='sqlalchemy'))]
@classmethod
def generate_scenarios(cls):
cls.scenarios = testscenarios.multiply_scenarios(
cls.scenarios,
cls.storage_scenarios)
def setUp(self):
super(StorageTest, self).setUp()
self._tenant_id = samples.TENANT
self._other_tenant_id = '8d3ae50089ea4142-9c6e1269db6a0b64'
self.conf.set_override('backend', self.storage_backend, 'storage')
self.storage = storage.get_storage()
self.storage.init()
def insert_data(self):
working_data = copy.deepcopy(samples.RATED_DATA)
self.storage.append(working_data, self._tenant_id)
working_data = copy.deepcopy(samples.RATED_DATA)
self.storage.append(working_data, self._other_tenant_id)
self.storage.commit(self._tenant_id)
self.storage.commit(self._other_tenant_id)
def insert_different_data_two_tenants(self):
working_data = copy.deepcopy(samples.RATED_DATA)
del working_data[1]
self.storage.append(working_data, self._tenant_id)
working_data = copy.deepcopy(samples.RATED_DATA)
del working_data[0]
self.storage.append(working_data, self._other_tenant_id)
self.storage.commit(self._tenant_id)
self.storage.commit(self._other_tenant_id)
# Filtering
def test_filter_period(self):
working_data = copy.deepcopy(samples.RATED_DATA)
usage_start, data = self.storage._filter_period(working_data)
self.assertEqual(samples.FIRST_PERIOD_BEGIN, usage_start)
self.assertEqual(samples.RATED_DATA[0]['usage'], data)
expected_remaining_data = [{
"period": samples.SECOND_PERIOD,
"usage": samples.RATED_DATA[1]['usage']}]
self.assertEqual(expected_remaining_data, working_data)
usage_start, data = self.storage._filter_period(working_data)
self.assertEqual(samples.SECOND_PERIOD_BEGIN, usage_start)
self.assertEqual(samples.RATED_DATA[1]['usage'], data)
self.assertEqual([], working_data)
# Data integrity
def test_has_data_flag_behaviour(self):
self.assertNotIn(self._tenant_id, self.storage._has_data)
self.storage.nodata(
samples.FIRST_PERIOD_BEGIN,
samples.FIRST_PERIOD_END,
self._tenant_id)
self.assertNotIn(self._tenant_id, self.storage._has_data)
working_data = copy.deepcopy(samples.RATED_DATA)
working_data = [working_data[1]]
self.storage.append(working_data, self._tenant_id)
self.assertTrue(self.storage._has_data[self._tenant_id])
self.storage.commit(self._tenant_id)
self.assertNotIn(self._tenant_id, self.storage._has_data)
def test_notify_no_data(self):
self.storage.nodata(
samples.FIRST_PERIOD_BEGIN,
samples.FIRST_PERIOD_END,
self._tenant_id)
working_data = copy.deepcopy(samples.RATED_DATA)
working_data = [working_data[1]]
self.storage.append(working_data, self._tenant_id)
kwargs = {
'begin': samples.FIRST_PERIOD_BEGIN,
'end': samples.FIRST_PERIOD_END,
'tenant_id': self._tenant_id}
self.assertRaises(
storage.NoTimeFrame,
self.storage.get_time_frame,
**kwargs)
kwargs['res_type'] = '_NO_DATA_'
stored_data = self.storage.get_time_frame(**kwargs)
self.assertEqual(1, len(stored_data))
self.assertEqual(1, len(stored_data[0]['usage']))
self.assertIn('_NO_DATA_', stored_data[0]['usage'])
def test_send_nodata_between_data(self):
working_data = copy.deepcopy(samples.RATED_DATA)
for period in working_data:
for service, data in sorted(period['usage'].items()):
sub_data = [{
'period': period['period'],
'usage': {
service: data}}]
self.storage.append(sub_data, self._tenant_id)
if service == 'compute':
self.storage.nodata(
period['period']['begin'],
period['period']['end'],
self._tenant_id)
self.storage.commit(self._tenant_id)
self.assertRaises(
storage.NoTimeFrame,
self.storage.get_time_frame,
begin=samples.FIRST_PERIOD_BEGIN,
end=samples.SECOND_PERIOD_END,
res_type='_NO_DATA_')
def test_auto_commit_on_period_change(self):
working_data = copy.deepcopy(samples.RATED_DATA)
self.storage.append(working_data, self._tenant_id)
stored_data = self.storage.get_time_frame(
begin=samples.FIRST_PERIOD_BEGIN,
end=samples.SECOND_PERIOD_END)
self.assertEqual(2, len(stored_data))
expected_data = copy.deepcopy(samples.STORED_DATA)
# We only stored the first timeframe, the second one is waiting for a
# commit or an append with the next timeframe.
del expected_data[2]
self.assertEqual(
expected_data,
stored_data)
def test_create_session_on_append(self):
self.assertNotIn(self._tenant_id, self.storage._session)
working_data = copy.deepcopy(samples.RATED_DATA)
self.storage.append(working_data, self._tenant_id)
self.assertIn(self._tenant_id, self.storage._session)
self.assertIsInstance(
self.storage._session[self._tenant_id],
sqlalchemy.orm.session.Session)
def test_delete_session_on_commit(self):
working_data = copy.deepcopy(samples.RATED_DATA)
self.storage.append(working_data, self._tenant_id)
self.storage.commit(self._tenant_id)
self.assertNotIn(self._tenant_id, self.storage._session)
def test_update_period_on_append(self):
self.assertNotIn(self._tenant_id, self.storage.usage_start)
self.assertNotIn(self._tenant_id, self.storage.usage_start_dt)
self.assertNotIn(self._tenant_id, self.storage.usage_end)
self.assertNotIn(self._tenant_id, self.storage.usage_end_dt)
working_data = copy.deepcopy(samples.RATED_DATA)
self.storage.append([working_data[0]], self._tenant_id)
self.assertEqual(
self.storage.usage_start[self._tenant_id],
samples.FIRST_PERIOD_BEGIN)
self.assertEqual(
self.storage.usage_start_dt[self._tenant_id],
ck_utils.ts2dt(samples.FIRST_PERIOD_BEGIN))
self.assertEqual(
self.storage.usage_end[self._tenant_id],
samples.FIRST_PERIOD_END)
self.assertEqual(
self.storage.usage_end_dt[self._tenant_id],
ck_utils.ts2dt(samples.FIRST_PERIOD_END))
self.storage.append([working_data[1]], self._tenant_id)
self.assertEqual(
self.storage.usage_start[self._tenant_id],
samples.SECOND_PERIOD_BEGIN)
self.assertEqual(
self.storage.usage_start_dt[self._tenant_id],
ck_utils.ts2dt(samples.SECOND_PERIOD_BEGIN))
self.assertEqual(
self.storage.usage_end[self._tenant_id],
samples.SECOND_PERIOD_END)
self.assertEqual(
self.storage.usage_end_dt[self._tenant_id],
ck_utils.ts2dt(samples.SECOND_PERIOD_END))
def test_clear_period_info_on_commit(self):
working_data = copy.deepcopy(samples.RATED_DATA)
self.storage.append(working_data, self._tenant_id)
self.storage.commit(self._tenant_id)
self.assertNotIn(self._tenant_id, self.storage.usage_start)
self.assertNotIn(self._tenant_id, self.storage.usage_start_dt)
self.assertNotIn(self._tenant_id, self.storage.usage_end)
self.assertNotIn(self._tenant_id, self.storage.usage_end_dt)
# Queries
# Data
def test_get_no_frame_when_nothing_in_storage(self):
self.assertRaises(
storage.NoTimeFrame,
self.storage.get_time_frame,
begin=samples.FIRST_PERIOD_BEGIN - 3600,
end=samples.FIRST_PERIOD_BEGIN)
def test_get_frame_filter_outside_data(self):
self.insert_different_data_two_tenants()
self.assertRaises(
storage.NoTimeFrame,
self.storage.get_time_frame,
begin=samples.FIRST_PERIOD_BEGIN - 3600,
end=samples.FIRST_PERIOD_BEGIN)
def test_get_frame_without_filter_but_timestamp(self):
self.insert_different_data_two_tenants()
data = self.storage.get_time_frame(
begin=samples.FIRST_PERIOD_BEGIN,
end=samples.SECOND_PERIOD_END)
self.assertEqual(3, len(data))
def test_get_frame_on_one_period(self):
self.insert_different_data_two_tenants()
data = self.storage.get_time_frame(
begin=samples.FIRST_PERIOD_BEGIN,
end=samples.FIRST_PERIOD_END)
self.assertEqual(2, len(data))
def test_get_frame_on_one_period_and_one_tenant(self):
self.insert_different_data_two_tenants()
data = self.storage.get_time_frame(
begin=samples.FIRST_PERIOD_BEGIN,
end=samples.FIRST_PERIOD_END,
tenant_id=self._tenant_id)
self.assertEqual(2, len(data))
def test_get_frame_on_one_period_and_one_tenant_outside_data(self):
self.insert_different_data_two_tenants()
self.assertRaises(
storage.NoTimeFrame,
self.storage.get_time_frame,
begin=samples.FIRST_PERIOD_BEGIN,
end=samples.FIRST_PERIOD_END,
tenant_id=self._other_tenant_id)
def test_get_frame_on_two_periods(self):
self.insert_different_data_two_tenants()
data = self.storage.get_time_frame(
begin=samples.FIRST_PERIOD_BEGIN,
end=samples.SECOND_PERIOD_END)
self.assertEqual(3, len(data))
# State
def test_get_state_when_nothing_in_storage(self):
state = self.storage.get_state()
self.assertIsNone(state)
def test_get_latest_global_state(self):
self.insert_different_data_two_tenants()
state = self.storage.get_state()
self.assertEqual(samples.SECOND_PERIOD_BEGIN, state)
def test_get_state_on_rated_tenant(self):
self.insert_different_data_two_tenants()
state = self.storage.get_state(self._tenant_id)
self.assertEqual(samples.FIRST_PERIOD_BEGIN, state)
state = self.storage.get_state(self._other_tenant_id)
self.assertEqual(samples.SECOND_PERIOD_BEGIN, state)
def test_get_state_on_no_data_frame(self):
self.storage.nodata(
samples.FIRST_PERIOD_BEGIN,
samples.FIRST_PERIOD_END,
self._tenant_id)
self.storage.commit(self._tenant_id)
state = self.storage.get_state(self._tenant_id)
self.assertEqual(samples.FIRST_PERIOD_BEGIN, state)
# Total
def test_get_empty_total(self):
self.insert_data()
total = self.storage.get_total(
begin=ck_utils.ts2dt(samples.FIRST_PERIOD_BEGIN - 3600),
end=ck_utils.ts2dt(samples.FIRST_PERIOD_BEGIN))
self.assertIsNone(total)
def test_get_total_without_filter_but_timestamp(self):
self.insert_data()
total = self.storage.get_total(
begin=ck_utils.ts2dt(samples.FIRST_PERIOD_BEGIN),
end=ck_utils.ts2dt(samples.SECOND_PERIOD_END))
# FIXME(sheeprine): floating point error (transition to decimal)
self.assertEqual(1.9473999999999998, total)
def test_get_total_filtering_on_one_period(self):
self.insert_data()
total = self.storage.get_total(
begin=ck_utils.ts2dt(samples.FIRST_PERIOD_BEGIN),
end=ck_utils.ts2dt(samples.FIRST_PERIOD_END))
self.assertEqual(1.1074, total)
def test_get_total_filtering_on_one_period_and_one_tenant(self):
self.insert_data()
total = self.storage.get_total(
begin=ck_utils.ts2dt(samples.FIRST_PERIOD_BEGIN),
end=ck_utils.ts2dt(samples.FIRST_PERIOD_END),
tenant_id=self._tenant_id)
self.assertEqual(0.5537, total)
def test_get_total_filtering_on_service(self):
self.insert_data()
total = self.storage.get_total(
begin=ck_utils.ts2dt(samples.FIRST_PERIOD_BEGIN),
end=ck_utils.ts2dt(samples.FIRST_PERIOD_END),
service='compute')
self.assertEqual(0.84, total)
@mock.patch.object(ck_utils, 'utcnow',
return_value=ck_utils.ts2dt(samples.INITIAL_TIMESTAMP))
def test_get_total_no_filter(self, patch_utcnow_mock):
self.insert_data()
total = self.storage.get_total()
self.assertEqual(1.9473999999999998, total)
self.assertEqual(2, patch_utcnow_mock.call_count)
# Tenants
def test_get_empty_tenant_with_nothing_in_storage(self):
tenants = self.storage.get_tenants(
begin=ck_utils.ts2dt(samples.FIRST_PERIOD_BEGIN),
end=ck_utils.ts2dt(samples.SECOND_PERIOD_BEGIN))
self.assertEqual([], tenants)
def test_get_empty_tenant_list(self):
self.insert_data()
tenants = self.storage.get_tenants(
begin=ck_utils.ts2dt(samples.FIRST_PERIOD_BEGIN - 3600),
end=ck_utils.ts2dt(samples.FIRST_PERIOD_BEGIN))
self.assertEqual([], tenants)
def test_get_tenants_filtering_on_period(self):
self.insert_different_data_two_tenants()
tenants = self.storage.get_tenants(
begin=ck_utils.ts2dt(samples.FIRST_PERIOD_BEGIN),
end=ck_utils.ts2dt(samples.SECOND_PERIOD_END))
self.assertListEqual(
[self._tenant_id, self._other_tenant_id],
tenants)
tenants = self.storage.get_tenants(
begin=ck_utils.ts2dt(samples.FIRST_PERIOD_BEGIN),
end=ck_utils.ts2dt(samples.FIRST_PERIOD_END))
self.assertListEqual(
[self._tenant_id],
tenants)
tenants = self.storage.get_tenants(
begin=ck_utils.ts2dt(samples.SECOND_PERIOD_BEGIN),
end=ck_utils.ts2dt(samples.SECOND_PERIOD_END))
self.assertListEqual(
[self._other_tenant_id],
tenants)
StorageTest.generate_scenarios()