From e4dbb169890792b6cabbd89a56ccd01a1cf1c34d Mon Sep 17 00:00:00 2001 From: Utkarsh Singh Date: Mon, 13 Nov 2023 20:07:07 +0000 Subject: [PATCH 1/3] Updated salt/modules/disk.py to fix postPrefixes by a factor of 10. postPrefixes renamed to unit_suffixes as per themowski's suggestions. --- salt/modules/disk.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/salt/modules/disk.py b/salt/modules/disk.py index fb78906b18fa..a35239b4becd 100644 --- a/salt/modules/disk.py +++ b/salt/modules/disk.py @@ -39,19 +39,19 @@ def _parse_numbers(text): return decimal.Decimal(text) try: - postPrefixes = { - "K": "10E3", - "M": "10E6", - "G": "10E9", - "T": "10E12", - "P": "10E15", - "E": "10E18", - "Z": "10E21", - "Y": "10E24", + unit_suffixes = { + "K": "1E3", + "M": "1E6", + "G": "1E9", + "T": "1E12", + "P": "1E15", + "E": "1E18", + "Z": "1E21", + "Y": "1E24", } - if text[-1] in postPrefixes.keys(): + if text[-1] in unit_suffixes.keys(): v = decimal.Decimal(text[:-1]) - v = v * decimal.Decimal(postPrefixes[text[-1]]) + v = v * decimal.Decimal(unit_suffixes[text[-1]]) return v else: return decimal.Decimal(text) From 105b512c93f61e863cde5f45a797b9d0bc41575d Mon Sep 17 00:00:00 2001 From: Utkarsh Singh Date: Sat, 2 Dec 2023 18:44:31 -0600 Subject: [PATCH 2/3] Updated unit test with test_disk.py for disk.py changes. --- tests/unit/modules/test_disk.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 tests/unit/modules/test_disk.py diff --git a/tests/unit/modules/test_disk.py b/tests/unit/modules/test_disk.py new file mode 100644 index 000000000000..31a547f3e2fb --- /dev/null +++ b/tests/unit/modules/test_disk.py @@ -0,0 +1,21 @@ +import unittest +from your_module import _parse_numbers + +class TestParseNumbers(unittest.TestCase): + + def test_integer(self): + result = _parse_numbers("42") + self.assertEqual(result, decimal.Decimal("42")) + + def test_with_kilo(self): + result = _parse_numbers("1.5K") + self.assertEqual(result, decimal.Decimal("1500")) + + def test_with_mega(self): + result = _parse_numbers("2.5M") + self.assertEqual(result, decimal.Decimal("2500000")) + + # Add more test cases for other units and scenarios + +if __name__ == '__main__': + unittest.main() From ecb41d99b583dec71ad477efc31cbd756115d3ae Mon Sep 17 00:00:00 2001 From: Utkarsh Singh Date: Sat, 2 Dec 2023 18:51:04 -0600 Subject: [PATCH 3/3] Updated test_disk.py --- tests/unit/modules/test_disk.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/modules/test_disk.py b/tests/unit/modules/test_disk.py index 31a547f3e2fb..7fbef3a84b0f 100644 --- a/tests/unit/modules/test_disk.py +++ b/tests/unit/modules/test_disk.py @@ -1,5 +1,5 @@ import unittest -from your_module import _parse_numbers +from salt.modules.disk import _parse_numbers class TestParseNumbers(unittest.TestCase):