From daa5b40e29273b28622e1060d39b453713cba84e Mon Sep 17 00:00:00 2001 From: Tim Flink Date: Mar 16 2017 04:59:13 +0000 Subject: Merge branch 'develop' into 'master' for 0.4.7 release --- diff --git a/jobtriggers/cloud_compose_complete_msg.py b/jobtriggers/cloud_compose_complete_msg.py index 39fe4fa..bd39e66 100644 --- a/jobtriggers/cloud_compose_complete_msg.py +++ b/jobtriggers/cloud_compose_complete_msg.py @@ -2,6 +2,9 @@ import fedmsg import fedmsg.encoding import fedmsg.consumers +import json +import requests + from . import config from .jobtrigger import JobTrigger from . import exceptions as exc @@ -24,6 +27,42 @@ def _pull_version_release(compose_id): return name, version, release +def _fetch_metadata(compose_url): + """This method simply isolates the one network call for the metadata out + to make it easier to test the other logic. We'll presume that the 'requests' + library does what it's supposed to. + + Input is the 'location' from fedmsg and it returns a json object from the + metadata file.""" + + metadata_path = "/metadata/images.json" + metadata = requests.get(compose_url + metadata_path) + print metadata.status_code + if metadata.status_code is not 200: + raise exc.TriggerMsgError("Couldn't pull metadata from Koji.") + + metadata = json.loads(metadata.text) + + return metadata + + +def _find_image(compose_url): + """Parse out the images from the compose metadata to determine which test to + to run. This does not include Rawhide composes, as those include more than + one qcow2 image to test.""" + + metadata = _fetch_metadata(compose_url) + + x86_images = metadata['payload']['images']['CloudImages']['x86_64'] + + for image in x86_images: + if image['format'] == 'qcow2': + # Have to add the / here to make a valid URL because the compose in + # the fedmsg doesn't have a trailing slash and the path in the + # metadata doesn't have a leading slash. + return compose_url + "/" + image['path'] + + def _process(msg): """This method preps the incoming fedmsg data so the trigger can set off the task. Expects fedmsg.body data as input and returns a dict @@ -35,11 +74,7 @@ def _process(msg): raise exc.TriggerMsgError("Rejecting message because status not " "'FINISHED'.") - # Split this up to make the lines shorter - # FIXME: We shouldn't hard code these paths here - item = ("{0}/CloudImages/x86_64/" - "images/{1}.x86_64.qcow2").format(msg['msg']['location'], - msg['msg']['compose_id']) + item = _find_image(msg['msg']['location']) name, version, release = _pull_version_release(msg['msg']['compose_id']) diff --git a/setup.py b/setup.py index d0a6fab..7a65a3f 100644 --- a/setup.py +++ b/setup.py @@ -23,7 +23,7 @@ moksha_consumer = ( setup( name='jobtriggers', - version='0.4.6', + version='0.4.7', description='triggering jobs via fedmsg', author='Tim Flink', author_email='tflink@fedoraproject.org', diff --git a/taskotron-trigger.spec b/taskotron-trigger.spec index a7a340f..db61e51 100644 --- a/taskotron-trigger.spec +++ b/taskotron-trigger.spec @@ -1,6 +1,6 @@ Name: taskotron-trigger # NOTE: if you update version, *make sure* to also update `setup.py` -Version: 0.4.6 +Version: 0.4.7 Release: 1%{?dist} Summary: Triggering Taskotron jobs via fedmsg @@ -76,6 +76,9 @@ install -d %{buildroot}%{_sharedstatedir}/taskotron-trigger/ %config(noreplace) %{_sysconfdir}/logrotate.d/taskotron-trigger %changelog +* Wed Mar 15 2017 Tim Flink 0.4.7-1 +- Fix hardcoded paths in cloud_compose_completed (D1170) + * Fri Mar 10 2017 Martin Krizek - 0.4.6-1 - Add support for cloud and atomic composes (D1157) - support multiple builders for tasks stored in distgit (D1159) diff --git a/testing/test_cloud_compose_trigger.py b/testing/test_cloud_compose_trigger.py index ed63418..d9b3745 100644 --- a/testing/test_cloud_compose_trigger.py +++ b/testing/test_cloud_compose_trigger.py @@ -9,11 +9,9 @@ from jobtriggers import exceptions as exc # These comprise just the interesting bits from the fedmsg that we want to # consume with our trigger. -location = "http://kojipkgs.fedoraproject.org/compose/Fedora-Cloud-24-20170224.0/compose" - template_msg = ('{"msg": { "status": "FINISHED",' '"location":' - '"http://kojipkgs.fedoraproject.org/compose/Fedora-Cloud-24-20170224.0/compose",' + '"https://kojipkgs.fedoraproject.org/compose/Fedora-Cloud-25-20170313.0/compose",' '"compose_id": ""}}') msg_good = json.loads(template_msg) @@ -26,8 +24,38 @@ msg_atomic = json.loads(template_msg) msg_atomic['msg']['compose_id'] = "Fedora-Atomic-25-20170302.0" +@pytest.fixture(autouse=True) +def _no_requests(monkeypatch): + monkeypatch.setattr(cloud_compose_complete_msg, + "_fetch_metadata", + _fetch_metadata) + + +@pytest.fixture(scope="module") +def _fetch_metadata(compose_url): + """This is a stub to provide mocked requests objects so we don't have + to hit the network in our tests.""" + + # This is really ugly, but it's as sparse a dataset as we need to + # complete the test. + dummy_data = ('{"payload":' + '{"images":' + '{"CloudImages":' + '{"x86_64": [' + '{"format": "qcow2",' + '"path": "/test/path/image.qcow2"' + '}]' + '}' + '}' + '}' + '}') + + return json.loads(dummy_data) + + def test_process(): """Test that nothing fails with known good data.""" + data = cloud_compose_complete_msg._process(msg_good) assert data['message_type'] == 'CloudCompose' assert data['name'] == 'Fedora-Cloud' @@ -38,9 +66,8 @@ def test_process(): def test_process_atomic(): """Test that the detection of Atomic composes sets the correct message_type, which would be "AtomicCompose".""" - data = cloud_compose_complete_msg._process(msg_atomic) - print data + data = cloud_compose_complete_msg._process(msg_atomic) assert data['name'] == "Fedora-Atomic" assert data['version'] == "Atomic"