blob: daae6e01031357336a2999c02d93571138eb5035 [file] [log] [blame]
Allen Lid7a679c2017-07-13 15:16:08 -07001# 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
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
13
Allen Lid7a679c2017-07-13 15:16:08 -070014from chromite.lib import cros_test_lib
15from chromite.scripts.sysmon import puppet_metrics
Mike Frysinger40ffb532021-02-12 07:36:08 -050016from chromite.third_party import mock
Allen Lid7a679c2017-07-13 15:16:08 -070017
18
Mike Frysinger4f6e7a72019-08-28 11:17:13 -040019_SUMMARY = u"""\
Allen Lid7a679c2017-07-13 15:16:08 -070020---
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 Frysinger80de5012019-08-01 14:10:53 -040056"""
Allen Lid7a679c2017-07-13 15:16:08 -070057
58
59class TestPuppetRunSummary(cros_test_lib.TestCase):
60 """Tests for _PuppetRunSummary."""
61
62 def test_config_version(self):
Mike Frysinger4f6e7a72019-08-28 11:17:13 -040063 summary = puppet_metrics._PuppetRunSummary(io.StringIO(_SUMMARY))
Allen Lid7a679c2017-07-13 15:16:08 -070064 self.assertEqual(summary.config_version, 1499979608)
65
66 def test_puppet_version(self):
Mike Frysinger4f6e7a72019-08-28 11:17:13 -040067 summary = puppet_metrics._PuppetRunSummary(io.StringIO(_SUMMARY))
Allen Lid7a679c2017-07-13 15:16:08 -070068 self.assertEqual(summary.puppet_version, '3.4.3')
69
70 def test_events(self):
Mike Frysinger4f6e7a72019-08-28 11:17:13 -040071 summary = puppet_metrics._PuppetRunSummary(io.StringIO(_SUMMARY))
Allen Lid7a679c2017-07-13 15:16:08 -070072 self.assertEqual(summary.events, {
73 'failure': 0,
74 'success': 7
75 })
76
77 def test_resources(self):
Mike Frysinger4f6e7a72019-08-28 11:17:13 -040078 summary = puppet_metrics._PuppetRunSummary(io.StringIO(_SUMMARY))
Allen Lid7a679c2017-07-13 15:16:08 -070079 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 Frysinger4f6e7a72019-08-28 11:17:13 -040091 summary = puppet_metrics._PuppetRunSummary(io.StringIO(_SUMMARY))
Allen Lid7a679c2017-07-13 15:16:08 -070092 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 Frysinger4f6e7a72019-08-28 11:17:13 -0400111 summary = puppet_metrics._PuppetRunSummary(io.StringIO(_SUMMARY))
Allen Lid7a679c2017-07-13 15:16:08 -0700112 self.assertEqual(summary.last_run_time, 1499979671)
113
114
115class 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 Lif8d67982017-10-09 18:45:59 -0700128 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 Lid7a679c2017-07-13 15:16:08 -0700131
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 Lif8d67982017-10-09 18:45:59 -0700188 mock.call('puppet/age', (), None,
189 20329.0, enforce_ge=mock.ANY),
Allen Lid7a679c2017-07-13 15:16:08 -0700190 ]
Mike Frysingera4aecbe2020-02-18 23:32:22 -0500191 setter.assert_has_calls(calls, any_order=True)
Allen Lid7a679c2017-07-13 15:16:08 -0700192 self.assertEqual(len(setter.mock_calls), len(calls))