diff --git a/lib/ht.py b/lib/ht.py index 57dcdcaf2a005939c250ed9200945317bdde8be7..54b3ea07ee7af29cf05e9a080ba12b6fa55cac0a 100644 --- a/lib/ht.py +++ b/lib/ht.py @@ -178,6 +178,9 @@ TPositiveInt = TAnd(TInt, lambda v: v >= 0) #: a strictly positive integer TStrictPositiveInt = TAnd(TInt, lambda v: v > 0) +#: Number +TNumber = TOr(TInt, TFloat) + def TListOf(my_type): """Checks if a given value is a list with all elements of the same type. diff --git a/lib/opcodes.py b/lib/opcodes.py index b89444d3ead3c162c22eea4b43cd66f8cf3d9744..61f29e81a5bea98c3a23baf76b547b2f5f0a31e6 100644 --- a/lib/opcodes.py +++ b/lib/opcodes.py @@ -1206,7 +1206,7 @@ class OpTestDelay(OpCode): """ OP_DSC_FIELD = "duration" OP_PARAMS = [ - ("duration", ht.NoDefault, ht.TFloat), + ("duration", ht.NoDefault, ht.TNumber), ("on_master", True, ht.TBool), ("on_nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString)), ("repeat", 0, ht.TPositiveInt) diff --git a/lib/query.py b/lib/query.py index 1bb014d148e1ec5a5f07c068d56bbbc38e0b311d..84a8394ebeb41c493f6c6e9b0fa8575ff8ceaf9e 100644 --- a/lib/query.py +++ b/lib/query.py @@ -101,7 +101,7 @@ _VERIFY_FN = { QFT_BOOL: ht.TBool, QFT_NUMBER: ht.TInt, QFT_UNIT: ht.TInt, - QFT_TIMESTAMP: ht.TOr(ht.TInt, ht.TFloat), + QFT_TIMESTAMP: ht.TNumber, QFT_OTHER: lambda _: True, } diff --git a/test/ganeti.ht_unittest.py b/test/ganeti.ht_unittest.py index 34ae6715e0c0d49fcf1e14c2123d600e2637b75f..a2cdf3b04d706d529f339f7c09fd127539377bc7 100755 --- a/test/ganeti.ht_unittest.py +++ b/test/ganeti.ht_unittest.py @@ -53,6 +53,7 @@ class TestTypeChecks(unittest.TestCase): def testInt(self): for val in [-100, -3, 0, 16, 128, 923874]: self.assertTrue(ht.TInt(val)) + self.assertTrue(ht.TNumber(val)) for val in [False, True, None, "", [], "Hello", 0.0, 0.23, -3818.163]: self.assertFalse(ht.TInt(val)) @@ -76,10 +77,19 @@ class TestTypeChecks(unittest.TestCase): def testFloat(self): for val in [-100.21, -3.0, 0.0, 16.12, 128.3433, 923874.928]: self.assertTrue(ht.TFloat(val)) + self.assertTrue(ht.TNumber(val)) for val in [False, True, None, "", [], "Hello", 0, 28, -1, -3281]: self.assertFalse(ht.TFloat(val)) + def testNumber(self): + for val in [-100, -3, 0, 16, 128, 923874, + -100.21, -3.0, 0.0, 16.12, 128.3433, 923874.928]: + self.assertTrue(ht.TNumber(val)) + + for val in [False, True, None, "", [], "Hello", "1"]: + self.assertFalse(ht.TNumber(val)) + def testString(self): for val in ["", "abc", "Hello World", "123", u"", u"\u272C", u"abc"]: