blob: e0d4ea5ff10c4567affdbe9c6969e9676527be98 [file] [log] [blame]
Allen Lica61e062017-07-13 13:15:21 -07001# 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
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):
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
29def _mock_forked_process(name, cmdline):
30 parent_proc = _mock_process(name, cmdline)
31 return _mock_process(name, cmdline, parent=parent_proc)
32
33
Allen Li3992c662018-01-05 15:26:36 -080034def _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 Lica61e062017-07-13 13:15:21 -070044class TestProcMetrics(cros_test_lib.TestCase):
45 """Tests for proc_metrics."""
46
47 def setUp(self):
Mike Frysinger8d6a51d2021-02-12 07:40:03 -050048 patcher = mock.patch(
49 'chromite.third_party.infra_libs.ts_mon.common.interface.state.store',
50 autospec=True)
Allen Lica61e062017-07-13 13:15:21 -070051 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 Lica61e062017-07-13 13:15:21 -070070 _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 Guo17542e02022-06-29 13:48:15 -070084 name='gs_offloader.py',
Allen Li3511a832018-06-27 14:41:01 -070085 cmdline=['/usr/bin/python',
86 '/usr/local/autotest/site_utils/gs_offloader.py',
87 '-s', '--parallelism=30']
88 ),
89 _mock_process(
Allen Lica61e062017-07-13 13:15:21 -070090 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 Li3992c662018-01-05 15:26:36 -080096 _mock_process(
Prathmesh Prabhu5ed6f902018-05-07 14:13:02 -070097 name='lxc-start',
Congbin Guo17542e02022-06-29 13:48:15 -070098 cmdline=['[lxc monitor] /usr/local/autotest/containers'
Prathmesh Prabhu5ed6f902018-05-07 14:13:02 -070099 ' 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 Keshet98f33792019-10-29 11:05:53 -0700114 _mock_process(
115 name='getty',
116 cmdline=['/sbin/getty', '-8', '38400', 'console',]
117 ),
Congbin Guo17542e02022-06-29 13:48:15 -0700118 _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 Lica61e062017-07-13 13:15:21 -0700150 ]
151 proc_metrics.collect_proc_info()
152
153 setter = self.store.set
Allen Li3992c662018-01-05 15:26:36 -0800154 calls = []
155 calls.extend(_expected_calls_for('autoserv'))
Congbin Guo17542e02022-06-29 13:48:15 -0700156 calls.extend(_expected_calls_for('curl'))
Aviv Keshet98f33792019-10-29 11:05:53 -0700157 calls.extend(_expected_calls_for('getty'))
Congbin Guo17542e02022-06-29 13:48:15 -0700158 calls.extend(_expected_calls_for('gs_archive_server'))
Allen Li3511a832018-06-27 14:41:01 -0700159 calls.extend(_expected_calls_for('gs_offloader'))
Congbin Guo17542e02022-06-29 13:48:15 -0700160 calls.extend(_expected_calls_for('gsutil'))
161 calls.extend(_expected_calls_for('java'))
Prathmesh Prabhu5ed6f902018-05-07 14:13:02 -0700162 calls.extend(_expected_calls_for('lxc-attach'))
Congbin Guo17542e02022-06-29 13:48:15 -0700163 calls.extend(_expected_calls_for('lxc-start'))
164 calls.extend(_expected_calls_for('sshd'))
165 calls.extend(_expected_calls_for('swarming_bot'))
Allen Li3511a832018-06-27 14:41:01 -0700166 calls.extend(_expected_calls_for('sysmon'))
Allen Li3992c662018-01-05 15:26:36 -0800167 calls.extend(_expected_calls_for('other'))
Allen Lica61e062017-07-13 13:15:21 -0700168 setter.assert_has_calls(calls)
169 self.assertEqual(len(setter.mock_calls), len(calls))