lib: add @patch decorator to config for unittests

This commit is contained in:
Jens Langhammer 2020-11-16 12:48:53 +01:00
parent 5faafbbca6
commit 8cc063ded2
2 changed files with 19 additions and 10 deletions

View File

@ -34,7 +34,6 @@ class ConfigLoader:
loaded_file = [] loaded_file = []
__config = {} __config = {}
__sub_dicts = []
def __init__(self): def __init__(self):
super().__init__() super().__init__()
@ -129,12 +128,12 @@ class ConfigLoader:
self.update(self.__config, outer) self.update(self.__config, outer)
@contextmanager @contextmanager
# pylint: disable=invalid-name def patch(self, path: str, value: Any):
def cd(self, sub: str): """Context manager for unittests to patch a value"""
"""Contextmanager that descends into sub-dict. Can be chained.""" original_value = self.y(path)
self.__sub_dicts.append(sub) self.y_set(path, value)
yield yield
self.__sub_dicts.pop() self.y_set(path, original_value)
@property @property
def raw(self) -> dict: def raw(self) -> dict:
@ -146,8 +145,6 @@ class ConfigLoader:
"""Access attribute by using yaml path""" """Access attribute by using yaml path"""
# Walk sub_dicts before parsing path # Walk sub_dicts before parsing path
root = self.raw root = self.raw
for sub in self.__sub_dicts:
root = root.get(sub, None)
# Walk each component of the path # Walk each component of the path
for comp in path.split(sep): for comp in path.split(sep):
if root and comp in root: if root and comp in root:
@ -156,6 +153,18 @@ class ConfigLoader:
return default return default
return root return root
def y_set(self, path: str, value: Any, sep="."):
"""Set value using same syntax as y()"""
# Walk sub_dicts before parsing path
root = self.raw
# Walk each component of the path
path_parts = path.split(sep)
for comp in path_parts[:-1]:
if comp not in root:
root[comp] = {}
root = root.get(comp)
root[path_parts[-1]] = value
def y_bool(self, path: str, default=False) -> bool: def y_bool(self, path: str, default=False) -> bool:
"""Wrapper for y that converts value into boolean""" """Wrapper for y that converts value into boolean"""
return str(self.y(path, default)).lower() == "true" return str(self.y(path, default)).lower() == "true"

View File

@ -1,13 +1,13 @@
"""outpost tests""" """outpost tests"""
from os import environ from os import environ
from unittest.case import skipUnless from unittest.case import skipUnless
from unittest.mock import patch
from django.test import TestCase from django.test import TestCase
from guardian.models import UserObjectPermission from guardian.models import UserObjectPermission
from passbook.crypto.models import CertificateKeyPair from passbook.crypto.models import CertificateKeyPair
from passbook.flows.models import Flow from passbook.flows.models import Flow
from passbook.lib.config import CONFIG
from passbook.outposts.controllers.k8s.base import NeedsUpdate from passbook.outposts.controllers.k8s.base import NeedsUpdate
from passbook.outposts.controllers.k8s.deployment import DeploymentReconciler from passbook.outposts.controllers.k8s.deployment import DeploymentReconciler
from passbook.outposts.controllers.kubernetes import KubernetesController from passbook.outposts.controllers.kubernetes import KubernetesController
@ -104,7 +104,7 @@ class OutpostKubernetesTests(TestCase):
deployment_reconciler.get_reference_object(), deployment_reconciler.get_reference_object(),
) )
with patch.object(deployment_reconciler, "image_base", "test"): with CONFIG.patch("outposts.docker_image_base", "test"):
with self.assertRaises(NeedsUpdate): with self.assertRaises(NeedsUpdate):
deployment_reconciler.reconcile( deployment_reconciler.reconcile(
deployment_reconciler.retrieve(), deployment_reconciler.retrieve(),