Skip to content

Commit

Permalink
[process] Fix test
Browse files Browse the repository at this point in the history
- remove mock of `open` (useless since psutil upgrade)
- add `Threads` to mocked `status` file (otherwise psutil crashes), and
test related metric
  • Loading branch information
olivielpeau committed Oct 25, 2016
1 parent 40852e7 commit 8bc67cd
Showing 1 changed file with 3 additions and 20 deletions.
23 changes: 3 additions & 20 deletions tests/checks/integration/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ def _fake_procfs(arg, root=my_procfs):
_fake_procfs({
'1': {
'status': (
"Name:\t%s\n"
"Name:\t%s\nThreads:\t1\n"
) % unique_process_name,
'stat': ('1 (%s) S 0 1 1 ' + ' 0' * 46) % unique_process_name,
'cmdline': unique_process_name,
Expand Down Expand Up @@ -428,30 +428,12 @@ def import_mock(name, i_globals={}, i_locals={}, fromlist=[], level=-1, orig_imp
return m
return orig_import(name, i_globals, i_locals, fromlist, level)

orig_open = open

def open_mock(name, *args):
from mock import MagicMock

# Work around issue addressed here: https://github.com/giampaolo/psutil/pull/715
# TODO: Remove open_mock if the patch lands
# We can't use patch here because 1) we're reloading psutil, and 2) the problem is happening during the import.
# NB: The values generated here are mostly ignored, and will correctly be overwritten once we set PROCFS_PATH
if name == '/proc/stat':
handle = MagicMock(spec=file)
handle.write.return_value = None
handle.__enter__.return_value = handle
handle.readline.return_value = 'cpu 13002 0 18504 377363817 1986 2 2960 0 0 0'
return handle
return orig_open(name, *args)

# contextlib.nested is deprecated in favor of with MGR1, MGR2, ... etc, but we have too many mocks to fit on one line and apparently \ line
# continuation is not flake8 compliant, even when semantically required (as here). Patch is unlikely to throw errors that are suppressed, so
# the main downside of contextlib is avoided.
with contextlib.nested(patch('sys.platform', 'linux'),
patch('socket.AF_PACKET', create=True),
patch('__builtin__.__import__', side_effect=import_mock),
patch('__builtin__.open', side_effect=open_mock)):
patch('__builtin__.__import__', side_effect=import_mock)):
if not already_linux:
# Reloading psutil fails on linux, but we only need to do so if we didn't start out on a linux platform
reload(psutil)
Expand All @@ -470,6 +452,7 @@ def open_mock(name, *args):
self.assertServiceCheckOK('process.up', count=1, tags=['process:moved_procfs'])

self.assertMetric('system.processes.number', at_least=1, tags=expected_tags)
self.assertMetric('system.processes.threads', at_least=1, tags=expected_tags)
self.assertMetric('system.processes.run_time.avg', at_least=1, tags=expected_tags)
self.assertMetric('system.processes.run_time.max', at_least=1, tags=expected_tags)
self.assertMetric('system.processes.run_time.min', at_least=1, tags=expected_tags)
Expand Down

0 comments on commit 8bc67cd

Please sign in to comment.