richardmaw-codethink pushed to branch master at BuildStream / buildstream
Commits:
- 
9a045080
by Josh Smith at 2018-11-06T13:23:19Z
- 
d3a07e6b
by Josh Smith at 2018-11-06T13:23:19Z
- 
0c09fb9c
by richardmaw-codethink at 2018-11-06T14:12:20Z
3 changed files:
Changes:
| ... | ... | @@ -634,7 +634,7 @@ def _parse_size(size, volume): | 
| 634 | 634 |  | 
| 635 | 635 |  # _pretty_size()
 | 
| 636 | 636 |  #
 | 
| 637 | -# Converts a number of bytes into a string representation in KB, MB, GB, TB
 | |
| 637 | +# Converts a number of bytes into a string representation in KiB, MiB, GiB, TiB
 | |
| 638 | 638 |  # represented as K, M, G, T etc.
 | 
| 639 | 639 |  #
 | 
| 640 | 640 |  # Args:
 | 
| ... | ... | @@ -646,10 +646,11 @@ def _parse_size(size, volume): | 
| 646 | 646 |  def _pretty_size(size, dec_places=0):
 | 
| 647 | 647 |      psize = size
 | 
| 648 | 648 |      unit = 'B'
 | 
| 649 | -    for unit in ('B', 'K', 'M', 'G', 'T'):
 | |
| 649 | +    units = ('B', 'K', 'M', 'G', 'T')
 | |
| 650 | +    for unit in units:
 | |
| 650 | 651 |          if psize < 1024:
 | 
| 651 | 652 |              break
 | 
| 652 | -        else:
 | |
| 653 | +        elif unit != units[-1]:
 | |
| 653 | 654 |              psize /= 1024
 | 
| 654 | 655 |      return "{size:g}{unit}".format(size=round(psize, dec_places), unit=unit)
 | 
| 655 | 656 |  | 
| 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 | 
