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 | |
Congbin Guo | 16b64d5 | 2023-02-10 17:50:30 -0800 | [diff] [blame] | 19 | def _mock_process(name, cmdline, parent=None, num_threads=10): |
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 |
Congbin Guo | 16b64d5 | 2023-02-10 17:50:30 -0800 | [diff] [blame] | 23 | proc.num_threads.return_value = num_threads |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 24 | proc.cpu_percent.return_value = 2 |
Congbin Guo | 18b4ed7 | 2023-02-11 19:16:16 -0800 | [diff] [blame^] | 25 | |
| 26 | proc.cpu_times.return_value.system = 10 |
| 27 | proc.cpu_times.return_value.user = 11 |
| 28 | proc.cpu_times.return_value.iowait = 12 |
| 29 | proc.cpu_times.return_value.children_system = 13 |
| 30 | proc.cpu_times.return_value.children_user = 14 |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 31 | if parent is not None: |
| 32 | proc.parent.return_value = parent |
| 33 | return proc |
Allen Li | ca61e06 | 2017-07-13 13:15:21 -0700 | [diff] [blame] | 34 | |
| 35 | |
| 36 | def _mock_forked_process(name, cmdline): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 37 | parent_proc = _mock_process(name, cmdline) |
| 38 | return _mock_process(name, cmdline, parent=parent_proc) |
Allen Li | ca61e06 | 2017-07-13 13:15:21 -0700 | [diff] [blame] | 39 | |
| 40 | |
Allen Li | 3992c66 | 2018-01-05 15:26:36 -0800 | [diff] [blame] | 41 | def _expected_calls_for(name): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 42 | """Return expected calls for a process metric.""" |
| 43 | return [ |
| 44 | mock.call("proc/count", (name,), None, 1, enforce_ge=mock.ANY), |
Congbin Guo | 16b64d5 | 2023-02-10 17:50:30 -0800 | [diff] [blame] | 45 | mock.call("proc/thread_count", (name,), None, 10, enforce_ge=mock.ANY), |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 46 | mock.call("proc/cpu_percent", (name,), None, 2, enforce_ge=mock.ANY), |
| 47 | ] |
Allen Li | 3992c66 | 2018-01-05 15:26:36 -0800 | [diff] [blame] | 48 | |
| 49 | |
Allen Li | ca61e06 | 2017-07-13 13:15:21 -0700 | [diff] [blame] | 50 | class TestProcMetrics(cros_test_lib.TestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 51 | """Tests for proc_metrics.""" |
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 setUp(self): |
| 54 | patcher = mock.patch( |
| 55 | "chromite.third_party.infra_libs.ts_mon.common.interface.state.store", |
| 56 | autospec=True, |
| 57 | ) |
| 58 | self.store = patcher.start() |
| 59 | self.addCleanup(patcher.stop) |
Allen Li | ca61e06 | 2017-07-13 13:15:21 -0700 | [diff] [blame] | 60 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 61 | def test_collect(self): |
| 62 | with mock.patch("psutil.process_iter", autospec=True) as process_iter: |
| 63 | process_iter.return_value = [ |
| 64 | _mock_process( |
| 65 | name="autoserv", |
| 66 | cmdline=[ |
| 67 | "/usr/bin/python", |
| 68 | "-u", |
| 69 | "/usr/local/autotest/server/autoserv", |
| 70 | "-p", |
| 71 | "-r", |
| 72 | ( |
| 73 | "/usr/local/autotest/results/hosts/" |
| 74 | "chromeos4-row3-rack13-host9/646252-provision" |
| 75 | "/20171307125911" |
| 76 | ), |
| 77 | "-m", |
| 78 | "chromeos4-row3-rack13-host9", |
| 79 | "--verbose", |
| 80 | "--lab", |
| 81 | "True", |
| 82 | "--provision", |
| 83 | "--job-labels", |
| 84 | "cros-version:winky-release/R61-9741.0.0", |
| 85 | ], |
| 86 | ), |
| 87 | _mock_forked_process( |
| 88 | name="autoserv", |
| 89 | cmdline=[ |
| 90 | "/usr/bin/python", |
| 91 | "-u", |
| 92 | "/usr/local/autotest/server/autoserv", |
| 93 | "-p", |
| 94 | "-r", |
| 95 | ( |
| 96 | "/usr/local/autotest/results/hosts/" |
| 97 | "chromeos4-row3-rack13-host9/646252-provision" |
| 98 | "/20171307125911" |
| 99 | ), |
| 100 | "-m", |
| 101 | "chromeos4-row3-rack13-host9", |
| 102 | "--verbose", |
| 103 | "--lab", |
| 104 | "True", |
| 105 | "--provision", |
| 106 | "--job-labels", |
| 107 | "cros-version:winky-release/R61-9741.0.0", |
| 108 | ], |
| 109 | ), |
| 110 | _mock_process( |
| 111 | name="gs_offloader.py", |
| 112 | cmdline=[ |
| 113 | "/usr/bin/python", |
| 114 | "/usr/local/autotest/site_utils/gs_offloader.py", |
| 115 | "-s", |
| 116 | "--parallelism=30", |
| 117 | ], |
| 118 | ), |
| 119 | _mock_process( |
| 120 | name="python", |
| 121 | cmdline=[ |
| 122 | ( |
| 123 | "/usr/local/google/home/chromeos-test/.cache/cros_venv" |
| 124 | "/venv-2.7.6-5addca6cf590166d7b70e22a95bea4a0" |
| 125 | "/bin/python" |
| 126 | ), |
| 127 | "-m", |
| 128 | "chromite.scripts.sysmon", |
| 129 | "--interval", |
| 130 | "60", |
| 131 | ], |
| 132 | ), |
| 133 | _mock_process( |
| 134 | name="lxc-start", |
| 135 | cmdline=[ |
| 136 | "[lxc monitor] /usr/local/autotest/containers" |
| 137 | " test_196499100_1525673902_240543]" |
| 138 | ], |
| 139 | ), |
| 140 | _mock_process( |
| 141 | name="lxc-attach", |
| 142 | cmdline=[ |
| 143 | "lxc-attach", |
| 144 | "-P", |
| 145 | "/usr/local/autotest/containers", |
| 146 | "-n", |
| 147 | "test_196499100_1525673902_240543", |
| 148 | "--", |
| 149 | "bash", |
| 150 | "-c", |
| 151 | ( |
| 152 | "/usr/local/autotest/server/autoserv" |
| 153 | " -s -P 196499100-chromeos-test/group0 ..." |
| 154 | ), |
| 155 | ], |
| 156 | ), |
| 157 | _mock_process( |
| 158 | name="getty", |
| 159 | cmdline=[ |
| 160 | "/sbin/getty", |
| 161 | "-8", |
| 162 | "38400", |
| 163 | "console", |
| 164 | ], |
| 165 | ), |
| 166 | _mock_process( |
| 167 | name="sshd", cmdline=["sshd:", "chromeos-test", "[priv]"] |
| 168 | ), |
| 169 | _mock_process( |
| 170 | name="python3.8", |
| 171 | cmdline=[ |
| 172 | "/usr/bin/python3.8", |
| 173 | "/home/chromeos-test/skylab_bots/" |
| 174 | "c6-r16-r17-h13.2757785382/swarming_bot.1.zip", |
| 175 | "start_bot", |
| 176 | ], |
| 177 | ), |
| 178 | _mock_process( |
| 179 | name="curl", cmdline=["curl", "server:port/path"] |
| 180 | ), |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 181 | _mock_process(name="java", cmdline=["java", "-Xmx4g", "..."]), |
| 182 | _mock_process( |
| 183 | name="python", |
| 184 | cmdline=[ |
| 185 | "python", |
| 186 | "/tmp/chromeos-cache/common/gsutil_4.57.tar.gz/" |
| 187 | "gsutil/gsutil", |
| 188 | "-o", |
| 189 | "Boto:num_retries=10", |
| 190 | "cat", |
| 191 | "gs://eve-release/R100-14488.0.0/file", |
| 192 | ], |
| 193 | ), |
Congbin Guo | 522cd98 | 2022-10-06 11:47:28 -0700 | [diff] [blame] | 194 | _mock_process( |
| 195 | name="common-tls", |
Congbin Guo | fcb436b | 2023-01-23 20:36:01 -0800 | [diff] [blame] | 196 | cmdline=["/opt/infra-tools/common-tls", "-port", "..."], |
Congbin Guo | 522cd98 | 2022-10-06 11:47:28 -0700 | [diff] [blame] | 197 | ), |
| 198 | _mock_process( |
| 199 | name="fleet-tlw", |
Congbin Guo | fcb436b | 2023-01-23 20:36:01 -0800 | [diff] [blame] | 200 | cmdline=["/opt/infra-tools/fleet-tlw", "-port", "..."], |
Congbin Guo | 522cd98 | 2022-10-06 11:47:28 -0700 | [diff] [blame] | 201 | ), |
| 202 | _mock_process( |
Congbin Guo | fcb436b | 2023-01-23 20:36:01 -0800 | [diff] [blame] | 203 | name="drone-agent", cmdline=["/opt/infra-tools/drone-agent"] |
Congbin Guo | 522cd98 | 2022-10-06 11:47:28 -0700 | [diff] [blame] | 204 | ), |
Congbin Guo | fcb436b | 2023-01-23 20:36:01 -0800 | [diff] [blame] | 205 | _mock_process(name="dnsmasq", cmdline=["dnsmasq", "..."]), |
Congbin Guo | 522cd98 | 2022-10-06 11:47:28 -0700 | [diff] [blame] | 206 | _mock_process( |
| 207 | name="labservice", |
Congbin Guo | fcb436b | 2023-01-23 20:36:01 -0800 | [diff] [blame] | 208 | cmdline=["/opt/infra-tools/labservice", "-addr", "..."], |
Congbin Guo | 522cd98 | 2022-10-06 11:47:28 -0700 | [diff] [blame] | 209 | ), |
| 210 | _mock_process( |
| 211 | name="cloud_sql_proxy", |
| 212 | cmdline=[ |
Congbin Guo | fcb436b | 2023-01-23 20:36:01 -0800 | [diff] [blame] | 213 | "/opt/cloud_sql_proxy", |
| 214 | "-dir=/var/run/tko_proxy", |
Congbin Guo | 522cd98 | 2022-10-06 11:47:28 -0700 | [diff] [blame] | 215 | "-instances=google.com:chromeos-lab:us-central1:tko", |
Congbin Guo | fcb436b | 2023-01-23 20:36:01 -0800 | [diff] [blame] | 216 | "-credential_file=...", |
Congbin Guo | 522cd98 | 2022-10-06 11:47:28 -0700 | [diff] [blame] | 217 | ], |
| 218 | ), |
Congbin Guo | 3cdc11e | 2022-10-11 16:02:32 -0700 | [diff] [blame] | 219 | _mock_process( |
| 220 | name="downloader", |
Congbin Guo | fcb436b | 2023-01-23 20:36:01 -0800 | [diff] [blame] | 221 | cmdline=["./downloader", "-credential-file", "..."], |
Congbin Guo | 3cdc11e | 2022-10-11 16:02:32 -0700 | [diff] [blame] | 222 | ), |
Congbin Guo | a843250 | 2023-01-23 20:31:01 -0800 | [diff] [blame] | 223 | _mock_process( |
| 224 | name="cipd", cmdline=["cipd", "ensure", "-root", "..."] |
| 225 | ), |
| 226 | _mock_process( |
| 227 | name="podman", cmdline=["podman", "run", "image:tag"] |
| 228 | ), |
| 229 | _mock_process( |
| 230 | name="podman", cmdline=["podman", "pull", "image:tag"] |
| 231 | ), |
Congbin Guo | 4ccf063 | 2023-02-12 00:01:14 -0800 | [diff] [blame] | 232 | _mock_process(name="adb", cmdline=["adb", "..."]), |
| 233 | _mock_process(name="bbagent", cmdline=["bbagent", "..."]), |
| 234 | _mock_process(name="cloudtail", cmdline=["cloudtail", "..."]), |
| 235 | _mock_process(name="kubelet", cmdline=["kubelet", "..."]), |
| 236 | _mock_process( |
| 237 | name="phosphorus", cmdline=["phosphorus", "upload-to-tko"] |
| 238 | ), |
| 239 | _mock_process( |
| 240 | name="python", |
| 241 | cmdline=[ |
| 242 | "bin/python", |
| 243 | "-u", |
| 244 | "-s", |
| 245 | ".../recipe_engine/main.py", |
| 246 | ], |
| 247 | ), |
| 248 | _mock_process( |
| 249 | name="python3.8", |
| 250 | cmdline=[ |
| 251 | "bin/python3.8", |
| 252 | "-u", |
| 253 | ".../swarming_bot.3.zip", |
| 254 | "run_isolated", |
| 255 | "...", |
| 256 | ], |
| 257 | ), |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 258 | ] |
| 259 | proc_metrics.collect_proc_info() |
Allen Li | ca61e06 | 2017-07-13 13:15:21 -0700 | [diff] [blame] | 260 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 261 | setter = self.store.set |
| 262 | calls = [] |
Congbin Guo | 4ccf063 | 2023-02-12 00:01:14 -0800 | [diff] [blame] | 263 | calls.extend(_expected_calls_for("adb")) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 264 | calls.extend(_expected_calls_for("autoserv")) |
Congbin Guo | 4ccf063 | 2023-02-12 00:01:14 -0800 | [diff] [blame] | 265 | calls.extend(_expected_calls_for("bbagent")) |
Congbin Guo | 3cdc11e | 2022-10-11 16:02:32 -0700 | [diff] [blame] | 266 | calls.extend(_expected_calls_for("cache-downloader")) |
Congbin Guo | a843250 | 2023-01-23 20:31:01 -0800 | [diff] [blame] | 267 | calls.extend(_expected_calls_for("cipd")) |
Congbin Guo | 4ccf063 | 2023-02-12 00:01:14 -0800 | [diff] [blame] | 268 | calls.extend(_expected_calls_for("cloudtail")) |
Congbin Guo | 522cd98 | 2022-10-06 11:47:28 -0700 | [diff] [blame] | 269 | calls.extend(_expected_calls_for("common-tls")) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 270 | calls.extend(_expected_calls_for("curl")) |
Congbin Guo | 522cd98 | 2022-10-06 11:47:28 -0700 | [diff] [blame] | 271 | calls.extend(_expected_calls_for("dnsmasq")) |
| 272 | calls.extend(_expected_calls_for("drone-agent")) |
| 273 | calls.extend(_expected_calls_for("fleet-tlw")) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 274 | calls.extend(_expected_calls_for("getty")) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 275 | calls.extend(_expected_calls_for("gs_offloader")) |
| 276 | calls.extend(_expected_calls_for("gsutil")) |
| 277 | calls.extend(_expected_calls_for("java")) |
Congbin Guo | 4ccf063 | 2023-02-12 00:01:14 -0800 | [diff] [blame] | 278 | calls.extend(_expected_calls_for("k8s_system")) |
Congbin Guo | 522cd98 | 2022-10-06 11:47:28 -0700 | [diff] [blame] | 279 | calls.extend(_expected_calls_for("labservice")) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 280 | calls.extend(_expected_calls_for("lxc-attach")) |
| 281 | calls.extend(_expected_calls_for("lxc-start")) |
Congbin Guo | a843250 | 2023-01-23 20:31:01 -0800 | [diff] [blame] | 282 | calls.extend(_expected_calls_for("podman-pull")) |
| 283 | calls.extend(_expected_calls_for("podman-run")) |
Congbin Guo | 4ccf063 | 2023-02-12 00:01:14 -0800 | [diff] [blame] | 284 | calls.extend(_expected_calls_for("phosphorus")) |
| 285 | calls.extend(_expected_calls_for("recipe")) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 286 | calls.extend(_expected_calls_for("sshd")) |
| 287 | calls.extend(_expected_calls_for("swarming_bot")) |
Congbin Guo | 4ccf063 | 2023-02-12 00:01:14 -0800 | [diff] [blame] | 288 | calls.extend(_expected_calls_for("swarming_sub_task")) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 289 | calls.extend(_expected_calls_for("sysmon")) |
Congbin Guo | 522cd98 | 2022-10-06 11:47:28 -0700 | [diff] [blame] | 290 | calls.extend(_expected_calls_for("tko_proxy")) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 291 | calls.extend(_expected_calls_for("other")) |
| 292 | setter.assert_has_calls(calls) |
| 293 | self.assertEqual(len(setter.mock_calls), len(calls)) |