Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 1 | # 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 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 |
| 13 | |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 14 | from chromite.lib import cros_test_lib |
| 15 | from chromite.scripts.sysmon import puppet_metrics |
Mike Frysinger | 40ffb53 | 2021-02-12 07:36:08 -0500 | [diff] [blame] | 16 | from chromite.third_party import mock |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 17 | |
| 18 | |
Mike Frysinger | 4f6e7a7 | 2019-08-28 11:17:13 -0400 | [diff] [blame] | 19 | _SUMMARY = u"""\ |
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): |
| 60 | """Tests for _PuppetRunSummary.""" |
| 61 | |
| 62 | def test_config_version(self): |
Mike Frysinger | 4f6e7a7 | 2019-08-28 11:17:13 -0400 | [diff] [blame] | 63 | summary = puppet_metrics._PuppetRunSummary(io.StringIO(_SUMMARY)) |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 64 | self.assertEqual(summary.config_version, 1499979608) |
| 65 | |
| 66 | def test_puppet_version(self): |
Mike Frysinger | 4f6e7a7 | 2019-08-28 11:17:13 -0400 | [diff] [blame] | 67 | summary = puppet_metrics._PuppetRunSummary(io.StringIO(_SUMMARY)) |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 68 | self.assertEqual(summary.puppet_version, '3.4.3') |
| 69 | |
| 70 | def test_events(self): |
Mike Frysinger | 4f6e7a7 | 2019-08-28 11:17:13 -0400 | [diff] [blame] | 71 | summary = puppet_metrics._PuppetRunSummary(io.StringIO(_SUMMARY)) |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 72 | self.assertEqual(summary.events, { |
| 73 | 'failure': 0, |
| 74 | 'success': 7 |
| 75 | }) |
| 76 | |
| 77 | def test_resources(self): |
Mike Frysinger | 4f6e7a7 | 2019-08-28 11:17:13 -0400 | [diff] [blame] | 78 | summary = puppet_metrics._PuppetRunSummary(io.StringIO(_SUMMARY)) |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 79 | self.assertEqual(summary.resources, { |
| 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 | |
| 90 | def test_times(self): |
Mike Frysinger | 4f6e7a7 | 2019-08-28 11:17:13 -0400 | [diff] [blame] | 91 | summary = puppet_metrics._PuppetRunSummary(io.StringIO(_SUMMARY)) |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 92 | self.assertEqual(summary.times, { |
| 93 | 'config_retrieval': 2.862796974, |
| 94 | 'cron': 0.004638468, |
| 95 | 'exec': 11.494792536, |
| 96 | 'file': 0.618018423, |
| 97 | 'file_line': 0.003589435, |
| 98 | 'filebucket': 0.000341392, |
| 99 | 'group': 0.017957332, |
| 100 | 'ini_subsetting': 0.001235189, |
| 101 | 'mount': 0.001416499, |
| 102 | 'other': 0, |
| 103 | 'package': 4.315027644000001, |
| 104 | 'schedule': 0.001541641, |
| 105 | 'service': 10.242378408, |
| 106 | 'user': 0.001673407, |
| 107 | 'vcsrepo': 23.393381029, |
| 108 | }) |
| 109 | |
| 110 | def test_last_run_time(self): |
Mike Frysinger | 4f6e7a7 | 2019-08-28 11:17:13 -0400 | [diff] [blame] | 111 | summary = puppet_metrics._PuppetRunSummary(io.StringIO(_SUMMARY)) |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 112 | self.assertEqual(summary.last_run_time, 1499979671) |
| 113 | |
| 114 | |
| 115 | class TestPuppetMetrics(cros_test_lib.TempDirTestCase): |
| 116 | """Tests for puppet_metrics.""" |
| 117 | |
| 118 | def setUp(self): |
| 119 | patcher = mock.patch('infra_libs.ts_mon.common.interface.state.store', |
| 120 | autospec=True) |
| 121 | self.store = patcher.start() |
| 122 | self.addCleanup(patcher.stop) |
| 123 | self.tempfile = os.path.join(self.tempdir, 'last_run_summary.yaml') |
| 124 | |
| 125 | def test_collect(self): |
| 126 | with open(self.tempfile, 'w') as f: |
| 127 | f.write(_SUMMARY) |
Allen Li | f8d6798 | 2017-10-09 18:45:59 -0700 | [diff] [blame] | 128 | with mock.patch('time.time', return_value=1500000000): |
| 129 | with mock.patch.object(puppet_metrics, 'LAST_RUN_FILE', self.tempfile): |
| 130 | puppet_metrics.collect_puppet_summary() |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 131 | |
| 132 | setter = self.store.set |
| 133 | calls = [ |
| 134 | mock.call('puppet/version/config', (), None, |
| 135 | 1499979608, enforce_ge=mock.ANY), |
| 136 | mock.call('puppet/version/puppet', (), None, |
| 137 | '3.4.3', enforce_ge=mock.ANY), |
| 138 | mock.call('puppet/events', ('failure',), None, |
| 139 | 0, enforce_ge=mock.ANY), |
| 140 | mock.call('puppet/events', ('success',), None, |
| 141 | 7, enforce_ge=mock.ANY), |
| 142 | mock.call('puppet/resources', ('scheduled',), None, |
| 143 | 0, enforce_ge=mock.ANY), |
| 144 | mock.call('puppet/resources', ('skipped',), None, |
| 145 | 1, enforce_ge=mock.ANY), |
| 146 | mock.call('puppet/resources', ('restarted',), None, |
| 147 | 0, enforce_ge=mock.ANY), |
| 148 | mock.call('puppet/resources', ('changed',), None, |
| 149 | 7, enforce_ge=mock.ANY), |
| 150 | mock.call('puppet/resources', ('failed',), None, |
| 151 | 0, enforce_ge=mock.ANY), |
| 152 | mock.call('puppet/resources', ('other',), None, |
| 153 | 203, enforce_ge=mock.ANY), |
| 154 | mock.call('puppet/resources', ('failed_to_restart',), None, |
| 155 | 0, enforce_ge=mock.ANY), |
| 156 | mock.call('puppet/resources', ('out_of_sync',), None, |
| 157 | 7, enforce_ge=mock.ANY), |
| 158 | mock.call('puppet/times', ('vcsrepo',), None, |
| 159 | 23.393381029, enforce_ge=mock.ANY), |
| 160 | mock.call('puppet/times', ('exec',), None, |
| 161 | 11.494792536, enforce_ge=mock.ANY), |
| 162 | mock.call('puppet/times', ('cron',), None, |
| 163 | 0.004638468, enforce_ge=mock.ANY), |
| 164 | mock.call('puppet/times', ('file_line',), None, |
| 165 | 0.003589435, enforce_ge=mock.ANY), |
| 166 | mock.call('puppet/times', ('config_retrieval',), None, |
| 167 | 2.862796974, enforce_ge=mock.ANY), |
| 168 | mock.call('puppet/times', ('user',), None, |
| 169 | 0.001673407, enforce_ge=mock.ANY), |
| 170 | mock.call('puppet/times', ('file',), None, |
| 171 | 0.618018423, enforce_ge=mock.ANY), |
| 172 | mock.call('puppet/times', ('group',), None, |
| 173 | 0.017957332, enforce_ge=mock.ANY), |
| 174 | mock.call('puppet/times', ('service',), None, |
| 175 | 10.242378408, enforce_ge=mock.ANY), |
| 176 | mock.call('puppet/times', ('package',), None, |
| 177 | 4.315027644000001, enforce_ge=mock.ANY), |
| 178 | mock.call('puppet/times', ('mount',), None, |
| 179 | 0.001416499, enforce_ge=mock.ANY), |
| 180 | mock.call('puppet/times', ('schedule',), None, |
| 181 | 0.001541641, enforce_ge=mock.ANY), |
| 182 | mock.call('puppet/times', ('other',), None, |
| 183 | 0.0, enforce_ge=mock.ANY), |
| 184 | mock.call('puppet/times', ('ini_subsetting',), None, |
| 185 | 0.001235189, enforce_ge=mock.ANY), |
| 186 | mock.call('puppet/times', ('filebucket',), None, |
| 187 | 0.000341392, enforce_ge=mock.ANY), |
Allen Li | f8d6798 | 2017-10-09 18:45:59 -0700 | [diff] [blame] | 188 | mock.call('puppet/age', (), None, |
| 189 | 20329.0, enforce_ge=mock.ANY), |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 190 | ] |
Mike Frysinger | a4aecbe | 2020-02-18 23:32:22 -0500 | [diff] [blame] | 191 | setter.assert_has_calls(calls, any_order=True) |
Allen Li | d7a679c | 2017-07-13 15:16:08 -0700 | [diff] [blame] | 192 | self.assertEqual(len(setter.mock_calls), len(calls)) |