From e689602be5c8c94b67159a39dfbaff33c3e225ed Mon Sep 17 00:00:00 2001 From: Jana Cupova Date: Jul 20 2021 08:36:53 +0000 Subject: Add btype to protonmsg Fixes: https://pagure.io/koji/issue/1217 --- diff --git a/hub/kojihub.py b/hub/kojihub.py index 51d8341..3b181e8 100644 --- a/hub/kojihub.py +++ b/hub/kojihub.py @@ -4636,10 +4636,12 @@ def get_build_type(buildInfo, strict=False): Returns a dictionary whose keys are type names and whose values are the type info corresponding to that type """ - - binfo = get_build(buildInfo, strict=strict) - if not binfo: - return None + if not isinstance(buildInfo, dict) or 'extra' not in buildInfo: + binfo = get_build(buildInfo, strict=strict) + if not binfo: + return None + else: + binfo = buildInfo query = QueryProcessor( tables=['btype'], diff --git a/plugins/hub/protonmsg.py b/plugins/hub/protonmsg.py index 3fc62b0..bbf4df5 100644 --- a/plugins/hub/protonmsg.py +++ b/plugins/hub/protonmsg.py @@ -17,7 +17,7 @@ from proton.reactor import Container import koji from koji.context import context from koji.plugin import callback, convert_datetime, ignore_error -from kojihub import QueryProcessor, InsertProcessor +from kojihub import QueryProcessor, InsertProcessor, get_build_type CONFIG_FILE = '/etc/koji-hub/plugins/protonmsg.conf' CONFIG = None @@ -210,11 +210,13 @@ def prep_build_state_change(cbtype, *args, **kws): old = koji.BUILD_STATES[old] new = koji.BUILD_STATES[kws['new']] address = 'build.' + new.lower() + btypeinfo = get_build_type(kws['info']) kws['info'] = _strip_extra(kws['info']) props = {'type': cbtype[4:], 'name': kws['info']['name'], 'version': kws['info']['version'], 'release': kws['info']['release'], + 'btypes': btypeinfo, 'attribute': kws['attribute'], 'old': old, 'new': new} diff --git a/tests/test_plugins/test_protonmsg.py b/tests/test_plugins/test_protonmsg.py index 7bbd6eb..6e2f3bc 100644 --- a/tests/test_plugins/test_protonmsg.py +++ b/tests/test_plugins/test_protonmsg.py @@ -1,13 +1,17 @@ from __future__ import absolute_import -import six -import protonmsg + import tempfile import unittest +import protonmsg +import mock +import six from mock import patch, MagicMock -from koji.context import context from six.moves.configparser import ConfigParser, SafeConfigParser +from koji.context import context + + class TestProtonMsg(unittest.TestCase): def setUp(self): self.conf = tempfile.NamedTemporaryFile() @@ -26,6 +30,7 @@ extra_limit = 2048 protonmsg.CONFIG_FILE = self.conf.name protonmsg.CONFIG = None protonmsg.LOG = MagicMock() + self.get_build_type = mock.patch('protonmsg.get_build_type').start() def tearDown(self): if hasattr(context, 'protonmsg_msgs'): @@ -122,18 +127,48 @@ extra_limit = 2048 def test_prep_build_state_change(self): info = {'name': 'test-pkg', 'version': '1.0', - 'release': '1'} + 'release': '1', + 'build_id': 1} + assert_info = {'name': 'test-pkg', + 'version': '1.0', + 'release': '1', + 'btypes': {'image': {'build_id': 1}}} + self.get_build_type.return_value = {'image': {'build_id': 1}} protonmsg.prep_build_state_change('postBuildStateChange', info=info, attribute='volume_id', old=0, new=1) # no messages should be created for callbacks where attribute != state self.assertFalse(hasattr(context, 'protonmsg_msgs')) + self.get_build_type.return_value = {'image': {'build_id': 1}} protonmsg.prep_build_state_change('postBuildStateChange', info=info, attribute='state', old=0, new=1) self.assertMsg('build.complete', type='BuildStateChange', attribute='state', old='BUILDING', new='COMPLETE', - **info) + **assert_info) + + def test_prep_build_state_change_with_empty_build_type(self): + info = {'name': 'test-pkg', + 'version': '1.0', + 'release': '1', + 'build_id': 1} + assert_info = {'name': 'test-pkg', + 'version': '1.0', + 'release': '1', + 'btypes': {}} + self.get_build_type.return_value = {} + protonmsg.prep_build_state_change('postBuildStateChange', + info=info, attribute='volume_id', + old=0, new=1) + # no messages should be created for callbacks where attribute != state + self.assertFalse(hasattr(context, 'protonmsg_msgs')) + self.get_build_type.return_value = {} + protonmsg.prep_build_state_change('postBuildStateChange', + info=info, attribute='state', + old=0, new=1) + self.assertMsg('build.complete', type='BuildStateChange', + attribute='state', old='BUILDING', new='COMPLETE', + **assert_info) def test_prep_import(self): build = {'name': 'test-pkg', 'version': '1.0', 'release': '1'} @@ -185,8 +220,9 @@ extra_limit = 2048 user='test-user', **build) def test_prep_repo_init(self): - protonmsg.prep_repo_init('postRepoInit', tag={'name': 'test-tag', - 'arches': set(['x86_64', 'i386'])}, repo_id=1234, task_id=25) + protonmsg.prep_repo_init('postRepoInit', + tag={'name': 'test-tag', 'arches': set(['x86_64', 'i386'])}, + repo_id=1234, task_id=25) self.assertMsg('repo.init', type='RepoInit', tag='test-tag', repo_id=1234, task_id=25) def test_prep_repo_done(self): @@ -222,6 +258,7 @@ extra_limit = 2048 def test_send_queued_msgs_success(self, Container): context.protonmsg_msgs = [{'address': 'test.topic', 'props': {'testheader': 1}, 'body': 'test body'}] + def clear_msgs(): del context.protonmsg_msgs[:] Container.return_value.run.side_effect = clear_msgs @@ -248,6 +285,7 @@ test_mode = on conf.flush() protonmsg.CONFIG_FILE = conf.name protonmsg.CONFIG = None + def clear_msgs(): del context.protonmsg_msgs[:] Container.return_value.run.side_effect = clear_msgs