blob: d5595619484594c73bc5fc6032b3121c0e6f35cf [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
Mike Frysinger4f6e7a72019-08-28 11:17:13 -040013import io
Allen Lid7a679c2017-07-13 15:16:08 -070014import os
15
16import mock
17
18from chromite.lib import cros_test_lib
19from chromite.scripts.sysmon import puppet_metrics
20
21
Mike Frysinger4f6e7a72019-08-28 11:17:13 -040022_SUMMARY = u"""\
Allen Lid7a679c2017-07-13 15:16:08 -070023---
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%
Mike Frysinger80de5012019-08-01 14:10:53 -040059"""
Allen Lid7a679c2017-07-13 15:16:08 -070060
61
62class TestPuppetRunSummary(cros_test_lib.TestCase):
63 """Tests for _PuppetRunSummary."""
64
65 def test_config_version(self):
Mike Frysinger4f6e7a72019-08-28 11:17:13 -040066 summary = puppet_metrics._PuppetRunSummary(io.StringIO(_SUMMARY))
Allen Lid7a679c2017-07-13 15:16:08 -070067 self.assertEqual(summary.config_version, 1499979608)
68
69 def test_puppet_version(self):
Mike Frysinger4f6e7a72019-08-28 11:17:13 -040070 summary = puppet_metrics._PuppetRunSummary(io.StringIO(_SUMMARY))
Allen Lid7a679c2017-07-13 15:16:08 -070071 self.assertEqual(summary.puppet_version, '3.4.3')
72
73 def test_events(self):
Mike Frysinger4f6e7a72019-08-28 11:17:13 -040074 summary = puppet_metrics._PuppetRunSummary(io.StringIO(_SUMMARY))
Allen Lid7a679c2017-07-13 15:16:08 -070075 self.assertEqual(summary.events, {
76 'failure': 0,
77 'success': 7
78 })
79
80 def test_resources(self):
Mike Frysinger4f6e7a72019-08-28 11:17:13 -040081 summary = puppet_metrics._PuppetRunSummary(io.StringIO(_SUMMARY))
Allen Lid7a679c2017-07-13 15:16:08 -070082 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):
Mike Frysinger4f6e7a72019-08-28 11:17:13 -040094 summary = puppet_metrics._PuppetRunSummary(io.StringIO(_SUMMARY))
Allen Lid7a679c2017-07-13 15:16:08 -070095 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):
Mike Frysinger4f6e7a72019-08-28 11:17:13 -0400114 summary = puppet_metrics._PuppetRunSummary(io.StringIO(_SUMMARY))
Allen Lid7a679c2017-07-13 15:16:08 -0700115 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)
Allen Lif8d67982017-10-09 18:45:59 -0700131 with mock.patch('time.time', return_value=1500000000):
132 with mock.patch.object(puppet_metrics, 'LAST_RUN_FILE', self.tempfile):
133 puppet_metrics.collect_puppet_summary()
Allen Lid7a679c2017-07-13 15:16:08 -0700134
135 setter = self.store.set
136 calls = [
137 mock.call('puppet/version/config', (), None,
138 1499979608, enforce_ge=mock.ANY),
139 mock.call('puppet/version/puppet', (), None,
140 '3.4.3', enforce_ge=mock.ANY),
141 mock.call('puppet/events', ('failure',), None,
142 0, enforce_ge=mock.ANY),
143 mock.call('puppet/events', ('success',), None,
144 7, enforce_ge=mock.ANY),
145 mock.call('puppet/resources', ('scheduled',), None,
146 0, enforce_ge=mock.ANY),
147 mock.call('puppet/resources', ('skipped',), None,
148 1, enforce_ge=mock.ANY),
149 mock.call('puppet/resources', ('restarted',), None,
150 0, enforce_ge=mock.ANY),
151 mock.call('puppet/resources', ('changed',), None,
152 7, enforce_ge=mock.ANY),
153 mock.call('puppet/resources', ('failed',), None,
154 0, enforce_ge=mock.ANY),
155 mock.call('puppet/resources', ('other',), None,
156 203, enforce_ge=mock.ANY),
157 mock.call('puppet/resources', ('failed_to_restart',), None,
158 0, enforce_ge=mock.ANY),
159 mock.call('puppet/resources', ('out_of_sync',), None,
160 7, enforce_ge=mock.ANY),
161 mock.call('puppet/times', ('vcsrepo',), None,
162 23.393381029, enforce_ge=mock.ANY),
163 mock.call('puppet/times', ('exec',), None,
164 11.494792536, enforce_ge=mock.ANY),
165 mock.call('puppet/times', ('cron',), None,
166 0.004638468, enforce_ge=mock.ANY),
167 mock.call('puppet/times', ('file_line',), None,
168 0.003589435, enforce_ge=mock.ANY),
169 mock.call('puppet/times', ('config_retrieval',), None,
170 2.862796974, enforce_ge=mock.ANY),
171 mock.call('puppet/times', ('user',), None,
172 0.001673407, enforce_ge=mock.ANY),
173 mock.call('puppet/times', ('file',), None,
174 0.618018423, enforce_ge=mock.ANY),
175 mock.call('puppet/times', ('group',), None,
176 0.017957332, enforce_ge=mock.ANY),
177 mock.call('puppet/times', ('service',), None,
178 10.242378408, enforce_ge=mock.ANY),
179 mock.call('puppet/times', ('package',), None,
180 4.315027644000001, enforce_ge=mock.ANY),
181 mock.call('puppet/times', ('mount',), None,
182 0.001416499, enforce_ge=mock.ANY),
183 mock.call('puppet/times', ('schedule',), None,
184 0.001541641, enforce_ge=mock.ANY),
185 mock.call('puppet/times', ('other',), None,
186 0.0, enforce_ge=mock.ANY),
187 mock.call('puppet/times', ('ini_subsetting',), None,
188 0.001235189, enforce_ge=mock.ANY),
189 mock.call('puppet/times', ('filebucket',), None,
190 0.000341392, enforce_ge=mock.ANY),
Allen Lif8d67982017-10-09 18:45:59 -0700191 mock.call('puppet/age', (), None,
192 20329.0, enforce_ge=mock.ANY),
Allen Lid7a679c2017-07-13 15:16:08 -0700193 ]
Mike Frysingera4aecbe2020-02-18 23:32:22 -0500194 setter.assert_has_calls(calls, any_order=True)
Allen Lid7a679c2017-07-13 15:16:08 -0700195 self.assertEqual(len(setter.mock_calls), len(calls))