|
13 | 13 | # under the License.
|
14 | 14 |
|
15 | 15 | import unittest
|
| 16 | +from decimal import Decimal |
16 | 17 | from os import path
|
17 | 18 |
|
18 | 19 | import yaml
|
19 | 20 |
|
20 |
| -from kubernetes import utils, client |
| 21 | +from kubernetes import client, utils |
21 | 22 | from kubernetes.client.rest import ApiException
|
22 | 23 | from kubernetes.e2e_test import base
|
| 24 | +from kubernetes.utils import quantity |
23 | 25 |
|
24 | 26 |
|
25 | 27 | class TestUtils(unittest.TestCase):
|
@@ -605,3 +607,70 @@ def test_create_from_list_in_multi_resource_yaml_namespaced(self):
|
605 | 607 | name="mock-pod-1", namespace=self.test_namespace, body={})
|
606 | 608 | app_api.delete_namespaced_deployment(
|
607 | 609 | name="mock", namespace=self.test_namespace, body={})
|
| 610 | + |
| 611 | + |
| 612 | +class TestUtilsUnitTests(unittest.TestCase): |
| 613 | + |
| 614 | + def test_format_quantity(self): |
| 615 | + """Unit test for quantity.format_quantity. Testing the different SI suffixes and |
| 616 | + function should return the expected string""" |
| 617 | + |
| 618 | + # == unknown suffixes == |
| 619 | + self.assertRaises( |
| 620 | + ValueError, lambda: quantity.format_quantity(Decimal(1_000), "kb") |
| 621 | + ) |
| 622 | + self.assertRaises( |
| 623 | + ValueError, lambda: quantity.format_quantity(Decimal(1_000), "ki") |
| 624 | + ) |
| 625 | + self.assertRaises( |
| 626 | + ValueError, lambda: quantity.format_quantity(Decimal(1_000), "foo") |
| 627 | + ) |
| 628 | + |
| 629 | + # == no suffix == |
| 630 | + self.assertEqual(quantity.format_quantity(Decimal(1_000), ""), "1000") |
| 631 | + self.assertEqual(quantity.format_quantity(Decimal(1_000), None), "1000") |
| 632 | + |
| 633 | + # == base 1024 == |
| 634 | + self.assertEqual(quantity.format_quantity(Decimal(1024), "Ki"), "1Ki") |
| 635 | + self.assertEqual(quantity.format_quantity(Decimal(1024**2), "Mi"), "1Mi") |
| 636 | + self.assertEqual(quantity.format_quantity(Decimal(1024**3), "Gi"), "1Gi") |
| 637 | + self.assertEqual(quantity.format_quantity(Decimal(1024**4), "Ti"), "1Ti") |
| 638 | + self.assertEqual(quantity.format_quantity(Decimal(1024**5), "Pi"), "1Pi") |
| 639 | + self.assertEqual(quantity.format_quantity(Decimal(1024**6), "Ei"), "1Ei") |
| 640 | + self.assertEqual(quantity.format_quantity(Decimal(1024**2), "Ki"), "1024Ki") |
| 641 | + self.assertEqual(quantity.format_quantity(Decimal((1024**3) / 2), "Gi"), "0.5Gi") |
| 642 | + # Decimal((1024**3)/3) are 0.3333333333333333148296162562Gi; expecting to |
| 643 | + # be quantized to 0.3Gi |
| 644 | + self.assertEqual( |
| 645 | + quantity.format_quantity( |
| 646 | + Decimal( |
| 647 | + (1024**3) / 3), |
| 648 | + "Gi", |
| 649 | + quantize=Decimal(.5)), |
| 650 | + "0.3Gi") |
| 651 | + |
| 652 | + # == base 1000 == |
| 653 | + self.assertEqual(quantity.format_quantity(Decimal(0.000_000_001), "n"), "1n") |
| 654 | + self.assertEqual(quantity.format_quantity(Decimal(0.000_001), "u"), "1u") |
| 655 | + self.assertEqual(quantity.format_quantity(Decimal(0.001), "m"), "1m") |
| 656 | + self.assertEqual(quantity.format_quantity(Decimal(1_000), "k"), "1k") |
| 657 | + self.assertEqual(quantity.format_quantity(Decimal(1_000_000), "M"), "1M") |
| 658 | + self.assertEqual(quantity.format_quantity(Decimal(1_000_000_000), "G"), "1G") |
| 659 | + self.assertEqual( |
| 660 | + quantity.format_quantity(Decimal(1_000_000_000_000), "T"), "1T" |
| 661 | + ) |
| 662 | + self.assertEqual( |
| 663 | + quantity.format_quantity(Decimal(1_000_000_000_000_000), "P"), "1P" |
| 664 | + ) |
| 665 | + self.assertEqual( |
| 666 | + quantity.format_quantity(Decimal(1_000_000_000_000_000_000), "E"), "1E" |
| 667 | + ) |
| 668 | + self.assertEqual(quantity.format_quantity(Decimal(1_000_000), "k"), "1000k") |
| 669 | + # Decimal(1_000_000/3) are 333.3333333333333139307796955k; expecting to |
| 670 | + # be quantized to 333k |
| 671 | + self.assertEqual( |
| 672 | + quantity.format_quantity( |
| 673 | + Decimal(1_000_000 / 3), "k", quantize=Decimal(1000) |
| 674 | + ), |
| 675 | + "333k", |
| 676 | + ) |
0 commit comments