def diskfree_result(spec): """Return result of a single disk usage check.""" diagnostics = [] parts = shlex.split(spec) path, parts = parts[0], parts[1:] usage = psutil.disk_usage(path) # -> sdiskusage(total=112263569408, used=59510784000, free=47026511872, percent=53.0) ok = True for threshold in parts: try: if threshold.endswith('%'): expected = usage.total * int(threshold[:-1], 10) / 100.0 else: expected = iec2bytes(threshold) except (ValueError, TypeError) as cause: ok = False diagnostics.append("Unparsable threshold {threshold!r}: {cause}" .format(threshold=threshold, cause=cause)) else: if usage.free < expected: ok = False diagnostics.append("violated {threshold} condition ({percent:.1f}% {free} free)".format( threshold=threshold, free=bytes2iec(usage.free, compact=True), percent=100.0 - usage.percent)) comment = '{spec} [{percent:.1f}% {free}/{total} free]'.format( spec=spec, total=bytes2iec(usage.total, compact=True), free=bytes2iec(usage.free, compact=True), percent=100.0 - usage.percent, ) return ok, 'diskfree', comment, '\n'.join(diagnostics)
def test_bytes_to_iec_overflow(): assert humanize.bytes2iec(2**90 - 2**80) == '1023.0 YiB' with pytest.raises(ValueError): humanize.bytes2iec(2**90)
def test_bytes_to_iec_values(): for exp in range(1, 9): for val in (1, 999, 1000, 1023): assert humanize.bytes2iec(val * 2**(10 * exp)) == '{:4d}.0 {}'.format( val, humanize.IEC_UNITS[exp])
def test_bytes_to_iec_compact_values(): assert humanize.bytes2iec(0, compact=True) == '0bytes' assert humanize.bytes2iec(1023, compact=True) == '1023bytes' assert humanize.bytes2iec(1024, compact=True) == '1.0KiB'
def test_bytes_to_iec_small_values(): assert humanize.bytes2iec(0) == ' 0 bytes' assert humanize.bytes2iec(1023) == '1023 bytes'
def test_bytes_to_iec_negative_number(): with pytest.raises(ValueError): humanize.bytes2iec(-1)