summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Kavanagh <alex@ajkavanagh.co.uk>2016-04-27 22:44:03 +0000
committerAlex Kavanagh <alex@ajkavanagh.co.uk>2018-05-08 15:05:20 +0100
commitf6831363b737394834cd4ada99fee11f8d7eb127 (patch)
tree1185aadcad8c87a49e6e26315d2f5ad71db4e631
parentc9ed6c057531d57b52a8f55d04c91c2093a59edf (diff)
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
Notes
Notes (review): Verified+1: Canonical CI <uosci-testing-bot@ubuntu.com> Code-Review+2: Liam Young <liam.young@canonical.com> Workflow+1: Liam Young <liam.young@canonical.com> Verified+2: Zuul Submitted-by: Zuul Submitted-at: Tue, 08 May 2018 15:58:41 +0000 Reviewed-on: https://review.openstack.org/360055 Project: openstack/charm-interface-keystone Branch: refs/heads/master
-rw-r--r--.gitignore1
-rw-r--r--.testr.conf8
-rw-r--r--interface.yaml9
-rw-r--r--test-requirements.txt6
-rw-r--r--tox.ini28
-rw-r--r--unit_tests/__init__.py0
-rw-r--r--unit_tests/test_requires.py239
7 files changed, 270 insertions, 21 deletions
diff --git a/.gitignore b/.gitignore
index 9dd3eb8..60c0686 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
1.tox 1.tox
2.testrepository 2.testrepository
3.unit-state.db
diff --git a/.testr.conf b/.testr.conf
new file mode 100644
index 0000000..801646b
--- /dev/null
+++ b/.testr.conf
@@ -0,0 +1,8 @@
1[DEFAULT]
2test_command=OS_STDOUT_CAPTURE=${OS_STDOUT_CAPTURE:-1} \
3 OS_STDERR_CAPTURE=${OS_STDERR_CAPTURE:-1} \
4 OS_TEST_TIMEOUT=${OS_TEST_TIMEOUT:-60} \
5 ${PYTHON:-python} -m subunit.run discover -t ./ ./unit_tests $LISTOPT $IDOPTION
6
7test_id_option=--load-list $IDFILE
8test_list_option=--list
diff --git a/interface.yaml b/interface.yaml
index f01f858..5e6d0a0 100644
--- a/interface.yaml
+++ b/interface.yaml
@@ -1,3 +1,12 @@
1name: keystone 1name: keystone
2summary: Interface for integrating with Keystone identity service 2summary: Interface for integrating with Keystone identity service
3maintainer: OpenStack Charmers <openstack-dev@lists.openstack.org> 3maintainer: OpenStack Charmers <openstack-dev@lists.openstack.org>
4ignore:
5 - 'unit_tests'
6 - 'Makefile'
7 - '.testr.conf'
8 - 'test-requirements.txt'
9 - 'tox.ini'
10 - '.gitignore'
11 - '.gitreview'
12 - '.unit-state.db'
diff --git a/test-requirements.txt b/test-requirements.txt
index 095ec9c..c706224 100644
--- a/test-requirements.txt
+++ b/test-requirements.txt
@@ -1,2 +1,6 @@
1flake8>=2.2.4,<=2.4.1 1# Lint and unit test requirements
2flake8
2os-testr>=0.4.1 3os-testr>=0.4.1
4charms.reactive
5mock>=1.2
6coverage>=3.6
diff --git a/tox.ini b/tox.ini
index c395138..d83ef84 100644
--- a/tox.ini
+++ b/tox.ini
@@ -1,40 +1,28 @@
1[tox] 1[tox]
2envlist = pep8,py27,py34,py35
3skipsdist = True 2skipsdist = True
3envlist = pep8,py35
4skip_missing_interpreters = True 4skip_missing_interpreters = True
5 5
6[testenv] 6[testenv]
7setenv = VIRTUAL_ENV={envdir} 7setenv = VIRTUAL_ENV={envdir}
8 PYTHONHASHSEED=0 8 PYTHONHASHSEED=0
9 TERM=linux
9install_command = 10install_command =
10 pip install --allow-unverified python-apt {opts} {packages} 11 pip install {opts} {packages}
11commands = ostestr {posargs}
12
13[testenv:py27]
14basepython = python2.7
15deps = -r{toxinidir}/test-requirements.txt
16# TODO: Need to write unit tests then remove the following command.
17commands = /bin/true
18
19[testenv:py34]
20basepython = python3.4
21deps = -r{toxinidir}/test-requirements.txt
22# TODO: Need to write unit tests then remove the following command.
23commands = /bin/true
24 12
25[testenv:py35] 13[testenv:py35]
26basepython = python3.5 14basepython = python3
27deps = -r{toxinidir}/test-requirements.txt 15deps = -r{toxinidir}/test-requirements.txt
28# TODO: Need to write unit tests then remove the following command. 16commands = ostestr {posargs}
29commands = /bin/true
30 17
31[testenv:pep8] 18[testenv:pep8]
32basepython = python2.7 19basepython = python2.7
33deps = -r{toxinidir}/test-requirements.txt 20deps = -r{toxinidir}/test-requirements.txt
34commands = flake8 {posargs} 21commands = flake8 {posargs} . unit_tests
35 22
36[testenv:venv] 23[testenv:venv]
37commands = {posargs} 24commands = {posargs}
38 25
39[flake8] 26[flake8]
40ignore = E402,E226 27# E402 ignore necessary for path append before sys module import in actions
28ignore = E402
diff --git a/unit_tests/__init__.py b/unit_tests/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/unit_tests/__init__.py
diff --git a/unit_tests/test_requires.py b/unit_tests/test_requires.py
new file mode 100644
index 0000000..7fb63d5
--- /dev/null
+++ b/unit_tests/test_requires.py
@@ -0,0 +1,239 @@
1# Licensed under the Apache License, Version 2.0 (the "License");
2# you may not use this file except in compliance with the License.
3# You may obtain a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS,
9# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10# See the License for the specific language governing permissions and
11# limitations under the License.
12
13
14import unittest
15import mock
16
17import requires
18
19
20_hook_args = {}
21
22
23def mock_hook(*args, **kwargs):
24
25 def inner(f):
26 # remember what we were passed. Note that we can't actually determine
27 # the class we're attached to, as the decorator only gets the function.
28 _hook_args[f.__name__] = dict(args=args, kwargs=kwargs)
29 return f
30 return inner
31
32
33class TestKeystoneRequires(unittest.TestCase):
34
35 @classmethod
36 def setUpClass(cls):
37 cls._patched_hook = mock.patch('charms.reactive.hook', mock_hook)
38 cls._patched_hook_started = cls._patched_hook.start()
39 # force requires to rerun the mock_hook decorator:
40 # try except is Python2/Python3 compatibility as Python3 has moved
41 # reload to importlib.
42 try:
43 reload(requires)
44 except NameError:
45 import importlib
46 importlib.reload(requires)
47
48 @classmethod
49 def tearDownClass(cls):
50 cls._patched_hook.stop()
51 cls._patched_hook_started = None
52 cls._patched_hook = None
53 # and fix any breakage we did to the module
54 try:
55 reload(requires)
56 except NameError:
57 import importlib
58 importlib.reload(requires)
59
60 def setUp(self):
61 self.kr = requires.KeystoneRequires('some-relation', [])
62 self._patches = {}
63 self._patches_start = {}
64
65 def tearDown(self):
66 self.kr = None
67 for k, v in self._patches.items():
68 v.stop()
69 setattr(self, k, None)
70 self._patches = None
71 self._patches_start = None
72
73 def patch_kr(self, attr, return_value=None):
74 mocked = mock.patch.object(self.kr, attr)
75 self._patches[attr] = mocked
76 started = mocked.start()
77 started.return_value = return_value
78 self._patches_start[attr] = started
79 setattr(self, attr, started)
80
81 def test_registered_hooks(self):
82 # test that the hooks actually registered the relation expressions that
83 # are meaningful for this interface: this is to handle regressions.
84 # The keys are the function names that the hook attaches to.
85 hook_patterns = {
86 'joined': ('{requires:keystone}-relation-joined', ),
87 'changed': ('{requires:keystone}-relation-changed', ),
88 'departed': ('{requires:keystone}-relation-{broken,departed}', ),
89 }
90 for k, v in _hook_args.items():
91 self.assertEqual(hook_patterns[k], v['args'])
92
93 def test_changed(self):
94 self.patch_kr('update_state')
95 self.kr.changed()
96 self.update_state.assert_called_once_with()
97
98 def test_joined(self):
99 self.patch_kr('update_state')
100 self.patch_kr('set_state')
101 self.kr.joined()
102 self.set_state.assert_called_once_with('{relation_name}.connected')
103 self.update_state.assert_called_once_with()
104
105 def test_departed(self):
106 self.patch_kr('update_state')
107 self.kr.departed()
108 self.update_state.assert_called_once_with()
109
110 def test_base_data_complete(self):
111 self.patch_kr('private_address', '1')
112 self.patch_kr('service_host', '2')
113 self.patch_kr('service_protocol', '3')
114 self.patch_kr('service_port', '4')
115 self.patch_kr('auth_host', '5')
116 self.patch_kr('auth_protocol', '6')
117 self.patch_kr('auth_port', '7')
118 assert self.kr.base_data_complete() is True
119 self.auth_port.return_value = None
120 assert self.kr.base_data_complete() is False
121
122 def test_auth_data_complete(self):
123 self.patch_kr('service_tenant', '1')
124 self.patch_kr('service_username', '2')
125 self.patch_kr('service_password', '3')
126 self.patch_kr('service_tenant_id', '4')
127 assert self.kr.auth_data_complete() is True
128 self.service_tenant.return_value = None
129 assert self.kr.auth_data_complete() is False
130
131 def test_ssl_data_complete(self):
132 self.patch_kr('ssl_cert_admin', '1')
133 self.patch_kr('ssl_cert_internal', '2')
134 self.patch_kr('ssl_cert_public', '3')
135 self.patch_kr('ssl_key_admin', '4')
136 self.patch_kr('ssl_key_internal', '5')
137 self.patch_kr('ssl_key_public', '6')
138 self.patch_kr('ca_cert', '7')
139 assert self.kr.ssl_data_complete() is True
140 self.ca_cert.return_value = None
141 assert self.kr.ssl_data_complete() is False
142 self.ca_cert.return_value = '7'
143 self.ssl_key_public.return_value = '__null__'
144 assert self.kr.ssl_data_complete() is False
145
146 def test_ssl_data_complete_legacy(self):
147 self.patch_kr('ssl_key', '1')
148 self.patch_kr('ssl_cert', '2')
149 self.patch_kr('ca_cert', '3')
150 assert self.kr.ssl_data_complete_legacy() is True
151 self.ca_cert.return_value = None
152 assert self.kr.ssl_data_complete_legacy() is False
153 self.ca_cert.return_value = '3'
154 self.ssl_key.return_value = '__null__'
155 assert self.kr.ssl_data_complete_legacy() is False
156
157 def test_update_state(self):
158 self.patch_kr('base_data_complete', False)
159 self.patch_kr('ssl_data_complete', False)
160 self.patch_kr('ssl_data_complete_legacy', False)
161 self.patch_kr('auth_data_complete', False)
162 self.patch_kr('set_state')
163 self.patch_kr('remove_state')
164 # test when not all base data is available.
165 self.kr.update_state()
166 self.remove_state.assert_any_call('{relation_name}.available')
167 self.remove_state.assert_any_call('{relation_name}.available.ssl')
168 self.remove_state.assert_any_call(
169 '{relation_name}.available.ssl_legacy')
170 self.remove_state.assert_any_call('{relation_name}.available.auth')
171 self.set_state.assert_not_called()
172 self.remove_state.reset_mock()
173 # test when just the base data is available.
174 self.base_data_complete.return_value = True
175 self.kr.update_state()
176 self.set_state.assert_called_once_with('{relation_name}.available')
177 self.remove_state.assert_any_call('{relation_name}.available.ssl')
178 self.remove_state.assert_any_call(
179 '{relation_name}.available.ssl_legacy')
180 self.remove_state.assert_any_call('{relation_name}.available.auth')
181 self.set_state.reset_mock()
182 self.remove_state.reset_mock()
183 # test ssl_data_complete
184 self.ssl_data_complete.return_value = True
185 self.kr.update_state()
186 self.set_state.assert_any_call('{relation_name}.available')
187 self.set_state.assert_any_call('{relation_name}.available.ssl')
188 self.remove_state.assert_any_call(
189 '{relation_name}.available.ssl_legacy')
190 self.remove_state.assert_any_call('{relation_name}.available.auth')
191 self.set_state.reset_mock()
192 self.remove_state.reset_mock()
193 # test ssl_data_complete_legacy
194 self.ssl_data_complete_legacy.return_value = True
195 self.kr.update_state()
196 self.set_state.assert_any_call('{relation_name}.available')
197 self.set_state.assert_any_call('{relation_name}.available.ssl')
198 self.set_state.assert_any_call(
199 '{relation_name}.available.ssl_legacy')
200 self.remove_state.assert_any_call('{relation_name}.available.auth')
201 self.set_state.reset_mock()
202 self.remove_state.reset_mock()
203 # test auth_data_complete()
204 self.auth_data_complete.return_value = True
205 self.kr.update_state()
206 self.set_state.assert_any_call('{relation_name}.available')
207 self.set_state.assert_any_call('{relation_name}.available.ssl')
208 self.set_state.assert_any_call(
209 '{relation_name}.available.ssl_legacy')
210 self.set_state.assert_any_call('{relation_name}.available.auth')
211 self.remove_state.assert_not_called()
212
213 def test_register_endpoints(self):
214 self.patch_kr('set_local')
215 self.patch_kr('set_remote')
216 self.kr.register_endpoints('s', 'r', 'p_url', 'i_url', 'a_url')
217 result = {
218 'service': 's',
219 'public_url': 'p_url',
220 'internal_url': 'i_url',
221 'admin_url': 'a_url',
222 'region': 'r',
223 }
224 self.set_local.assert_called_once_with(**result)
225 self.set_remote.assert_called_once_with(**result)
226
227 def test_request_keystone_endpoint_information(self):
228 self.patch_kr('set_local')
229 self.patch_kr('set_remote')
230 result = {
231 'service': 'None',
232 'public_url': 'None',
233 'internal_url': 'None',
234 'admin_url': 'None',
235 'region': 'None',
236 }
237 self.kr.request_keystone_endpoint_information()
238 self.set_local.assert_called_once_with(**result)
239 self.set_remote.assert_called_once_with(**result)