blob: 243ea78a7bd4662c4752aa6f91bd065ab588d607 [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
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
16from chromite.scripts.sysmon import puppet_metrics
17
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):
Mike Frysinger8d6a51d2021-02-12 07:40:03 -0500119 patcher = mock.patch(
120 'chromite.third_party.infra_libs.ts_mon.common.interface.state.store',
121 autospec=True)
Allen Lid7a679c2017-07-13 15:16:08 -0700122 self.store = patcher.start()
123 self.addCleanup(patcher.stop)
124 self.tempfile = os.path.join(self.tempdir, 'last_run_summary.yaml')
125
126 def test_collect(self):
127 with open(self.tempfile, 'w') as f:
128 f.write(_SUMMARY)
Allen Lif8d67982017-10-09 18:45:59 -0700129 with mock.patch('time.time', return_value=1500000000):
130 with mock.patch.object(puppet_metrics, 'LAST_RUN_FILE', self.tempfile):
131 puppet_metrics.collect_puppet_summary()
Allen Lid7a679c2017-07-13 15:16:08 -0700132
133 setter = self.store.set
134 calls = [
135 mock.call('puppet/version/config', (), None,
136 1499979608, enforce_ge=mock.ANY),
137 mock.call('puppet/version/puppet', (), None,
138 '3.4.3', enforce_ge=mock.ANY),
139 mock.call('puppet/events', ('failure',), None,
140 0, enforce_ge=mock.ANY),
141 mock.call('puppet/events', ('success',), None,
142 7, enforce_ge=mock.ANY),
143 mock.call('puppet/resources', ('scheduled',), None,
144 0, enforce_ge=mock.ANY),
145 mock.call('puppet/resources', ('skipped',), None,
146 1, enforce_ge=mock.ANY),
147 mock.call('puppet/resources', ('restarted',), None,
148 0, enforce_ge=mock.ANY),
149 mock.call('puppet/resources', ('changed',), None,
150 7, enforce_ge=mock.ANY),
151 mock.call('puppet/resources', ('failed',), None,
152 0, enforce_ge=mock.ANY),
153 mock.call('puppet/resources', ('other',), None,
154 203, enforce_ge=mock.ANY),
155 mock.call('puppet/resources', ('failed_to_restart',), None,
156 0, enforce_ge=mock.ANY),
157 mock.call('puppet/resources', ('out_of_sync',), None,
158 7, enforce_ge=mock.ANY),
159 mock.call('puppet/times', ('vcsrepo',), None,
160 23.393381029, enforce_ge=mock.ANY),
161 mock.call('puppet/times', ('exec',), None,
162 11.494792536, enforce_ge=mock.ANY),
163 mock.call('puppet/times', ('cron',), None,
164 0.004638468, enforce_ge=mock.ANY),
165 mock.call('puppet/times', ('file_line',), None,
166 0.003589435, enforce_ge=mock.ANY),
167 mock.call('puppet/times', ('config_retrieval',), None,
168 2.862796974, enforce_ge=mock.ANY),
169 mock.call('puppet/times', ('user',), None,
170 0.001673407, enforce_ge=mock.ANY),
171 mock.call('puppet/times', ('file',), None,
172 0.618018423, enforce_ge=mock.ANY),
173 mock.call('puppet/times', ('group',), None,
174 0.017957332, enforce_ge=mock.ANY),
175 mock.call('puppet/times', ('service',), None,
176 10.242378408, enforce_ge=mock.ANY),
177 mock.call('puppet/times', ('package',), None,
178 4.315027644000001, enforce_ge=mock.ANY),
179 mock.call('puppet/times', ('mount',), None,
180 0.001416499, enforce_ge=mock.ANY),
181 mock.call('puppet/times', ('schedule',), None,
182 0.001541641, enforce_ge=mock.ANY),
183 mock.call('puppet/times', ('other',), None,
184 0.0, enforce_ge=mock.ANY),
185 mock.call('puppet/times', ('ini_subsetting',), None,
186 0.001235189, enforce_ge=mock.ANY),
187 mock.call('puppet/times', ('filebucket',), None,
188 0.000341392, enforce_ge=mock.ANY),
Allen Lif8d67982017-10-09 18:45:59 -0700189 mock.call('puppet/age', (), None,
190 20329.0, enforce_ge=mock.ANY),
Allen Lid7a679c2017-07-13 15:16:08 -0700191 ]
Mike Frysingera4aecbe2020-02-18 23:32:22 -0500192 setter.assert_has_calls(calls, any_order=True)
Allen Lid7a679c2017-07-13 15:16:08 -0700193 self.assertEqual(len(setter.mock_calls), len(calls))