]> git.apps.os.sepia.ceph.com Git - ceph.git/commit
ceph-volume: human_readable_size() refactor 43982/head
authorGuillaume Abrioux <gabrioux@redhat.com>
Tue, 16 Nov 2021 19:32:38 +0000 (20:32 +0100)
committerGuillaume Abrioux <gabrioux@redhat.com>
Wed, 17 Nov 2021 08:22:28 +0000 (09:22 +0100)
commit6940856f233f4d365a119eed90ff88fd918f6916
tree2486a045b6119e75956ae03fb9c6426ecd7b705e
parented18f0aa93be9b2d31100f1ce4c373fd311cae1c
ceph-volume: human_readable_size() refactor

This commit refactors the `human_readable_size()` function.

The current implementation has a couple of issues:

in a 'human readable' mindset, I would expect `human_readable_size(1024)` to
return '1.00 KB' instead of '1024.00 KB'.

```
In [1]: from ceph_volume.util.disk import human_readable_size

In [2]: human_readable_size(1024)
Out[2]: '1024.00 B'

In [3]: human_readable_size(1024*1024)
Out[3]: '1024.00 KB'

```

Also, it doesn't support PB unit:

```
In [4]: human_readable_size(1024*1024*1024*1024*1024)
Out[4]: '1024.00 TB'

In [5]: human_readable_size(1024*1024*1024*1024*1024*1024)
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-31-0859861661dc> in <module>
----> 1 human_readable_size(1024*1024*1024*1024*1024*1024)

~/GIT/ceph/src/ceph-volume/ceph_volume/util/disk.py in human_readable_size(size)
    640     return "{size:.2f} {suffix}".format(
    641         size=size,
--> 642         suffix=suffixes[suffix_index])
    643
    644

IndexError: list index out of range
```

This commit fixes this.

Fixes: https://tracker.ceph.com/issues/48492
Signed-off-by: Guillaume Abrioux <gabrioux@redhat.com>
src/ceph-volume/ceph_volume/tests/util/test_disk.py
src/ceph-volume/ceph_volume/util/disk.py