Allen Li | ca61e06 | 2017-07-13 13:15:21 -0700 | [diff] [blame] | 1 | # Copyright 2017 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Unit tests for proc_metrics.""" |
| 6 | |
| 7 | # pylint: disable=protected-access |
| 8 | |
| 9 | from __future__ import absolute_import |
Allen Li | ca61e06 | 2017-07-13 13:15:21 -0700 | [diff] [blame] | 10 | |
Mike Frysinger | 166fea0 | 2021-02-12 05:30:33 -0500 | [diff] [blame] | 11 | from unittest import mock |
| 12 | |
Mike Frysinger | cb56b64 | 2019-08-25 15:33:08 -0400 | [diff] [blame] | 13 | import psutil # pylint: disable=import-error |
Allen Li | ca61e06 | 2017-07-13 13:15:21 -0700 | [diff] [blame] | 14 | |
| 15 | from chromite.lib import cros_test_lib |
| 16 | from chromite.scripts.sysmon import proc_metrics |
| 17 | |
| 18 | |
| 19 | def _mock_process(name, cmdline, parent=None): |
| 20 | proc = mock.Mock(dir(psutil.Process)) |
| 21 | proc.name.return_value = name |
| 22 | proc.cmdline.return_value = cmdline |
| 23 | proc.cpu_percent.return_value = 2 |
| 24 | if parent is not None: |
| 25 | proc.parent.return_value = parent |
| 26 | return proc |
| 27 | |
| 28 | |
| 29 | def _mock_forked_process(name, cmdline): |
| 30 | parent_proc = _mock_process(name, cmdline) |
| 31 | return _mock_process(name, cmdline, parent=parent_proc) |
| 32 | |
| 33 | |
Allen Li | 3992c66 | 2018-01-05 15:26:36 -0800 | [diff] [blame] | 34 | def _expected_calls_for(name): |
| 35 | """Return expected calls for a process metric.""" |
| 36 | return [ |
| 37 | mock.call('proc/count', (name,), None, |
| 38 | 1, enforce_ge=mock.ANY), |
| 39 | mock.call('proc/cpu_percent', (name,), None, |
| 40 | 2, enforce_ge=mock.ANY), |
| 41 | ] |
| 42 | |
| 43 | |
Allen Li | ca61e06 | 2017-07-13 13:15:21 -0700 | [diff] [blame] | 44 | class TestProcMetrics(cros_test_lib.TestCase): |
| 45 | """Tests for proc_metrics.""" |
| 46 | |
| 47 | def setUp(self): |
Mike Frysinger | 8d6a51d | 2021-02-12 07:40:03 -0500 | [diff] [blame] | 48 | patcher = mock.patch( |
| 49 | 'chromite.third_party.infra_libs.ts_mon.common.interface.state.store', |
| 50 | autospec=True) |
Allen Li | ca61e06 | 2017-07-13 13:15:21 -0700 | [diff] [blame] | 51 | self.store = patcher.start() |
| 52 | self.addCleanup(patcher.stop) |
| 53 | |
| 54 | def test_collect(self): |
| 55 | with mock.patch('psutil.process_iter', autospec=True) as process_iter: |
| 56 | process_iter.return_value = [ |
| 57 | _mock_process( |
| 58 | name='autoserv', |
| 59 | cmdline=['/usr/bin/python', |
| 60 | '-u', '/usr/local/autotest/server/autoserv', |
| 61 | '-p', |
| 62 | '-r', ('/usr/local/autotest/results/hosts/' |
| 63 | 'chromeos4-row3-rack13-host9/646252-provision' |
| 64 | '/20171307125911'), |
| 65 | '-m', 'chromeos4-row3-rack13-host9', |
| 66 | '--verbose', '--lab', 'True', |
| 67 | '--provision', '--job-labels', |
| 68 | 'cros-version:winky-release/R61-9741.0.0'] |
| 69 | ), |
Allen Li | ca61e06 | 2017-07-13 13:15:21 -0700 | [diff] [blame] | 70 | _mock_forked_process( |
| 71 | name='autoserv', |
| 72 | cmdline=['/usr/bin/python', |
| 73 | '-u', '/usr/local/autotest/server/autoserv', |
| 74 | '-p', |
| 75 | '-r', ('/usr/local/autotest/results/hosts/' |
| 76 | 'chromeos4-row3-rack13-host9/646252-provision' |
| 77 | '/20171307125911'), |
| 78 | '-m', 'chromeos4-row3-rack13-host9', |
| 79 | '--verbose', '--lab', 'True', |
| 80 | '--provision', '--job-labels', |
| 81 | 'cros-version:winky-release/R61-9741.0.0'] |
| 82 | ), |
| 83 | _mock_process( |
Congbin Guo | 17542e0 | 2022-06-29 13:48:15 -0700 | [diff] [blame] | 84 | name='gs_offloader.py', |
Allen Li | 3511a83 | 2018-06-27 14:41:01 -0700 | [diff] [blame] | 85 | cmdline=['/usr/bin/python', |
| 86 | '/usr/local/autotest/site_utils/gs_offloader.py', |
| 87 | '-s', '--parallelism=30'] |
| 88 | ), |
| 89 | _mock_process( |
Allen Li | ca61e06 | 2017-07-13 13:15:21 -0700 | [diff] [blame] | 90 | name='python', |
| 91 | cmdline=[('/usr/local/google/home/chromeos-test/.cache/cros_venv' |
| 92 | '/venv-2.7.6-5addca6cf590166d7b70e22a95bea4a0' |
| 93 | '/bin/python'), |
| 94 | '-m', 'chromite.scripts.sysmon', '--interval', '60'] |
| 95 | ), |
Allen Li | 3992c66 | 2018-01-05 15:26:36 -0800 | [diff] [blame] | 96 | _mock_process( |
Prathmesh Prabhu | 5ed6f90 | 2018-05-07 14:13:02 -0700 | [diff] [blame] | 97 | name='lxc-start', |
Congbin Guo | 17542e0 | 2022-06-29 13:48:15 -0700 | [diff] [blame] | 98 | cmdline=['[lxc monitor] /usr/local/autotest/containers' |
Prathmesh Prabhu | 5ed6f90 | 2018-05-07 14:13:02 -0700 | [diff] [blame] | 99 | ' test_196499100_1525673902_240543]'] |
| 100 | ), |
| 101 | _mock_process( |
| 102 | name='lxc-attach', |
| 103 | cmdline=['lxc-attach', |
| 104 | '-P', |
| 105 | '/usr/local/autotest/containers', |
| 106 | '-n', |
| 107 | 'test_196499100_1525673902_240543', |
| 108 | '--', |
| 109 | 'bash', |
| 110 | '-c', |
| 111 | ('/usr/local/autotest/server/autoserv' |
| 112 | ' -s -P 196499100-chromeos-test/group0 ...')] |
| 113 | ), |
Aviv Keshet | 98f3379 | 2019-10-29 11:05:53 -0700 | [diff] [blame] | 114 | _mock_process( |
| 115 | name='getty', |
| 116 | cmdline=['/sbin/getty', '-8', '38400', 'console',] |
| 117 | ), |
Congbin Guo | 17542e0 | 2022-06-29 13:48:15 -0700 | [diff] [blame] | 118 | _mock_process( |
| 119 | name='sshd', |
| 120 | cmdline=['sshd:', 'chromeos-test', '[priv]'] |
| 121 | ), |
| 122 | _mock_process( |
| 123 | name='python3.8', |
| 124 | cmdline=['/usr/bin/python3.8', |
| 125 | '/home/chromeos-test/skylab_bots/' |
| 126 | 'c6-r16-r17-h13.2757785382/swarming_bot.1.zip', |
| 127 | 'start_bot'] |
| 128 | ), |
| 129 | _mock_process( |
| 130 | name='curl', |
| 131 | cmdline=['curl', 'server:port/path'] |
| 132 | ), |
| 133 | _mock_process( |
| 134 | name='python', |
| 135 | cmdline=['python', '-m', 'gs_archive_server', '-p', '18000', |
| 136 | '-c', '127.0.0.1:8082'] |
| 137 | ), |
| 138 | _mock_process( |
| 139 | name='java', |
| 140 | cmdline=['java', '-Xmx4g', '...'] |
| 141 | ), |
| 142 | _mock_process( |
| 143 | name='python', |
| 144 | cmdline=['python', |
| 145 | '/tmp/chromeos-cache/common/gsutil_4.57.tar.gz/' |
| 146 | 'gsutil/gsutil', |
| 147 | '-o', 'Boto:num_retries=10', |
| 148 | 'cat', 'gs://eve-release/R100-14488.0.0/file'] |
| 149 | ), |
Allen Li | ca61e06 | 2017-07-13 13:15:21 -0700 | [diff] [blame] | 150 | ] |
| 151 | proc_metrics.collect_proc_info() |
| 152 | |
| 153 | setter = self.store.set |
Allen Li | 3992c66 | 2018-01-05 15:26:36 -0800 | [diff] [blame] | 154 | calls = [] |
| 155 | calls.extend(_expected_calls_for('autoserv')) |
Congbin Guo | 17542e0 | 2022-06-29 13:48:15 -0700 | [diff] [blame] | 156 | calls.extend(_expected_calls_for('curl')) |
Aviv Keshet | 98f3379 | 2019-10-29 11:05:53 -0700 | [diff] [blame] | 157 | calls.extend(_expected_calls_for('getty')) |
Congbin Guo | 17542e0 | 2022-06-29 13:48:15 -0700 | [diff] [blame] | 158 | calls.extend(_expected_calls_for('gs_archive_server')) |
Allen Li | 3511a83 | 2018-06-27 14:41:01 -0700 | [diff] [blame] | 159 | calls.extend(_expected_calls_for('gs_offloader')) |
Congbin Guo | 17542e0 | 2022-06-29 13:48:15 -0700 | [diff] [blame] | 160 | calls.extend(_expected_calls_for('gsutil')) |
| 161 | calls.extend(_expected_calls_for('java')) |
Prathmesh Prabhu | 5ed6f90 | 2018-05-07 14:13:02 -0700 | [diff] [blame] | 162 | calls.extend(_expected_calls_for('lxc-attach')) |
Congbin Guo | 17542e0 | 2022-06-29 13:48:15 -0700 | [diff] [blame] | 163 | calls.extend(_expected_calls_for('lxc-start')) |
| 164 | calls.extend(_expected_calls_for('sshd')) |
| 165 | calls.extend(_expected_calls_for('swarming_bot')) |
Allen Li | 3511a83 | 2018-06-27 14:41:01 -0700 | [diff] [blame] | 166 | calls.extend(_expected_calls_for('sysmon')) |
Allen Li | 3992c66 | 2018-01-05 15:26:36 -0800 | [diff] [blame] | 167 | calls.extend(_expected_calls_for('other')) |
Allen Li | ca61e06 | 2017-07-13 13:15:21 -0700 | [diff] [blame] | 168 | setter.assert_has_calls(calls) |
| 169 | self.assertEqual(len(setter.mock_calls), len(calls)) |