Skip to content

Commit

Permalink
Add XiBytes units
Browse files Browse the repository at this point in the history
  • Loading branch information
micafer committed Nov 27, 2024
1 parent 603a92a commit 60f7875
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 8 deletions.
3 changes: 3 additions & 0 deletions changelog
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,6 @@ RADL 1.33
* Minor internal code changes.
* Add remove_net_interface function.

RADL 1.34

* Add XiBytes units
2 changes: 1 addition & 1 deletion radl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@


__all__ = ['radl_parse', 'radl_json', 'radl']
__version__ = '1.3.3'
__version__ = '1.3.4'
21 changes: 17 additions & 4 deletions radl/radl.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,25 @@ def UnitToValue(unit):

if not unit:
return 1
unit = unit[0].upper()
unit = unit.upper()
if unit.endswith("B"):
unit = unit[:-1]
if unit == "K":
return 1000
elif unit == "KI":
return 1024
if unit == "M":
elif unit == "M":
return 1000 * 1000
elif unit == "MI":
return 1024 * 1024
if unit == "G":
elif unit == "G":
return 1000 * 1000 * 1000
elif unit == "GI":
return 1024 * 1024 * 1024
elif unit == "T":
return 1000 * 1000 * 1000 * 1000
elif unit == "TI":
return 1024 * 1024 * 1024 * 1024
return 1


Expand Down Expand Up @@ -1045,7 +1057,8 @@ def check_ansible_host(f, radl0):
return False
return True

mem_units = ["", "B", "K", "M", "G", "KB", "MB", "GB"]
mem_units = ["", "B", "K", "M", "G", "T", "KB", "MB", "GB", "TB",
"KI", "MI", "GI", "KIB", "MIB", "GIB", "TIB"]
SIMPLE_FEATURES = {
"spot": (str, ["YES", "NO"]),
"image_type": (str, ["VMDK", "QCOW", "QCOW2", "RAW"]),
Expand Down
20 changes: 18 additions & 2 deletions test/TestRADL.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ def test_basic0(self):
s = r.get_system_by_name("main")
self.assertEqual(s.getValue("cpu.arch"), "x86_64")
self.assertEqual(s.getValue("net_interface.0.connection"), "publica")
self.assertEqual(s.getValue("memory.size"), 536870912)
self.assertEqual(s.getValue("memory.size"), 512000000)
s = r.get_system_by_name("wn")
self.assertEqual(s.getValue("disk.0.image.url"), ['one://server.com/1', 'one://server2.com/1'])
self.assertEqual(s.getValue("memory.size"), 536870912)

radl_json = dump_radl_json(r)
r = parse_radl_json(radl_json)
Expand All @@ -77,9 +78,10 @@ def test_basic0(self):
self.assertEqual(s.getValue("cpu.arch"), "x86_64")
self.assertEqual(s.getValue("net_interface.0.connection"), "publica")
self.assertEqual(s.getValue("cpu.arch"), "x86_64")
self.assertEqual(s.getValue("memory.size"), 536870912)
self.assertEqual(s.getValue("memory.size"), 512000000)
s = r.get_system_by_name("wn")
self.assertEqual(s.getValue("disk.0.image.url"), ['one://server.com/1', 'one://server2.com/1'])
self.assertEqual(s.getValue("memory.size"), 536870912)

def test_references(self):

Expand Down Expand Up @@ -481,6 +483,20 @@ def test_remove_net_interface(self):
self.assertIsNone(radl.systems[0].getValue('net_interface.2.connection'))
self.assertIsNone(radl.systems[0].getValue('net_interface.3.connection'))

def test_units(self):
radl_data = """
system test (
memory.size = 1G and
disk.0.size = 10GiB
)"""
radl = parse_radl(radl_data)
self.assertEqual(1, radl.systems[0].getFeature('memory.size').getValue("G"))
self.assertEqual(954, radl.systems[0].getFeature('memory.size').getValue("MI"))
self.assertEqual(1000, radl.systems[0].getFeature('memory.size').getValue("M"))

self.assertEqual(10737, radl.systems[0].getFeature('disk.0.size').getValue("M"))
self.assertEqual(10240, radl.systems[0].getFeature('disk.0.size').getValue("MI"))


if __name__ == "__main__":
unittest.main()
2 changes: 1 addition & 1 deletion test/test_radl_1.radl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ disk.0.os.flavour='ubuntu'
system wn (
cpu.arch='x86_64' and
cpu.count>=0.5 and
memory.size>=512m and
memory.size>=512mi and
disk.0.os.name='linux' and
disk.0.image.url = ['one://server.com/1','one://server2.com/1']
)

0 comments on commit 60f7875

Please sign in to comment.