Пример #1
0
def test_iec_to_bytes_units():
    for exp, iec_unit in enumerate(humanize.IEC_UNITS):
        assert humanize.iec2bytes('1' + iec_unit) == 2**(10 * exp)
        assert humanize.iec2bytes('1' + iec_unit.lower()) == 2**(10 * exp)
        assert humanize.iec2bytes('1' + iec_unit.upper()) == 2**(10 * exp)
        assert humanize.iec2bytes('1  ' + iec_unit) == 2**(10 * exp)
        assert humanize.iec2bytes(' 1 ' + iec_unit + ' ') == 2**(10 * exp)
Пример #2
0
def test_iec_to_bytes_negative_numbers():
    for bad in (-1, '-1', '-42KiB'):
        assert humanize.iec2bytes(
            bad,
            only_positive=False) < 0, "{} returns negative size".format(bad)
        with pytest.raises(ValueError):
            humanize.iec2bytes(bad)
Пример #3
0
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)
Пример #4
0
def test_iec_to_bytes_with_float_value():
    assert humanize.iec2bytes('1.5K') == 1024 + 512
    assert humanize.iec2bytes('1.5B') == 1
Пример #5
0
def test_iec_to_bytes_bases():
    for spec in ('42', '0b101', '0O7', '0x42'):
        assert humanize.iec2bytes(spec) == int(spec, base=0)
Пример #6
0
def test_iec_to_bytes_already_a_number():
    assert humanize.iec2bytes(0) == 0
    assert humanize.iec2bytes(1024) == 1024
Пример #7
0
def test_iec_to_bytes_bad_unit():
    for bad in ('bytes', 'zz', 'a', ' 0', 'zkib', 'mb'):
        with pytest.raises(ValueError):
            humanize.iec2bytes('1' + bad)
Пример #8
0
def test_iec_to_bytes_bad_number():
    for bad in ('', '0O9', '0b2', '0xZZZ', 'abc', 'b', 'kib', ' kib'):
        with pytest.raises(ValueError):
            humanize.iec2bytes(bad)
Пример #9
0
def test_iec_to_bytes_short_units():
    for exp, iec_unit in enumerate(humanize.IEC_UNITS):
        assert humanize.iec2bytes('1' + iec_unit[0]) == 2**(10 * exp)