Skip to content

Commit

Permalink
handle the disutil deprecation in test_actionhandler.py
Browse files Browse the repository at this point in the history
  • Loading branch information
feng-j678 committed Nov 25, 2024
1 parent 865334d commit 3269944
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
29 changes: 1 addition & 28 deletions src/extension/src/Utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,40 +69,13 @@ def get_datetime_from_str(date_str):
def get_str_from_datetime(date):
return date.strftime(Constants.UTC_DATETIME_FORMAT)

@staticmethod
def compare_versions(version_a, version_b):
"""Compare two versions:
returns:
-1 if version_a < version_b
0 if version_a == version_b
1 if version-a > version_b
"""

def normalize(v):
return [int(x) if x.isdigit() else x for x in v.split(".")]

a_parts = normalize(version_a)
b_parts = normalize(version_b)

for a,b in zip(a_parts, b_parts):
if a < b:
return -1
elif a > b:
return 1

# If all matched so far, the longer version is greater
return (len(a_parts) > len(b_parts)) - (len(a_parts) < len(b_parts))

@staticmethod
def extract_version(path):
"""
Extract the version part from a given path.
Example: /var/lib/waagent/Microsoft.CPlat.Core.LinuxPatchExtension-1.2.5/config -> 1.2.5
"""
match = re.search(r'-([\d]+\.[\d]+\.[\d]+)$', path)
if not match:
# Retry for cases where the path has a trailing folder or slash
match = re.search(r'-([\d]+\.[\d]+\.[\d]+)(?:/|$)', path)
match = re.search(r'-([\d]+\.[\d]+\.[\d]+)', path)
return match.group(1) if match else ""

@staticmethod
Expand Down
33 changes: 32 additions & 1 deletion src/extension/tests/Test_Utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,38 @@ def test_delete_file_failure(self):
# Remove the directory after the test
shutil.rmtree(test_dir)

def test_extract_version(self):
def test_extract_sorted_versions(self):
# Test extract version logic
self.assertEqual(self.utility.extract_version("Microsoft.CPlat.Core.LinuxPatchExtension-1.2.25"), "1.2.25")
self.assertEqual(self.utility.extract_version("Microsoft.CPlat.Core.LinuxPatchExtension-1.2.25-abc"), "1.2.25")
self.assertEqual(self.utility.extract_version("Microsoft.CPlat.Core.LinuxPatchExtension-1.2.25+abc.123"), "1.2.25")
self.assertEqual(self.utility.extract_version("Microsoft.CPlat.Core.LinuxPatchExtension-1.2.25-abc+def.123"),"1.2.25")
self.assertEqual(self.utility.extract_version("Microsoft.CPlat.Core.LinuxPatchExtension-1.21.1001"), "1.21.1001")
self.assertEqual(self.utility.extract_version("Microsoft.CPlat.Core.LinuxPatchExtension-1.6.100"), "1.6.100")
self.assertEqual(self.utility.extract_version("Microsoft.CPlat.Core.LinuxPatchExtension-1.6.99"), "1.6.99")
self.assertEqual(self.utility.extract_version("Microsoft.CPlat.Core.LinuxPatchExtension-1.6."), "")
self.assertEqual(self.utility.extract_version("Microsoft.CPlat.Core.LinuxPatchExtension-a.b.c"), "")

# Test sort versions logic
unsorted_path_versions = [
"Microsoft.CPlat.Core.LinuxPatchExtension-1.2.25-abc+def.123",
"Microsoft.CPlat.Core.LinuxPatchExtension-1.21.1001",
"Microsoft.CPlat.Core.LinuxPatchExtension-1.6.100",
"Microsoft.CPlat.Core.LinuxPatchExtension-1.6.99",
"Microsoft.CPlat.Core.LinuxPatchExtension-1.21.100",
"Microsoft.CPlat.Core.LinuxPatchExtension-1.2.25-abc",
]

expected_sorted_path_versions = [
"Microsoft.CPlat.Core.LinuxPatchExtension-1.21.1001",
"Microsoft.CPlat.Core.LinuxPatchExtension-1.21.100",
"Microsoft.CPlat.Core.LinuxPatchExtension-1.6.100",
"Microsoft.CPlat.Core.LinuxPatchExtension-1.6.99",
"Microsoft.CPlat.Core.LinuxPatchExtension-1.2.25-abc+def.123",
"Microsoft.CPlat.Core.LinuxPatchExtension-1.2.25-abc"
]

# valid versions
self.assertEqual(self.utility.sort_versions(unsorted_path_versions), expected_sorted_path_versions)


0 comments on commit 3269944

Please sign in to comment.