Add unit testing for reactive handlers

This commit is contained in:
James Page 2017-01-27 17:39:29 +00:00
parent 541049a35f
commit db2a30579d
5 changed files with 117 additions and 48 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

@ -28,12 +28,17 @@ DOMAIN_CONF = "/etc/keystone/domains/keystone.{}.conf"
KEYSTONE_CONF_TEMPLATE = "keystone.conf"
class KeystoneLDAPConfigurationAdapter(charms_openstack.adapters.ConfigurationAdapter):
'''Charm specific configuration adapter to deal with ldap config flag parsing'''
class KeystoneLDAPConfigurationAdapter(
charms_openstack.adapters.ConfigurationAdapter):
'''Charm specific configuration adapter to deal with ldap
config flag parsing
'''
@property
def ldap_options(self):
return os_utils.config_flags_parser(hookenv.config('ldap-config-flags'))
return os_utils.config_flags_parser(
hookenv.config('ldap-config-flags')
)
class KeystoneLDAPCharm(charms_openstack.charm.OpenStackCharm):
@ -56,7 +61,8 @@ class KeystoneLDAPCharm(charms_openstack.charm.OpenStackCharm):
def domain_name(self):
"""Domain name for the running application
:returns: string containing the current domain name for the application
:returns: string: containing the current domain name for the
application
"""
return hookenv.config('domain-name') or hookenv.service_name()
@ -113,8 +119,8 @@ def render_config(restart_trigger):
def assess_status():
"""Just call the KeystoneLDAPCharm.singleton.assess_status() command to update
status on the unit.
"""Just call the KeystoneLDAPCharm.singleton.assess_status() command
to update status on the unit.
"""
KeystoneLDAPCharm.singleton.assess_status()
@ -126,4 +132,4 @@ def configuration_complete():
def configuration_file():
"""Configuration file for current domain configuration"""
return KeystoneLDAPCharm.singleton.configuration_file
return KeystoneLDAPCharm.singleton.configuration_file

View File

@ -0,0 +1,94 @@
# 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.keystone_ldap_handlers as handlers
import charms_openstack.test_utils as test_utils
class TestRegisteredHooks(test_utils.TestRegisteredHooks):
def test_hooks(self):
defaults = [
'charm.installed',
'update-status']
hook_set = {
'when': {
'configure_domain_name': ('domain-backend.connected',
'config.complete'),
},
'when_not': {
'check_configuration': ('always.run',),
}
}
# test that the hooks were registered via the
# reactive.barbican_handlers
self.registered_hooks_test_helper(handlers, hook_set, defaults)
class TestKeystoneLDAPCharmHandlers(test_utils.PatchHelper):
def patch(self, obj, attr, return_value=None, side_effect=None):
mocked = mock.patch.object(obj, attr)
self._patches[attr] = mocked
started = mocked.start()
started.return_value = return_value
started.side_effect = side_effect
self._patches_start[attr] = started
setattr(self, attr, started)
def test_configure_domain_name_application(self):
self.patch(handlers.keystone_ldap, 'render_config')
self.patch(handlers.hookenv, 'config')
self.patch(handlers.hookenv, 'service_name')
self.config.return_value = None
self.service_name.return_value = 'keystone-ldap'
domain = mock.MagicMock()
handlers.configure_domain_name(domain)
self.render_config.assert_called_with(
domain.trigger_restart
)
domain.domain_name.assert_called_with(
'keystone-ldap'
)
def test_configure_domain_name_config(self):
self.patch(handlers.keystone_ldap, 'render_config')
self.patch(handlers.hookenv, 'config')
self.patch(handlers.hookenv, 'service_name')
self.config.return_value = 'mydomain'
self.service_name.return_value = 'keystone-ldap'
domain = mock.MagicMock()
handlers.configure_domain_name(domain)
self.render_config.assert_called_with(
domain.trigger_restart
)
domain.domain_name.assert_called_with(
'mydomain'
)
def test_check_configuration(self):
self.patch(handlers.keystone_ldap, 'configuration_complete')
self.patch(handlers.reactive, 'set_state')
self.patch(handlers.reactive, 'remove_state')
self.patch(handlers.keystone_ldap, 'assess_status')
self.configuration_complete.return_value = True
handlers.check_configuration()
self.set_state.assert_called_with('config.complete')
self.configuration_complete.return_value = False
handlers.check_configuration()
self.remove_state.assert_called_with('config.complete')
self.assertTrue(self.assess_status.called)

View File

@ -16,7 +16,7 @@ import unittest
import mock
import charm.openstack.sdn_charm as sdn_charm
import charm.openstack.keystone_ldap as keystone_ldap
class Helper(unittest.TestCase):
@ -42,3 +42,4 @@ class Helper(unittest.TestCase):
class TestSDNCharm(Helper):
pass

View File

@ -1,40 +0,0 @@
# 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.sdn_charm_handlers as handlers
import charms_openstack.test_utils as test_utils
class TestRegisteredHooks(test_utils.TestRegisteredHooks):
def test_hooks(self):
defaults = [
'charm.installed',
'config.changed',
'update-status']
hook_set = {
'when': {
},
'when_not': {
}
}
# test that the hooks were registered via the
# reactive.barbican_handlers
self.registered_hooks_test_helper(handlers, hook_set, defaults)
class TestSDNCharmHandles(test_utils.PatchHelper):