From 64231a1524518bf45474d79787c605c37e2e5836 Mon Sep 17 00:00:00 2001 From: Mike Ruckman Date: Mar 16 2017 04:40:14 +0000 Subject: Fix hardcoded paths in cloud_compose_completed_msg.py. Fixes T928 --- 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/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"