Mike Frysinger | e58c0e2 | 2017-10-04 15:43:30 -0400 | [diff] [blame^] | 1 | # -*- coding: utf-8 -*- |
Allen Li | ca61e06 | 2017-07-13 13:15:21 -0700 | [diff] [blame] | 2 | # Copyright 2017 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Unit tests for proc_metrics.""" |
| 7 | |
| 8 | # pylint: disable=protected-access |
| 9 | |
| 10 | from __future__ import absolute_import |
| 11 | from __future__ import print_function |
| 12 | |
| 13 | import mock |
| 14 | import psutil |
| 15 | |
| 16 | from chromite.lib import cros_test_lib |
| 17 | from chromite.scripts.sysmon import proc_metrics |
| 18 | |
| 19 | |
| 20 | def _mock_process(name, cmdline, parent=None): |
| 21 | proc = mock.Mock(dir(psutil.Process)) |
| 22 | proc.name.return_value = name |
| 23 | proc.cmdline.return_value = cmdline |
| 24 | proc.cpu_percent.return_value = 2 |
| 25 | if parent is not None: |
| 26 | proc.parent.return_value = parent |
| 27 | return proc |
| 28 | |
| 29 | |
| 30 | def _mock_forked_process(name, cmdline): |
| 31 | parent_proc = _mock_process(name, cmdline) |
| 32 | return _mock_process(name, cmdline, parent=parent_proc) |
| 33 | |
| 34 | |
| 35 | class TestProcMetrics(cros_test_lib.TestCase): |
| 36 | """Tests for proc_metrics.""" |
| 37 | |
| 38 | def setUp(self): |
| 39 | patcher = mock.patch('infra_libs.ts_mon.common.interface.state.store', |
| 40 | autospec=True) |
| 41 | self.store = patcher.start() |
| 42 | self.addCleanup(patcher.stop) |
| 43 | |
| 44 | def test_collect(self): |
| 45 | with mock.patch('psutil.process_iter', autospec=True) as process_iter: |
| 46 | process_iter.return_value = [ |
| 47 | _mock_process( |
| 48 | name='autoserv', |
| 49 | cmdline=['/usr/bin/python', |
| 50 | '-u', '/usr/local/autotest/server/autoserv', |
| 51 | '-p', |
| 52 | '-r', ('/usr/local/autotest/results/hosts/' |
| 53 | 'chromeos4-row3-rack13-host9/646252-provision' |
| 54 | '/20171307125911'), |
| 55 | '-m', 'chromeos4-row3-rack13-host9', |
| 56 | '--verbose', '--lab', 'True', |
| 57 | '--provision', '--job-labels', |
| 58 | 'cros-version:winky-release/R61-9741.0.0'] |
| 59 | ), |
| 60 | _mock_process( |
| 61 | name='apache2', |
| 62 | cmdline=['/usr/sbin/apache2', '-k', 'start'], |
| 63 | ), |
| 64 | _mock_forked_process( |
| 65 | name='autoserv', |
| 66 | cmdline=['/usr/bin/python', |
| 67 | '-u', '/usr/local/autotest/server/autoserv', |
| 68 | '-p', |
| 69 | '-r', ('/usr/local/autotest/results/hosts/' |
| 70 | 'chromeos4-row3-rack13-host9/646252-provision' |
| 71 | '/20171307125911'), |
| 72 | '-m', 'chromeos4-row3-rack13-host9', |
| 73 | '--verbose', '--lab', 'True', |
| 74 | '--provision', '--job-labels', |
| 75 | 'cros-version:winky-release/R61-9741.0.0'] |
| 76 | ), |
| 77 | _mock_process( |
| 78 | name='python', |
| 79 | cmdline=[('/usr/local/google/home/chromeos-test/.cache/cros_venv' |
| 80 | '/venv-2.7.6-5addca6cf590166d7b70e22a95bea4a0' |
| 81 | '/bin/python'), |
| 82 | '-m', 'chromite.scripts.sysmon', '--interval', '60'] |
| 83 | ), |
| 84 | ] |
| 85 | proc_metrics.collect_proc_info() |
| 86 | |
| 87 | setter = self.store.set |
| 88 | calls = [ |
| 89 | mock.call('proc/count', ('autoserv',), None, |
| 90 | 1, enforce_ge=mock.ANY), |
| 91 | mock.call('proc/cpu_percent', ('autoserv',), None, |
| 92 | 2, enforce_ge=mock.ANY), |
| 93 | mock.call('proc/count', ('sysmon',), None, |
| 94 | 1, enforce_ge=mock.ANY), |
| 95 | mock.call('proc/cpu_percent', ('sysmon',), None, |
| 96 | 2, enforce_ge=mock.ANY), |
| 97 | mock.call('proc/count', ('apache',), None, |
| 98 | 1, enforce_ge=mock.ANY), |
| 99 | mock.call('proc/cpu_percent', ('apache',), None, |
| 100 | 2, enforce_ge=mock.ANY), |
| 101 | mock.call('proc/count', ('other',), None, |
| 102 | 1, enforce_ge=mock.ANY), |
| 103 | mock.call('proc/cpu_percent', ('other',), None, |
| 104 | 2, enforce_ge=mock.ANY), |
| 105 | ] |
| 106 | setter.assert_has_calls(calls) |
| 107 | self.assertEqual(len(setter.mock_calls), len(calls)) |