Skip to content

Commit 0e02387

Browse files
committed
[patch] add version verification functions
1 parent 01be81a commit 0e02387

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ def get_version(rel_path):
6060
'kubernetes', # Apache Software License
6161
'kubeconfig', # BSD License
6262
'jinja2', # BSD License
63-
'jinja2-base64-filters' # MIT License
63+
'jinja2-base64-filters', # MIT License
64+
'semver' # BSD License
6465
],
6566
extras_require={
6667
'dev': [

src/mas/devops/mas.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from openshift.dynamic import DynamicClient
1919
from openshift.dynamic.exceptions import NotFoundError, ResourceNotFoundError, UnauthorizedError
2020
from jinja2 import Environment, FileSystemLoader
21+
import semver
2122

2223
from .ocp import getStorageClasses
2324
from .olm import getSubscription
@@ -305,3 +306,41 @@ def patchPendingPVC(dynClient: DynamicClient, namespace: str, pvcName: str, stor
305306
except NotFoundError:
306307
logger.error(f"PVC {pvcName} does not exist")
307308
return False
309+
310+
311+
def isVersionBefore(_compare_to_version, _current_version):
312+
"""
313+
The method does a modified semantic version comparison,
314+
as we want to treat any pre-release as == to the real release
315+
but in strict semantic versioning it is <
316+
ie. '8.6.0-pre.m1dev86' is converted to '8.6.0'
317+
"""
318+
if _current_version is None:
319+
print("Version is not informed. Returning False")
320+
return False
321+
322+
strippedVersion = _current_version.split("-")[0]
323+
if '.x' in strippedVersion:
324+
strippedVersion = strippedVersion.replace('.x', '.0')
325+
current_version = semver.VersionInfo.parse(strippedVersion)
326+
compareToVersion = semver.VersionInfo.parse(_compare_to_version)
327+
return current_version.compare(compareToVersion) < 0
328+
329+
330+
def isVersionAfter(_compare_to_version, _current_version):
331+
"""
332+
The method does a modified semantic version comparison,
333+
as we want to treat any pre-release as == to the real release
334+
but in strict semantic versioning it is <
335+
ie. '8.6.0-pre.m1dev86' is converted to '8.6.0'
336+
"""
337+
if _current_version is None:
338+
print("Version is not informed. Returning False")
339+
return False
340+
341+
strippedVersion = _current_version.split("-")[0]
342+
if '.x' in strippedVersion:
343+
strippedVersion = strippedVersion.replace('.x', '.0')
344+
current_version = semver.VersionInfo.parse(strippedVersion)
345+
compareToVersion = semver.VersionInfo.parse(_compare_to_version)
346+
return current_version.compare(compareToVersion) >= 0

test/src/test_mas.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,18 @@ def test_is_airgap_install():
6262
# The cluster we are using to test with does not have the MAS ICSP or IDMS installed
6363
assert mas.isAirgapInstall(dynClient) is False
6464
assert mas.isAirgapInstall(dynClient, checkICSP=False) is False
65+
66+
def test_version_before():
67+
assert mas.isVersionBefore('9.1.0','9.1.x-feature') is False
68+
assert mas.isVersionBefore('9.1.0','9.0.0') is True
69+
assert mas.isVersionBefore('8.11.1','9.1.0') is False
70+
assert mas.isVersionBefore('9.1.0','9.1.x-stable') is False
71+
72+
def test_version_before():
73+
assert mas.isVersionAfter('9.1.0','9.2.x-feature') is True
74+
assert mas.isVersionAfter('9.1.0','9.0.0') is False
75+
assert mas.isVersionAfter('8.11.1','9.1.0') is True
76+
assert mas.isVersionAfter('9.2.0','9.1.x-stable') is False
77+
78+
if __name__ == '__main__':
79+
test_version_before()

0 commit comments

Comments
 (0)