blob: 339152a805ccea3c7a1735ec44cd0ffa2aa04d2e [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2017 The ChromiumOS Authors
Allen Lica61e062017-07-13 13:15:21 -07002# 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
9from __future__ import absolute_import
Allen Lica61e062017-07-13 13:15:21 -070010
Mike Frysinger166fea02021-02-12 05:30:33 -050011from unittest import mock
12
Mike Frysingercb56b642019-08-25 15:33:08 -040013import psutil # pylint: disable=import-error
Allen Lica61e062017-07-13 13:15:21 -070014
15from chromite.lib import cros_test_lib
16from chromite.scripts.sysmon import proc_metrics
17
18
19def _mock_process(name, cmdline, parent=None):
Alex Klein1699fab2022-09-08 08:46:06 -060020 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 Lica61e062017-07-13 13:15:21 -070027
28
29def _mock_forked_process(name, cmdline):
Alex Klein1699fab2022-09-08 08:46:06 -060030 parent_proc = _mock_process(name, cmdline)
31 return _mock_process(name, cmdline, parent=parent_proc)
Allen Lica61e062017-07-13 13:15:21 -070032
33
Allen Li3992c662018-01-05 15:26:36 -080034def _expected_calls_for(name):
Alex Klein1699fab2022-09-08 08:46:06 -060035 """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 Li3992c662018-01-05 15:26:36 -080040
41
Allen Lica61e062017-07-13 13:15:21 -070042class TestProcMetrics(cros_test_lib.TestCase):
Alex Klein1699fab2022-09-08 08:46:06 -060043 """Tests for proc_metrics."""
Allen Lica61e062017-07-13 13:15:21 -070044
Alex Klein1699fab2022-09-08 08:46:06 -060045 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 Lica61e062017-07-13 13:15:21 -070052
Alex Klein1699fab2022-09-08 08:46:06 -060053 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 ),
173 _mock_process(
174 name="python",
175 cmdline=[
176 "python",
177 "-m",
178 "gs_archive_server",
179 "-p",
180 "18000",
181 "-c",
182 "127.0.0.1:8082",
183 ],
184 ),
185 _mock_process(name="java", cmdline=["java", "-Xmx4g", "..."]),
186 _mock_process(
187 name="python",
188 cmdline=[
189 "python",
190 "/tmp/chromeos-cache/common/gsutil_4.57.tar.gz/"
191 "gsutil/gsutil",
192 "-o",
193 "Boto:num_retries=10",
194 "cat",
195 "gs://eve-release/R100-14488.0.0/file",
196 ],
197 ),
198 ]
199 proc_metrics.collect_proc_info()
Allen Lica61e062017-07-13 13:15:21 -0700200
Alex Klein1699fab2022-09-08 08:46:06 -0600201 setter = self.store.set
202 calls = []
203 calls.extend(_expected_calls_for("autoserv"))
204 calls.extend(_expected_calls_for("curl"))
205 calls.extend(_expected_calls_for("getty"))
206 calls.extend(_expected_calls_for("gs_archive_server"))
207 calls.extend(_expected_calls_for("gs_offloader"))
208 calls.extend(_expected_calls_for("gsutil"))
209 calls.extend(_expected_calls_for("java"))
210 calls.extend(_expected_calls_for("lxc-attach"))
211 calls.extend(_expected_calls_for("lxc-start"))
212 calls.extend(_expected_calls_for("sshd"))
213 calls.extend(_expected_calls_for("swarming_bot"))
214 calls.extend(_expected_calls_for("sysmon"))
215 calls.extend(_expected_calls_for("other"))
216 setter.assert_has_calls(calls)
217 self.assertEqual(len(setter.mock_calls), len(calls))