From 19416d32e21bf3b39fc3298c26b25068e4c515f6 Mon Sep 17 00:00:00 2001
From: Lukas Holecek <hluk@email.cz>
Date: May 22 2019 16:12:23 +0000
Subject: Fix handling date/time format for fedora-messaging


Fixes: https://pagure.io/fedora-infrastructure/issue/7818

Signed-off-by: Lukas Holecek <hluk@email.cz>

---

diff --git a/greenwave/consumers/resultsdb.py b/greenwave/consumers/resultsdb.py
index 2553514..d4a1d3b 100644
--- a/greenwave/consumers/resultsdb.py
+++ b/greenwave/consumers/resultsdb.py
@@ -239,12 +239,16 @@ class ResultsDBHandler(fedmsg.consumers.FedmsgConsumer):
         message = message.get('body', message)
         log.debug('Processing message "%s"', message)
 
+        msg = message['msg']
         try:
-            testcase = message['msg']['testcase']['name']  # New format
+            testcase = msg['testcase']['name']
         except KeyError:
-            testcase = message['msg']['task']['name']  # Old format
+            testcase = msg['task']['name']
 
-        submit_time = message['msg']['submit_time']
+        try:
+            submit_time = msg['submit_time']
+        except KeyError:
+            submit_time = msg['result']['submit_time']
 
         with self.flask_app.app_context():
             for subject_type, subject_identifier in self.announcement_subjects(message):
diff --git a/greenwave/tests/test_resultsdb_consumer.py b/greenwave/tests/test_resultsdb_consumer.py
index 85fca67..118f866 100644
--- a/greenwave/tests/test_resultsdb_consumer.py
+++ b/greenwave/tests/test_resultsdb_consumer.py
@@ -440,3 +440,129 @@ def test_decision_change_for_modules(
                 'subject_identifier': nsvc,
                 'previous': None,
             }
+
+
+@mock.patch('greenwave.resources.ResultsRetriever.retrieve')
+@mock.patch('greenwave.resources.retrieve_decision')
+@mock.patch('greenwave.resources.retrieve_scm_from_koji')
+def test_real_fedora_messaging_msg(
+        mock_retrieve_scm_from_koji,
+        mock_retrieve_decision,
+        mock_retrieve_results):
+    message = {
+        'msg': {
+            'task': {
+                'type': 'bodhi_update',
+                'item': 'FEDORA-2019-9244c8b209',
+                'name': 'update.advisory_boot'
+            },
+            'result': {
+                'id': 23523568,
+                'submit_time': '2019-04-24 13:06:12 UTC',
+                'prev_outcome': None,
+                'outcome': 'PASSED',
+                'log_url': 'https://openqa.stg.fedoraproject.org/tests/528801'
+            }
+        }
+    }
+
+    result = {
+        "data": {
+            "arch": [
+                "x86_64"
+            ],
+            "firmware": [
+                "bios"
+            ],
+            "item": [
+                "FEDORA-2019-9244c8b209"
+            ],
+            "meta.conventions": [
+                "result fedora.bodhi"
+            ],
+            "scenario": [
+                "fedora.updates-server.x86_64.64bit"
+            ],
+            "source": [
+                "openqa"
+            ],
+            "type": [
+                "bodhi_update"
+            ]
+        },
+        "groups": [
+            "61d11797-79cd-579f-b150-349cc77e0941",
+            "222c442c-5d94-528f-9b9e-3fb379edf657",
+            "0f3309ea-6d4c-59b2-b422-d73e9b8511f3"
+        ],
+        "href": "https://taskotron.stg.fedoraproject.org/resultsdb_api/api/v2.0/results/23523568",
+        "id": 23523568,
+        "note": "",
+        "outcome": "PASSED",
+        "ref_url": "https://openqa.stg.fedoraproject.org/tests/528801",
+        "submit_time": "2019-04-24T13:06:12.135146",
+        "testcase": {
+            "href": "https://taskotron.stg.fedoraproject.org/resultsdb_api/api/v2.0/testcases/update.advisory_boot", # noqa
+            "name": "update.advisory_boot",
+            "ref_url": "https://openqa.stg.fedoraproject.org/tests/546627"
+        }
+    }
+
+    policies = dedent("""
+        --- !Policy
+        id: test_policy
+        product_versions: [fedora-rawhide]
+        decision_context: test_context
+        subject_type: bodhi_update
+        rules:
+          - !PassingTestCaseRule {test_case_name: update.advisory_boot}
+    """)
+
+    config = 'fedora-messaging'
+    publish = 'greenwave.consumers.resultsdb.fedora_messaging.api.publish'
+
+    with mock.patch('greenwave.config.Config.MESSAGING', config):
+        with mock.patch(publish) as mock_fedmsg:
+            result = {
+                'id': 1,
+                'testcase': {'name': 'dist.rpmdeplint'},
+                'outcome': 'PASSED',
+                'data': {'item': 'FEDORA-2019-9244c8b209', 'type': 'bodhi_update'},
+                'submit_time': '2019-04-24 13:06:12.135146'
+            }
+            mock_retrieve_results.return_value = [result]
+
+            def retrieve_decision(url, data):
+                #pylint: disable=unused-argument
+                if 'when' in data:
+                    return None
+                return {}
+            mock_retrieve_decision.side_effect = retrieve_decision
+
+            hub = mock.MagicMock()
+            hub.config = {
+                'environment': 'environment',
+                'topic_prefix': 'topic_prefix',
+            }
+            handler = greenwave.consumers.resultsdb.ResultsDBHandler(hub)
+
+            handler.flask_app.config['policies'] = Policy.safe_load_all(policies)
+            with handler.flask_app.app_context():
+                handler.consume(message)
+
+            assert len(mock_fedmsg.mock_calls) == 1
+
+            mock_call = mock_fedmsg.mock_calls[0][1][0]
+            assert mock_call.topic == 'greenwave.decision.update'
+            actual_msgs_sent = mock_call.body
+
+            assert actual_msgs_sent == {
+                'decision_context': 'test_context',
+                'product_version': 'fedora-rawhide',
+                'subject': [
+                    {'item': 'FEDORA-2019-9244c8b209', 'type': 'bodhi_update'},
+                ],
+                'subject_type': 'bodhi_update',
+                'subject_identifier': 'FEDORA-2019-9244c8b209',
+                'previous': None,
+            }
diff --git a/greenwave/utils.py b/greenwave/utils.py
index ff0301c..e218dfe 100644
--- a/greenwave/utils.py
+++ b/greenwave/utils.py
@@ -159,6 +159,13 @@ def right_before_this_time(timestamp):
     result).
     """
     date_format = '%Y-%m-%dT%H:%M:%S.%f'
+
+    if timestamp.endswith(' UTC'):
+        # date/time format submitted by resultsdb using fedora-messaging
+        from_date_format = '%Y-%m-%d %H:%M:%S UTC'
+    else:
+        from_date_format = date_format
+
     return datetime.datetime.strftime(
-        datetime.datetime.strptime(timestamp, date_format) -
+        datetime.datetime.strptime(timestamp, from_date_format) -
         datetime.timedelta(microseconds=1), date_format)

