Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2017 The ChromiumOS Authors |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 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 puppet_metrics.""" |
| 6 | |
| 7 | # pylint: disable=protected-access |
| 8 | |
| 9 | from __future__ import absolute_import |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 10 | |
Mike Frysinger | 4f6e7a7 | 2019-08-28 11:17:13 -0400 | [diff] [blame] | 11 | import io |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 12 | import os |
Mike Frysinger | 166fea0 | 2021-02-12 05:30:33 -0500 | [diff] [blame] | 13 | from unittest import mock |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 14 | |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 15 | from chromite.lib import cros_test_lib |
Mike Frysinger | 31fdddd | 2023-02-24 15:50:55 -0500 | [diff] [blame] | 16 | from chromite.lib import osutils |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 17 | from chromite.scripts.sysmon import puppet_metrics |
| 18 | |
| 19 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 20 | _SUMMARY = """\ |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 21 | --- |
| 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 Frysinger | 80de501 | 2019-08-01 14:10:53 -0400 | [diff] [blame] | 57 | """ |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 58 | |
| 59 | |
| 60 | class TestPuppetRunSummary(cros_test_lib.TestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 61 | """Tests for _PuppetRunSummary.""" |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 62 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 63 | def test_config_version(self): |
| 64 | summary = puppet_metrics._PuppetRunSummary(io.StringIO(_SUMMARY)) |
| 65 | self.assertEqual(summary.config_version, 1499979608) |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 66 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 67 | def test_puppet_version(self): |
| 68 | summary = puppet_metrics._PuppetRunSummary(io.StringIO(_SUMMARY)) |
| 69 | self.assertEqual(summary.puppet_version, "3.4.3") |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 70 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 71 | def test_events(self): |
| 72 | summary = puppet_metrics._PuppetRunSummary(io.StringIO(_SUMMARY)) |
| 73 | self.assertEqual(summary.events, {"failure": 0, "success": 7}) |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 74 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 75 | 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 Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 90 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 91 | 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 Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 113 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 114 | def test_last_run_time(self): |
| 115 | summary = puppet_metrics._PuppetRunSummary(io.StringIO(_SUMMARY)) |
| 116 | self.assertEqual(summary.last_run_time, 1499979671) |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 117 | |
| 118 | |
| 119 | class TestPuppetMetrics(cros_test_lib.TempDirTestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 120 | """Tests for puppet_metrics.""" |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 121 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 122 | def setUp(self): |
| 123 | patcher = mock.patch( |
Trent Apted | c4f366a | 2023-05-16 15:32:48 +1000 | [diff] [blame^] | 124 | "chromite.third_party.infra_libs.ts_mon.common.interface.state." |
| 125 | "store", |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 126 | 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 Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 131 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 132 | def test_collect(self): |
Mike Frysinger | 31fdddd | 2023-02-24 15:50:55 -0500 | [diff] [blame] | 133 | osutils.WriteFile(self.tempfile, _SUMMARY) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 134 | 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 Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 139 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 140 | 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)) |