diff options
author | Josh Smith <qinusty@gmail.com> | 2018-09-19 09:32:14 +0100 |
---|---|---|
committer | richardmaw-codethink <richard.maw@codethink.co.uk> | 2018-11-06 13:23:19 +0000 |
commit | d3a07e6b4a4d581755d4bf350562b775cad9f69b (patch) | |
tree | 7188868673d7a801b66cc6b20f625f28110c684a | |
parent | 9a045080ac1e5c3554baccb566d06303ab9b2fbd (diff) | |
download | buildstream-d3a07e6b4a4d581755d4bf350562b775cad9f69b.tar.gz |
Add regression test for _pretty_size
-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 |