diff options
author | bschubert15 <bschubert15@bloomberg.net> | 2020-04-17 11:29:24 +0100 |
---|---|---|
committer | Benjamin Schubert <contact@benschubert.me> | 2020-05-08 21:24:39 +0100 |
commit | 1fe5189e5fffbd6ca7465a08f3e41c94cd5d5d23 (patch) | |
tree | d029c64e9f9964e18c261134a9e231ba0da345b7 | |
parent | fd0a87b9166a4cc0a28f3b41b98c6b0606b02419 (diff) | |
download | buildstream-1fe5189e5fffbd6ca7465a08f3e41c94cd5d5d23.tar.gz |
cachekey.py: Move source tests for cache keys in sourcetests
This now expectes plugin authors to provide the elements and expected
cache keys for their sources.
-rw-r--r-- | src/buildstream/testing/_sourcetests/cachekey.py | 73 | ||||
-rw-r--r-- | src/buildstream/testing/repo.py | 12 |
2 files changed, 85 insertions, 0 deletions
diff --git a/src/buildstream/testing/_sourcetests/cachekey.py b/src/buildstream/testing/_sourcetests/cachekey.py new file mode 100644 index 000000000..3c8724476 --- /dev/null +++ b/src/buildstream/testing/_sourcetests/cachekey.py @@ -0,0 +1,73 @@ +# +# Copyright (C) 2019 Bloomberg Finance LP +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library. If not, see <http://www.gnu.org/licenses/>. +# + +# Pylint doesn't play well with fixtures and dependency injection from pytest +# pylint: disable=redefined-outer-name + +import os +import pytest + +from buildstream import _yaml +from .. import cli # pylint: disable=unused-import +from .. import create_repo +from .._utils.site import IS_LINUX, MACHINE_ARCH +from .utils import kind # pylint: disable=unused-import + + +# Project directory +TOP_DIR = os.path.dirname(os.path.realpath(__file__)) +DATA_DIR = os.path.join(TOP_DIR, "project") + + +@pytest.mark.skipif(MACHINE_ARCH != "x86-64", reason="Cache keys depend on architecture") +@pytest.mark.skipif(not IS_LINUX, reason="Only available on linux") +@pytest.mark.datafiles(DATA_DIR) +def test_cache_key(cli, tmpdir, datafiles, kind): + project = str(datafiles) + elements_path = os.path.join(project, "elements") + + # Create our repo object of the given source type with + # the bin files, and then collect the initial ref. + # + repo = create_repo(kind, str(tmpdir)) + + test_elements = repo.get_element_and_keys_for_cache_key_stability_test() + + expected_cache_keys = {} + + for index, (element, cache_key) in enumerate(test_elements): + element_name = "{}-{}.bst".format(kind, index) + _yaml.roundtrip_dump(element, os.path.join(elements_path, element_name)) + expected_cache_keys[element_name] = cache_key + + result = cli.run( + project=project, + silent=True, + args=["show", "--format", "%{name}::{%full-key}", *expected_cache_keys.keys()] + ) + result.assert_success() + cache_keys = { + [l.split("::") for l in result.output.splitlines()] + } + + error_msg = """\ +A cache key needs an update, some keys have changes. +You will need to update the `repo` for {} and change the key +it returns. +""".format(kind) + + assert expected_cache_keys == cache_keys, error_msg diff --git a/src/buildstream/testing/repo.py b/src/buildstream/testing/repo.py index 1b46ec806..a190429ed 100644 --- a/src/buildstream/testing/repo.py +++ b/src/buildstream/testing/repo.py @@ -23,6 +23,7 @@ Repo - Utility class for testing source plugins """ import os import shutil +from typing import Dict, List, Tuple class Repo: @@ -108,3 +109,14 @@ class Repo: repo_type = type(self) new_repo = repo_type(dest, subdir) return new_repo + + def get_element_and_keys_for_cache_key_stability_test(self) -> List[Tuple[Dict, str]]: + """Get a list of element content files and their expected cache key. + + This should return a list of BuildStream elements together with the + cache key they are expected to have. + + This ensures that sources don't unintentionally break their cache keys + and that BuildStream itself doesn't either. + """ + raise NotImplementedError("Element cache keys tests should be implemented") |