Skip to content
Snippets Groups Projects
Commit 26ae6bb0 authored by Qinusty's avatar Qinusty Committed by Qinusty
Browse files

tests: Add unit tests for utils.py

Added test cases for _parse_size and sha256sum.
parent a36d45c7
No related branches found
No related tags found
No related merge requests found
Pipeline #30225075 failed
from buildstream import utils
from ..testutils import mock_os
import os
import pytest
from contextlib import contextmanager
KB = 1024
MB = (KB * 1024)
GB = (MB * 1024)
TB = (GB * 1024)
KiB = 1024
MiB = (KiB * 1024)
GiB = (MiB * 1024)
TiB = (GiB * 1024)
@pytest.mark.parametrize("args, output", [
((KB - 1, 2), "1023B"),
((MB, 2), "1M"),
((KB, 2), "1K"),
((GB, 2), "1G"),
((MB + (MB * 0.1), 2), "1.1M"),
((KB + (KB * 0.152), 2), "1.15K"),
((GB * 1023, 2), "1023G"),
((GB * 1025, 0), "1T"),
((TB * 1025, 2), "1025T"),
((KiB - 1, 2), "1023B"),
((MiB, 2), "1M"),
((KiB, 2), "1K"),
((GiB, 2), "1G"),
((MiB + (MiB * 0.1), 2), "1.1M"),
((KiB + (KiB * 0.152), 2), "1.15K"),
((GiB * 1023, 2), "1023G"),
((GiB * 1025, 0), "1T"),
((TiB * 1025, 2), "1025T"),
])
def test_pretty_size(args, output):
assert utils._pretty_size(*args) == output
@pytest.mark.parametrize("input_, output", [
("abcdefgh", "9c56cc51b374c3ba189210d5b6d4bf57790d351c96c47c02190ecf1e430635ab"),
("123123123213213213", "fb7841aecde5c99ab5c2e1287377fa33e0f647f129a7876b9349f632263b9277"),
("{}!£!£<>}", "b564e0b71213d377dd0bfdf0654186a92649657892cd2cdf3aa7aaac18d917dd"),
])
def test_sha256sum(tmpdir, input_, output):
dir_ = str(tmpdir)
file_ = os.path.join(dir_, "file")
with open(file_, "w") as f:
f.write(input_)
sha265sum = utils.sha256sum(file_)
assert sha265sum == output, "sha256sum output varies from the expected output."
@pytest.mark.parametrize("input_, output", [
("102341", 102341),
("2K", KiB * 2),
("1024M", MiB * 1024),
("10G", GiB * 10),
("1T", TiB),
])
def test_parse_size(input_, output):
assert utils._parse_size(input_, None) == output, "_parse_size() output varies from the expected output."
@pytest.mark.parametrize("size_disk, percent", [
(GiB * 100, 50),
(GiB * 100, 37),
(GiB * 12, 78),
(TiB * 12, 78),
(MiB * 2, 12),
])
def test_parse_size_percent(size_disk, percent):
BLOCK_SIZE = 4096
expected_output = size_disk * (percent / 100)
f_blocks = size_disk / BLOCK_SIZE
patched_statvfs = mock_os.mock_statvfs(f_blocks=f_blocks, f_bsize=BLOCK_SIZE)
with mock_os.monkey_patch("statvfs", patched_statvfs):
size = utils._parse_size("{}%".format(percent), None)
assert size == expected_output, "_parse_size() output varies from the expected output."
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment