summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTristan Van Berkom <tristan.vanberkom@codethink.co.uk>2019-01-20 12:52:16 -0500
committerTristan Van Berkom <tristan.vanberkom@codethink.co.uk>2019-01-24 13:02:08 -0500
commit9fd9fbb99fe54fda7599c39c014727a157c9ee2a (patch)
tree0c8bafddc0303072bce75872c9867d0f44598ef1
parent24ca2f469230a2b9a1ce6a487f1341b4428d4477 (diff)
downloadbuildstream-9fd9fbb99fe54fda7599c39c014727a157c9ee2a.tar.gz
_artifactcache.py: Refactored to use utils._get_volume_size()
This will benefit from a better UtilError being raised, and and turns the artifact cache's local function into a one liner. The loop which finds the first existing directory in the given path has been removed, being meaningless due to the call to os.makedirs() in ArtifactCache.__init__(). The local function was renamed to _get_cache_volume_size() and no longer takes any arguments, which is more suitable for the function as it serves as a testing override surface for unittest.mock(). The following test cases which use the function to override the ArtifactCache behavior have been updated to use the new overridable function name: tests/artifactcache/cache_size.py tests/artifactcache/expiry.py
-rw-r--r--buildstream/_artifactcache.py33
-rw-r--r--tests/artifactcache/cache_size.py2
-rw-r--r--tests/artifactcache/expiry.py4
3 files changed, 19 insertions, 20 deletions
diff --git a/buildstream/_artifactcache.py b/buildstream/_artifactcache.py
index b1afdf377..725b5fbee 100644
--- a/buildstream/_artifactcache.py
+++ b/buildstream/_artifactcache.py
@@ -307,7 +307,7 @@ class ArtifactCache():
# it is greater than the actual cache size.
#
# Returns:
- # (int) An approximation of the artifact cache size.
+ # (int) An approximation of the artifact cache size, in bytes.
#
def get_cache_size(self):
@@ -848,19 +848,16 @@ class ArtifactCache():
else:
headroom = 2e9
- artifactdir_volume = self.context.artifactdir
- while not os.path.exists(artifactdir_volume):
- artifactdir_volume = os.path.dirname(artifactdir_volume)
-
try:
- cache_quota = utils._parse_size(self.context.config_cache_quota, artifactdir_volume)
+ cache_quota = utils._parse_size(self.context.config_cache_quota,
+ self.context.artifactdir)
except utils.UtilError as e:
raise LoadError(LoadErrorReason.INVALID_DATA,
"{}\nPlease specify the value in bytes or as a % of full disk space.\n"
"\nValid values are, for example: 800M 10G 1T 50%\n"
.format(str(e))) from e
- available_space, total_size = self._get_volume_space_info_for(artifactdir_volume)
+ total_size, available_space = self._get_cache_volume_size()
cache_size = self.get_cache_size()
# Ensure system has enough storage for the cache_quota
@@ -903,19 +900,21 @@ class ArtifactCache():
self._cache_quota = cache_quota - headroom
self._cache_lower_threshold = self._cache_quota / 2
- # _get_volume_space_info_for
- #
- # Get the available space and total space for the given volume
+ # _get_cache_volume_size()
#
- # Args:
- # volume: volume for which to get the size
+ # Get the available space and total space for the volume on
+ # which the artifact cache is located.
#
# Returns:
- # A tuple containing first the availabe number of bytes on the requested
- # volume, then the total number of bytes of the volume.
- def _get_volume_space_info_for(self, volume):
- stat = os.statvfs(volume)
- return stat.f_bsize * stat.f_bavail, stat.f_bsize * stat.f_blocks
+ # (int): The total number of bytes on the volume
+ # (int): The number of available bytes on the volume
+ #
+ # NOTE: We use this stub to allow the test cases
+ # to override what an artifact cache thinks
+ # about it's disk size and available bytes.
+ #
+ def _get_cache_volume_size(self):
+ return utils._get_volume_size(self.context.artifactdir)
# _configured_remote_artifact_cache_specs():
diff --git a/tests/artifactcache/cache_size.py b/tests/artifactcache/cache_size.py
index 11c8f6194..63ab9ad07 100644
--- a/tests/artifactcache/cache_size.py
+++ b/tests/artifactcache/cache_size.py
@@ -80,7 +80,7 @@ def test_quota_over_1024T(cli, tmpdir):
_yaml.dump({'name': 'main'}, str(project.join("project.conf")))
volume_space_patch = mock.patch(
- "buildstream._artifactcache.ArtifactCache._get_volume_space_info_for",
+ "buildstream._artifactcache.ArtifactCache._get_cache_volume_size",
autospec=True,
return_value=(1025 * TiB, 1025 * TiB)
)
diff --git a/tests/artifactcache/expiry.py b/tests/artifactcache/expiry.py
index d7bafe7e8..2230b70bd 100644
--- a/tests/artifactcache/expiry.py
+++ b/tests/artifactcache/expiry.py
@@ -358,9 +358,9 @@ def test_invalid_cache_quota(cli, datafiles, tmpdir, quota, err_domain, err_reas
total_space = 10000
volume_space_patch = mock.patch(
- "buildstream._artifactcache.ArtifactCache._get_volume_space_info_for",
+ "buildstream._artifactcache.ArtifactCache._get_cache_volume_size",
autospec=True,
- return_value=(free_space, total_space),
+ return_value=(total_space, free_space),
)
cache_size_patch = mock.patch(