monasca-agent/tests/common.py
Laszlo Hegedus 874a4beaed Refactor process test
The tests relied on psutils Python module which is not part of monasca.
The used functions and classes are substituted by mock objects that
simulate their behavior, making the test independent of third party
modules.

Changed the assertTrue calls to more specific asserts.

common.py is refactored to make it pep8 compatible

Change-Id: I6badfd3612ba52f55b73219b4581e94ff79cc73b
2016-05-24 16:20:07 +00:00

52 lines
1.6 KiB
Python

import os
import sys
import inspect
import monasca_agent.collector.checks as checks
import monasca_agent.common.config as configuration
from monasca_agent.common.util import Paths
base_config = configuration.Config(os.path.join(os.path.dirname(__file__),
'test-agent.yaml'))
def load_check(name, config):
checksd_path = Paths().get_checksd_path()
if checksd_path not in sys.path:
sys.path.append(checksd_path)
check_module = __import__(name)
check_class = None
classes = inspect.getmembers(check_module, inspect.isclass)
for name, clsmember in classes:
if clsmember == checks.AgentCheck:
continue
if issubclass(clsmember, checks.AgentCheck):
check_class = clsmember
if checks.AgentCheck in clsmember.__bases__:
continue
else:
break
if check_class is None:
raise Exception(
"Unable to import check %s. Missing a class that inherits "
"AgentCheck" % name)
init_config = config.get('init_config', None)
instances = config.get('instances')
agent_config = base_config.get_config(sections='Main')
# init the check class
try:
return check_class(
name, init_config=init_config, agent_config=agent_config,
instances=instances)
except Exception:
# Backwards compatitiblity for old checks that don't support the
# instances argument.
c = check_class(name, init_config=init_config,
agent_config=agent_config)
c.instances = instances
return c