Put back the unit tests and enable the ignore

This adds back in the unit tests that were removed due to charm-tools
issue #192 [1] where the ignore option was 'ignored' by charm-tools.
This meant that the unit tests could not be ignored, and they ended
up in the built charm which then failed.

[1] https://github.com/juju/charm-tools/issues/192

Change-Id: Idbde830cac9ddc8ee3622fa6b7fd8a88e81e2811
This commit is contained in:
Alex Kavanagh 2016-04-27 22:44:03 +00:00
parent c9ed6c0575
commit f6831363b7
7 changed files with 270 additions and 21 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
.tox
.testrepository
.unit-state.db

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

@ -1,3 +1,12 @@
name: keystone
summary: Interface for integrating with Keystone identity service
maintainer: OpenStack Charmers <openstack-dev@lists.openstack.org>
ignore:
- 'unit_tests'
- 'Makefile'
- '.testr.conf'
- 'test-requirements.txt'
- 'tox.ini'
- '.gitignore'
- '.gitreview'
- '.unit-state.db'

View File

@ -1,2 +1,6 @@
flake8>=2.2.4,<=2.4.1
# Lint and unit test requirements
flake8
os-testr>=0.4.1
charms.reactive
mock>=1.2
coverage>=3.6

28
tox.ini
View File

@ -1,40 +1,28 @@
[tox]
envlist = pep8,py27,py34,py35
skipsdist = True
envlist = pep8,py35
skip_missing_interpreters = True
[testenv]
setenv = VIRTUAL_ENV={envdir}
PYTHONHASHSEED=0
TERM=linux
install_command =
pip install --allow-unverified python-apt {opts} {packages}
commands = ostestr {posargs}
[testenv:py27]
basepython = python2.7
deps = -r{toxinidir}/test-requirements.txt
# TODO: Need to write unit tests then remove the following command.
commands = /bin/true
[testenv:py34]
basepython = python3.4
deps = -r{toxinidir}/test-requirements.txt
# TODO: Need to write unit tests then remove the following command.
commands = /bin/true
pip install {opts} {packages}
[testenv:py35]
basepython = python3.5
basepython = python3
deps = -r{toxinidir}/test-requirements.txt
# TODO: Need to write unit tests then remove the following command.
commands = /bin/true
commands = ostestr {posargs}
[testenv:pep8]
basepython = python2.7
deps = -r{toxinidir}/test-requirements.txt
commands = flake8 {posargs}
commands = flake8 {posargs} . unit_tests
[testenv:venv]
commands = {posargs}
[flake8]
ignore = E402,E226
# E402 ignore necessary for path append before sys module import in actions
ignore = E402

0
unit_tests/__init__.py Normal file
View File

239
unit_tests/test_requires.py Normal file
View File

