Unit tests and lint fixes

This commit is contained in:
Liam Young 2018-07-27 11:23:01 +00:00
parent 70b7b69a4c
commit b760285a04
6 changed files with 197 additions and 8 deletions

8
.testr.conf Normal file
View File

@ -0,0 +1,8 @@
[DEFAULT]
test_command=OS_STDOUT_CAPTURE=${OS_STDOUT_CAPTURE:-1} \
OS_STDERR_CAPTURE=${OS_STDERR_CAPTURE:-1} \
OS_TEST_TIMEOUT=${OS_TEST_TIMEOUT:-60} \
${PYTHON:-python} -m subunit.run discover -t ./ ./unit_tests $LISTOPT $IDOPTION
test_id_option=--load-list $IDFILE
test_list_option=--list

View File

@ -18,13 +18,9 @@
from __future__ import absolute_import
import collections
import subprocess
import charmhelpers.core.hookenv as hookenv
import charms_openstack.charm
import charms_openstack.adapters
import charms_openstack.ip as os_ip
PACKAGES = ['nova-conductor']
NOVA_DIR = '/etc/nova/'
@ -71,7 +67,7 @@ class NovaCellControllerCharm(charms_openstack.charm.HAOpenStackCharm):
]),
}
sync_cmd = ['nova-manage', 'db', 'sync' ,'--local_cell']
sync_cmd = ['nova-manage', 'db', 'sync', '--local_cell']
def get_amqp_credentials(self):
"""Provide the default amqp username and vhost as a tuple.
@ -95,7 +91,6 @@ class NovaCellControllerCharm(charms_openstack.charm.HAOpenStackCharm):
"""
return [{'username': 'nova', 'database': 'nova'}]
def states_to_check(self, required_relations=None):
"""Override the default states_to_check() for the assess_status
functionality so that, if we have to have an HSM relation, then enforce

View File

@ -25,9 +25,7 @@ from charms.reactive.relations import (
)
from charms.reactive.flags import (
is_flag_set,
set_flag,
clear_flag,
)
# This charm's library contains all of the handler code associated with

7
test-requirements.txt Normal file
View File

@ -0,0 +1,7 @@
# Lint and unit test requirements
flake8
os-testr>=0.4.1
charms.reactive
mock>=1.2
coverage>=3.6
git+https://github.com/openstack/charms.openstack.git#egg=charms-openstack

22
unit_tests/__init__.py Normal file
View File

@ -0,0 +1,22 @@
# Copyright 2016 Canonical Ltd
#
# 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.
import sys
sys.path.append('src')
sys.path.append('src/lib')
# Mock out charmhelpers so that we can test without it.
import charms_openstack.test_mocks # noqa
charms_openstack.test_mocks.mock_charmhelpers()

View File

@ -0,0 +1,159 @@
# Copyright 2016 Canonical Ltd
#
# 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.
from __future__ import absolute_import
from __future__ import print_function
import mock
import reactive.nova_cell_controller_handlers as handlers
import charms_openstack.test_utils as test_utils
class TestRegisteredHooks(test_utils.TestRegisteredHooks):
def test_hooks(self):
defaults = [
'charm.installed',
'amqp.connected',
'shared-db.connected',
'config.changed',
'update-status']
hook_set = {
'when': {
'render_stuff': (
'shared-db.available',
'amqp.available',),
'db_setup': ('config.rendered',),
'send_compute_data': (
'endpoint.nova-cell-compute.changed',
'endpoint.cloud-compute.joined',),
'send_cell_data': (
'shared-db.available',
'endpoint.nova-cell-compute.joined',
'amqp.available',)},
'when_not': {
'db_setup': ('shared-db.synced',)}
}
# test that the hooks were registered via the
# reactive.nova_cell_controller_handlers
self.registered_hooks_test_helper(handlers, hook_set, defaults)
class TestRenderStuff(test_utils.PatchHelper):
def bob(self):
self.ncc_charm = mock.MagicMock()
self.patch_object(handlers.charm, 'provide_charm_instance',
new=mock.MagicMock())
self.provide_charm_instance().__enter__.return_value = self.ncc_charm
self.provide_charm_instance().__exit__.return_value = None
self.patch_object(handlers, 'set_flag')
self.patch_object(handlers.hookenv, 'config')
self.mock_nc_ep = mock.MagicMock()
self.mock_ncc_ep = mock.MagicMock()
self.mock_amqp_ep = mock.MagicMock()
self.mock_db_ep = mock.MagicMock()
def _endpoint_from_flag(ep):
mocks = {
'endpoint.cloud-compute.joined': self.mock_nc_ep,
'endpoint.nova-cell-compute.joined': self.mock_ncc_ep,
'endpoint.nova-cell-compute.changed': self.mock_ncc_ep,
'amqp.available': self.mock_amqp_ep,
'shared-db.available': self.mock_db_ep}
return mocks[ep]
self.patch_object(handlers, 'endpoint_from_flag',
side_effect=_endpoint_from_flag)
def test_render_stuff(self):
self.bob()
handlers.render_stuff('arg1', 'arg2')
self.ncc_charm.render_with_interfaces.assert_called_once_with(
('arg1', 'arg2'))
self.ncc_charm.assess_status.assert_called_once_with()
self.set_flag.assert_called_once_with('config.rendered')
def test_db_setup(self):
self.bob()
handlers.db_setup('arg1')
self.ncc_charm.db_sync.assert_called_once_with()
self.ncc_charm.restart_all.assert_called_once_with()
self.set_flag.assert_called_once_with('shared-db.synced')
def test_send_compute_data(self):
self.bob()
self.mock_ncc_ep.get_region.return_value = {
'region': 'Region52'}
self.mock_ncc_ep.get_volume_data.return_value = {
'volume_service': 'cinder'}
self.mock_ncc_ep.get_ec2_data.return_value = {
'ec2_host': 'http://ec2host'}
self.mock_ncc_ep.get_restart_trigger.return_value = {
'restart_trigger': 'a-uuid'}
self.mock_ncc_ep.get_network_data.return_value = {
'quantum_url': 'http://neutron:1234/api',
'quantum_plugin': 'ovs',
'network_manager': 'neutron',
'quantum_security_groups': True,
}
self.mock_ncc_ep.get_console_data.return_value = {
'serial_console_base_url': 'http://nova/serial',
'enable_serial_console': True,
}
handlers.send_compute_data()
self.mock_nc_ep.set_network_data.assert_called_once_with(
'http://neutron:1234/api',
neutron_plugin='ovs',
network_manager='neutron',
enable_security_groups=True)
self.mock_nc_ep.set_console_data.assert_called_once_with(
serial_console_base_url='http://nova/serial',
enable_serial_console=True,
)
self.mock_nc_ep.set_region.assert_called_once_with(
'Region52')
self.mock_nc_ep.set_volume_data.assert_called_once_with(
'cinder')
self.mock_nc_ep.set_ec2_data.assert_called_once_with(
'http://ec2host')
self.mock_nc_ep.trigger_remote_restart.assert_called_once_with(
restart_key='a-uuid')
def test_send_cell_data(self):
self.bob()
mock_amqp_conv = mock.MagicMock()
mock_amqp_conv.units = [
'rabbitmq-server-cell2/0',
'rabbitmq-server-cell2/1']
self.mock_amqp_ep.conversation.return_value = mock_amqp_conv
mock_db_conv = mock.MagicMock()
mock_db_conv.units = [
'percona-cluster-cell2/0',
'percona-clustercell2/1']
self.mock_db_ep.conversation.return_value = mock_db_conv
self.config.return_value = 'cell2'
handlers.send_cell_data()
self.mock_ncc_ep.send_cell_data.assert_called_once_with(
'cell2',
'rabbitmq-server-cell2',
'percona-cluster-cell2')