269a15a8fc
The mock third party library was needed for mock support in py2 runtimes. Since we now only support py36 and later, we can use the standard lib unittest.mock module instead. Note that https://github.com/openstack/charms.openstack is used during tests and he need `mock`, unfortunatelly it doesn't declare `mock` in its requirements so it retrieve mock from other charm project (cross dependency). So we depend on charms.openstack first and when Ib1ed5b598a52375e29e247db9ab4786df5b6d142 will be merged then CI will pass without errors. Depends-On: Ib1ed5b598a52375e29e247db9ab4786df5b6d142 Change-Id: I07af25792bf55b00092ec9b1d47497e2f422fe6c
141 lines
3.7 KiB
Python
141 lines
3.7 KiB
Python
import os
|
|
import logging
|
|
|
|
import unittest
|
|
import yaml
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
def load_config():
|
|
'''
|
|
Walk backwords from __file__ looking for config.yaml, load and return the
|
|
'options' section'
|
|
'''
|
|
config = None
|
|
f = __file__
|
|
while config is None:
|
|
d = os.path.dirname(f)
|
|
if os.path.isfile(os.path.join(d, 'config.yaml')):
|
|
config = os.path.join(d, 'config.yaml')
|
|
break
|
|
f = d
|
|
|
|
if not config:
|
|
logging.error('Could not find config.yaml in any parent directory '
|
|
'of %s. ' % __file__)
|
|
raise Exception
|
|
|
|
return yaml.safe_load(open(config).read())['options']
|
|
|
|
|
|
def get_default_config():
|
|
'''
|
|
Load default charm config from config.yaml return as a dict.
|
|
If no default is set in config.yaml, its value is None.
|
|
'''
|
|
default_config = {}
|
|
config = load_config()
|
|
for k, v in config.items():
|
|
if 'default' in v:
|
|
default_config[k] = v['default']
|
|
else:
|
|
default_config[k] = None
|
|
return default_config
|
|
|
|
|
|
class CharmTestCase(unittest.TestCase):
|
|
|
|
def setUp(self, obj, patches):
|
|
super(CharmTestCase, self).setUp()
|
|
self.patches = patches
|
|
self.obj = obj
|
|
self.test_config = TestConfig()
|
|
self.test_relation = TestRelation()
|
|
self.patch_all()
|
|
self._patches = {}
|
|
self._patches_start = {}
|
|
|
|
def patch(self, method):
|
|
if "." in method:
|
|
_m = patch(method)
|
|
else:
|
|
_m = patch.object(self.obj, method)
|
|
mock = _m.start()
|
|
self.addCleanup(_m.stop)
|
|
return mock
|
|
|
|
def patch_all(self):
|
|
for method in self.patches:
|
|
if "." in method:
|
|
attr = method.split('.')[-1]
|
|
else:
|
|
attr = method
|
|
setattr(self, attr, self.patch(method))
|
|
|
|
def tearDown(self):
|
|
for k, v in self._patches.items():
|
|
v.stop()
|
|
setattr(self, k, None)
|
|
self._patches = None
|
|
self._patches_start = None
|
|
|
|
def patch_object(self, obj, attr, name=None, **kwargs):
|
|
"""Patch a patchable thing. Uses mock.patch.object() to do the work.
|
|
Automatically unpatches at the end of the test.
|
|
|
|
The mock gets added to the test object (self) using 'name' or the attr
|
|
passed in the arguments.
|
|
|
|
:param obj: an object that needs to have an attribute patched.
|
|
:param attr: <string> that represents the attribute being patched.
|
|
:param name: optional <string> name to call the mock.
|
|
:param **kwargs: any other args to pass to mock.patch()
|
|
"""
|
|
if obj is None:
|
|
mocked = patch(attr, **kwargs)
|
|
else:
|
|
mocked = patch.object(obj, attr, **kwargs)
|
|
if name is None:
|
|
name = attr
|
|
started = mocked.start()
|
|
self._patches[name] = mocked
|
|
self._patches_start[name] = started
|
|
setattr(self, name, started)
|
|
|
|
|
|
class TestConfig(object):
|
|
|
|
def __init__(self):
|
|
self.config = get_default_config()
|
|
|
|
def get(self, attr):
|
|
try:
|
|
return self.config[attr]
|
|
except KeyError:
|
|
return None
|
|
|
|
def get_all(self):
|
|
return self.config
|
|
|
|
def set(self, attr, value):
|
|
if attr not in self.config:
|
|
raise KeyError
|
|
self.config[attr] = value
|
|
|
|
|
|
class TestRelation(object):
|
|
|
|
def __init__(self, relation_data={}):
|
|
self.relation_data = relation_data
|
|
|
|
def set(self, relation_data):
|
|
self.relation_data = relation_data
|
|
|
|
def get(self, attr=None, unit=None, rid=None):
|
|
if attr is None:
|
|
return self.relation_data
|
|
elif attr in self.relation_data:
|
|
return self.relation_data[attr]
|
|
return None
|