@ -0,0 +1,239 @@
# 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 unittest
import mock
import requires
_hook_args = {}
def mock_hook(*args, **kwargs):
def inner(f):
# remember what we were passed. Note that we can't actually determine
# the class we're attached to, as the decorator only gets the function.
_hook_args[f.__name__] = dict(args=args, kwargs=kwargs)
return f
return inner
class TestKeystoneRequires(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls._patched_hook = mock.patch('charms.reactive.hook', mock_hook)
cls._patched_hook_started = cls._patched_hook.start()
# force requires to rerun the mock_hook decorator:
# try except is Python2/Python3 compatibility as Python3 has moved
# reload to importlib.
try:
reload(requires)
except NameError:
import importlib
importlib.reload(requires)
@classmethod
def tearDownClass(cls):
cls._patched_hook.stop()
cls._patched_hook_started = None
cls._patched_hook = None
# and fix any breakage we did to the module
try:
reload(requires)
except NameError:
import importlib
importlib.reload(requires)
def setUp(self):
self.kr = requires.KeystoneRequires('some-relation', [])
self._patches = {}
self._patches_start = {}
def tearDown(self):
self.kr = None
for k, v in self._patches.items():
v.stop()
setattr(self, k, None)
self._patches = None
self._patches_start = None
def patch_kr(self, attr, return_value=None):
mocked = mock.patch.object(self.kr, attr)
self._patches[attr] = mocked
started = mocked.start()
started.return_value = return_value
self._patches_start[attr] = started
setattr(self, attr, started)
def test_registered_hooks(self):
# test that the hooks actually registered the relation expressions that
# are meaningful for this interface: this is to handle regressions.
# The keys are the function names that the hook attaches to.
hook_patterns = {
'joined': ('{requires:keystone}-relation-joined', ),
'changed': ('{requires:keystone}-relation-changed', ),
'departed': ('{requires:keystone}-relation-{broken,departed}', ),
}
for k, v in _hook_args.items():
self.assertEqual(hook_patterns[k], v['args'])
def test_changed(self):
self.patch_kr('update_state')
self.kr.changed()
self.update_state.assert_called_once_with()
def test_joined(self):
self.patch_kr('update_state')
self.patch_kr('set_state')
self.kr.joined()
self.set_state.assert_called_once_with('{relation_name}.connected')
self.update_state.assert_called_once_with()
def test_departed(self):
self.patch_kr('update_state')
self.kr.departed()
self.update_state.assert_called_once_with()
def test_base_data_complete(self):
self.patch_kr('private_address', '1')
self.patch_kr('service_host', '2')
self.patch_kr('service_protocol', '3')
self.patch_kr('service_port', '4')
self.patch_kr('auth_host', '5')
self.patch_kr('auth_protocol', '6')
self.patch_kr('auth_port', '7')
assert self.kr.base_data_complete() is True
self.auth_port.return_value = None
assert self.kr.base_data_complete() is False
def test_auth_data_complete(self):
self.patch_kr('service_tenant', '1')
self.patch_kr('service_username', '2')
self.patch_kr('service_password', '3')
self.patch_kr('service_tenant_id', '4')
assert self.kr.auth_data_complete() is True
self.service_tenant.return_value = None
assert self.kr.auth_data_complete() is False
def test_ssl_data_complete(self):
self.patch_kr('ssl_cert_admin', '1')
self.patch_kr('ssl_cert_internal', '2')
self.patch_kr('ssl_cert_public', '3')
self.patch_kr('ssl_key_admin', '4')
self.patch_kr('ssl_key_internal', '5')
self.patch_kr('ssl_key_public', '6')
self.patch_kr('ca_cert', '7')
assert self.kr.ssl_data_complete() is True
self.ca_cert.return_value = None
assert self.kr.ssl_data_complete() is False
self.ca_cert.return_value = '7'
self.ssl_key_public.return_value = '__null__'
assert self.kr.ssl_data_complete() is False
def test_ssl_data_complete_legacy(self):
self.patch_kr('ssl_key', '1')
self.patch_kr('ssl_cert', '2')
self.patch_kr('ca_cert', '3')
assert self.kr.ssl_data_complete_legacy() is True
self.ca_cert.return_value = None
assert self.kr.ssl_data_complete_legacy() is False
self.ca_cert.return_value = '3'
self.ssl_key.return_value = '__null__'
assert self.kr.ssl_data_complete_legacy() is False
def test_update_state(self):
self.patch_kr('base_data_complete', False)
self.patch_kr('ssl_data_complete', False)
self.patch_kr('ssl_data_complete_legacy', False)
self.patch_kr('auth_data_complete', False)
self.patch_kr('set_state')
self.patch_kr('remove_state')
# test when not all base data is available.
self.kr.update_state()
self.remove_state.assert_any_call('{relation_name}.available')
self.remove_state.assert_any_call('{relation_name}.available.ssl')
self.remove_state.assert_any_call(
'{relation_name}.available.ssl_legacy')
self.remove_state.assert_any_call('{relation_name}.available.auth')
self.set_state.assert_not_called()
self.remove_state.reset_mock()
# test when just the base data is available.
self.base_data_complete.return_value = True
self.kr.update_state()
self.set_state.assert_called_once_with('{relation_name}.available')
self.remove_state.assert_any_call('{relation_name}.available.ssl')
self.remove_state.assert_any_call(
'{relation_name}.available.ssl_legacy')
self.remove_state.assert_any_call('{relation_name}.available.auth')
self.set_state.reset_mock()
self.remove_state.reset_mock()
# test ssl_data_complete
self.ssl_data_complete.return_value = True
self.kr.update_state()
self.set_state.assert_any_call('{relation_name}.available')
self.set_state.assert_any_call('{relation_name}.available.ssl')
self.remove_state.assert_any_call(
'{relation_name}.available.ssl_legacy')
self.remove_state.assert_any_call('{relation_name}.available.auth')
self.set_state.reset_mock()
self.remove_state.reset_mock()
# test ssl_data_complete_legacy
self.ssl_data_complete_legacy.return_value = True
self.kr.update_state()
self.set_state.assert_any_call('{relation_name}.available')
self.set_state.assert_any_call('{relation_name}.available.ssl')
self.set_state.assert_any_call(
'{relation_name}.available.ssl_legacy')
self.remove_state.assert_any_call('{relation_name}.available.auth')
self.set_state.reset_mock()
self.remove_state.reset_mock()
# test auth_data_complete()
self.auth_data_complete.return_value = True
self.kr.update_state()
self.set_state.assert_any_call('{relation_name}.available')
self.set_state.assert_any_call('{relation_name}.available.ssl')
self.set_state.assert_any_call(
'{relation_name}.available.ssl_legacy')
self.set_state.assert_any_call('{relation_name}.available.auth')
self.remove_state.assert_not_called()
def test_register_endpoints(self):
self.patch_kr('set_local')
self.patch_kr('set_remote')
self.kr.register_endpoints('s', 'r', 'p_url', 'i_url', 'a_url')
result = {
'service': 's',
'public_url': 'p_url',
'internal_url': 'i_url',
'admin_url': 'a_url',
'region': 'r',
}
self.set_local.assert_called_once_with(**result)
self.set_remote.assert_called_once_with(**result)
def test_request_keystone_endpoint_information(self):
self.patch_kr('set_local')
self.patch_kr('set_remote')
result = {
'service': 'None',
'public_url': 'None',
'internal_url': 'None',
'admin_url': 'None',
'region': 'None',
}
self.kr.request_keystone_endpoint_information()
self.set_local.assert_called_once_with(**result)
self.set_remote.assert_called_once_with(**result)