Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(407)

Side by Side Diff: infra/services/mastermon/test/pollers_test.py

Issue 1877953002: mastermon: collect step results count (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Refactoring + tests Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « infra/services/mastermon/pollers.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright (c) 2015 The Chromium Authors. All rights reserved. 1 # Copyright (c) 2015 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 import json 5 import json
6 import os 6 import os
7 import tempfile
8 import time 7 import time
9 import unittest 8 import unittest
10 9
11 import mock 10 import mock
12 import requests 11 import requests
13 12
14 from infra_libs import temporary_directory 13 from infra_libs import temporary_directory
15 from infra_libs import ts_mon 14 from infra_libs import ts_mon
16 from infra.services.mastermon import pollers 15 from infra.services.mastermon import pollers
17 16
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 f.write('%s\n' % json.dumps(data)) 203 f.write('%s\n' % json.dumps(data))
205 return f.name 204 return f.name
206 205
207 def test_no_file(self): 206 def test_no_file(self):
208 with temporary_directory(prefix='poller-test-') as tempdir: 207 with temporary_directory(prefix='poller-test-') as tempdir:
209 filename = os.path.join(tempdir, 'no-such-file') 208 filename = os.path.join(tempdir, 'no-such-file')
210 p = pollers.FilePoller(filename, {}) 209 p = pollers.FilePoller(filename, {})
211 self.assertTrue(p.poll()) 210 self.assertTrue(p.poll())
212 self.assertFalse(os.path.isfile(pollers.rotated_filename(filename))) 211 self.assertFalse(os.path.isfile(pollers.rotated_filename(filename)))
213 212
214 @mock.patch('infra_libs.ts_mon.CounterMetric.increment') 213 def test_file_has_data(self):
215 @mock.patch('infra_libs.ts_mon.CumulativeDistributionMetric.add')
216 def test_file_has_data(self, fake_add, fake_increment):
217 result1 = {'builder': 'b1', 'slave': 's1', 214 result1 = {'builder': 'b1', 'slave': 's1',
218 'result': 'r1', 'project_id': 'chromium', 215 'result': 'r1', 'project_id': 'chromium',
219 'subproject_tag': 'unknown'} 216 'subproject_tag': 'unknown'}
220 result2 = {'builder': 'b1', 'slave': 's1', 217 result2 = {'builder': 'b1', 'slave': 's1',
221 'result': 'r1', 'project_id': 'unknown', 218 'result': 'r1', 'project_id': 'unknown',
222 'subproject_tag': 'unknown'} 219 'subproject_tag': 'unknown'}
220 result3 = {'builder': 'b1', 'slave': 's1',
221 'step_result': 'r1', 'project_id': 'chromium',
222 'subproject_tag': 'unknown'}
223 # Check that we've listed all the required metric fields. 223 # Check that we've listed all the required metric fields.
224 self.assertEqual(set(result1), set(pollers.FilePoller.field_keys)) 224 self.assertEqual(set(result1), set(pollers.FilePoller.build_field_keys))
225 self.assertEqual(set(result2), set(pollers.FilePoller.field_keys)) 225 self.assertEqual(set(result2), set(pollers.FilePoller.build_field_keys))
226 self.assertEqual(set(result3), set(pollers.FilePoller.step_field_keys))
226 227
227 data1 = result1.copy() 228 data = [r.copy() for r in (result1, result2, result3)]
228 data2 = result2.copy() 229 data[0]['random'] = 'value' # Extra field, should be ignored.
229 data1['random'] = 'value' # Extra field, should be ignored. 230 del data[1]['project_id'] # Missing field, should become 'unknown'.
230 del data2['project_id'] # Missing field, should become 'unknown'. 231 data[1]['duration_s'] = 5
231 data2['duration_s'] = 5 232 data[1]['pending_s'] = 1
232 data2['pending_s'] = 1 233 data[1]['total_s'] = data[1]['pending_s'] + data[1]['duration_s']
233 data2['total_s'] = data2['pending_s'] + data2['duration_s'] 234 data[1]['pre_test_time_s'] = 2
234 data2['pre_test_time_s'] = 2
235 with temporary_directory(prefix='poller-test-') as tempdir: 235 with temporary_directory(prefix='poller-test-') as tempdir:
236 filename = self.create_data_file(tempdir, [data1, data2]) 236 filename = self.create_data_file(tempdir, data)
237 p = pollers.FilePoller(filename, {}) 237 p = pollers.FilePoller(filename, {})
238 self.assertTrue(p.poll()) 238 self.assertTrue(p.poll())
239 fake_increment.assert_any_call(result1) 239
240 fake_increment.assert_any_call(result2) 240 self.assertEqual(pollers.FilePoller.result_count.get(result1), 1)
241 fake_add.assert_any_call(data2['duration_s'], result2) 241 self.assertEqual(pollers.FilePoller.result_count.get(result2), 1)
242 fake_add.assert_any_call(data2['pending_s'], result2) 242 self.assertEqual(pollers.FilePoller.step_results_count.get(result3), 1)
243 fake_add.assert_any_call(data2['total_s'], result2) 243
244 self.assertFalse(os.path.isfile(filename)) 244 self.assertFalse(os.path.isfile(filename))
245 # Make sure the rotated file is still there - for debugging. 245 # Make sure the rotated file is still there - for debugging.
246 self.assertTrue(os.path.isfile(pollers.rotated_filename(filename))) 246 self.assertTrue(os.path.isfile(pollers.rotated_filename(filename)))
247 247
248 def test_file_has_bad_data(self): 248 def test_file_has_bad_data(self):
249 """Mostly a smoke test: don't crash on bad data.""" 249 """Mostly a smoke test: don't crash on bad data."""
250 with temporary_directory(prefix='poller-test-') as tempdir: 250 with temporary_directory(prefix='poller-test-') as tempdir:
251 filename = self.create_data_file(tempdir, []) 251 filename = self.create_data_file(tempdir, [])
252 with open(filename, 'a') as f: 252 with open(filename, 'a') as f:
253 f.write('}') 253 f.write('}')
254 p = pollers.FilePoller(filename, {}) 254 p = pollers.FilePoller(filename, {})
255 self.assertTrue(p.poll()) 255 self.assertTrue(p.poll())
256 self.assertFalse(os.path.isfile(filename)) 256 self.assertFalse(os.path.isfile(filename))
257 # Make sure the rotated file is still there - for debugging. 257 # Make sure the rotated file is still there - for debugging.
258 self.assertTrue(os.path.isfile(pollers.rotated_filename(filename))) 258 self.assertTrue(os.path.isfile(pollers.rotated_filename(filename)))
259 259
260 def test_safe_remove_error(self): 260 def test_safe_remove_error(self):
261 """Smoke test: the function should not raise an exception.""" 261 """Smoke test: the function should not raise an exception."""
262 pollers.safe_remove('nonexistent-file') 262 pollers.safe_remove('nonexistent-file')
OLDNEW
« no previous file with comments | « infra/services/mastermon/pollers.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698