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