lib: add @patch decorator to config for unittests
This commit is contained in:
parent
5faafbbca6
commit
8cc063ded2
|
@ -34,7 +34,6 @@ class ConfigLoader:
|
|||
loaded_file = []
|
||||
|
||||
__config = {}
|
||||
__sub_dicts = []
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
@ -129,12 +128,12 @@ class ConfigLoader:
|
|||
self.update(self.__config, outer)
|
||||
|
||||
@contextmanager
|
||||
# pylint: disable=invalid-name
|
||||
def cd(self, sub: str):
|
||||
"""Contextmanager that descends into sub-dict. Can be chained."""
|
||||
self.__sub_dicts.append(sub)
|
||||
def patch(self, path: str, value: Any):
|
||||
"""Context manager for unittests to patch a value"""
|
||||
original_value = self.y(path)
|
||||
self.y_set(path, value)
|
||||
yield
|
||||
self.__sub_dicts.pop()
|
||||
self.y_set(path, original_value)
|
||||
|
||||
@property
|
||||
def raw(self) -> dict:
|
||||
|
@ -146,8 +145,6 @@ class ConfigLoader:
|
|||
"""Access attribute by using yaml path"""
|
||||
# Walk sub_dicts before parsing path
|
||||
root = self.raw
|
||||
for sub in self.__sub_dicts:
|
||||
root = root.get(sub, None)
|
||||
# Walk each component of the path
|
||||
for comp in path.split(sep):
|
||||
if root and comp in root:
|
||||
|
@ -156,6 +153,18 @@ class ConfigLoader:
|
|||
return default
|
||||
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:
|
||||
"""Wrapper for y that converts value into boolean"""
|
||||
return str(self.y(path, default)).lower() == "true"
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
"""outpost tests"""
|
||||
from os import environ
|
||||
from unittest.case import skipUnless
|
||||
from unittest.mock import patch
|
||||
|
||||
from django.test import TestCase
|
||||
from guardian.models import UserObjectPermission
|
||||
|
||||
from passbook.crypto.models import CertificateKeyPair
|
||||
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.deployment import DeploymentReconciler
|
||||
from passbook.outposts.controllers.kubernetes import KubernetesController
|
||||
|
@ -104,7 +104,7 @@ class OutpostKubernetesTests(TestCase):
|
|||
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):
|
||||
deployment_reconciler.reconcile(
|
||||
deployment_reconciler.retrieve(),
|
||||
|
|
Reference in New Issue