blob: 018eda7156b3a8f5feba8077a13e1aaa989f709a [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2017 The ChromiumOS Authors
Allen Lid7a679c2017-07-13 15:16:08 -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 puppet_metrics."""
6
7# pylint: disable=protected-access
8
9from __future__ import absolute_import
Allen Lid7a679c2017-07-13 15:16:08 -070010
Mike Frysinger4f6e7a72019-08-28 11:17:13 -040011import io
Allen Lid7a679c2017-07-13 15:16:08 -070012import os
Mike Frysinger166fea02021-02-12 05:30:33 -050013from unittest import mock
Allen Lid7a679c2017-07-13 15:16:08 -070014
Allen Lid7a679c2017-07-13 15:16:08 -070015from chromite.lib import cros_test_lib
Mike Frysinger31fdddd2023-02-24 15:50:55 -050016from chromite.lib import osutils
Allen Lid7a679c2017-07-13 15:16:08 -070017from chromite.scripts.sysmon import puppet_metrics
18
19
Alex Klein1699fab2022-09-08 08:46:06 -060020_SUMMARY = """\
Allen Lid7a679c2017-07-13 15:16:08 -070021---
22 version:
23 config: 1499979608
24 puppet: "3.4.3"
25 resources:
26 changed: 7
27 failed: 0
28 failed_to_restart: 0
29 out_of_sync: 7
30 restarted: 0
31 scheduled: 0
32 skipped: 1
33 total: 218
34 time:
35 config_retrieval: 2.862796974
36 cron: 0.004638468
37 exec: 11.494792536
38 file: 0.618018423
39 file_line: 0.003589435
40 filebucket: 0.000341392
41 group: 0.017957332
42 ini_subsetting: 0.001235189
43 mount: 0.001416499
44 package: 4.315027644000001
45 schedule: 0.001541641
46 service: 10.242378408
47 total: 52.958788377
48 user: 0.001673407
49 vcsrepo: 23.393381029
50 last_run: 1499979671
51 changes:
52 total: 7
53 events:
54 failure: 0
55 success: 7
56 total: 7%
Mike Frysinger80de5012019-08-01 14:10:53 -040057"""
Allen Lid7a679c2017-07-13 15:16:08 -070058
59
60class TestPuppetRunSummary(cros_test_lib.TestCase):
Alex Klein1699fab2022-09-08 08:46:06 -060061 """Tests for _PuppetRunSummary."""
Allen Lid7a679c2017-07-13 15:16:08 -070062
Alex Klein1699fab2022-09-08 08:46:06 -060063 def test_config_version(self):
64 summary = puppet_metrics._PuppetRunSummary(io.StringIO(_SUMMARY))
65 self.assertEqual(summary.config_version, 1499979608)
Allen Lid7a679c2017-07-13 15:16:08 -070066
Alex Klein1699fab2022-09-08 08:46:06 -060067 def test_puppet_version(self):
68 summary = puppet_metrics._PuppetRunSummary(io.StringIO(_SUMMARY))
69 self.assertEqual(summary.puppet_version, "3.4.3")
Allen Lid7a679c2017-07-13 15:16:08 -070070
Alex Klein1699fab2022-09-08 08:46:06 -060071 def test_events(self):
72 summary = puppet_metrics._PuppetRunSummary(io.StringIO(_SUMMARY))
73 self.assertEqual(summary.events, {"failure": 0, "success": 7})
Allen Lid7a679c2017-07-13 15:16:08 -070074
Alex Klein1699fab2022-09-08 08:46:06 -060075 def test_resources(self):
76 summary = puppet_metrics._PuppetRunSummary(io.StringIO(_SUMMARY))
77 self.assertEqual(
78 summary.resources,
79 {
80 "changed": 7,
81 "failed": 0,
82 "failed_to_restart": 0,
83 "out_of_sync": 7,
84 "restarted": 0,
85 "scheduled": 0,
86 "skipped": 1,
87 "other": 203,
88 },
89 )
Allen Lid7a679c2017-07-13 15:16:08 -070090
Alex Klein1699fab2022-09-08 08:46:06 -060091 def test_times(self):
92 summary = puppet_metrics._PuppetRunSummary(io.StringIO(_SUMMARY))
93 self.assertEqual(
94 summary.times,
95 {
96 "config_retrieval": 2.862796974,
97 "cron": 0.004638468,
98 "exec": 11.494792536,
99 "file": 0.618018423,
100 "file_line": 0.003589435,
101 "filebucket": 0.000341392,
102 "group": 0.017957332,
103 "ini_subsetting": 0.001235189,
104 "mount": 0.001416499,
105 "other": 0,
106 "package": 4.315027644000001,
107 "schedule": 0.001541641,
108 "service": 10.242378408,
109 "user": 0.001673407,
110 "vcsrepo": 23.393381029,
111 },
112 )
Allen Lid7a679c2017-07-13 15:16:08 -0700113
Alex Klein1699fab2022-09-08 08:46:06 -0600114 def test_last_run_time(self):
115 summary = puppet_metrics._PuppetRunSummary(io.StringIO(_SUMMARY))
116 self.assertEqual(summary.last_run_time, 1499979671)
Allen Lid7a679c2017-07-13 15:16:08 -0700117
118
119class TestPuppetMetrics(cros_test_lib.TempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600120 """Tests for puppet_metrics."""
Allen Lid7a679c2017-07-13 15:16:08 -0700121
Alex Klein1699fab2022-09-08 08:46:06 -0600122 def setUp(self):
123 patcher = mock.patch(
Trent Aptedc4f366a2023-05-16 15:32:48 +1000124 "chromite.third_party.infra_libs.ts_mon.common.interface.state."
125 "store",
Alex Klein1699fab2022-09-08 08:46:06 -0600126 autospec=True,
127 )
128 self.store = patcher.start()
129 self.addCleanup(patcher.stop)
130 self.tempfile = os.path.join(self.tempdir, "last_run_summary.yaml")
Allen Lid7a679c2017-07-13 15:16:08 -0700131
Alex Klein1699fab2022-09-08 08:46:06 -0600132 def test_collect(self):
Mike Frysinger31fdddd2023-02-24 15:50:55 -0500133 osutils.WriteFile(self.tempfile, _SUMMARY)
Alex Klein1699fab2022-09-08 08:46:06 -0600134 with mock.patch("time.time", return_value=1500000000):
135 with mock.patch.object(
136 puppet_metrics, "LAST_RUN_FILE", self.tempfile
137 ):
138 puppet_metrics.collect_puppet_summary()
Allen Lid7a679c2017-07-13 15:16:08 -0700139
Alex Klein1699fab2022-09-08 08:46:06 -0600140 setter = self.store.set
141 calls = [
142 mock.call(
143 "puppet/version/config",
144 (),
145 None,
146 1499979608,
147 enforce_ge=mock.ANY,
148 ),
149 mock.call(
150 "puppet/version/puppet", (), None, "3.4.3", enforce_ge=mock.ANY
151 ),
152 mock.call(
153 "puppet/events", ("failure",), None, 0, enforce_ge=mock.ANY
154 ),
155 mock.call(
156 "puppet/events", ("success",), None, 7, enforce_ge=mock.ANY
157 ),
158 mock.call(
159 "puppet/resources", ("scheduled",), None, 0, enforce_ge=mock.ANY
160 ),
161 mock.call(
162 "puppet/resources", ("skipped",), None, 1, enforce_ge=mock.ANY
163 ),
164 mock.call(
165 "puppet/resources", ("restarted",), None, 0, enforce_ge=mock.ANY
166 ),
167 mock.call(
168 "puppet/resources", ("changed",), None, 7, enforce_ge=mock.ANY
169 ),
170 mock.call(
171 "puppet/resources", ("failed",), None, 0, enforce_ge=mock.ANY
172 ),
173 mock.call(
174 "puppet/resources", ("other",), None, 203, enforce_ge=mock.ANY
175 ),
176 mock.call(
177 "puppet/resources",
178 ("failed_to_restart",),
179 None,
180 0,
181 enforce_ge=mock.ANY,
182 ),
183 mock.call(
184 "puppet/resources",
185 ("out_of_sync",),
186 None,
187 7,
188 enforce_ge=mock.ANY,
189 ),
190 mock.call(
191 "puppet/times",
192 ("vcsrepo",),
193 None,
194 23.393381029,
195 enforce_ge=mock.ANY,
196 ),
197 mock.call(
198 "puppet/times",
199 ("exec",),
200 None,
201 11.494792536,
202 enforce_ge=mock.ANY,
203 ),
204 mock.call(
205 "puppet/times",
206 ("cron",),
207 None,
208 0.004638468,
209 enforce_ge=mock.ANY,
210 ),
211 mock.call(
212 "puppet/times",
213 ("file_line",),
214 None,
215 0.003589435,
216 enforce_ge=mock.ANY,
217 ),
218 mock.call(
219 "puppet/times",
220 ("config_retrieval",),
221 None,
222 2.862796974,
223 enforce_ge=mock.ANY,
224 ),
225 mock.call(
226 "puppet/times",
227 ("user",),
228 None,
229 0.001673407,
230 enforce_ge=mock.ANY,
231 ),
232 mock.call(
233 "puppet/times",
234 ("file",),
235 None,
236 0.618018423,
237 enforce_ge=mock.ANY,
238 ),
239 mock.call(
240 "puppet/times",
241 ("group",),
242 None,
243 0.017957332,
244 enforce_ge=mock.ANY,
245 ),
246 mock.call(
247 "puppet/times",
248 ("service",),
249 None,
250 10.242378408,
251 enforce_ge=mock.ANY,
252 ),
253 mock.call(
254 "puppet/times",
255 ("package",),
256 None,
257 4.315027644000001,
258 enforce_ge=mock.ANY,
259 ),
260 mock.call(
261 "puppet/times",
262 ("mount",),
263 None,
264 0.001416499,
265 enforce_ge=mock.ANY,
266 ),
267 mock.call(
268 "puppet/times",
269 ("schedule",),
270 None,
271 0.001541641,
272 enforce_ge=mock.ANY,
273 ),
274 mock.call(
275 "puppet/times", ("other",), None, 0.0, enforce_ge=mock.ANY
276 ),
277 mock.call(
278 "puppet/times",
279 ("ini_subsetting",),
280 None,
281 0.001235189,
282 enforce_ge=mock.ANY,
283 ),
284 mock.call(
285 "puppet/times",
286 ("filebucket",),
287 None,
288 0.000341392,
289 enforce_ge=mock.ANY,
290 ),
291 mock.call("puppet/age", (), None, 20329.0, enforce_ge=mock.ANY),
292 ]
293 setter.assert_has_calls(calls, any_order=True)
294 self.assertEqual(len(setter.mock_calls), len(calls))