Cherry pick cl/214534911
Rework CompareMachineSize so that it will return -1 if any element is
smaller, it will return 0 if all are equal, will return 1 if all are
equal or greater.
Bug: None
Test: m acloud_test && acloud_test
Change-Id: Icc07b0c52c1218e33a71a512bf70b2304a7e168a
diff --git a/internal/lib/gcompute_client.py b/internal/lib/gcompute_client.py
index 451cc7f..c645edc 100755
--- a/internal/lib/gcompute_client.py
+++ b/internal/lib/gcompute_client.py
@@ -1240,25 +1240,30 @@
zone: A string representing a zone, e.g. "us-central1-f"
Returns:
- 1 if size of the first type is greater than the second type.
- 2 if size of the first type is smaller than the second type.
- 0 if they are equal.
+ -1 if any metric of machine size of the first type is smaller than
+ the second type.
+ 0 if all metrics of machine size are equal.
+ 1 if at least one metric of machine size of the first type is
+ greater than the second type and all metrics of first type are
+ greater or equal to the second type.
Raises:
errors.DriverError: For malformed response.
"""
machine_info_1 = self.GetMachineType(machine_type_1, zone)
machine_info_2 = self.GetMachineType(machine_type_2, zone)
+ result = 0
for metric in self.MACHINE_SIZE_METRICS:
if metric not in machine_info_1 or metric not in machine_info_2:
raise errors.DriverError(
"Malformed machine size record: Can't find '%s' in %s or %s"
% (metric, machine_info_1, machine_info_2))
- if machine_info_1[metric] - machine_info_2[metric] > 0:
- return 1
- elif machine_info_1[metric] - machine_info_2[metric] < 0:
+ cmp_result = machine_info_1[metric] - machine_info_2[metric]
+ if cmp_result < 0:
return -1
- return 0
+ elif cmp_result > 0:
+ result = 1
+ return result
def GetSerialPortOutput(self, instance, zone, port=1):
"""Get serial port output.
diff --git a/internal/lib/gcompute_client_test.py b/internal/lib/gcompute_client_test.py
index 11a49a9..dc9a44f 100644
--- a/internal/lib/gcompute_client_test.py
+++ b/internal/lib/gcompute_client_test.py
@@ -910,8 +910,20 @@
machine_info_2 = {"guestCpus": 10, "memoryMb": 200}
self._CompareMachineSizeTestHelper(machine_info_1, machine_info_2, -1)
+ def testCompareMachineSizeSmallSmallerOnSecond(self):
+ """Test CompareMachineSize where the first one is smaller."""
+ machine_info_1 = {"guestCpus": 11, "memoryMb": 100}
+ machine_info_2 = {"guestCpus": 10, "memoryMb": 200}
+ self._CompareMachineSizeTestHelper(machine_info_1, machine_info_2, -1)
+
def testCompareMachineSizeLarge(self):
"""Test CompareMachineSize where the first one is larger."""
+ machine_info_1 = {"guestCpus": 11, "memoryMb": 200}
+ machine_info_2 = {"guestCpus": 10, "memoryMb": 100}
+ self._CompareMachineSizeTestHelper(machine_info_1, machine_info_2, 1)
+
+ def testCompareMachineSizeLargeWithEqualElement(self):
+ """Test CompareMachineSize where the first one is larger."""
machine_info_1 = {"guestCpus": 10, "memoryMb": 200}
machine_info_2 = {"guestCpus": 10, "memoryMb": 100}
self._CompareMachineSizeTestHelper(machine_info_1, machine_info_2, 1)