From 87f81a762d637dc60bd092415349e1be061c2f92 Mon Sep 17 00:00:00 2001 From: Jan Kaluza Date: Nov 16 2018 13:38:24 +0000 Subject: Support SCM URLs without the namespace. It is possible to have Koji build from SCM URL without the namespace. Currently, Greenwave disallows this which makes it unusable for such builds. In this commit, the `namespace` is set to an empty string in this case and methods using namespace are updated to behave correctly when empty string is passed to them as namespace. --- diff --git a/greenwave/resources.py b/greenwave/resources.py index 3f74b7a..37879d9 100644 --- a/greenwave/resources.py +++ b/greenwave/resources.py @@ -161,12 +161,9 @@ def retrieve_scm_from_koji_build(nvr, build, koji_url): path_components = url.path.rsplit('/', 2) if len(path_components) < 3: - raise BadGateway( - 'Failed to parse SCM URL "{}" from Koji build "{}" at "{}" ' - '(expected second to last component to be namespace)' - .format(source, nvr, koji_url)) - - namespace = path_components[-2] + namespace = "" + else: + namespace = path_components[-2] rev = url.fragment if not rev: @@ -184,7 +181,8 @@ def retrieve_scm_from_koji_build(nvr, build, koji_url): def retrieve_yaml_remote_rule(rev, pkg_name, pkg_namespace): """ Retrieve cached gating.yaml content for a given rev. """ data = { - "DIST_GIT_BASE_URL": current_app.config['DIST_GIT_BASE_URL'].rstrip('/') + '/', + "DIST_GIT_BASE_URL": (current_app.config['DIST_GIT_BASE_URL'].rstrip('/') + + ('/' if pkg_namespace else '')), "pkg_namespace": pkg_namespace, "pkg_name": pkg_name, "rev": rev diff --git a/greenwave/tests/test_policies.py b/greenwave/tests/test_policies.py index 794ac05..4b91669 100644 --- a/greenwave/tests/test_policies.py +++ b/greenwave/tests/test_policies.py @@ -335,7 +335,8 @@ rules: [] assert not policy.applies_to('dummy_context', 'epel-7', 'bodhi_update') -def test_remote_rule_policy(tmpdir): +@pytest.mark.parametrize('namespace', ["rpms", ""]) +def test_remote_rule_policy(tmpdir, namespace): """ Testing the RemoteRule with the koji interaction. In this case we are just mocking koji """ @@ -367,7 +368,7 @@ rules: app = create_app('greenwave.config.TestingConfig') with app.app_context(): with mock.patch('greenwave.resources.retrieve_scm_from_koji') as scm: - scm.return_value = ('rpms', 'nethack', 'c3c47a08a66451cb9686c49f040776ed35a0d1bb') + scm.return_value = (namespace, 'nethack', 'c3c47a08a66451cb9686c49f040776ed35a0d1bb') with mock.patch('greenwave.resources.retrieve_yaml_remote_rule') as f: f.return_value = remote_fragment policies = load_policies(tmpdir.strpath) diff --git a/greenwave/tests/test_retrieve_gating_yaml.py b/greenwave/tests/test_retrieve_gating_yaml.py index b4f5c22..cdd484d 100644 --- a/greenwave/tests/test_retrieve_gating_yaml.py +++ b/greenwave/tests/test_retrieve_gating_yaml.py @@ -1,10 +1,13 @@ # SPDX-License-Identifier: GPL-2.0+ import pytest +import mock from werkzeug.exceptions import BadGateway -from greenwave.resources import retrieve_scm_from_koji_build +import greenwave.app_factory +from greenwave.resources import ( + retrieve_scm_from_koji_build, retrieve_yaml_remote_rule) KOJI_URL = 'https://koji.fedoraproject.org/kojihub' @@ -51,15 +54,16 @@ def test_retrieve_scm_from_build_with_missing_source(): retrieve_scm_from_koji_build(nvr, build, KOJI_URL) -def test_retrieve_scm_from_build_with_bad_source(): +def test_retrieve_scm_from_build_without_namespace(): nvr = 'foo-1.2.3-1.fc29' build = { 'nvr': nvr, 'source': 'git+https://src.fedoraproject.org/foo.git#deadbeef', } - expected_error = 'expected second to last component to be namespace' - with pytest.raises(BadGateway, match=expected_error): - retrieve_scm_from_koji_build(nvr, build, KOJI_URL) + namespace, pkg_name, rev = retrieve_scm_from_koji_build(nvr, build, KOJI_URL) + assert namespace == '' + assert rev == 'deadbeef' + assert pkg_name == 'foo' def test_retrieve_scm_from_build_with_missing_rev(): @@ -71,3 +75,21 @@ def test_retrieve_scm_from_build_with_missing_rev(): expected_error = 'missing URL fragment with SCM revision information' with pytest.raises(BadGateway, match=expected_error): retrieve_scm_from_koji_build(nvr, build, KOJI_URL) + + +def test_retrieve_yaml_remote_rule_no_namespace(): + app = greenwave.app_factory.create_app() + with app.app_context(): + with mock.patch('greenwave.resources.requests_session') as session: + # Return 404, because we are only interested in the URL in the request + # and whether it is correct even with empty namespace. + response = mock.MagicMock() + response.status_code = 404 + session.request.return_value = response + retrieve_yaml_remote_rule("deadbeaf", "pkg", "") + + expected_call = mock.call( + 'HEAD', + 'https://src.fedoraproject.org/pkg/raw/deadbeaf/f/gating.yaml', + headers={'Content-Type': 'application/json'}, timeout=60) + assert session.request.mock_calls == [expected_call]