Merge lp:~ajkavanagh/charm-helpers/fix-alphanumerica-comparisons into lp:charm-helpers
- fix-alphanumerica-comparisons
- Merge into devel
Status: | Merged |
---|---|
Merged at revision: | 725 |
Proposed branch: | lp:~ajkavanagh/charm-helpers/fix-alphanumerica-comparisons |
Merge into: | lp:charm-helpers |
Diff against target: |
674 lines (+210/-173) 13 files modified
charmhelpers/contrib/hardening/ssh/checks/config.py (+14/-6) charmhelpers/contrib/network/ip.py (+2/-4) charmhelpers/contrib/openstack/amulet/utils.py (+2/-1) charmhelpers/contrib/openstack/context.py (+2/-2) charmhelpers/contrib/openstack/neutron.py (+12/-7) charmhelpers/contrib/openstack/utils.py (+4/-88) charmhelpers/core/host.py (+2/-0) charmhelpers/core/host_factory/centos.py (+16/-0) charmhelpers/core/host_factory/ubuntu.py (+32/-0) charmhelpers/core/strutils.py (+53/-0) tests/contrib/openstack/test_os_utils.py (+3/-64) tests/core/test_host.py (+10/-1) tests/core/test_strutils.py (+58/-0) |
To merge this branch: | bzr merge lp:~ajkavanagh/charm-helpers/fix-alphanumerica-comparisons |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
David Ames (community) | Approve | ||
Review via email:
|
Commit message
Description of the change
A bit of an reorganisation due to finding additional comparisons in charm-helpers itself.
This moves the BasicStringComp
CompareHostRele
charmhelpers.
This means a removal of a circular import and enables the use of the function from
non-openstack charms.
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
Alex Kavanagh (ajkavanagh) wrote : | # |
Good catch on the import. See my other comments addressing your questions. Thanks.
- 723. By Alex Kavanagh
-
Fix incorrect import for amulet/utils.py
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
Alex Kavanagh (ajkavanagh) wrote : | # |
Having had a bit more time to think on the import question of CompareHostRele
- 724. By Alex Kavanagh
-
Provide "not-implemented" version of CompareHostRele
ases() for Centos This provides a CompareHostRele
ases() class for Centos that raises
NotImplementedError() if it is used. This is provided so that modules
that import the class will still work on Centos, but functions that
try to use it won't. The functions that are using CompareHostReleases()
are currently specific to Ubuntu.
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
David Ames (thedac) wrote : | # |
This looks good.
Merging.
Preview Diff
1 | === modified file 'charmhelpers/contrib/hardening/ssh/checks/config.py' |
2 | --- charmhelpers/contrib/hardening/ssh/checks/config.py 2016-09-23 08:40:19 +0000 |
3 | +++ charmhelpers/contrib/hardening/ssh/checks/config.py 2017-03-26 10:20:30 +0000 |
4 | @@ -27,7 +27,10 @@ |
5 | apt_install, |
6 | apt_update, |
7 | ) |
8 | -from charmhelpers.core.host import lsb_release |
9 | +from charmhelpers.core.host import ( |
10 | + lsb_release, |
11 | + CompareHostReleases, |
12 | +) |
13 | from charmhelpers.contrib.hardening.audits.file import ( |
14 | TemplatedFile, |
15 | FileContentAudit, |
16 | @@ -68,7 +71,8 @@ |
17 | 'weak': default + ',hmac-sha1'} |
18 | |
19 | # Use newer ciphers on Ubuntu Trusty and above |
20 | - if lsb_release()['DISTRIB_CODENAME'].lower() >= 'trusty': |
21 | + _release = lsb_release()['DISTRIB_CODENAME'].lower() |
22 | + if CompareHostReleases(_release) >= 'trusty': |
23 | log("Detected Ubuntu 14.04 or newer, using new macs", level=DEBUG) |
24 | macs = macs_66 |
25 | |
26 | @@ -96,7 +100,8 @@ |
27 | 'weak': weak} |
28 | |
29 | # Use newer kex on Ubuntu Trusty and above |
30 | - if lsb_release()['DISTRIB_CODENAME'].lower() >= 'trusty': |
31 | + _release = lsb_release()['DISTRIB_CODENAME'].lower() |
32 | + if CompareHostReleases(_release) >= 'trusty': |
33 | log('Detected Ubuntu 14.04 or newer, using new key exchange ' |
34 | 'algorithms', level=DEBUG) |
35 | kex = kex_66 |
36 | @@ -119,7 +124,8 @@ |
37 | 'weak': default + ',aes256-cbc,aes192-cbc,aes128-cbc'} |
38 | |
39 | # Use newer ciphers on ubuntu Trusty and above |
40 | - if lsb_release()['DISTRIB_CODENAME'].lower() >= 'trusty': |
41 | + _release = lsb_release()['DISTRIB_CODENAME'].lower() |
42 | + if CompareHostReleases(_release) >= 'trusty': |
43 | log('Detected Ubuntu 14.04 or newer, using new ciphers', |
44 | level=DEBUG) |
45 | cipher = ciphers_66 |
46 | @@ -291,7 +297,8 @@ |
47 | self.fail_cases = [] |
48 | settings = utils.get_settings('ssh') |
49 | |
50 | - if lsb_release()['DISTRIB_CODENAME'].lower() >= 'trusty': |
51 | + _release = lsb_release()['DISTRIB_CODENAME'].lower() |
52 | + if CompareHostReleases(_release) >= 'trusty': |
53 | if not settings['server']['weak_hmac']: |
54 | self.pass_cases.append(r'^MACs.+,hmac-ripemd160$') |
55 | else: |
56 | @@ -364,7 +371,8 @@ |
57 | self.fail_cases = [] |
58 | settings = utils.get_settings('ssh') |
59 | |
60 | - if lsb_release()['DISTRIB_CODENAME'].lower() >= 'trusty': |
61 | + _release = lsb_release()['DISTRIB_CODENAME'].lower() |
62 | + if CompareHostReleases(_release) >= 'trusty': |
63 | if not settings['server']['weak_hmac']: |
64 | self.pass_cases.append(r'^MACs.+,hmac-ripemd160$') |
65 | else: |
66 | |
67 | === modified file 'charmhelpers/contrib/network/ip.py' |
68 | --- charmhelpers/contrib/network/ip.py 2017-03-14 11:46:12 +0000 |
69 | +++ charmhelpers/contrib/network/ip.py 2017-03-26 10:20:30 +0000 |
70 | @@ -31,6 +31,7 @@ |
71 | |
72 | from charmhelpers.core.host import ( |
73 | lsb_release, |
74 | + CompareHostReleases, |
75 | ) |
76 | |
77 | try: |
78 | @@ -537,11 +538,8 @@ |
79 | |
80 | def assert_charm_supports_ipv6(): |
81 | """Check whether we are able to support charms ipv6.""" |
82 | - # workaround circular import -- means we have a code organisation issue, |
83 | - # but charms depend on this location for this function ... |
84 | - from charmhelpers.contrib.openstack.utils import CompareUbuntuReleases |
85 | release = lsb_release()['DISTRIB_CODENAME'].lower() |
86 | - if CompareUbuntuReleases(release) < "trusty": |
87 | + if CompareHostReleases(release) < "trusty": |
88 | raise Exception("IPv6 is not supported in the charms for Ubuntu " |
89 | "versions less than Trusty 14.04") |
90 | |
91 | |
92 | === modified file 'charmhelpers/contrib/openstack/amulet/utils.py' |
93 | --- charmhelpers/contrib/openstack/amulet/utils.py 2017-02-27 22:11:45 +0000 |
94 | +++ charmhelpers/contrib/openstack/amulet/utils.py 2017-03-26 10:20:30 +0000 |
95 | @@ -40,6 +40,7 @@ |
96 | AmuletUtils |
97 | ) |
98 | from charmhelpers.core.decorators import retry_on_exception |
99 | +from charmhelpers.core.host import CompareHostReleases |
100 | |
101 | DEBUG = logging.DEBUG |
102 | ERROR = logging.ERROR |
103 | @@ -1255,7 +1256,7 @@ |
104 | contents = self.file_contents_safe(sentry_unit, '/etc/memcached.conf', |
105 | fatal=True) |
106 | ubuntu_release, _ = self.run_cmd_unit(sentry_unit, 'lsb_release -cs') |
107 | - if ubuntu_release <= 'trusty': |
108 | + if CompareHostReleases(ubuntu_release) <= 'trusty': |
109 | memcache_listen_addr = 'ip6-localhost' |
110 | else: |
111 | memcache_listen_addr = '::1' |
112 | |
113 | === modified file 'charmhelpers/contrib/openstack/context.py' |
114 | --- charmhelpers/contrib/openstack/context.py 2017-03-14 11:46:12 +0000 |
115 | +++ charmhelpers/contrib/openstack/context.py 2017-03-26 10:20:30 +0000 |
116 | @@ -59,6 +59,7 @@ |
117 | write_file, |
118 | pwgen, |
119 | lsb_release, |
120 | + CompareHostReleases, |
121 | ) |
122 | from charmhelpers.contrib.hahelpers.cluster import ( |
123 | determine_apache_port, |
124 | @@ -94,7 +95,6 @@ |
125 | git_determine_usr_bin, |
126 | git_determine_python_path, |
127 | enable_memcache, |
128 | - CompareUbuntuReleases, |
129 | ) |
130 | from charmhelpers.core.unitdata import kv |
131 | |
132 | @@ -1606,7 +1606,7 @@ |
133 | # Trusty version of memcached does not support ::1 as a listen |
134 | # address so use host file entry instead |
135 | release = lsb_release()['DISTRIB_CODENAME'].lower() |
136 | - if CompareUbuntuReleases(release) > 'trusty': |
137 | + if CompareHostReleases(release) > 'trusty': |
138 | ctxt['memcache_server'] = '::1' |
139 | else: |
140 | ctxt['memcache_server'] = 'ip6-localhost' |
141 | |
142 | === modified file 'charmhelpers/contrib/openstack/neutron.py' |
143 | --- charmhelpers/contrib/openstack/neutron.py 2016-11-25 22:50:01 +0000 |
144 | +++ charmhelpers/contrib/openstack/neutron.py 2017-03-26 10:20:30 +0000 |
145 | @@ -23,7 +23,10 @@ |
146 | ERROR, |
147 | ) |
148 | |
149 | -from charmhelpers.contrib.openstack.utils import os_release |
150 | +from charmhelpers.contrib.openstack.utils import ( |
151 | + os_release, |
152 | + CompareOpenStackReleases, |
153 | +) |
154 | |
155 | |
156 | def headers_package(): |
157 | @@ -198,7 +201,8 @@ |
158 | }, |
159 | 'plumgrid': { |
160 | 'config': '/etc/neutron/plugins/plumgrid/plumgrid.ini', |
161 | - 'driver': 'neutron.plugins.plumgrid.plumgrid_plugin.plumgrid_plugin.NeutronPluginPLUMgridV2', |
162 | + 'driver': ('neutron.plugins.plumgrid.plumgrid_plugin' |
163 | + '.plumgrid_plugin.NeutronPluginPLUMgridV2'), |
164 | 'contexts': [ |
165 | context.SharedDBContext(user=config('database-user'), |
166 | database=config('database'), |
167 | @@ -225,7 +229,7 @@ |
168 | 'server_services': ['neutron-server'] |
169 | } |
170 | } |
171 | - if release >= 'icehouse': |
172 | + if CompareOpenStackReleases(release) >= 'icehouse': |
173 | # NOTE: patch in ml2 plugin for icehouse onwards |
174 | plugins['ovs']['config'] = '/etc/neutron/plugins/ml2/ml2_conf.ini' |
175 | plugins['ovs']['driver'] = 'neutron.plugins.ml2.plugin.Ml2Plugin' |
176 | @@ -233,10 +237,10 @@ |
177 | 'neutron-plugin-ml2'] |
178 | # NOTE: patch in vmware renames nvp->nsx for icehouse onwards |
179 | plugins['nvp'] = plugins['nsx'] |
180 | - if release >= 'kilo': |
181 | + if CompareOpenStackReleases(release) >= 'kilo': |
182 | plugins['midonet']['driver'] = ( |
183 | 'neutron.plugins.midonet.plugin.MidonetPluginV2') |
184 | - if release >= 'liberty': |
185 | + if CompareOpenStackReleases(release) >= 'liberty': |
186 | plugins['midonet']['driver'] = ( |
187 | 'midonet.neutron.plugin_v1.MidonetPluginV2') |
188 | plugins['midonet']['server_packages'].remove( |
189 | @@ -244,10 +248,11 @@ |
190 | plugins['midonet']['server_packages'].append( |
191 | 'python-networking-midonet') |
192 | plugins['plumgrid']['driver'] = ( |
193 | - 'networking_plumgrid.neutron.plugins.plugin.NeutronPluginPLUMgridV2') |
194 | + 'networking_plumgrid.neutron.plugins' |
195 | + '.plugin.NeutronPluginPLUMgridV2') |
196 | plugins['plumgrid']['server_packages'].remove( |
197 | 'neutron-plugin-plumgrid') |
198 | - if release >= 'mitaka': |
199 | + if CompareOpenStackReleases(release) >= 'mitaka': |
200 | plugins['nsx']['server_packages'].remove('neutron-plugin-vmware') |
201 | plugins['nsx']['server_packages'].append('python-vmware-nsx') |
202 | plugins['nsx']['config'] = '/etc/neutron/nsx.ini' |
203 | |
204 | === modified file 'charmhelpers/contrib/openstack/utils.py' |
205 | --- charmhelpers/contrib/openstack/utils.py 2017-03-20 13:44:46 +0000 |
206 | +++ charmhelpers/contrib/openstack/utils.py 2017-03-26 10:20:30 +0000 |
207 | @@ -33,9 +33,7 @@ |
208 | |
209 | from charmhelpers.contrib.network import ip |
210 | |
211 | -from charmhelpers.core import ( |
212 | - unitdata, |
213 | -) |
214 | +from charmhelpers.core import unitdata |
215 | |
216 | from charmhelpers.core.hookenv import ( |
217 | action_fail, |
218 | @@ -55,6 +53,8 @@ |
219 | application_version_set, |
220 | ) |
221 | |
222 | +from charmhelpers.core.strutils import BasicStringComparator |
223 | + |
224 | from charmhelpers.contrib.storage.linux.lvm import ( |
225 | deactivate_lvm_volume_group, |
226 | is_lvm_physical_volume, |
227 | @@ -97,24 +97,6 @@ |
228 | DISTRO_PROPOSED = ('deb http://archive.ubuntu.com/ubuntu/ %s-proposed ' |
229 | 'restricted main multiverse universe') |
230 | |
231 | -UBUNTU_RELEASES = ( |
232 | - 'lucid', |
233 | - 'maverick', |
234 | - 'natty', |
235 | - 'oneiric', |
236 | - 'precise', |
237 | - 'quantal', |
238 | - 'raring', |
239 | - 'saucy', |
240 | - 'trusty', |
241 | - 'utopic', |
242 | - 'vivid', |
243 | - 'wily', |
244 | - 'xenial', |
245 | - 'yakkety', |
246 | - 'zesty', |
247 | -) |
248 | - |
249 | OPENSTACK_RELEASES = ( |
250 | 'diablo', |
251 | 'essex', |
252 | @@ -272,59 +254,6 @@ |
253 | DEFAULT_LOOPBACK_SIZE = '5G' |
254 | |
255 | |
256 | -class BasicStringComparator(object): |
257 | - """Provides a class that will compare strings from an iterator type object. |
258 | - Used to provide > and < comparisons on strings that may not necessarily be |
259 | - alphanumerically ordered. e.g. OpenStack or Ubuntu releases AFTER the |
260 | - z-wrap. |
261 | - """ |
262 | - |
263 | - _list = None |
264 | - |
265 | - def __init__(self, item): |
266 | - if self._list is None: |
267 | - raise Exception("Must define the _list in the class definition!") |
268 | - try: |
269 | - self.index = self._list.index(item) |
270 | - except Exception: |
271 | - raise KeyError("Item '{}' is not in list '{}'" |
272 | - .format(item, self._list)) |
273 | - |
274 | - def __eq__(self, other): |
275 | - assert isinstance(other, str) or isinstance(other, self.__class__) |
276 | - return self.index == self._list.index(other) |
277 | - |
278 | - def __ne__(self, other): |
279 | - return not self.__eq__(other) |
280 | - |
281 | - def __lt__(self, other): |
282 | - assert isinstance(other, str) or isinstance(other, self.__class__) |
283 | - return self.index < self._list.index(other) |
284 | - |
285 | - def __ge__(self, other): |
286 | - return not self.__lt__(other) |
287 | - |
288 | - def __gt__(self, other): |
289 | - assert isinstance(other, str) or isinstance(other, self.__class__) |
290 | - return self.index > self._list.index(other) |
291 | - |
292 | - def __le__(self, other): |
293 | - return not self.__gt__(other) |
294 | - |
295 | - def __str__(self): |
296 | - """Always give back the item at the index so it can be used in |
297 | - comparisons like: |
298 | - |
299 | - s_mitaka = CompareOpenStack('mitaka') |
300 | - s_newton = CompareOpenstack('newton') |
301 | - |
302 | - assert s_newton > s_mitaka |
303 | - |
304 | - @returns: <string> |
305 | - """ |
306 | - return self._list[self.index] |
307 | - |
308 | - |
309 | class CompareOpenStackReleases(BasicStringComparator): |
310 | """Provide comparisons of OpenStack releases. |
311 | |
312 | @@ -336,17 +265,6 @@ |
313 | _list = OPENSTACK_RELEASES |
314 | |
315 | |
316 | -class CompareUbuntuReleases(BasicStringComparator): |
317 | - """Provide comparisons of Ubuntu releases. |
318 | - |
319 | - Use in the form of |
320 | - |
321 | - if CompareUbuntuReleases(release) > 'trusty': |
322 | - # do something with mitaka |
323 | - """ |
324 | - _list = UBUNTU_RELEASES |
325 | - |
326 | - |
327 | def error_out(msg): |
328 | juju_log("FATAL ERROR: %s" % msg, level='ERROR') |
329 | sys.exit(1) |
330 | @@ -2081,9 +1999,7 @@ |
331 | if not _release: |
332 | _release = get_os_codename_install_source(source) |
333 | |
334 | - # TODO: this should be changed to a numeric comparison using a known list |
335 | - # of releases and comparing by index. |
336 | - return _release >= 'mitaka' |
337 | + return CompareOpenStackReleases(_release) >= 'mitaka' |
338 | |
339 | |
340 | def token_cache_pkgs(source=None, release=None): |
341 | |
342 | === modified file 'charmhelpers/core/host.py' |
343 | --- charmhelpers/core/host.py 2017-03-08 06:54:54 +0000 |
344 | +++ charmhelpers/core/host.py 2017-03-26 10:20:30 +0000 |
345 | @@ -45,6 +45,7 @@ |
346 | add_new_group, |
347 | lsb_release, |
348 | cmp_pkgrevno, |
349 | + CompareHostReleases, |
350 | ) # flake8: noqa -- ignore F401 for this import |
351 | elif __platform__ == "centos": |
352 | from charmhelpers.core.host_factory.centos import ( |
353 | @@ -52,6 +53,7 @@ |
354 | add_new_group, |
355 | lsb_release, |
356 | cmp_pkgrevno, |
357 | + CompareHostReleases, |
358 | ) # flake8: noqa -- ignore F401 for this import |
359 | |
360 | UPDATEDB_PATH = '/etc/updatedb.conf' |
361 | |
362 | === modified file 'charmhelpers/core/host_factory/centos.py' |
363 | --- charmhelpers/core/host_factory/centos.py 2016-08-11 15:47:44 +0000 |
364 | +++ charmhelpers/core/host_factory/centos.py 2017-03-26 10:20:30 +0000 |
365 | @@ -2,6 +2,22 @@ |
366 | import yum |
367 | import os |
368 | |
369 | +from charmhelpers.core.strutils import BasicStringComparator |
370 | + |
371 | + |
372 | +class CompareHostReleases(BasicStringComparator): |
373 | + """Provide comparisons of Host releases. |
374 | + |
375 | + Use in the form of |
376 | + |
377 | + if CompareHostReleases(release) > 'trusty': |
378 | + # do something with mitaka |
379 | + """ |
380 | + |
381 | + def __init__(self, item): |
382 | + raise NotImplementedError( |
383 | + "CompareHostReleases() is not implemented for CentOS") |
384 | + |
385 | |
386 | def service_available(service_name): |
387 | # """Determine whether a system service is available.""" |
388 | |
389 | === modified file 'charmhelpers/core/host_factory/ubuntu.py' |
390 | --- charmhelpers/core/host_factory/ubuntu.py 2016-08-11 15:47:44 +0000 |
391 | +++ charmhelpers/core/host_factory/ubuntu.py 2017-03-26 10:20:30 +0000 |
392 | @@ -1,5 +1,37 @@ |
393 | import subprocess |
394 | |
395 | +from charmhelpers.core.strutils import BasicStringComparator |
396 | + |
397 | + |
398 | +UBUNTU_RELEASES = ( |
399 | + 'lucid', |
400 | + 'maverick', |
401 | + 'natty', |
402 | + 'oneiric', |
403 | + 'precise', |
404 | + 'quantal', |
405 | + 'raring', |
406 | + 'saucy', |
407 | + 'trusty', |
408 | + 'utopic', |
409 | + 'vivid', |
410 | + 'wily', |
411 | + 'xenial', |
412 | + 'yakkety', |
413 | + 'zesty', |
414 | +) |
415 | + |
416 | + |
417 | +class CompareHostReleases(BasicStringComparator): |
418 | + """Provide comparisons of Ubuntu releases. |
419 | + |
420 | + Use in the form of |
421 | + |
422 | + if CompareHostReleases(release) > 'trusty': |
423 | + # do something with mitaka |
424 | + """ |
425 | + _list = UBUNTU_RELEASES |
426 | + |
427 | |
428 | def service_available(service_name): |
429 | """Determine whether a system service is available""" |
430 | |
431 | === modified file 'charmhelpers/core/strutils.py' |
432 | --- charmhelpers/core/strutils.py 2016-07-06 14:41:05 +0000 |
433 | +++ charmhelpers/core/strutils.py 2017-03-26 10:20:30 +0000 |
434 | @@ -68,3 +68,56 @@ |
435 | msg = "Unable to interpret string value '%s' as bytes" % (value) |
436 | raise ValueError(msg) |
437 | return int(matches.group(1)) * (1024 ** BYTE_POWER[matches.group(2)]) |
438 | + |
439 | + |
440 | +class BasicStringComparator(object): |
441 | + """Provides a class that will compare strings from an iterator type object. |
442 | + Used to provide > and < comparisons on strings that may not necessarily be |
443 | + alphanumerically ordered. e.g. OpenStack or Ubuntu releases AFTER the |
444 | + z-wrap. |
445 | + """ |
446 | + |
447 | + _list = None |
448 | + |
449 | + def __init__(self, item): |
450 | + if self._list is None: |
451 | + raise Exception("Must define the _list in the class definition!") |
452 | + try: |
453 | + self.index = self._list.index(item) |
454 | + except Exception: |
455 | + raise KeyError("Item '{}' is not in list '{}'" |
456 | + .format(item, self._list)) |
457 | + |
458 | + def __eq__(self, other): |
459 | + assert isinstance(other, str) or isinstance(other, self.__class__) |
460 | + return self.index == self._list.index(other) |
461 | + |
462 | + def __ne__(self, other): |
463 | + return not self.__eq__(other) |
464 | + |
465 | + def __lt__(self, other): |
466 | + assert isinstance(other, str) or isinstance(other, self.__class__) |
467 | + return self.index < self._list.index(other) |
468 | + |
469 | + def __ge__(self, other): |
470 | + return not self.__lt__(other) |
471 | + |
472 | + def __gt__(self, other): |
473 | + assert isinstance(other, str) or isinstance(other, self.__class__) |
474 | + return self.index > self._list.index(other) |
475 | + |
476 | + def __le__(self, other): |
477 | + return not self.__gt__(other) |
478 | + |
479 | + def __str__(self): |
480 | + """Always give back the item at the index so it can be used in |
481 | + comparisons like: |
482 | + |
483 | + s_mitaka = CompareOpenStack('mitaka') |
484 | + s_newton = CompareOpenstack('newton') |
485 | + |
486 | + assert s_newton > s_mitaka |
487 | + |
488 | + @returns: <string> |
489 | + """ |
490 | + return self._list[self.index] |
491 | |
492 | === modified file 'tests/contrib/openstack/test_os_utils.py' |
493 | --- tests/contrib/openstack/test_os_utils.py 2017-03-20 13:44:46 +0000 |
494 | +++ tests/contrib/openstack/test_os_utils.py 2017-03-26 10:20:30 +0000 |
495 | @@ -9,71 +9,10 @@ |
496 | def setUp(self): |
497 | super(UtilsTests, self).setUp() |
498 | |
499 | - def test_basic_string_comparator_class_fails_instantiation(self): |
500 | - try: |
501 | - utils.BasicStringComparator('hello') |
502 | - raise Exception("instantiating BasicStringComparator should fail") |
503 | - except Exception as e: |
504 | - assert (str(e) == "Must define the _list in the class definition!") |
505 | - |
506 | - def test_basic_string_comparator_class(self): |
507 | - |
508 | - class MyComparator(utils.BasicStringComparator): |
509 | - |
510 | - _list = ('zomg', 'bartlet', 'over', 'and') |
511 | - |
512 | - x = MyComparator('zomg') |
513 | - self.assertEquals(x.index, 0) |
514 | - y = MyComparator('over') |
515 | - self.assertEquals(y.index, 2) |
516 | - self.assertTrue(x == 'zomg') |
517 | - self.assertTrue(x != 'bartlet') |
518 | - self.assertTrue(x == x) |
519 | - self.assertTrue(x != y) |
520 | - self.assertTrue(x < y) |
521 | - self.assertTrue(y > x) |
522 | - self.assertTrue(x < 'bartlet') |
523 | - self.assertTrue(y > 'bartlet') |
524 | - self.assertTrue(x >= 'zomg') |
525 | - self.assertTrue(x <= 'zomg') |
526 | - self.assertTrue(x >= x) |
527 | - self.assertTrue(x <= x) |
528 | - self.assertTrue(y >= 'zomg') |
529 | - self.assertTrue(y <= 'over') |
530 | - self.assertTrue(y >= x) |
531 | - self.assertTrue(x <= y) |
532 | - # ensure that something not in the list dies |
533 | - try: |
534 | - MyComparator('nope') |
535 | - raise Exception("MyComparator('nope') should have failed") |
536 | - except Exception as e: |
537 | - self.assertTrue(isinstance(e, KeyError)) |
538 | - |
539 | - def test_basic_string_comparator_fails_different_comparators(self): |
540 | - |
541 | - class MyComparator1(utils.BasicStringComparator): |
542 | - |
543 | - _list = ('the truth is out there'.split(' ')) |
544 | - |
545 | - class MyComparator2(utils.BasicStringComparator): |
546 | - |
547 | - _list = ('no one in space can hear you scream'.split(' ')) |
548 | - |
549 | - x = MyComparator1('is') |
550 | - y = MyComparator2('you') |
551 | - try: |
552 | - x > y |
553 | - raise Exception("Comparing different comparators should fail") |
554 | - except Exception as e: |
555 | - self.assertTrue(isinstance(e, AssertionError)) |
556 | - |
557 | def test_compare_openstack_comparator(self): |
558 | self.assertTrue(utils.CompareOpenStackReleases('mitaka') < 'newton') |
559 | self.assertTrue(utils.CompareOpenStackReleases('pike') > 'essex') |
560 | |
561 | - def test_compare_ubuntu_releases(self): |
562 | - self.assertTrue(utils.CompareUbuntuReleases('yakkety') < 'zesty') |
563 | - |
564 | @mock.patch.object(utils, 'config') |
565 | @mock.patch('charmhelpers.contrib.openstack.utils.relation_set') |
566 | @mock.patch('charmhelpers.contrib.openstack.utils.relation_ids') |
567 | @@ -226,19 +165,19 @@ |
568 | _os_release): |
569 | # Check call with 'release' |
570 | self.assertFalse(utils.enable_memcache(release='icehouse')) |
571 | - self.assertTrue(utils.enable_memcache(release='zebra')) |
572 | + self.assertTrue(utils.enable_memcache(release='ocata')) |
573 | # Check call with 'source' |
574 | _os_release.return_value = None |
575 | _get_os_codename_install_source.return_value = 'icehouse' |
576 | self.assertFalse(utils.enable_memcache(source='distro')) |
577 | _os_release.return_value = None |
578 | - _get_os_codename_install_source.return_value = 'zebra' |
579 | + _get_os_codename_install_source.return_value = 'ocata' |
580 | self.assertTrue(utils.enable_memcache(source='distro')) |
581 | # Check call with 'package' |
582 | _os_release.return_value = 'icehouse' |
583 | _get_os_codename_install_source.return_value = None |
584 | self.assertFalse(utils.enable_memcache(package='pkg1')) |
585 | - _os_release.return_value = 'zebra' |
586 | + _os_release.return_value = 'ocata' |
587 | _get_os_codename_install_source.return_value = None |
588 | self.assertTrue(utils.enable_memcache(package='pkg1')) |
589 | |
590 | |
591 | === modified file 'tests/core/test_host.py' |
592 | --- tests/core/test_host.py 2017-03-08 06:54:54 +0000 |
593 | +++ tests/core/test_host.py 2017-03-26 10:20:30 +0000 |
594 | @@ -1814,4 +1814,13 @@ |
595 | |
596 | self.assertTrue(handle.read.call_count == 1) |
597 | self.assertTrue(handle.seek.call_count == 1) |
598 | - handle.write.assert_called_once_with('PRUNEPATHS="/tmp /srv/node /tmp/test"') |
599 | + handle.write.assert_called_once_with( |
600 | + 'PRUNEPATHS="/tmp /srv/node /tmp/test"') |
601 | + |
602 | + |
603 | +class TestHostCompator(TestCase): |
604 | + |
605 | + def test_compare_ubuntu_releases(self): |
606 | + from charmhelpers.osplatform import get_platform |
607 | + if get_platform() == 'ubuntu': |
608 | + self.assertTrue(host.CompareHostReleases('yakkety') < 'zesty') |
609 | |
610 | === modified file 'tests/core/test_strutils.py' |
611 | --- tests/core/test_strutils.py 2015-09-17 11:04:18 +0000 |
612 | +++ tests/core/test_strutils.py 2017-03-26 10:20:30 +0000 |
613 | @@ -47,3 +47,61 @@ |
614 | |
615 | self.assertRaises(ValueError, strutils.bytes_from_string, None) |
616 | self.assertRaises(ValueError, strutils.bytes_from_string, 'foo') |
617 | + |
618 | + def test_basic_string_comparator_class_fails_instantiation(self): |
619 | + try: |
620 | + strutils.BasicStringComparator('hello') |
621 | + raise Exception("instantiating BasicStringComparator should fail") |
622 | + except Exception as e: |
623 | + assert (str(e) == "Must define the _list in the class definition!") |
624 | + |
625 | + def test_basic_string_comparator_class(self): |
626 | + |
627 | + class MyComparator(strutils.BasicStringComparator): |
628 | + |
629 | + _list = ('zomg', 'bartlet', 'over', 'and') |
630 | + |
631 | + x = MyComparator('zomg') |
632 | + self.assertEquals(x.index, 0) |
633 | + y = MyComparator('over') |
634 | + self.assertEquals(y.index, 2) |
635 | + self.assertTrue(x == 'zomg') |
636 | + self.assertTrue(x != 'bartlet') |
637 | + self.assertTrue(x == x) |
638 | + self.assertTrue(x != y) |
639 | + self.assertTrue(x < y) |
640 | + self.assertTrue(y > x) |
641 | + self.assertTrue(x < 'bartlet') |
642 | + self.assertTrue(y > 'bartlet') |
643 | + self.assertTrue(x >= 'zomg') |
644 | + self.assertTrue(x <= 'zomg') |
645 | + self.assertTrue(x >= x) |
646 | + self.assertTrue(x <= x) |
647 | + self.assertTrue(y >= 'zomg') |
648 | + self.assertTrue(y <= 'over') |
649 | + self.assertTrue(y >= x) |
650 | + self.assertTrue(x <= y) |
651 | + # ensure that something not in the list dies |
652 | + try: |
653 | + MyComparator('nope') |
654 | + raise Exception("MyComparator('nope') should have failed") |
655 | + except Exception as e: |
656 | + self.assertTrue(isinstance(e, KeyError)) |
657 | + |
658 | + def test_basic_string_comparator_fails_different_comparators(self): |
659 | + |
660 | + class MyComparator1(strutils.BasicStringComparator): |
661 | + |
662 | + _list = ('the truth is out there'.split(' ')) |
663 | + |
664 | + class MyComparator2(strutils.BasicStringComparator): |
665 | + |
666 | + _list = ('no one in space can hear you scream'.split(' ')) |
667 | + |
668 | + x = MyComparator1('is') |
669 | + y = MyComparator2('you') |
670 | + try: |
671 | + x > y |
672 | + raise Exception("Comparing different comparators should fail") |
673 | + except Exception as e: |
674 | + self.assertTrue(isinstance(e, AssertionError)) |
One fix request (import) and a couple of clarifying questions in-line.