blob: d74161dbee964c8c37b6115c324d3aacff617214 [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
10from __future__ import print_function
11
12from cStringIO import StringIO
13import os
14
15import mock
16
17from chromite.lib import cros_test_lib
18from chromite.scripts.sysmon import puppet_metrics
19
20
21_SUMMARY = '''\
22---
23 version:
24 config: 1499979608
25 puppet: "3.4.3"
26 resources:
27 changed: 7
28 failed: 0
29 failed_to_restart: 0
30 out_of_sync: 7
31 restarted: 0
32 scheduled: 0
33 skipped: 1
34 total: 218
35 time:
36 config_retrieval: 2.862796974
37 cron: 0.004638468
38 exec: 11.494792536
39 file: 0.618018423
40 file_line: 0.003589435
41 filebucket: 0.000341392
42 group: 0.017957332
43 ini_subsetting: 0.001235189
44 mount: 0.001416499
45 package: 4.315027644000001
46 schedule: 0.001541641
47 service: 10.242378408
48 total: 52.958788377
49 user: 0.001673407
50 vcsrepo: 23.393381029
51 last_run: 1499979671
52 changes:
53 total: 7
54 events:
55 failure: 0
56 success: 7
57 total: 7%
58'''
59
60
61class TestPuppetRunSummary(cros_test_lib.TestCase):
62 """Tests for _PuppetRunSummary."""
63
64 def test_config_version(self):
65 summary = puppet_metrics._PuppetRunSummary(StringIO(_SUMMARY))
66 self.assertEqual(summary.config_version, 1499979608)
67
68 def test_puppet_version(self):
69 summary = puppet_metrics._PuppetRunSummary(StringIO(_SUMMARY))
70 self.assertEqual(summary.puppet_version, '3.4.3')
71
72 def test_events(self):
73 summary = puppet_metrics._PuppetRunSummary(StringIO(_SUMMARY))
74 self.assertEqual(summary.events, {
75 'failure': 0,
76 'success': 7
77 })
78
79 def test_resources(self):
80 summary = puppet_metrics._PuppetRunSummary(StringIO(_SUMMARY))
81 self.assertEqual(summary.resources, {
82 'changed': 7,
83 'failed': 0,
84 'failed_to_restart': 0,
85 'out_of_sync': 7,
86 'restarted': 0,
87 'scheduled': 0,
88 'skipped': 1,
89 'other': 203,
90 })
91
92 def test_times(self):
93 summary = puppet_metrics._PuppetRunSummary(StringIO(_SUMMARY))
94 self.assertEqual(summary.times, {
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
112 def test_last_run_time(self):
113 summary = puppet_metrics._PuppetRunSummary(StringIO(_SUMMARY))
114 self.assertEqual(summary.last_run_time, 1499979671)
115
116
117class TestPuppetMetrics(cros_test_lib.TempDirTestCase):
118 """Tests for puppet_metrics."""
119
120 def setUp(self):
121 patcher = mock.patch('infra_libs.ts_mon.common.interface.state.store',
122 autospec=True)
123 self.store = patcher.start()
124 self.addCleanup(patcher.stop)
125 self.tempfile = os.path.join(self.tempdir, 'last_run_summary.yaml')
126
127 def test_collect(self):
128 with open(self.tempfile, 'w') as f:
129 f.write(_SUMMARY)
130 with mock.patch.object(puppet_metrics, 'LAST_RUN_FILE', self.tempfile):
131 puppet_metrics.collect_puppet_summary()
132
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),
189 ]
190 setter.assert_has_calls(calls)
191 self.assertEqual(len(setter.mock_calls), len(calls))