Add tests for the DB state manager

This patch also defines a testcase base class which handles the database
connection setup.

Fix the format() calls for python 2.6.

Add eventlet requirement.

Change-Id: Id110aa817a680cc02c882ea24f275eb127a738a8
This commit is contained in:
Gauvain Pocentek 2014-09-26 15:19:20 +02:00
parent c3eecc835d
commit 4a67f81068
6 changed files with 87 additions and 10 deletions

View File

@ -38,8 +38,8 @@ class StateManager(object):
def _gen_filename(self):
# FIXME(sheeprine): Basepath can't be enforced at the moment
filename = '{}_{}.state'.format(self._type,
self._uid)
filename = '{0}_{1}.state'.format(self._type,
self._uid)
return filename
def _open(self, mode='rb'):
@ -100,7 +100,7 @@ class DBStateManager(object):
self._db = api.get_instance().get_state()
def _gen_name(self, state_type, uid):
name = '{}_{}'.format(state_type, uid)
name = '{0}_{1}'.format(state_type, uid)
return name
def get_state(self):

View File

@ -0,0 +1,36 @@
# -*- coding: utf-8 -*-
# Copyright 2014 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: Gauvain Pocentek
#
from oslo.config import fixture as config_fixture
import testscenarios
import testtools
from cloudkitty.db import api as db_api
class TestCase(testscenarios.TestWithScenarios, testtools.TestCase):
scenarios = [
('sqlite', dict(db_url='sqlite:///'))
]
def setUp(self):
super(TestCase, self).setUp()
self.conf = self.useFixture(config_fixture.Config()).conf
self.conf.set_override('connection', self.db_url, 'database')
self.conn = db_api.get_instance()
migration = self.conn.get_migration()
migration.upgrade('head')

View File

@ -1,6 +0,0 @@
import testtools
class FakeTest(testtools.TestCase):
def test_foo(self):
pass

View File

@ -0,0 +1,45 @@
# -*- coding: utf-8 -*-
# Copyright 2014 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: Gauvain Pocentek
#
import datetime
from cloudkitty import state
from cloudkitty import tests
class DBStateManagerTest(tests.TestCase):
def setUp(self):
super(DBStateManagerTest, self).setUp()
self.sm = state.DBStateManager('testuser', 'osrtf')
def test_gen_name(self):
name = self.sm._gen_name('testuser', 'osrtf')
self.assertEqual(name, 'testuser_osrtf')
def test_state_access(self):
now = datetime.datetime.now()
self.sm.set_state(now)
result = self.sm.get_state()
self.assertEqual(result, str(now))
def test_metadata_access(self):
metadata = {'foo': 'bar'}
now = datetime.datetime.now()
self.sm.set_state(now)
self.sm.set_metadata(metadata)
result = self.sm.get_metadata()
self.assertEqual(result, metadata)

View File

@ -1,3 +1,5 @@
pbr>=0.6,!=0.7,<1.0
eventlet>=0.13.0
python-ceilometerclient
python-keystoneclient
iso8601
@ -10,6 +12,5 @@ oslo.messaging
oslo.db
oslo.i18n
sqlalchemy
pbr>=0.6,!=0.7,<1.0
six>=1.7.0
stevedore

View File

@ -1,6 +1,7 @@
hacking>=0.9.2,<0.10
discover
testtools
testscenarios
testrepository
mock>=1.0
sphinx>=1.1.2,!=1.2.0,<1.3