|
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):
|
@@ -563,3 +565,70 @@ def test_create_from_list_in_multi_resource_yaml_namespaced(self):
|
563 | 565 | name="mock-pod-1", namespace=self.test_namespace, body={})
|
564 | 566 | app_api.delete_namespaced_deployment(
|
565 | 567 | name="mock", namespace=self.test_namespace, body={})
|
| 568 | + |
| 569 | + |
| 570 | +class TestUtilsUnitTests(unittest.TestCase): |
| 571 | + |
| 572 | + def test_format_quantity(self): |
| 573 | + """Unit test for quantity.format_quantity. Testing the different SI suffixes and |
| 574 | + function should return the expected string""" |
| 575 | + |
| 576 | + # == unknown suffixes == |
| 577 | + self.assertRaises( |
| 578 | + ValueError, lambda: quantity.format_quantity(Decimal(1_000), "kb") |
| 579 | + ) |
| 580 | + self.assertRaises( |
| 581 | + ValueError, lambda: quantity.format_quantity(Decimal(1_000), "ki") |
| 582 | + ) |
| 583 | + self.assertRaises( |
| 584 | + ValueError, lambda: quantity.format_quantity(Decimal(1_000), "foo") |
| 585 | + ) |
| 586 | + |
| 587 | + # == no suffix == |
| 588 | + self.assertEqual(quantity.format_quantity(Decimal(1_000), ""), "1000") |
| 589 | + self.assertEqual(quantity.format_quantity(Decimal(1_000), None), "1000") |
| 590 | + |
| 591 | + # == base 1024 == |
| 592 | + self.assertEqual(quantity.format_quantity(Decimal(1024), "Ki"), "1Ki") |
| 593 | + self.assertEqual(quantity.format_quantity(Decimal(1024**2), "Mi"), "1Mi") |
| 594 | + self.assertEqual(quantity.format_quantity(Decimal(1024**3), "Gi"), "1Gi") |
| 595 | + self.assertEqual(quantity.format_quantity(Decimal(1024**4), "Ti"), "1Ti") |
| 596 | + self.assertEqual(quantity.format_quantity(Decimal(1024**5), "Pi"), "1Pi") |
| 597 | + self.assertEqual(quantity.format_quantity(Decimal(1024**6), "Ei"), "1Ei") |
| 598 | + self.assertEqual(quantity.format_quantity(Decimal(1024**2), "Ki"), "1024Ki") |
| 599 | + self.assertEqual(quantity.format_quantity(Decimal((1024**3) / 2), "Gi"), "0.5Gi") |
| 600 | + # Decimal((1024**3)/3) are 0.3333333333333333148296162562Gi; expecting to |
| 601 | + # be quantized to 0.3Gi |
| 602 | + self.assertEqual( |
| 603 | + quantity.format_quantity( |
| 604 | + Decimal( |
| 605 | + (1024**3) / 3), |
| 606 | + "Gi", |
| 607 | + quantize=Decimal(.5)), |
| 608 | + "0.3Gi") |
| 609 | + |
| 610 | + # == base 1000 == |
| 611 | + self.assertEqual(quantity.format_quantity(Decimal(0.000_000_001), "n"), "1n") |
| 612 | + self.assertEqual(quantity.format_quantity(Decimal(0.000_001), "u"), "1u") |
| 613 | + self.assertEqual(quantity.format_quantity(Decimal(0.001), "m"), "1m") |
| 614 | + self.assertEqual(quantity.format_quantity(Decimal(1_000), "k"), "1k") |
| 615 | + self.assertEqual(quantity.format_quantity(Decimal(1_000_000), "M"), "1M") |
| 616 | + self.assertEqual(quantity.format_quantity(Decimal(1_000_000_000), "G"), "1G") |
| 617 | + self.assertEqual( |
| 618 | + quantity.format_quantity(Decimal(1_000_000_000_000), "T"), "1T" |
| 619 | + ) |
| 620 | + self.assertEqual( |
| 621 | + quantity.format_quantity(Decimal(1_000_000_000_000_000), "P"), "1P" |
| 622 | + ) |
| 623 | + self.assertEqual( |
| 624 | + quantity.format_quantity(Decimal(1_000_000_000_000_000_000), "E"), "1E" |
| 625 | + ) |
| 626 | + self.assertEqual(quantity.format_quantity(Decimal(1_000_000), "k"), "1000k") |
| 627 | + # Decimal(1_000_000/3) are 333.3333333333333139307796955k; expecting to |
| 628 | + # be quantized to 333k |
| 629 | + self.assertEqual( |
| 630 | + quantity.format_quantity( |
| 631 | + Decimal(1_000_000 / 3), "k", quantize=Decimal(1000) |
| 632 | + ), |
| 633 | + "333k", |
| 634 | + ) |
0 commit comments