blob: 8556527a55a1f55c597817ad83843ed31c60c6a8 [file] [log] [blame]
Mike Frysingere58c0e22017-10-04 15:43:30 -04001# -*- coding: utf-8 -*-
Allen Lid7a679c2017-07-13 15:16:08 -07002# Copyright 2017 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Unit tests for puppet_metrics."""
7
8# pylint: disable=protected-access
9
10from __future__ import absolute_import
11from __future__ import print_function
12
13from cStringIO import StringIO
14import os
15
16import mock
17
18from chromite.lib import cros_test_lib
19from chromite.scripts.sysmon import puppet_metrics
20
21
22_SUMMARY = '''\
23---
24 version:
25 config: 1499979608
26 puppet: "3.4.3"
27 resources:
28 changed: 7
29 failed: 0
30 failed_to_restart: 0
31 out_of_sync: 7
32 restarted: 0
33 scheduled: 0
34 skipped: 1
35 total: 218
36 time:
37 config_retrieval: 2.862796974
38 cron: 0.004638468
39 exec: 11.494792536
40 file: 0.618018423
41 file_line: 0.003589435
42 filebucket: 0.000341392
43 group: 0.017957332
44 ini_subsetting: 0.001235189
45 mount: 0.001416499
46 package: 4.315027644000001
47 schedule: 0.001541641
48 service: 10.242378408
49 total: 52.958788377
50 user: 0.001673407
51 vcsrepo: 23.393381029
52 last_run: 1499979671
53 changes:
54 total: 7
55 events:
56 failure: 0
57 success: 7
58 total: 7%
59'''
60
61
62class TestPuppetRunSummary(cros_test_lib.TestCase):
63 """Tests for _PuppetRunSummary."""
64
65 def test_config_version(self):
66 summary = puppet_metrics._PuppetRunSummary(StringIO(_SUMMARY))
67 self.assertEqual(summary.config_version, 1499979608)
68
69 def test_puppet_version(self):
70 summary = puppet_metrics._PuppetRunSummary(StringIO(_SUMMARY))
71 self.assertEqual(summary.puppet_version, '3.4.3')
72
73 def test_events(self):
74 summary = puppet_metrics._PuppetRunSummary(StringIO(_SUMMARY))
75 self.assertEqual(summary.events, {
76 'failure': 0,
77 'success': 7
78 })
79
80 def test_resources(self):
81 summary = puppet_metrics._PuppetRunSummary(StringIO(_SUMMARY))
82 self.assertEqual(summary.resources, {
83 'changed': 7,
84 'failed': 0,
85 'failed_to_restart': 0,
86 'out_of_sync': 7,
87 'restarted': 0,
88 'scheduled': 0,
89 'skipped': 1,
90 'other': 203,
91 })
92
93 def test_times(self):
94 summary = puppet_metrics._PuppetRunSummary(StringIO(_SUMMARY))
95 self.assertEqual(summary.times, {
96 'config_retrieval': 2.862796974,
97 'cron': 0.004638468,
98 'exec': 11.494792536,
99 'file': 0.618018423,
100 'file_line': 0.003589435,
101 'filebucket': 0.000341392,
102 'group': 0.017957332,
103 'ini_subsetting': 0.001235189,
104 'mount': 0.001416499,
105 'other': 0,
106 'package': 4.315027644000001,
107 'schedule': 0.001541641,
108 'service': 10.242378408,
109 'user': 0.001673407,
110 'vcsrepo': 23.393381029,
111 })
112
113 def test_last_run_time(self):
114 summary = puppet_metrics._PuppetRunSummary(StringIO(_SUMMARY))
115 self.assertEqual(summary.last_run_time, 1499979671)
116
117
118class TestPuppetMetrics(cros_test_lib.TempDirTestCase):
119 """Tests for puppet_metrics."""
120
121 def setUp(self):
122 patcher = mock.patch('infra_libs.ts_mon.common.interface.state.store',
123 autospec=True)
124 self.store = patcher.start()
125 self.addCleanup(patcher.stop)
126 self.tempfile = os.path.join(self.tempdir, 'last_run_summary.yaml')
127
128 def test_collect(self):
129 with open(self.tempfile, 'w') as f:
130 f.write(_SUMMARY)
131 with mock.patch.object(puppet_metrics, 'LAST_RUN_FILE', self.tempfile):
132 puppet_metrics.collect_puppet_summary()
133
134 setter = self.store.set
135 calls = [
136 mock.call('puppet/version/config', (), None,
137 1499979608, enforce_ge=mock.ANY),
138 mock.call('puppet/version/puppet', (), None,
139 '3.4.3', enforce_ge=mock.ANY),
140 mock.call('puppet/events', ('failure',), None,
141 0, enforce_ge=mock.ANY),
142 mock.call('puppet/events', ('success',), None,
143 7, enforce_ge=mock.ANY),
144 mock.call('puppet/resources', ('scheduled',), None,
145 0, enforce_ge=mock.ANY),
146 mock.call('puppet/resources', ('skipped',), None,
147 1, enforce_ge=mock.ANY),
148 mock.call('puppet/resources', ('restarted',), None,
149 0, enforce_ge=mock.ANY),
150 mock.call('puppet/resources', ('changed',), None,
151 7, enforce_ge=mock.ANY),
152 mock.call('puppet/resources', ('failed',), None,
153 0, enforce_ge=mock.ANY),
154 mock.call('puppet/resources', ('other',), None,
155 203, enforce_ge=mock.ANY),
156 mock.call('puppet/resources', ('failed_to_restart',), None,
157 0, enforce_ge=mock.ANY),
158 mock.call('puppet/resources', ('out_of_sync',), None,
159 7, enforce_ge=mock.ANY),
160 mock.call('puppet/times', ('vcsrepo',), None,
161 23.393381029, enforce_ge=mock.ANY),
162 mock.call('puppet/times', ('exec',), None,
163 11.494792536, enforce_ge=mock.ANY),
164 mock.call('puppet/times', ('cron',), None,
165 0.004638468, enforce_ge=mock.ANY),
166 mock.call('puppet/times', ('file_line',), None,
167 0.003589435, enforce_ge=mock.ANY),
168 mock.call('puppet/times', ('config_retrieval',), None,
169 2.862796974, enforce_ge=mock.ANY),
170 mock.call('puppet/times', ('user',), None,
171 0.001673407, enforce_ge=mock.ANY),
172 mock.call('puppet/times', ('file',), None,
173 0.618018423, enforce_ge=mock.ANY),
174 mock.call('puppet/times', ('group',), None,
175 0.017957332, enforce_ge=mock.ANY),
176 mock.call('puppet/times', ('service',), None,
177 10.242378408, enforce_ge=mock.ANY),
178 mock.call('puppet/times', ('package',), None,
179 4.315027644000001, enforce_ge=mock.ANY),
180 mock.call('puppet/times', ('mount',), None,
181 0.001416499, enforce_ge=mock.ANY),
182 mock.call('puppet/times', ('schedule',), None,
183 0.001541641, enforce_ge=mock.ANY),
184 mock.call('puppet/times', ('other',), None,
185 0.0, enforce_ge=mock.ANY),
186 mock.call('puppet/times', ('ini_subsetting',), None,
187 0.001235189, enforce_ge=mock.ANY),
188 mock.call('puppet/times', ('filebucket',), None,
189 0.000341392, enforce_ge=mock.ANY),
190 ]
191 setter.assert_has_calls(calls)
192 self.assertEqual(len(setter.mock_calls), len(calls))