diff --git a/nova/api/openstack/api_version_request.py b/nova/api/openstack/api_version_request.py index ef5d0760cdb0..450b2717fd86 100644 --- a/nova/api/openstack/api_version_request.py +++ b/nova/api/openstack/api_version_request.py @@ -15,6 +15,7 @@ import re from nova import exception +from nova.i18n import _ # Define the minimum and maximum version of the API across all of the # REST API. The format of the version is: @@ -108,16 +109,40 @@ class APIVersionRequest(object): def is_null(self): return self.ver_major == 0 and self.ver_minor == 0 - def __cmp__(self, other): - if not isinstance(other, APIVersionRequest): - raise TypeError - return cmp((self.ver_major, self.ver_minor), - (other.ver_major, other.ver_minor)) + def _format_type_error(self, other): + return TypeError(_("'%(other)s' should be an instance of '%(cls)s'") % + {"other": other, "cls": self.__class__}) def __lt__(self, other): + if not isinstance(other, APIVersionRequest): + raise self._format_type_error(other) + return ((self.ver_major, self.ver_minor) < (other.ver_major, other.ver_minor)) + def __eq__(self, other): + if not isinstance(other, APIVersionRequest): + raise self._format_type_error(other) + + return ((self.ver_major, self.ver_minor) == + (other.ver_major, other.ver_minor)) + + def __gt__(self, other): + if not isinstance(other, APIVersionRequest): + raise self._format_type_error(other) + + return ((self.ver_major, self.ver_minor) > + (other.ver_major, other.ver_minor)) + + def __le__(self, other): + return self < other or self == other + + def __ne__(self, other): + return not self.__eq__(other) + + def __ge__(self, other): + return self > other or self == other + def matches(self, min_version, max_version): """Returns whether the version object represents a version greater than or equal to the minimum version and less than diff --git a/nova/tests/unit/api/openstack/test_api_version_request.py b/nova/tests/unit/api/openstack/test_api_version_request.py index 8850eb5eaa1e..519ed97165fd 100644 --- a/nova/tests/unit/api/openstack/test_api_version_request.py +++ b/nova/tests/unit/api/openstack/test_api_version_request.py @@ -79,13 +79,19 @@ class APIVersionRequestTests(test.NoDBTestCase): v4 = api_version_request.APIVersionRequest("2.0") v_null = api_version_request.APIVersionRequest() + self.assertTrue(v_null < v2) self.assertTrue(v1 < v2) + self.assertTrue(v1 <= v2) + self.assertTrue(v1 <= v4) + self.assertTrue(v2 > v_null) self.assertTrue(v3 > v2) + self.assertTrue(v1 >= v4) + self.assertTrue(v3 >= v2) self.assertTrue(v1 != v2) self.assertTrue(v1 == v4) self.assertTrue(v1 != v_null) self.assertTrue(v_null == v_null) - self.assertRaises(TypeError, v1.__cmp__, "2.1") + self.assertRaises(TypeError, v1.__lt__, "2.1") def test_version_matches(self): v1 = api_version_request.APIVersionRequest("2.0") diff --git a/tox.ini b/tox.ini index b831f97ff34f..c14226729478 100644 --- a/tox.ini +++ b/tox.ini @@ -37,6 +37,7 @@ commands = find . -type f -name "*.pyc" -delete python -m subunit.run discover -t . ./nova/tests/ --list python -m testtools.run \ + nova.tests.unit.api.openstack.test_api_version_request \ nova.tests.unit.compute.test_keypairs \ nova.tests.unit.db.test_db_api \ nova.tests.unit.scheduler.filters.test_affinity_filters \