#438 Fix handling date/time format for fedora-messaging
Merged 4 years ago by gnaponie. Opened 4 years ago by lholecek.
lholecek/greenwave fix-resultsdb-fedora-messaging  into  master

@@ -239,12 +239,16 @@ 

          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):

@@ -440,3 +440,129 @@ 

                  '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,

+             }

file modified
+8 -1
@@ -159,6 +159,13 @@ 

      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)

For the test: can we just use an existing one with "parameterized" with the other type of submit_time?
Beside that: +1!

For the test: can we just use an existing one with "parameterized" with the other type of submit_time?

The test is more about ensuring the real-world message and data from resultsdb work well and won't break in future.

I would rather keep this test. That said, I noticed that lot of tests use similar code, would be nice idea to abstract it to functions/fixtures (for future refactoring).

Sounds good! Let's keep it this way and maybe let's think about some refactoring in the future.

Commit 6fde421 fixes this pull-request

Pull-Request has been merged by gnaponie

4 years ago

Pull-Request has been merged by gnaponie

4 years ago