summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--glusternagios/utils.py17
-rw-r--r--tests/utilsTests.py18
2 files changed, 35 insertions, 0 deletions
diff --git a/glusternagios/utils.py b/glusternagios/utils.py
index 44c4727..18acb77 100644
--- a/glusternagios/utils.py
+++ b/glusternagios/utils.py
@@ -486,3 +486,20 @@ def xml2dict(tree):
else:
d[tree.tag] = text
return d
+
+
+def convertSize(val, unitFrom, unitTo):
+ ''' Convert size from one unit to another
+ For example, KB to MB
+ '''
+ units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
+ unitFromIndex = units.index(unitFrom)
+ unitToIndex = units.index(unitTo)
+
+ if unitFromIndex < unitToIndex:
+ convFactor = 1 << (unitToIndex - unitFromIndex) * 10
+ return float(val) / convFactor
+ if unitFromIndex > unitToIndex:
+ convFactor = 1 << (unitFromIndex - unitToIndex) * 10
+ return float(val) * convFactor
+ return val
diff --git a/tests/utilsTests.py b/tests/utilsTests.py
index 69da659..fdf1ec3 100644
--- a/tests/utilsTests.py
+++ b/tests/utilsTests.py
@@ -146,3 +146,21 @@ class xml2dictTests(TestCaseBase):
utils.xml2dict("not an etree object")
self.assertRaises(AttributeError, _xml2dict)
+
+
+class convertsizeTests(TestCaseBase):
+ def testBytesToMB(self):
+ retVal = utils.convertSize(778999, 'B', 'MB')
+ assert (round(retVal, 2) == 0.74)
+
+ def testPBtoMB(self):
+ retVal = utils.convertSize(8, 'PB', 'MB')
+ assert (round(retVal, 0) == 8589934592)
+
+ def testInvalidInput(self):
+ with self.assertRaises(ValueError):
+ utils.convertSize(89, 'P', 'M')
+
+ def testMBToMB(self):
+ retVal = utils.convertSize(900, 'MB', 'MB')
+ assert (retVal == 900)