diff options
author | Josh Smith <qinusty@gmail.com> | 2018-09-19 09:32:14 +0100 |
---|---|---|
committer | Qinusty <jrsmith9822@gmail.com> | 2018-09-19 09:44:04 +0000 |
commit | caa619cebc1c7a2cb3ee3a984d8b581df1ae6c47 (patch) | |
tree | ba8efad5c213c36354d219b392993edafcc2db1d | |
parent | bbe4151dfbf84cc910f00d2535778b479372b98e (diff) | |
download | buildstream-Qinusty/unit-test-utils.tar.gz |
Add regression test for _pretty_sizeQinusty/unit-test-utils
-rw-r--r-- | tests/testutils/mock_os.py | 44 | ||||
-rw-r--r-- | tests/utils/misc.py | 30 |
2 files changed, 74 insertions, 0 deletions
diff --git a/tests/testutils/mock_os.py b/tests/testutils/mock_os.py new file mode 100644 index 000000000..109593b16 --- /dev/null +++ b/tests/testutils/mock_os.py @@ -0,0 +1,44 @@ +from contextlib import contextmanager +import os + + +# MockAttributeResult +# +# A class to take a dictionary of kwargs and make them accessible via +# attributes of the object. +# +class MockAttributeResult(dict): + __getattr__ = dict.get + + +# mock_statvfs(): +# +# Gets a function which mocks statvfs and returns a statvfs result with the kwargs accessible. +# +# Returns: +# func(path) -> object: object will have all the kwargs accessible via object.kwarg +# +# Example: +# statvfs = mock_statvfs(f_blocks=10) +# result = statvfs("regardless/of/path") +# assert result.f_blocks == 10 # True +def mock_statvfs(**kwargs): + def statvfs(path): + return MockAttributeResult(kwargs) + return statvfs + + +# monkey_patch() +# +# with monkey_patch("statvfs", custom_statvfs): +# assert os.statvfs == custom_statvfs # True +# assert os.statvfs == custom_statvfs # False +# +@contextmanager +def monkey_patch(to_patch, patched_func): + orig = getattr(os, to_patch) + setattr(os, to_patch, patched_func) + try: + yield + finally: + setattr(os, to_patch, orig) diff --git a/tests/utils/misc.py b/tests/utils/misc.py new file mode 100644 index 000000000..ae584e4d5 --- /dev/null +++ b/tests/utils/misc.py @@ -0,0 +1,30 @@ +from buildstream import _yaml +from ..testutils import mock_os +from ..testutils.runcli import cli + +import os +import pytest + + +KiB = 1024 +MiB = (KiB * 1024) +GiB = (MiB * 1024) +TiB = (GiB * 1024) + + +def test_parse_size_over_1024T(cli, tmpdir): + BLOCK_SIZE = 4096 + cli.configure({ + 'cache': { + 'quota': 2048 * TiB + } + }) + project = tmpdir.join("main") + os.makedirs(str(project)) + _yaml.dump({'name': 'main'}, str(project.join("project.conf"))) + + bavail = (1025 * TiB) / BLOCK_SIZE + patched_statvfs = mock_os.mock_statvfs(f_bavail=bavail, f_bsize=BLOCK_SIZE) + with mock_os.monkey_patch("statvfs", patched_statvfs): + result = cli.run(project, args=["build", "file.bst"]) + assert "1025T of available system storage" in result.stderr |