Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2017 The ChromiumOS Authors |
Allen Li | ca61e06 | 2017-07-13 13:15:21 -0700 | [diff] [blame] | 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): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 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 |
Allen Li | ca61e06 | 2017-07-13 13:15:21 -0700 | [diff] [blame] | 27 | |
| 28 | |
| 29 | def _mock_forked_process(name, cmdline): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 30 | parent_proc = _mock_process(name, cmdline) |
| 31 | return _mock_process(name, cmdline, parent=parent_proc) |
Allen Li | ca61e06 | 2017-07-13 13:15:21 -0700 | [diff] [blame] | 32 | |
| 33 | |
Allen Li | 3992c66 | 2018-01-05 15:26:36 -0800 | [diff] [blame] | 34 | def _expected_calls_for(name): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 35 | """Return expected calls for a process metric.""" |
| 36 | return [ |
| 37 | mock.call("proc/count", (name,), None, 1, enforce_ge=mock.ANY), |
| 38 | mock.call("proc/cpu_percent", (name,), None, 2, enforce_ge=mock.ANY), |
| 39 | ] |
Allen Li | 3992c66 | 2018-01-05 15:26:36 -0800 | [diff] [blame] | 40 | |
| 41 | |
Allen Li | ca61e06 | 2017-07-13 13:15:21 -0700 | [diff] [blame] | 42 | class TestProcMetrics(cros_test_lib.TestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 43 | """Tests for proc_metrics.""" |
Allen Li | ca61e06 | 2017-07-13 13:15:21 -0700 | [diff] [blame] | 44 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 45 | def setUp(self): |
| 46 | patcher = mock.patch( |
| 47 | "chromite.third_party.infra_libs.ts_mon.common.interface.state.store", |
| 48 | autospec=True, |
| 49 | ) |
| 50 | self.store = patcher.start() |
| 51 | self.addCleanup(patcher.stop) |
Allen Li | ca61e06 | 2017-07-13 13:15:21 -0700 | [diff] [blame] | 52 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 53 | def test_collect(self): |
| 54 | with mock.patch("psutil.process_iter", autospec=True) as process_iter: |
| 55 | process_iter.return_value = [ |
| 56 | _mock_process( |
| 57 | name="autoserv", |
| 58 | cmdline=[ |
| 59 | "/usr/bin/python", |
| 60 | "-u", |
| 61 | "/usr/local/autotest/server/autoserv", |
| 62 | "-p", |
| 63 | "-r", |
| 64 | ( |
| 65 | "/usr/local/autotest/results/hosts/" |
| 66 | "chromeos4-row3-rack13-host9/646252-provision" |
| 67 | "/20171307125911" |
| 68 | ), |
| 69 | "-m", |
| 70 | "chromeos4-row3-rack13-host9", |
| 71 | "--verbose", |
| 72 | "--lab", |
| 73 | "True", |
| 74 | "--provision", |
| 75 | "--job-labels", |
| 76 | "cros-version:winky-release/R61-9741.0.0", |
| 77 | ], |
| 78 | ), |
| 79 | _mock_forked_process( |
| 80 | name="autoserv", |
| 81 | cmdline=[ |
| 82 | "/usr/bin/python", |
| 83 | "-u", |
| 84 | "/usr/local/autotest/server/autoserv", |
| 85 | "-p", |
| 86 | "-r", |
| 87 | ( |
| 88 | "/usr/local/autotest/results/hosts/" |
| 89 | "chromeos4-row3-rack13-host9/646252-provision" |
| 90 | "/20171307125911" |
| 91 | ), |
| 92 | "-m", |
| 93 | "chromeos4-row3-rack13-host9", |
| 94 | "--verbose", |
| 95 | "--lab", |
| 96 | "True", |
| 97 | "--provision", |
| 98 | "--job-labels", |
| 99 | "cros-version:winky-release/R61-9741.0.0", |
| 100 | ], |
| 101 | ), |
| 102 | _mock_process( |
| 103 | name="gs_offloader.py", |
| 104 | cmdline=[ |
| 105 | "/usr/bin/python", |
| 106 | "/usr/local/autotest/site_utils/gs_offloader.py", |
| 107 | "-s", |
| 108 | "--parallelism=30", |
| 109 | ], |
| 110 | ), |
| 111 | _mock_process( |
| 112 | name="python", |
| 113 | cmdline=[ |
| 114 | ( |
| 115 | "/usr/local/google/home/chromeos-test/.cache/cros_venv" |
| 116 | "/venv-2.7.6-5addca6cf590166d7b70e22a95bea4a0" |
| 117 | "/bin/python" |
| 118 | ), |
| 119 | "-m", |
| 120 | "chromite.scripts.sysmon", |
| 121 | "--interval", |
| 122 | "60", |
| 123 | ], |
| 124 | ), |
| 125 | _mock_process( |
| 126 | name="lxc-start", |
| 127 | cmdline=[ |
| 128 | "[lxc monitor] /usr/local/autotest/containers" |
| 129 | " test_196499100_1525673902_240543]" |
| 130 | ], |
| 131 | ), |
| 132 | _mock_process( |
| 133 | name="lxc-attach", |
| 134 | cmdline=[ |
| 135 | "lxc-attach", |
| 136 | "-P", |
| 137 | "/usr/local/autotest/containers", |
| 138 | "-n", |
| 139 | "test_196499100_1525673902_240543", |
| 140 | "--", |
| 141 | "bash", |
| 142 | "-c", |
| 143 | ( |
| 144 | "/usr/local/autotest/server/autoserv" |
| 145 | " -s -P 196499100-chromeos-test/group0 ..." |
| 146 | ), |
| 147 | ], |
| 148 | ), |
| 149 | _mock_process( |
| 150 | name="getty", |
| 151 | cmdline=[ |
| 152 | "/sbin/getty", |
| 153 | "-8", |
| 154 | "38400", |
| 155 | "console", |
| 156 | ], |
| 157 | ), |
| 158 | _mock_process( |
| 159 | name="sshd", cmdline=["sshd:", "chromeos-test", "[priv]"] |
| 160 | ), |
| 161 | _mock_process( |
| 162 | name="python3.8", |
| 163 | cmdline=[ |
| 164 | "/usr/bin/python3.8", |
| 165 | "/home/chromeos-test/skylab_bots/" |
| 166 | "c6-r16-r17-h13.2757785382/swarming_bot.1.zip", |
| 167 | "start_bot", |
| 168 | ], |
| 169 | ), |
| 170 | _mock_process( |
| 171 | name="curl", cmdline=["curl", "server:port/path"] |
| 172 | ), |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 173 | _mock_process(name="java", cmdline=["java", "-Xmx4g", "..."]), |
| 174 | _mock_process( |
| 175 | name="python", |
| 176 | cmdline=[ |
| 177 | "python", |
| 178 | "/tmp/chromeos-cache/common/gsutil_4.57.tar.gz/" |
| 179 | "gsutil/gsutil", |
| 180 | "-o", |
| 181 | "Boto:num_retries=10", |
| 182 | "cat", |
| 183 | "gs://eve-release/R100-14488.0.0/file", |
| 184 | ], |
| 185 | ), |
Congbin Guo | 522cd98 | 2022-10-06 11:47:28 -0700 | [diff] [blame^] | 186 | _mock_process( |
| 187 | name="common-tls", |
| 188 | cmdline=["/opt/infra-tools/common-tls", "-port", "..."] |
| 189 | ), |
| 190 | _mock_process( |
| 191 | name="fleet-tlw", |
| 192 | cmdline=["/opt/infra-tools/fleet-tlw", "-port", "..."] |
| 193 | ), |
| 194 | _mock_process( |
| 195 | name="drone-agent", |
| 196 | cmdline=["/opt/infra-tools/drone-agent"] |
| 197 | ), |
| 198 | _mock_process( |
| 199 | name="dnsmasq", |
| 200 | cmdline=["dnsmasq", "..."] |
| 201 | ), |
| 202 | _mock_process( |
| 203 | name="labservice", |
| 204 | cmdline=["/opt/infra-tools/labservice", "-addr", "..."] |
| 205 | ), |
| 206 | _mock_process( |
| 207 | name="cloud_sql_proxy", |
| 208 | cmdline=[ |
| 209 | "/opt/cloud_sql_proxy", "-dir=/var/run/tko_proxy", |
| 210 | "-instances=google.com:chromeos-lab:us-central1:tko", |
| 211 | "-credential_file=..." |
| 212 | ], |
| 213 | ), |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 214 | ] |
| 215 | proc_metrics.collect_proc_info() |
Allen Li | ca61e06 | 2017-07-13 13:15:21 -0700 | [diff] [blame] | 216 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 217 | setter = self.store.set |
| 218 | calls = [] |
| 219 | calls.extend(_expected_calls_for("autoserv")) |
Congbin Guo | 522cd98 | 2022-10-06 11:47:28 -0700 | [diff] [blame^] | 220 | calls.extend(_expected_calls_for("common-tls")) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 221 | calls.extend(_expected_calls_for("curl")) |
Congbin Guo | 522cd98 | 2022-10-06 11:47:28 -0700 | [diff] [blame^] | 222 | calls.extend(_expected_calls_for("dnsmasq")) |
| 223 | calls.extend(_expected_calls_for("drone-agent")) |
| 224 | calls.extend(_expected_calls_for("fleet-tlw")) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 225 | calls.extend(_expected_calls_for("getty")) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 226 | calls.extend(_expected_calls_for("gs_offloader")) |
| 227 | calls.extend(_expected_calls_for("gsutil")) |
| 228 | calls.extend(_expected_calls_for("java")) |
Congbin Guo | 522cd98 | 2022-10-06 11:47:28 -0700 | [diff] [blame^] | 229 | calls.extend(_expected_calls_for("labservice")) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 230 | calls.extend(_expected_calls_for("lxc-attach")) |
| 231 | calls.extend(_expected_calls_for("lxc-start")) |
| 232 | calls.extend(_expected_calls_for("sshd")) |
| 233 | calls.extend(_expected_calls_for("swarming_bot")) |
| 234 | calls.extend(_expected_calls_for("sysmon")) |
Congbin Guo | 522cd98 | 2022-10-06 11:47:28 -0700 | [diff] [blame^] | 235 | calls.extend(_expected_calls_for("tko_proxy")) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 236 | calls.extend(_expected_calls_for("other")) |
| 237 | setter.assert_has_calls(calls) |
| 238 | self.assertEqual(len(setter.mock_calls), len(calls)) |