blob: c1d933c37426ef412f3ef7edc44e73fb5c53eac6 [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(
124 "chromite.third_party.infra_libs.ts_mon.common.interface.state.store",
125 autospec=True,
126 )
127 self.store = patcher.start()
128 self.addCleanup(patcher.stop)
129 self.tempfile = os.path.join(self.tempdir, "last_run_summary.yaml")
Allen Lid7a679c2017-07-13 15:16:08 -0700130
Alex Klein1699fab2022-09-08 08:46:06 -0600131 def test_collect(self):
Mike Frysinger31fdddd2023-02-24 15:50:55 -0500132 osutils.WriteFile(self.tempfile, _SUMMARY)
Alex Klein1699fab2022-09-08 08:46:06 -0600133 with mock.patch("time.time", return_value=1500000000):
134 with mock.patch.object(
135 puppet_metrics, "LAST_RUN_FILE", self.tempfile
136 ):
137 puppet_metrics.collect_puppet_summary()
Allen Lid7a679c2017-07-13 15:16:08 -0700138
Alex Klein1699fab2022-09-08 08:46:06 -0600139 setter = self.store.set
140 calls = [
141 mock.call(
142 "puppet/version/config",
143 (),
144 None,
145 1499979608,
146 enforce_ge=mock.ANY,
147 ),
148 mock.call(
149 "puppet/version/puppet", (), None, "3.4.3", enforce_ge=mock.ANY
150 ),
151 mock.call(
152 "puppet/events", ("failure",), None, 0, enforce_ge=mock.ANY
153 ),
154 mock.call(
155 "puppet/events", ("success",), None, 7, enforce_ge=mock.ANY
156 ),
157 mock.call(
158 "puppet/resources", ("scheduled",), None, 0, enforce_ge=mock.ANY
159 ),
160 mock.call(
161 "puppet/resources", ("skipped",), None, 1, enforce_ge=mock.ANY
162 ),
163 mock.call(
164 "puppet/resources", ("restarted",), None, 0, enforce_ge=mock.ANY
165 ),
166 mock.call(
167 "puppet/resources", ("changed",), None, 7, enforce_ge=mock.ANY
168 ),
169 mock.call(
170 "puppet/resources", ("failed",), None, 0, enforce_ge=mock.ANY
171 ),
172 mock.call(
173 "puppet/resources", ("other",), None, 203, enforce_ge=mock.ANY
174 ),
175 mock.call(
176 "puppet/resources",
177 ("failed_to_restart",),
178 None,
179 0,
180 enforce_ge=mock.ANY,
181 ),
182 mock.call(
183 "puppet/resources",
184 ("out_of_sync",),
185 None,
186 7,
187 enforce_ge=mock.ANY,
188 ),
189 mock.call(
190 "puppet/times",
191 ("vcsrepo",),
192 None,
193 23.393381029,
194 enforce_ge=mock.ANY,
195 ),
196 mock.call(
197 "puppet/times",
198 ("exec",),
199 None,
200 11.494792536,
201 enforce_ge=mock.ANY,
202 ),
203 mock.call(
204 "puppet/times",
205 ("cron",),
206 None,
207 0.004638468,
208 enforce_ge=mock.ANY,
209 ),
210 mock.call(
211 "puppet/times",
212 ("file_line",),
213 None,
214 0.003589435,
215 enforce_ge=mock.ANY,
216 ),
217 mock.call(
218 "puppet/times",
219 ("config_retrieval",),
220 None,
221 2.862796974,
222 enforce_ge=mock.ANY,
223 ),
224 mock.call(
225 "puppet/times",
226 ("user",),
227 None,
228 0.001673407,
229 enforce_ge=mock.ANY,
230 ),
231 mock.call(
232 "puppet/times",
233 ("file",),
234 None,
235 0.618018423,
236 enforce_ge=mock.ANY,
237 ),
238 mock.call(
239 "puppet/times",
240 ("group",),
241 None,
242 0.017957332,
243 enforce_ge=mock.ANY,
244 ),
245 mock.call(
246 "puppet/times",
247 ("service",),
248 None,
249 10.242378408,
250 enforce_ge=mock.ANY,
251 ),
252 mock.call(
253 "puppet/times",
254 ("package",),
255 None,
256 4.315027644000001,
257 enforce_ge=mock.ANY,
258 ),
259 mock.call(
260 "puppet/times",
261 ("mount",),
262 None,
263 0.001416499,
264 enforce_ge=mock.ANY,
265 ),
266 mock.call(
267 "puppet/times",
268 ("schedule",),
269 None,
270 0.001541641,
271 enforce_ge=mock.ANY,
272 ),
273 mock.call(
274 "puppet/times", ("other",), None, 0.0, enforce_ge=mock.ANY
275 ),
276 mock.call(
277 "puppet/times",
278 ("ini_subsetting",),
279 None,
280 0.001235189,
281 enforce_ge=mock.ANY,
282 ),
283 mock.call(
284 "puppet/times",
285 ("filebucket",),
286 None,
287 0.000341392,
288 enforce_ge=mock.ANY,
289 ),
290 mock.call("puppet/age", (), None, 20329.0, enforce_ge=mock.ANY),
291 ]
292 setter.assert_has_calls(calls, any_order=True)
293 self.assertEqual(len(setter.mock_calls), len(calls))