Skip to content

Commit

Permalink
Use string length to cover corner case for decimal number count (Azur…
Browse files Browse the repository at this point in the history
…e#2223)

* Use string length to cover corner case

* lint style

* more than five 0 will be valid
  • Loading branch information
gtxistxgao authored Sep 10, 2020
1 parent ed68d69 commit 3c94965
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/aks-preview/azext_aks_preview/_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import os
import os.path
import re
from math import ceil, isnan, isclose
from math import isnan, isclose
from ipaddress import ip_network

from knack.log import get_logger
Expand Down Expand Up @@ -214,7 +214,7 @@ def validate_spot_max_price(namespace):
if not isnan(namespace.spot_max_price):
if namespace.priority != "Spot":
raise CLIError("--spot_max_price can only be set when --priority is Spot")
if namespace.spot_max_price > 0 and not isclose(namespace.spot_max_price * 100000 % 1, 0, rel_tol=1e-06):
if len(str(namespace.spot_max_price).split(".")) > 1 and len(str(namespace.spot_max_price).split(".")[1]) > 5:
raise CLIError("--spot_max_price can only include up to 5 decimal places")
if namespace.spot_max_price <= 0 and not isclose(namespace.spot_max_price, -1.0, rel_tol=1e-06):
raise CLIError(
Expand Down Expand Up @@ -409,4 +409,4 @@ def validate_addons(namespace):
f"The addon \"{addon_arg}\" is not a recognized addon option. Possible options: {all_addons}")

raise CLIError(
f"The addon \"{addon_arg}\" is not a recognized addon option. Did you mean {matches}? Possible options: {all_addons}")
f"The addon \"{addon_arg}\" is not a recognized addon option. Did you mean {matches}? Possible options: {all_addons}") # pylint:disable=line-too-long
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def test_throws_on_negative(self):

class TestSpotMaxPrice(unittest.TestCase):
def test_valid_cases(self):
valid = [5, 5.12345, -1.0]
valid = [5, 5.12345, -1.0, 0.068, 0.071, 5.00000000]
for v in valid:
validators.validate_spot_max_price(SpotMaxPriceNamespace(v))

Expand All @@ -192,6 +192,9 @@ def test_throws_if_non_valid_negative(self):
with self.assertRaises(CLIError) as cm:
validators.validate_spot_max_price(SpotMaxPriceNamespace(-2))
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))
with self.assertRaises(CLIError) as cm:
validators.validate_spot_max_price(SpotMaxPriceNamespace(0))
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))

def test_throws_if_input_max_price_for_regular(self):
ns = SpotMaxPriceNamespace(2)
Expand Down

0 comments on commit 3c94965

Please sign in to comment.