summaryrefslogtreecommitdiff
path: root/src/buildstream/testing/_sourcetests/cachekey.py
blob: 7d0c21e2f253fddfd45b307f7a9c622e7a49da33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#
#  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()

    if test_elements is NotImplemented:
        pytest.skip("Source {} doesn't implement `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 = dict(
        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