blob: e12a262b1442f35b181448f2884dc8fb48454c78 [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
10from __future__ import print_function
11
12import mock
13import psutil
14
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
34class TestProcMetrics(cros_test_lib.TestCase):
35 """Tests for proc_metrics."""
36
37 def setUp(self):
38 patcher = mock.patch('infra_libs.ts_mon.common.interface.state.store',
39 autospec=True)
40 self.store = patcher.start()
41 self.addCleanup(patcher.stop)
42
43 def test_collect(self):
44 with mock.patch('psutil.process_iter', autospec=True) as process_iter:
45 process_iter.return_value = [
46 _mock_process(
47 name='autoserv',
48 cmdline=['/usr/bin/python',
49 '-u', '/usr/local/autotest/server/autoserv',
50 '-p',
51 '-r', ('/usr/local/autotest/results/hosts/'
52 'chromeos4-row3-rack13-host9/646252-provision'
53 '/20171307125911'),
54 '-m', 'chromeos4-row3-rack13-host9',
55 '--verbose', '--lab', 'True',
56 '--provision', '--job-labels',
57 'cros-version:winky-release/R61-9741.0.0']
58 ),
59 _mock_process(
60 name='apache2',
61 cmdline=['/usr/sbin/apache2', '-k', 'start'],
62 ),
63 _mock_forked_process(
64 name='autoserv',
65 cmdline=['/usr/bin/python',
66 '-u', '/usr/local/autotest/server/autoserv',
67 '-p',
68 '-r', ('/usr/local/autotest/results/hosts/'
69 'chromeos4-row3-rack13-host9/646252-provision'
70 '/20171307125911'),
71 '-m', 'chromeos4-row3-rack13-host9',
72 '--verbose', '--lab', 'True',
73 '--provision', '--job-labels',
74 'cros-version:winky-release/R61-9741.0.0']
75 ),
76 _mock_process(
77 name='python',
78 cmdline=[('/usr/local/google/home/chromeos-test/.cache/cros_venv'
79 '/venv-2.7.6-5addca6cf590166d7b70e22a95bea4a0'
80 '/bin/python'),
81 '-m', 'chromite.scripts.sysmon', '--interval', '60']
82 ),
83 ]
84 proc_metrics.collect_proc_info()
85
86 setter = self.store.set
87 calls = [
88 mock.call('proc/count', ('autoserv',), None,
89 1, enforce_ge=mock.ANY),
90 mock.call('proc/cpu_percent', ('autoserv',), None,
91 2, enforce_ge=mock.ANY),
92 mock.call('proc/count', ('sysmon',), None,
93 1, enforce_ge=mock.ANY),
94 mock.call('proc/cpu_percent', ('sysmon',), None,
95 2, enforce_ge=mock.ANY),
96 mock.call('proc/count', ('apache',), None,
97 1, enforce_ge=mock.ANY),
98 mock.call('proc/cpu_percent', ('apache',), None,
99 2, enforce_ge=mock.ANY),
100 mock.call('proc/count', ('other',), None,
101 1, enforce_ge=mock.ANY),
102 mock.call('proc/cpu_percent', ('other',), None,
103 2, enforce_ge=mock.ANY),
104 ]
105 setter.assert_has_calls(calls)
106 self.assertEqual(len(setter.mock_calls), len(calls))