Skip to content

Commit 0009d7e

Browse files
authored
Should not compare float number with 0 (Azure#2213)
* cannot compare float number with 0 * bump version * add test * update history * don't touch unrelated file * add more tests * don't publish then
1 parent b8ce15f commit 0009d7e

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/aks-preview/azext_aks_preview/_validators.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
from ._consts import ADDONS
2424

25-
2625
logger = get_logger(__name__)
2726

2827

@@ -215,7 +214,7 @@ def validate_spot_max_price(namespace):
215214
if not isnan(namespace.spot_max_price):
216215
if namespace.priority != "Spot":
217216
raise CLIError("--spot_max_price can only be set when --priority is Spot")
218-
if namespace.spot_max_price * 100000 % 1 != 0:
217+
if namespace.spot_max_price > 0 and not isclose(namespace.spot_max_price * 100000 % 1, 0, rel_tol=1e-06):
219218
raise CLIError("--spot_max_price can only include up to 5 decimal places")
220219
if namespace.spot_max_price <= 0 and not isclose(namespace.spot_max_price, -1.0, rel_tol=1e-06):
221220
raise CLIError(

src/aks-preview/azext_aks_preview/tests/latest/test_validators.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,12 @@ def __init__(self, max_surge):
154154
self.max_surge = max_surge
155155

156156

157+
class SpotMaxPriceNamespace:
158+
def __init__(self, spot_max_price):
159+
self.priority = "Spot"
160+
self.spot_max_price = spot_max_price
161+
162+
157163
class TestMaxSurge(unittest.TestCase):
158164
def test_valid_cases(self):
159165
valid = ["5", "33%", "1", "100%"]
@@ -171,6 +177,30 @@ def test_throws_on_negative(self):
171177
self.assertTrue('positive' in str(cm.exception), msg=str(cm.exception))
172178

173179

180+
class TestSpotMaxPrice(unittest.TestCase):
181+
def test_valid_cases(self):
182+
valid = [5, 5.12345, -1.0]
183+
for v in valid:
184+
validators.validate_spot_max_price(SpotMaxPriceNamespace(v))
185+
186+
def test_throws_if_more_than_5(self):
187+
with self.assertRaises(CLIError) as cm:
188+
validators.validate_spot_max_price(SpotMaxPriceNamespace(5.123456))
189+
self.assertTrue('--spot_max_price can only include up to 5 decimal places' in str(cm.exception), msg=str(cm.exception))
190+
191+
def test_throws_if_non_valid_negative(self):
192+
with self.assertRaises(CLIError) as cm:
193+
validators.validate_spot_max_price(SpotMaxPriceNamespace(-2))
194+
self.assertTrue('--spot_max_price can only be any decimal value greater than zero, or -1 which indicates' in str(cm.exception), msg=str(cm.exception))
195+
196+
def test_throws_if_input_max_price_for_regular(self):
197+
ns = SpotMaxPriceNamespace(2)
198+
ns.priority = "Regular"
199+
with self.assertRaises(CLIError) as cm:
200+
validators.validate_spot_max_price(ns)
201+
self.assertTrue('--spot_max_price can only be set when --priority is Spot' in str(cm.exception), msg=str(cm.exception))
202+
203+
174204
class ValidateAddonsNamespace:
175205
def __init__(self, addons):
176206
self.addons = addons

0 commit comments

Comments
 (0)