This is an automated email from the ASF dual-hosted git repository. akitouni pushed a commit to branch abderrahim/buildstream-mirrors-merge in repository https://gitbox.apache.org/repos/asf/buildstream.git
commit 8e44d856a3ebcc367cd0681fba927c002b247c3b Author: Tristan van Berkom <[email protected]> AuthorDate: Sun Feb 11 18:12:30 2024 +0900 tests/frontend/mirror.py: Added test for source mirror plugins --- tests/frontend/mirror.py | 90 ++++++++++++++++++++++++++ tests/frontend/project/sourcemirrors/mirror.py | 27 ++++++++ 2 files changed, 117 insertions(+) diff --git a/tests/frontend/mirror.py b/tests/frontend/mirror.py index 6725ddb3b..ef391def8 100644 --- a/tests/frontend/mirror.py +++ b/tests/frontend/mirror.py @@ -807,3 +807,93 @@ def test_mirror_expand_project_and_toplevel_root(cli, tmpdir): # Success if the expanded %{project-root} is found assert foo_str in contents assert bar_str in contents + + +# Test a simple SourceMirror implementation which reads +# plugin configuration and behaves in the same way as default +# mirrors but using data in the plugin configuration instead. +# [email protected](DATA_DIR) [email protected]("datafiles") +def test_source_mirror_plugin(cli, tmpdir): + output_file = os.path.join(str(tmpdir), "output.txt") + project_dir = str(tmpdir) + element_dir = os.path.join(project_dir, "elements") + os.makedirs(element_dir, exist_ok=True) + element_name = "test.bst" + element_path = os.path.join(element_dir, element_name) + element = generate_element(output_file) + _yaml.roundtrip_dump(element, element_path) + + project_file = os.path.join(project_dir, "project.conf") + project = { + "name": "test", + "min-version": "2.0", + "element-path": "elements", + "aliases": { + "foo": "FOO/", + "bar": "BAR/", + }, + "mirrors": [ + { + "name": "middle-earth", + "kind": "mirror", + "aliases": { + "foo": ["<invalid>"], + "bar": ["<invalid>"], + }, + "config": { + "aliases": { + "foo": ["OOF/"], + "bar": ["RAB/"], + }, + }, + }, + { + "name": "arrakis", + "kind": "mirror", + "aliases": { + "foo": ["<invalid>"], + "bar": ["<invalid>"], + }, + "config": { + "aliases": { + "foo": ["%{project-root}/OFO/"], + "bar": ["%{project-root}/RBA/"], + }, + }, + }, + { + "name": "oz", + "kind": "mirror", + "aliases": { + "foo": ["<invalid>"], + "bar": ["<invalid>"], + }, + "config": { + "aliases": { + "foo": ["ooF/"], + "bar": ["raB/"], + }, + }, + }, + ], + "plugins": [ + {"origin": "local", "path": "sources", "sources": ["fetch_source"]}, + {"origin": "local", "path": "sourcemirrors", "source-mirrors": ["mirror"]}, + ], + } + + _yaml.roundtrip_dump(project, project_file) + + result = cli.run(project=project_dir, args=["--default-mirror", "arrakis", "source", "fetch", element_name]) + result.assert_success() + with open(output_file, encoding="utf-8") as f: + contents = f.read() + print(contents) + foo_str = os.path.join(project_dir, "OFO/repo1") + bar_str = os.path.join(project_dir, "RBA/repo2") + + # Success if the expanded %{project-root} is found + assert foo_str in contents + assert bar_str in contents diff --git a/tests/frontend/project/sourcemirrors/mirror.py b/tests/frontend/project/sourcemirrors/mirror.py new file mode 100644 index 000000000..99b16e84f --- /dev/null +++ b/tests/frontend/project/sourcemirrors/mirror.py @@ -0,0 +1,27 @@ +from buildstream import SourceMirror, MappingNode + + +# This mirror plugin basically implements the default behavior +# by loading the alias definitions as custom "config" configuration +# instead, and implementing the translate_url method. +# +class Sample(SourceMirror): + BST_MIN_VERSION = "2.0" + + def configure(self, node): + node.validate_keys(["aliases"]) + + self.aliases = {} + + aliases = node.get_mapping("aliases") + for alias_name, url_list in aliases.items(): + self.aliases[alias_name] = url_list.as_str_list() + + def translate_url(self, project_name, alias, alias_url, alias_substitute_url, source_url): + return self.aliases[alias][0] + source_url + + +# Plugin entry point +def setup(): + + return Sample
