From d1c4c3cc012bad8dc0c950995626fa299bcbfb88 Mon Sep 17 00:00:00 2001 From: Ralph Bean Date: Oct 26 2017 14:17:09 +0000 Subject: Change publication logic. - This is related to and likely conflicts with #103. - This is an attempt to fix #104. This changes our publication logic such that we only publish a message *for every decision context and product-version combination* that we know about instead of publishing *for every policy and product-version combination*. I think this is the right way to go since it is how other systems will be calling greenwave. They call with a *decision context* in their question, not with a particular policy in mind. We might have 10 different policies which all apply to the same context -- but it is the context that the external system cares about. Policies should be a more or less internal implementation detail for greenwave. --- diff --git a/functional-tests/consumers/test_resultsdb.py b/functional-tests/consumers/test_resultsdb.py index 56e3ea4..b04536b 100644 --- a/functional-tests/consumers/test_resultsdb.py +++ b/functional-tests/consumers/test_resultsdb.py @@ -103,10 +103,11 @@ def test_no_message_for_unchanged_decision( monkeypatch.setenv('TEST', 'true') load_config.return_value = {'greenwave_api_url': greenwave_server.url + 'api/v1.0'} nvr = testdatabuilder.unique_nvr() + # One result gets the decision in a certain state. testdatabuilder.create_result(item=nvr, testcase_name='dist.rpmdeplint', outcome='PASSED') - # create another new result for dist.rpmdeplint which passed again. + # Recording a new version of the same result shouldn't change our decision at all. new_result = testdatabuilder.create_result( item=nvr, testcase_name='dist.rpmdeplint', diff --git a/greenwave/consumers/resultsdb.py b/greenwave/consumers/resultsdb.py index 1114ea0..4d93dbc 100644 --- a/greenwave/consumers/resultsdb.py +++ b/greenwave/consumers/resultsdb.py @@ -9,6 +9,7 @@ and if the new result causes the decision to change it will publish a message to the message bus about the newly satisfied/unsatisfied policy. """ +import collections import copy import json import logging @@ -86,15 +87,27 @@ class ResultsDBHandler(fedmsg.consumers.FedmsgConsumer): testcase = task['name'] del task['name'] config = load_config() - applicable_policies = [] + + # Build a set of all policies which might apply to this new results + applicable_policies = set() for policy in config['policies']: for rule in policy.rules: if rule.test_case_name == testcase: - applicable_policies.append(policy) + applicable_policies.add(policy) + + # Given all of our applicable policies, build a map of all decision + # context we know about, and which product versions they relate to. + decision_contexts = collections.defaultdict(set) for policy in applicable_policies: - for product_version in policy.product_versions: + versions = set(policy.product_versions) + decision_contexts[policy.decision_context].update(versions) + + # For every context X version combination, ask greenwave if this new + # result pushes any decisions over a threshold. + for decision_context, product_versions in decision_contexts.items(): + for product_version in product_versions: data = { - 'decision_context': policy.decision_context, + 'decision_context': decision_context, 'product_version': product_version, 'subject': [task], } @@ -118,7 +131,7 @@ class ResultsDBHandler(fedmsg.consumers.FedmsgConsumer): msg = decision decision.update({ 'subject': [task], - 'decision_context': policy.decision_context, + 'decision_context': decision_context, 'product_version': product_version, 'previous': old_decision, })