Browse Source

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
changes/55/360055/2
Alex Kavanagh 3 years ago
parent
commit
f6831363b7
7 changed files with 270 additions and 21 deletions
  1. 1
    0
      .gitignore
  2. 8
    0
      .testr.conf
  3. 9
    0
      interface.yaml
  4. 5
    1
      test-requirements.txt
  5. 8
    20
      tox.ini
  6. 0
    0
      unit_tests/__init__.py
  7. 239
    0
      unit_tests/test_requires.py

+ 1
- 0
.gitignore View File

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

+ 8
- 0
.testr.conf View File

@@ -0,0 +1,8 @@
1
+[DEFAULT]
2
+test_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
+
7
+test_id_option=--load-list $IDFILE
8
+test_list_option=--list

+ 9
- 0
interface.yaml View File

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

+ 5
- 1
test-requirements.txt View File

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

+ 8
- 20
tox.ini View File

@@ -1,40 +1,28 @@
1 1
 [tox]
2
-envlist = pep8,py27,py34,py35
3 2
 skipsdist = True
3
+envlist = pep8,py35
4 4
 skip_missing_interpreters = True
5 5
 
6 6
 [testenv]
7 7
 setenv = VIRTUAL_ENV={envdir}
8 8
          PYTHONHASHSEED=0
9
+         TERM=linux
9 10
 install_command =
10
-  pip install --allow-unverified python-apt {opts} {packages}
11
-commands = ostestr {posargs}
12
-
13
-[testenv:py27]
14
-basepython = python2.7
15
-deps = -r{toxinidir}/test-requirements.txt
16
-# TODO: Need to write unit tests then remove the following command.
17
-commands = /bin/true
18
-
19
-[testenv:py34]
20
-basepython = python3.4
21
-deps = -r{toxinidir}/test-requirements.txt
22
-# TODO: Need to write unit tests then remove the following command.
23
-commands = /bin/true
11
+  pip install {opts} {packages}
24 12
 
25 13
 [testenv:py35]
26
-basepython = python3.5
14
+basepython = python3
27 15
 deps = -r{toxinidir}/test-requirements.txt
28
-# TODO: Need to write unit tests then remove the following command.
29
-commands = /bin/true
16
+commands = ostestr {posargs}
30 17
 
31 18
 [testenv:pep8]
32 19
 basepython = python2.7
33 20
 deps = -r{toxinidir}/test-requirements.txt
34
-commands = flake8 {posargs}
21
+commands = flake8 {posargs} . unit_tests
35 22
 
36 23
 [testenv:venv]
37 24
 commands = {posargs}
38 25
 
39 26
 [flake8]
40
-ignore = E402,E226
27
+# E402 ignore necessary for path append before sys module import in actions
28
+ignore = E402

+ 0
- 0
unit_tests/__init__.py View File


+ 239
- 0
unit_tests/test_requires.py View File

@@ -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
+
14
+import unittest
15
+import mock
16
+
17
+import requires
18
+
19
+
20
+_hook_args = {}
21
+
22
+
23
+def 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
+
33
+class 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)

Loading…
Cancel
Save