Qinusty pushed to branch Qinusty/unit-test-utils at BuildStream / buildstream
Commits:
- 
db5f83b4
by Josh Smith at 2018-09-19T08:36:13Z
2 changed files:
Changes:
| 1 | +from contextlib import contextmanager
 | |
| 2 | +import os
 | |
| 3 | + | |
| 4 | + | |
| 5 | +# MockAttributeResult
 | |
| 6 | +#
 | |
| 7 | +# A class to take a dictionary of kwargs and make them accessible via
 | |
| 8 | +# attributes of the object.
 | |
| 9 | +#
 | |
| 10 | +class MockAttributeResult(dict):
 | |
| 11 | +    __getattr__ = dict.get
 | |
| 12 | + | |
| 13 | + | |
| 14 | +# mock_statvfs():
 | |
| 15 | +#
 | |
| 16 | +# Gets a function which mocks statvfs and returns a statvfs result with the kwargs accessible.
 | |
| 17 | +#
 | |
| 18 | +# Returns:
 | |
| 19 | +#    func(path) -> object: object will have all the kwargs accessible via object.kwarg
 | |
| 20 | +#
 | |
| 21 | +# Example:
 | |
| 22 | +#    statvfs = mock_statvfs(f_blocks=10)
 | |
| 23 | +#    result = statvfs("regardless/of/path")
 | |
| 24 | +#    assert result.f_blocks == 10 # True
 | |
| 25 | +def mock_statvfs(**kwargs):
 | |
| 26 | +    def statvfs(path):
 | |
| 27 | +        return MockAttributeResult(kwargs)
 | |
| 28 | +    return statvfs
 | |
| 29 | + | |
| 30 | + | |
| 31 | +# monkey_patch()
 | |
| 32 | +#
 | |
| 33 | +# with monkey_patch("statvfs", custom_statvfs):
 | |
| 34 | +#    assert os.statvfs == custom_statvfs # True
 | |
| 35 | +# assert os.statvfs == custom_statvfs # False
 | |
| 36 | +#
 | |
| 37 | +@contextmanager
 | |
| 38 | +def monkey_patch(to_patch, patched_func):
 | |
| 39 | +    orig = getattr(os, to_patch)
 | |
| 40 | +    setattr(os, to_patch, patched_func)
 | |
| 41 | +    try:
 | |
| 42 | +        yield
 | |
| 43 | +    finally:
 | |
| 44 | +        setattr(os, to_patch, orig) | 
| 1 | +from buildstream import _yaml
 | |
| 2 | +from ..testutils import mock_os
 | |
| 3 | +from ..testutils.runcli import cli
 | |
| 4 | + | |
| 5 | +import os
 | |
| 6 | +import pytest
 | |
| 7 | + | |
| 8 | + | |
| 9 | +KiB = 1024
 | |
| 10 | +MiB = (KiB * 1024)
 | |
| 11 | +GiB = (MiB * 1024)
 | |
| 12 | +TiB = (GiB * 1024)
 | |
| 13 | + | |
| 14 | + | |
| 15 | +def test_parse_size_over_1024T(cli, tmpdir):
 | |
| 16 | +    BLOCK_SIZE = 4096
 | |
| 17 | +    cli.configure({
 | |
| 18 | +        'cache': {
 | |
| 19 | +            'quota': 2048 * TiB
 | |
| 20 | +        }
 | |
| 21 | +    })
 | |
| 22 | +    project = tmpdir.join("main")
 | |
| 23 | +    os.makedirs(str(project))
 | |
| 24 | +    _yaml.dump({'name': 'main'}, str(project.join("project.conf")))
 | |
| 25 | + | |
| 26 | +    bavail = (1025 * TiB) / BLOCK_SIZE
 | |
| 27 | +    patched_statvfs = mock_os.mock_statvfs(f_bavail=bavail, f_bsize=BLOCK_SIZE)
 | |
| 28 | +    with mock_os.monkey_patch("statvfs", patched_statvfs):
 | |
| 29 | +        result = cli.run(project, args=["build", "file.bst"])
 | |
| 30 | +        assert "1025T of available system storage" in result.stderr | |
| \ No newline at end of file | 
