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 |
| 16 | from chromite.scripts.sysmon import puppet_metrics |
| 17 | |
| 18 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 19 | _SUMMARY = """\ |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 20 | --- |
| 21 | version: |
| 22 | config: 1499979608 |
| 23 | puppet: "3.4.3" |
| 24 | resources: |
| 25 | changed: 7 |
| 26 | failed: 0 |
| 27 | failed_to_restart: 0 |
| 28 | out_of_sync: 7 |
| 29 | restarted: 0 |
| 30 | scheduled: 0 |
| 31 | skipped: 1 |
| 32 | total: 218 |
| 33 | time: |
| 34 | config_retrieval: 2.862796974 |
| 35 | cron: 0.004638468 |
| 36 | exec: 11.494792536 |
| 37 | file: 0.618018423 |
| 38 | file_line: 0.003589435 |
| 39 | filebucket: 0.000341392 |
| 40 | group: 0.017957332 |
| 41 | ini_subsetting: 0.001235189 |
| 42 | mount: 0.001416499 |
| 43 | package: 4.315027644000001 |
| 44 | schedule: 0.001541641 |
| 45 | service: 10.242378408 |
| 46 | total: 52.958788377 |
| 47 | user: 0.001673407 |
| 48 | vcsrepo: 23.393381029 |
| 49 | last_run: 1499979671 |
| 50 | changes: |
| 51 | total: 7 |
| 52 | events: |
| 53 | failure: 0 |
| 54 | success: 7 |
| 55 | total: 7% |
Mike Frysinger | 80de501 | 2019-08-01 14:10:53 -0400 | [diff] [blame] | 56 | """ |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 57 | |
| 58 | |
| 59 | class TestPuppetRunSummary(cros_test_lib.TestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 60 | """Tests for _PuppetRunSummary.""" |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 61 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 62 | def test_config_version(self): |
| 63 | summary = puppet_metrics._PuppetRunSummary(io.StringIO(_SUMMARY)) |
| 64 | self.assertEqual(summary.config_version, 1499979608) |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 65 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 66 | def test_puppet_version(self): |
| 67 | summary = puppet_metrics._PuppetRunSummary(io.StringIO(_SUMMARY)) |
| 68 | self.assertEqual(summary.puppet_version, "3.4.3") |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 69 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 70 | def test_events(self): |
| 71 | summary = puppet_metrics._PuppetRunSummary(io.StringIO(_SUMMARY)) |
| 72 | self.assertEqual(summary.events, {"failure": 0, "success": 7}) |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 73 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 74 | def test_resources(self): |
| 75 | summary = puppet_metrics._PuppetRunSummary(io.StringIO(_SUMMARY)) |
| 76 | self.assertEqual( |
| 77 | summary.resources, |
| 78 | { |
| 79 | "changed": 7, |
| 80 | "failed": 0, |
| 81 | "failed_to_restart": 0, |
| 82 | "out_of_sync": 7, |
| 83 | "restarted": 0, |
| 84 | "scheduled": 0, |
| 85 | "skipped": 1, |
| 86 | "other": 203, |
| 87 | }, |
| 88 | ) |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 89 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 90 | def test_times(self): |
| 91 | summary = puppet_metrics._PuppetRunSummary(io.StringIO(_SUMMARY)) |
| 92 | self.assertEqual( |
| 93 | summary.times, |
| 94 | { |
| 95 | "config_retrieval": 2.862796974, |
| 96 | "cron": 0.004638468, |
| 97 | "exec": 11.494792536, |
| 98 | "file": 0.618018423, |
| 99 | "file_line": 0.003589435, |
| 100 | "filebucket": 0.000341392, |
| 101 | "group": 0.017957332, |
| 102 | "ini_subsetting": 0.001235189, |
| 103 | "mount": 0.001416499, |
| 104 | "other": 0, |
| 105 | "package": 4.315027644000001, |
| 106 | "schedule": 0.001541641, |
| 107 | "service": 10.242378408, |
| 108 | "user": 0.001673407, |
| 109 | "vcsrepo": 23.393381029, |
| 110 | }, |
| 111 | ) |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 112 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 113 | def test_last_run_time(self): |
| 114 | summary = puppet_metrics._PuppetRunSummary(io.StringIO(_SUMMARY)) |
| 115 | self.assertEqual(summary.last_run_time, 1499979671) |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 116 | |
| 117 | |
| 118 | class TestPuppetMetrics(cros_test_lib.TempDirTestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 119 | """Tests for puppet_metrics.""" |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 120 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 121 | def setUp(self): |
| 122 | patcher = mock.patch( |
| 123 | "chromite.third_party.infra_libs.ts_mon.common.interface.state.store", |
| 124 | autospec=True, |
| 125 | ) |
| 126 | self.store = patcher.start() |
| 127 | self.addCleanup(patcher.stop) |
| 128 | self.tempfile = os.path.join(self.tempdir, "last_run_summary.yaml") |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 129 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 130 | def test_collect(self): |
| 131 | with open(self.tempfile, "w") as f: |
| 132 | f.write(_SUMMARY) |
| 133 | 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 Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 138 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 139 | 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)) |