From d6e72d220c546098abd37a0276659aa88eafe44b Mon Sep 17 00:00:00 2001 From: Jan Kaluza Date: Jan 25 2019 06:53:19 +0000 Subject: Do not overwrite "global" whitelist with handler-specific whitelist in handler.allow_build(). The `conf.handler_build_whitelist.get()` returns directly the dict from the whitelist. We later called .update() on that dict which effectively changed it in the conf.handler_build_whitelist. In this commit, we created copy of that dict instead so the update() does not get it into the Config class itself and stays local in allow_build method. --- diff --git a/freshmaker/handlers/__init__.py b/freshmaker/handlers/__init__.py index aeaac09..f7a4d8f 100644 --- a/freshmaker/handlers/__init__.py +++ b/freshmaker/handlers/__init__.py @@ -25,6 +25,7 @@ import abc import json import re import six +import copy from functools import wraps from freshmaker import conf, log, db, models @@ -368,8 +369,10 @@ class BaseHandler(object): :rtype: bool """ # Global rules - whitelist_rules = conf.handler_build_whitelist.get("global", {}) - blacklist_rules = conf.handler_build_blacklist.get("global", {}) + whitelist_rules = copy.deepcopy( + conf.handler_build_whitelist.get("global", {})) + blacklist_rules = copy.deepcopy( + conf.handler_build_blacklist.get("global", {})) # This handler rules handler_name = self.name diff --git a/tests/test_handler.py b/tests/test_handler.py index b195811..f406386 100644 --- a/tests/test_handler.py +++ b/tests/test_handler.py @@ -248,6 +248,40 @@ class TestAllowBuildBasedOnWhitelist(helpers.FreshmakerTestCase): """Test BaseHandler.allow_build""" @patch.object(freshmaker.conf, 'handler_build_whitelist', new={ + 'global': { + 'image': any_( + { + 'advisory_state': 'ON_QA', + 'advisory_name': 'RHBA-.*', + } + ) + }, + 'RebuildImagesOnImageAdvisoryChange': { + 'image': any_( + { + 'advisory_state': 'SHIPPED_LIVE', + 'advisory_name': 'RHBA-.*', + } + ) + } + }) + def test_whitelist_not_overwritten(self): + """ + Test that "global" config section is not overwritten by handler-specific + section after calling the handler.allow_build(). + """ + handler = MyHandler() + handler.name = "RebuildImagesOnImageAdvisoryChange" + allowed = handler.allow_build( + ArtifactType.IMAGE, advisory_state="SHIPPED_LIVE") + self.assertTrue(allowed) + + handler.name = "foo" + allowed = handler.allow_build( + ArtifactType.IMAGE, advisory_state="SHIPPED_LIVE") + self.assertFalse(allowed) + + @patch.object(freshmaker.conf, 'handler_build_whitelist', new={ 'MyHandler': { 'image': { 'name': 'test'