This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge "Add
versionutils
for version compatibility checks"
- Loading branch information
Showing
3 changed files
with
119 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# vim: tabstop=4 shiftwidth=4 softtabstop=4 | ||
|
||
# Copyright (c) 2013 OpenStack Foundation | ||
# All Rights Reserved. | ||
# | ||
# 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. | ||
|
||
""" | ||
Helpers for comparing version strings. | ||
""" | ||
|
||
import pkg_resources | ||
|
||
|
||
def is_compatible(requested_version, current_version, same_major=True): | ||
"""Determine whether `requested_version` is satisfied by | ||
`current_version`; in other words, `current_version` is >= | ||
`requested_version`. | ||
:param requested_version: version to check for compatibility | ||
:param current_version: version to check against | ||
:param same_major: if True, the major version must be identical between | ||
`requested_version` and `current_version`. This is used when a | ||
major-version difference indicates incompatibility between the two | ||
versions. Since this is the common-case in practice, the default is | ||
True. | ||
:returns: True if compatible, False if not | ||
""" | ||
requested_parts = pkg_resources.parse_version(requested_version) | ||
current_parts = pkg_resources.parse_version(current_version) | ||
|
||
if same_major and (requested_parts[0] != current_parts[0]): | ||
return False | ||
|
||
return current_parts >= requested_parts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# vim: tabstop=4 shiftwidth=4 softtabstop=4 | ||
|
||
# Copyright (c) 2013 OpenStack Foundation | ||
# All Rights Reserved. | ||
# | ||
# 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 openstack.common import test | ||
from openstack.common import versionutils | ||
|
||
|
||
class IsCompatibleTestCase(test.BaseTestCase): | ||
def test_same_version(self): | ||
self.assertTrue(versionutils.is_compatible('1', '1')) | ||
self.assertTrue(versionutils.is_compatible('1.0', '1.0')) | ||
self.assertTrue(versionutils.is_compatible('1.0.0', '1.0.0')) | ||
|
||
def test_requested_minor_greater(self): | ||
self.assertFalse(versionutils.is_compatible('1.1', '1.0')) | ||
|
||
def test_requested_minor_less_than(self): | ||
self.assertTrue(versionutils.is_compatible('1.0', '1.1')) | ||
|
||
def test_requested_patch_greater(self): | ||
self.assertFalse(versionutils.is_compatible('1.0.1', '1.0.0')) | ||
|
||
def test_requested_patch_less_than(self): | ||
self.assertTrue(versionutils.is_compatible('1.0.0', '1.0.1')) | ||
|
||
def test_requested_patch_not_present_same(self): | ||
self.assertTrue(versionutils.is_compatible('1.0', '1.0.0')) | ||
|
||
def test_requested_patch_not_present_less_than(self): | ||
self.assertTrue(versionutils.is_compatible('1.0', '1.0.1')) | ||
|
||
def test_current_patch_not_present_same(self): | ||
self.assertTrue(versionutils.is_compatible('1.0.0', '1.0')) | ||
|
||
def test_current_patch_not_present_less_than(self): | ||
self.assertFalse(versionutils.is_compatible('1.0.1', '1.0')) | ||
|
||
def test_same_major_true(self): | ||
"""Even though the current version is 2.0, since `same_major` defaults | ||
to `True`, 1.0 is deemed incompatible. | ||
""" | ||
self.assertFalse(versionutils.is_compatible('2.0', '1.0')) | ||
self.assertTrue(versionutils.is_compatible('1.0', '1.0')) | ||
self.assertFalse(versionutils.is_compatible('1.0', '2.0')) | ||
|
||
def test_same_major_false(self): | ||
"""With `same_major` set to False, then major version compatibiity | ||
rule is not enforced, so a current version of 2.0 is deemed to satisfy | ||
a requirement of 1.0. | ||
""" | ||
self.assertFalse(versionutils.is_compatible('2.0', '1.0', | ||
same_major=False)) | ||
self.assertTrue(versionutils.is_compatible('1.0', '1.0', | ||
same_major=False)) | ||
self.assertTrue(versionutils.is_compatible('1.0', '2.0', | ||
same_major=False)) |