diff options
author | Brett Holman <brett.holman@canonical.com> | 2022-12-15 08:42:32 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-15 08:42:32 -0700 |
commit | 5f5c3e196d3cd4c6b491fa5ab76da141684a81dc (patch) | |
tree | bbeb47d7593ed95b2d1436292cf0aac7593adaa8 | |
parent | 0bfed1dce38f2fa21c06b6f3037e47911c029f4b (diff) | |
download | cloud-init-git-5f5c3e196d3cd4c6b491fa5ab76da141684a81dc.tar.gz |
mounts: document weird prefix in schema (#1913)
Add test and support for parsing IEC prefix format.
-rw-r--r-- | cloudinit/config/schemas/schema-cloud-config-v1.json | 2 | ||||
-rw-r--r-- | cloudinit/util.py | 18 | ||||
-rw-r--r-- | tests/unittests/test_util.py | 5 |
3 files changed, 22 insertions, 3 deletions
diff --git a/cloudinit/config/schemas/schema-cloud-config-v1.json b/cloudinit/config/schemas/schema-cloud-config-v1.json index a91dc482..7ff80ce3 100644 --- a/cloudinit/config/schemas/schema-cloud-config-v1.json +++ b/cloudinit/config/schemas/schema-cloud-config-v1.json @@ -1764,7 +1764,7 @@ "description": "Path to the swap file to create" }, "size": { - "description": "The size in bytes of the swap file, 'auto' or a human-readable size abbreviation of the format <float_size><units> where units are one of B, K, M, G or T.", + "description": "The size in bytes of the swap file, 'auto' or a human-readable size abbreviation of the format <float_size><units> where units are one of B, K, M, G or T. **WARNING: Attempts to use IEC prefixes in your configuration prior to cloud-init version 23.1 will result in unexpected behavior. SI prefixes names (KB, MB) are required on pre-23.1 cloud-init, however IEC values are used. In summary, assume 1KB == 1024B, not 1000B**", "oneOf": [ { "enum": [ diff --git a/cloudinit/util.py b/cloudinit/util.py index 078ce1c2..96cd1b74 100644 --- a/cloudinit/util.py +++ b/cloudinit/util.py @@ -2761,11 +2761,25 @@ def read_meminfo(meminfo="/proc/meminfo", raw=False): def human2bytes(size): """Convert human string or integer to size in bytes + + In the original implementation, SI prefixes parse to IEC values + (1KB=1024B). Later, support for parsing IEC prefixes was added, + also parsing to IEC values (1KiB=1024B). To maintain backwards + compatibility for the long-used implementation, no fix is provided for SI + prefixes (to make 1KB=1000B may now violate user expectations). + + Future prospective callers of this function should consider implementing a + new function with more standard expectations (1KB=1000B and 1KiB=1024B) + + Examples: 10M => 10485760 - .5G => 536870912 + 10MB => 10485760 + 10MiB => 10485760 """ size_in = size - if size.endswith("B"): + if size.endswith("iB"): + size = size[:-2] + elif size.endswith("B"): size = size[:-1] mpliers = {"B": 1, "K": 2**10, "M": 2**20, "G": 2**30, "T": 2**40} diff --git a/tests/unittests/test_util.py b/tests/unittests/test_util.py index cc516942..fe933c0a 100644 --- a/tests/unittests/test_util.py +++ b/tests/unittests/test_util.py @@ -2648,6 +2648,11 @@ class TestHuman2Bytes: with pytest.raises(ValueError): util.human2bytes(test_i) + def test_ibibytes2bytes(self): + + assert util.human2bytes("0.5GiB") == 536870912 + assert util.human2bytes("100MiB") == 104857600 + class TestKernelVersion: """test kernel version function""" |