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
This commit is contained in:
Laszlo Hegedus 2016-05-24 08:52:49 +02:00
parent 53e77d8d3e
commit 874a4beaed
2 changed files with 75 additions and 29 deletions

View File

@ -2,13 +2,14 @@ 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 must be loaded before AgentCheck or it will try to load with no config file
base_config = configuration.Config(os.path.join(os.path.dirname(__file__), 'test-agent.yaml'))
from monasca_agent.collector.checks import AgentCheck
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()
@ -19,17 +20,18 @@ def load_check(name, config):
check_class = None
classes = inspect.getmembers(check_module, inspect.isclass)
for name, clsmember in classes:
if clsmember == AgentCheck:
if clsmember == checks.AgentCheck:
continue
if issubclass(clsmember, AgentCheck):
if issubclass(clsmember, checks.AgentCheck):
check_class = clsmember
if AgentCheck in clsmember.__bases__:
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)
"Unable to import check %s. Missing a class that inherits "
"AgentCheck" % name)
init_config = config.get('init_config', None)
instances = config.get('instances')
@ -38,11 +40,12 @@ def load_check(name, config):
# init the check class
try:
return check_class(
name, init_config=init_config, agent_config=agent_config, instances=instances)
except:
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 = check_class(name, init_config=init_config,
agent_config=agent_config)
c.instances = instances
return c

View File

@ -1,5 +1,4 @@
import os
import psutil
import mock
import unittest
from tests.common import load_check
@ -7,40 +6,84 @@ from tests.common import load_check
class TestSimpleProcess(unittest.TestCase):
def setUp(self):
p = psutil.Process(os.getpid())
config = {'init_config': {}, 'instances': [{'name': 'test',
'search_string': [p.name()],
'detailed': False}]}
self.psutil_process_iter_patcher = mock.patch('psutil.process_iter')
self.mock_process_iter = self.psutil_process_iter_patcher.start()
process_attrs = {
'name.return_value': 'process_name',
'pid': 1234,
'username.return_value': 'user',
'cmdline.return_value': '/usr/bin/process_name'
}
process = mock.Mock(**process_attrs)
self.mock_process_iter.return_value = [process]
config = {'init_config': {},
'instances': [{'name': 'test',
'search_string': ['process_name'],
'detailed': False}]}
self.check = load_check('process', config)
def tearDown(self):
self.psutil_process_iter_patcher.stop()
def testPidCount(self):
self.check.run()
metrics = self.check.get_metrics()
self.assertTrue(len(metrics) == 1, metrics)
self.assertTrue(metrics[0].name == 'process.pid_count')
self.assertEqual(1, len(metrics))
self.assertEqual('process.pid_count', metrics[0].name)
class TestDetailedProcess(unittest.TestCase):
def setUp(self):
p = psutil.Process(os.getpid())
config = {'init_config': {}, 'instances': [{'name': 'test',
'search_string': [p.name()],
'detailed': True}]}
self.psutil_process_patcher = mock.patch('psutil.Process')
self.psutil_process_iter_patcher = mock.patch('psutil.process_iter')
self.mock_process = self.psutil_process_patcher.start()
self.mock_process_iter = self.psutil_process_iter_patcher.start()
process_attrs = {
'name.return_value': 'process_name',
'pid': 1234,
'username.return_value': 'user',
'cmdline.return_value': '/usr/bin/process_name',
'memory_info_ex.return_value': mock.Mock(rss=1048576),
'num_threads.return_value': 1,
'num_fds.return_value': 1,
'cpu_percent.return_value': 1,
'io_counters.return_value': mock.Mock(**{'read_count': 1,
'write_count': 1,
'read_bytes': 1024,
'write_bytes': 1024})
}
process = mock.Mock(**process_attrs)
self.mock_process_iter.return_value = [process]
self.mock_process.return_value = process
config = {'init_config': {},
'instances': [{'name': 'test',
'search_string': ['process_name'],
'detailed': True}]}
self.check = load_check('process', config)
def tearDown(self):
self.psutil_process_patcher.stop()
self.psutil_process_iter_patcher.stop()
def testPidCount(self):
self.check.run()
metrics = self.check.get_metrics()
self.assertTrue(len(metrics) > 1, metrics)
self.assertGreater(len(metrics), 1)
def run_check(self):
self.check.prepare_run()
self.check.run()
metrics = self.check.get_metrics()
measurement_names = []
for metric in metrics:
measurement_names.append(metric.name)
measurement_names = [metric.name for metric in metrics]
measurement_names.sort()
return measurement_names
@ -58,9 +101,9 @@ class TestDetailedProcess(unittest.TestCase):
'process.pid_count',
'process.thread_count']
self.assertEquals(measurement_names, expected_names)
self.assertListEqual(measurement_names, expected_names)
# run again to get cpu_perc
expected_names.insert(0, 'process.cpu_perc')
measurement_names = self.run_check()
self.assertEquals(measurement_names, expected_names)
self.assertListEqual(measurement_names, expected_names)