Enabling API unit tests

API unit tests are enabled by copying the
ceilometer config files in venv and using
test instance of Stevedore which allows
control over building extensions using custom
entry points.

Change-Id: I601c301d7e0353392498b374a8d6e4741487dac7
This commit is contained in:
Rohit Jaiswal 2015-08-06 20:51:54 +00:00
parent f38c9f6c4a
commit 652d414be2
6 changed files with 68 additions and 7 deletions

View File

@ -69,13 +69,9 @@ monasca_test_setup.py - determines the ceilometer venv path and copies the relev
tox.ini - calls the commands for setup and runs the tests
test-requirements.txt - contains the dependencies required for testing
### Todo
Tests under API still need some work, they don't work since it required modification of entry_points.txt in ceilometer venv
# License
Copyright (c) 2014 Hewlett-Packard Development Company, L.P.
Copyright (c) 2015 Hewlett-Packard Development Company, L.P.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.

View File

@ -14,11 +14,15 @@
# under the License.
"""Test api with Monasca driver
"""
import mock
import pkg_resources
from oslo_config import cfg
from oslo_config import fixture as fixture_config
from oslotest import mockpatch
from stevedore import driver
from stevedore import extension
from ceilometer import storage
from ceilometer.tests import base as test_base
@ -37,6 +41,30 @@ class TestApi(test_base.BaseTestCase):
# cases for get_samples in
# ceilometer/tests/api/v2/test_api_with_monasca_driver.py?)
def _get_driver_from_entry_point(self, entry_point, namespace):
ep = pkg_resources.EntryPoint.parse(entry_point)
a_driver = extension.Extension('con_driver', ep,
ep.load(require=False), None)
mgr = driver.DriverManager.make_test_instance(
a_driver, namespace=namespace
)
mgr._init_plugins([a_driver])
return mgr
def get_connection_with_mock_driver_manager(self, url, namespace):
mgr = self._get_driver_from_entry_point(
entry_point='monasca = ceilometer.storage.impl_monasca:Connection',
namespace='ceilometer.metering.storage')
return mgr.driver(url)
def get_publisher_with_mock_driver_manager(self, url, namespace):
mgr = self._get_driver_from_entry_point(
entry_point='monasca = ceilometer.publisher.monclient:'
'MonascaPublisher',
namespace='ceilometer.publisher')
return mgr.driver(url)
def setUp(self):
super(TestApi, self).setUp()
self.PATH_PREFIX = '/v2'
@ -56,19 +84,27 @@ class TestApi(test_base.BaseTestCase):
self.CONF.import_opt('pipeline_cfg_file', 'ceilometer.pipeline')
self.CONF.set_override(
'pipeline_cfg_file',
self.path_get('etc/ceilometer/monasca_pipeline.yaml')
self.path_get('etc/ceilometer/pipeline.yaml')
)
self.CONF.import_opt('monasca_mappings',
'ceilometer.publisher.monasca_data_filter',
group='monasca')
self.CONF.set_override(
'monasca_mappings',
self.path_get('etc/ceilometer/monasca_field_definitions.yaml'),
group='monasca'
)
with mock.patch("ceilometer.monasca_client.Client") as mock_client:
with mock.patch("ceilometer.monasca_client.Client") as mock_client,\
mock.patch('ceilometer.storage.get_connection') as \
get_storage_conn, \
mock.patch('ceilometer.publisher.get_publisher') as get_pub:
get_storage_conn.side_effect = (
self.get_connection_with_mock_driver_manager)
get_pub.side_effect = self.get_publisher_with_mock_driver_manager
self.mock_mon_client = mock_client
self.conn = storage.get_connection('monasca://127.0.0.1:8080',
'ceilometer.metering.storage')

View File

@ -0,0 +1,7 @@
{
"context_is_admin": "role:admin",
"context_is_project": "project_id:%(target.project_id)s",
"context_is_owner": "user_id:%(target.user_id)s",
"segregation": "rule:context_is_admin",
"default": ""
}

View File

@ -20,3 +20,25 @@ ceilosca_files = {
# python cannot load our files for unit testing
for src, dest in ceilosca_files.items():
shutil.copyfile(src, dest)
ceilo_parent_dir = os.path.dirname(os.path.abspath(
os.path.dirname(ceilometer.__file__)))
ceilosca_conf_files = {
file: '%s/%s' % (ceilo_parent_dir, file)
for file in
[
'etc/ceilometer/monasca_field_definitions.yaml',
'etc/ceilometer/pipeline.yaml',
'etc/ceilometer/ceilometer.conf',
'etc/ceilometer/policy.json'
]
}
dest_conf_dir = ceilo_parent_dir + '/etc/ceilometer'
if not os.path.exists(dest_conf_dir):
os.makedirs(dest_conf_dir)
for src, dest in ceilosca_conf_files.items():
shutil.copyfile(src, dest)