|
| 1 | +# |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | +# |
| 14 | + |
| 15 | +# Pylint doesn't play well with fixtures and dependency injection from pytest |
| 16 | +# pylint: disable=redefined-outer-name |
| 17 | + |
| 18 | +import os |
| 19 | +import shutil |
| 20 | + |
| 21 | +import pytest |
| 22 | + |
| 23 | +from buildstream._testing import cli # pylint: disable=unused-import |
| 24 | +from buildstream.exceptions import ErrorDomain |
| 25 | + |
| 26 | +from tests.testutils import ( |
| 27 | + create_artifact_share, |
| 28 | + assert_shared, |
| 29 | + assert_not_shared, |
| 30 | +) |
| 31 | + |
| 32 | + |
| 33 | +DATA_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "show_artifact_cas_digest_project") |
| 34 | + |
| 35 | +# This tests that cache keys behave as expected when |
| 36 | +# dependencies have been specified as `strict` and |
| 37 | +# when building in strict mode. |
| 38 | +# |
| 39 | +# This test will: |
| 40 | +# |
| 41 | +# * Build the target once (and assert that it is cached) |
| 42 | +# * Modify some local files which are imported |
| 43 | +# by an import element which the target depends on |
| 44 | +# * Assert that the cached state of the target element |
| 45 | +# is as expected |
| 46 | +# |
| 47 | +# We run the test twice, once with an element which strict |
| 48 | +# depends on the changing import element, and one which |
| 49 | +# depends on it regularly. |
| 50 | +# |
| 51 | +@pytest.mark.datafiles(DATA_DIR) |
| 52 | +@pytest.mark.parametrize( |
| 53 | + "target, expected_digests", |
| 54 | + [ |
| 55 | + ("import-basic-files.bst", { |
| 56 | + "import-basic-files.bst": "7093d3c89029932ce1518bd2192e1d3cf60fd88e356b39195d10b87b598c78f0/168", |
| 57 | + }), |
| 58 | + ("import-executable-files.bst", { |
| 59 | + "import-executable-files.bst": "133a9ae2eda30945a363272ac14bb2c8a941770b5a37c2847c99934f2972ce4f/170", |
| 60 | + }), |
| 61 | + ("import-symlinks.bst", { |
| 62 | + "import-symlinks.bst": "95947ea55021e26cec4fd4f9de90d2b7f4f7d803ccc91656b3e1f2c9923ddf19/131", |
| 63 | + }), |
| 64 | + ], |
| 65 | +) |
| 66 | +def test_show_artifact_cas_digest(cli, tmpdir, datafiles, target, expected_digests): |
| 67 | + project = str(datafiles) |
| 68 | + expected_no_digest = "" |
| 69 | + |
| 70 | + # Configure a local cache |
| 71 | + local_cache = os.path.join(str(tmpdir), "cache") |
| 72 | + cli.configure({"cachedir": local_cache}) |
| 73 | + |
| 74 | + with create_artifact_share(os.path.join(str(tmpdir), "artifactshare")) as share: |
| 75 | + |
| 76 | + cli.configure({"artifacts": {"servers": [{"url": share.repo}]}}) |
| 77 | + |
| 78 | + # Check the element and its dependencies have not been built locally and are not existing in the remote cache |
| 79 | + for component in sorted(expected_digests.keys()): |
| 80 | + assert cli.get_element_state(project, component) == "buildable" |
| 81 | + assert_not_shared(cli, share, project, component) |
| 82 | + |
| 83 | + # Check the element and its dependencies have no artifact digest |
| 84 | + result = cli.run(project=project, silent=True, args=["show", "--format", "%{name},%{artifact-cas-digest}", target]) |
| 85 | + result.assert_success() |
| 86 | + |
| 87 | + digests = dict(line.split(",", 2) for line in result.output.splitlines()) |
| 88 | + assert len(digests) == len(expected_digests) |
| 89 | + |
| 90 | + for component, received in sorted(digests.items()): |
| 91 | + assert received == expected_no_digest |
| 92 | + |
| 93 | + # Build the element locally |
| 94 | + result = cli.run(project=project, silent=True, args=["build", target]) |
| 95 | + result.assert_success() |
| 96 | + |
| 97 | + # Check the element and its dependencies have been built locally and are not existing in the remote cache |
| 98 | + for component in sorted(expected_digests.keys()): |
| 99 | + assert cli.get_element_state(project, component) == "cached" |
| 100 | + assert_not_shared(cli, share, project, component) |
| 101 | + |
| 102 | + # Check the element and its dependencies have an artifact digest |
| 103 | + result = cli.run(project=project, silent=True, args=["show", "--format", "%{name},%{artifact-cas-digest}", target]) |
| 104 | + result.assert_success() |
| 105 | + |
| 106 | + digests = dict(line.split(",", 2) for line in result.output.splitlines()) |
| 107 | + assert len(digests) == len(expected_digests) |
| 108 | + |
| 109 | + for component, received in sorted(digests.items()): |
| 110 | + assert received == expected_digests[component] |
| 111 | + |
| 112 | + # Push the built artifacts to the remote cache |
| 113 | + for component in sorted(expected_digests.keys()): |
| 114 | + result = cli.run(project=project, args=["artifact", "push", component, "--artifact-remote", share.repo]) |
| 115 | + #result.assert_main_error(ErrorDomain.STREAM, None) |
| 116 | + |
| 117 | + # Check the element and its dependencies have been built locally and are existing in the remote cache |
| 118 | + for component in sorted(expected_digests.keys()): |
| 119 | + assert cli.get_element_state(project, component) == "cached" |
| 120 | + assert_shared(cli, share, project, component) |
| 121 | + |
| 122 | + # Delete the locally cached element but not its dependencies |
| 123 | + result = cli.run(project=project, silent=True, args=["artifact", "delete", target]) |
| 124 | + result.assert_success() |
| 125 | + |
| 126 | + # Check the element has been deleted locally |
| 127 | + for component, received in sorted(digests.items()): |
| 128 | + assert cli.get_element_state(project, target) == ("buildable" if component == target else "cached") |
| 129 | + assert_shared(cli, share, project, target) |
0 commit comments