From 3269944f9c662e6cb2cba90747e1ae91f07e80f7 Mon Sep 17 00:00:00 2001 From: johnfeng Date: Sun, 24 Nov 2024 20:49:13 -0800 Subject: [PATCH] handle the disutil deprecation in test_actionhandler.py --- src/extension/src/Utility.py | 29 +------------------------ src/extension/tests/Test_Utility.py | 33 ++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 29 deletions(-) diff --git a/src/extension/src/Utility.py b/src/extension/src/Utility.py index 93107864..d34cf4d9 100644 --- a/src/extension/src/Utility.py +++ b/src/extension/src/Utility.py @@ -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 diff --git a/src/extension/tests/Test_Utility.py b/src/extension/tests/Test_Utility.py index 0c50e625..4a318289 100644 --- a/src/extension/tests/Test_Utility.py +++ b/src/extension/tests/Test_Utility.py @@ -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) +