metrics_test.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. #!/usr/bin/env python
  2. # Copyright (c) 2018 The Chromium 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. import json
  6. import os
  7. import sys
  8. import unittest
  9. ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  10. sys.path.insert(0, ROOT_DIR)
  11. import metrics
  12. import metrics_utils
  13. import cStringIO
  14. from third_party import mock
  15. class TimeMock(object):
  16. def __init__(self):
  17. self._count = 0
  18. def __call__(self):
  19. self._count += 1
  20. return self._count * 1000
  21. class MetricsCollectorTest(unittest.TestCase):
  22. def setUp(self):
  23. self.config_file = os.path.join(ROOT_DIR, 'metrics.cfg')
  24. self.collector = metrics.MetricsCollector()
  25. # Keep track of the URL requests, file reads/writes and subprocess spawned.
  26. self.urllib2 = mock.Mock()
  27. self.print_notice = mock.Mock()
  28. self.print_version_change = mock.Mock()
  29. self.Popen = mock.Mock()
  30. self.FileWrite = mock.Mock()
  31. self.FileRead = mock.Mock()
  32. # So that we don't have to update the tests everytime we change the version.
  33. mock.patch('metrics.metrics_utils.CURRENT_VERSION', 0).start()
  34. mock.patch('metrics.urllib2', self.urllib2).start()
  35. mock.patch('metrics.subprocess.Popen', self.Popen).start()
  36. mock.patch('metrics.gclient_utils.FileWrite', self.FileWrite).start()
  37. mock.patch('metrics.gclient_utils.FileRead', self.FileRead).start()
  38. mock.patch('metrics.metrics_utils.print_notice', self.print_notice).start()
  39. mock.patch(
  40. 'metrics.metrics_utils.print_version_change',
  41. self.print_version_change).start()
  42. # Patch the methods used to get the system information, so we have a known
  43. # environment.
  44. mock.patch('metrics.tempfile.mkstemp',
  45. lambda: (None, '/tmp/metrics.json')).start()
  46. mock.patch('metrics.time.time',
  47. TimeMock()).start()
  48. mock.patch('metrics.metrics_utils.get_python_version',
  49. lambda: '2.7.13').start()
  50. mock.patch('metrics.gclient_utils.GetMacWinOrLinux',
  51. lambda: 'linux').start()
  52. mock.patch('metrics.detect_host_arch.HostArch',
  53. lambda: 'x86').start()
  54. mock.patch('metrics_utils.get_repo_timestamp',
  55. lambda _: 1234).start()
  56. self.default_metrics = {
  57. "python_version": "2.7.13",
  58. "execution_time": 1000,
  59. "timestamp": 0,
  60. "exit_code": 0,
  61. "command": "fun",
  62. "depot_tools_age": 1234,
  63. "host_arch": "x86",
  64. "host_os": "linux",
  65. }
  66. self.addCleanup(mock.patch.stopall)
  67. def assert_writes_file(self, expected_filename, expected_content):
  68. self.assertEqual(len(self.FileWrite.mock_calls), 1)
  69. filename, content = self.FileWrite.mock_calls[0][1]
  70. self.assertEqual(filename, expected_filename)
  71. self.assertEqual(json.loads(content), expected_content)
  72. def test_writes_config_if_not_exists(self):
  73. self.FileRead.side_effect = [IOError(2, "No such file or directory")]
  74. mock_response = mock.Mock()
  75. self.urllib2.urlopen.side_effect = [mock_response]
  76. mock_response.getcode.side_effect = [200]
  77. self.assertTrue(self.collector.config.is_googler)
  78. self.assertIsNone(self.collector.config.opted_in)
  79. self.assertEqual(self.collector.config.countdown, 10)
  80. self.assert_writes_file(
  81. self.config_file,
  82. {'is-googler': True, 'countdown': 10, 'opt-in': None, 'version': 0})
  83. def test_writes_config_if_not_exists_non_googler(self):
  84. self.FileRead.side_effect = [IOError(2, "No such file or directory")]
  85. mock_response = mock.Mock()
  86. self.urllib2.urlopen.side_effect = [mock_response]
  87. mock_response.getcode.side_effect = [403]
  88. self.assertFalse(self.collector.config.is_googler)
  89. self.assertIsNone(self.collector.config.opted_in)
  90. self.assertEqual(self.collector.config.countdown, 10)
  91. self.assert_writes_file(
  92. self.config_file,
  93. {'is-googler': False, 'countdown': 10, 'opt-in': None, 'version': 0})
  94. def test_disables_metrics_if_cant_write_config(self):
  95. self.FileRead.side_effect = [IOError(2, 'No such file or directory')]
  96. mock_response = mock.Mock()
  97. self.urllib2.urlopen.side_effect = [mock_response]
  98. mock_response.getcode.side_effect = [200]
  99. self.FileWrite.side_effect = [IOError(13, 'Permission denied.')]
  100. self.assertTrue(self.collector.config.is_googler)
  101. self.assertFalse(self.collector.config.opted_in)
  102. self.assertEqual(self.collector.config.countdown, 10)
  103. def assert_collects_metrics(self, update_metrics=None):
  104. expected_metrics = self.default_metrics
  105. self.default_metrics.update(update_metrics or {})
  106. # Assert we invoked the script to upload them.
  107. self.Popen.assert_called_with(
  108. [sys.executable, metrics.UPLOAD_SCRIPT], stdin=metrics.subprocess.PIPE)
  109. # Assert we collected the right metrics.
  110. write_call = self.Popen.return_value.stdin.write.call_args
  111. collected_metrics = json.loads(write_call[0][0])
  112. self.assertTrue(self.collector.collecting_metrics)
  113. self.assertEqual(collected_metrics, expected_metrics)
  114. def test_collects_system_information(self):
  115. """Tests that we collect information about the runtime environment."""
  116. self.FileRead.side_effect = [
  117. '{"is-googler": true, "countdown": 0, "opt-in": null, "version": 0}'
  118. ]
  119. @self.collector.collect_metrics('fun')
  120. def fun():
  121. pass
  122. fun()
  123. self.assert_collects_metrics()
  124. def test_collects_added_metrics(self):
  125. """Tests that we can collect custom metrics."""
  126. self.FileRead.side_effect = [
  127. '{"is-googler": true, "countdown": 0, "opt-in": null, "version": 0}'
  128. ]
  129. @self.collector.collect_metrics('fun')
  130. def fun():
  131. self.collector.add('foo', 'bar')
  132. fun()
  133. self.assert_collects_metrics({'foo': 'bar'})
  134. def test_collects_metrics_when_opted_in(self):
  135. """Tests that metrics are collected when the user opts-in."""
  136. self.FileRead.side_effect = [
  137. '{"is-googler": true, "countdown": 1234, "opt-in": true, "version": 0}'
  138. ]
  139. @self.collector.collect_metrics('fun')
  140. def fun():
  141. pass
  142. fun()
  143. self.assert_collects_metrics()
  144. @mock.patch('metrics.DISABLE_METRICS_COLLECTION', True)
  145. def test_metrics_collection_disabled(self):
  146. """Tests that metrics collection can be disabled via a global variable."""
  147. @self.collector.collect_metrics('fun')
  148. def fun():
  149. pass
  150. fun()
  151. self.assertFalse(self.collector.collecting_metrics)
  152. # We shouldn't have tried to read the config file.
  153. self.assertFalse(self.FileRead.called)
  154. # Nor tried to upload any metrics.
  155. self.assertFalse(self.Popen.called)
  156. def test_metrics_collection_disabled_not_googler(self):
  157. """Tests that metrics collection is disabled for non googlers."""
  158. self.FileRead.side_effect = [
  159. '{"is-googler": false, "countdown": 0, "opt-in": null, "version": 0}'
  160. ]
  161. @self.collector.collect_metrics('fun')
  162. def fun():
  163. pass
  164. fun()
  165. self.assertFalse(self.collector.collecting_metrics)
  166. self.assertFalse(self.collector.config.is_googler)
  167. self.assertIsNone(self.collector.config.opted_in)
  168. self.assertEqual(self.collector.config.countdown, 0)
  169. # Assert that we did not try to upload any metrics.
  170. self.assertFalse(self.Popen.called)
  171. def test_metrics_collection_disabled_opted_out(self):
  172. """Tests that metrics collection is disabled if the user opts out."""
  173. self.FileRead.side_effect = [
  174. '{"is-googler": true, "countdown": 0, "opt-in": false, "version": 0}'
  175. ]
  176. @self.collector.collect_metrics('fun')
  177. def fun():
  178. pass
  179. fun()
  180. self.assertFalse(self.collector.collecting_metrics)
  181. self.assertTrue(self.collector.config.is_googler)
  182. self.assertFalse(self.collector.config.opted_in)
  183. self.assertEqual(self.collector.config.countdown, 0)
  184. # Assert that we did not try to upload any metrics.
  185. self.assertFalse(self.Popen.called)
  186. def test_metrics_collection_disabled_non_zero_countdown(self):
  187. """Tests that metrics collection is disabled until the countdown expires."""
  188. self.FileRead.side_effect = [
  189. '{"is-googler": true, "countdown": 1, "opt-in": null, "version": 0}'
  190. ]
  191. @self.collector.collect_metrics('fun')
  192. def fun():
  193. pass
  194. fun()
  195. self.assertFalse(self.collector.collecting_metrics)
  196. self.assertTrue(self.collector.config.is_googler)
  197. self.assertFalse(self.collector.config.opted_in)
  198. self.assertEqual(self.collector.config.countdown, 1)
  199. # Assert that we did not try to upload any metrics.
  200. self.assertFalse(self.Popen.called)
  201. def test_handles_exceptions(self):
  202. """Tests that exception are caught and we exit with an appropriate code."""
  203. self.FileRead.side_effect = [
  204. '{"is-googler": true, "countdown": 0, "opt-in": true, "version": 0}'
  205. ]
  206. @self.collector.collect_metrics('fun')
  207. def fun():
  208. raise ValueError
  209. # When an exception is raised, we should catch it, update exit-code,
  210. # collect metrics, and re-raise it.
  211. with self.assertRaises(ValueError):
  212. fun()
  213. self.assert_collects_metrics({'exit_code': 1})
  214. def test_handles_system_exit(self):
  215. """Tests that the sys.exit code is respected and metrics are collected."""
  216. self.FileRead.side_effect = [
  217. '{"is-googler": true, "countdown": 0, "opt-in": true, "version": 0}'
  218. ]
  219. @self.collector.collect_metrics('fun')
  220. def fun():
  221. sys.exit(0)
  222. # When an exception is raised, we should catch it, update exit-code,
  223. # collect metrics, and re-raise it.
  224. with self.assertRaises(SystemExit) as cm:
  225. fun()
  226. self.assertEqual(cm.exception.code, 0)
  227. self.assert_collects_metrics({'exit_code': 0})
  228. def test_handles_system_exit_non_zero(self):
  229. """Tests that the sys.exit code is respected and metrics are collected."""
  230. self.FileRead.side_effect = [
  231. '{"is-googler": true, "countdown": 0, "opt-in": true, "version": 0}'
  232. ]
  233. @self.collector.collect_metrics('fun')
  234. def fun():
  235. sys.exit(123)
  236. # When an exception is raised, we should catch it, update exit-code,
  237. # collect metrics, and re-raise it.
  238. with self.assertRaises(SystemExit) as cm:
  239. fun()
  240. self.assertEqual(cm.exception.code, 123)
  241. self.assert_collects_metrics({'exit_code': 123})
  242. def test_prints_notice_non_zero_countdown(self):
  243. """Tests that a notice is printed while the countdown is non-zero."""
  244. self.FileRead.side_effect = [
  245. '{"is-googler": true, "countdown": 1234, "opt-in": null, "version": 0}'
  246. ]
  247. with self.assertRaises(SystemExit) as cm:
  248. with self.collector.print_notice_and_exit():
  249. pass
  250. self.assertEqual(cm.exception.code, 0)
  251. self.print_notice.assert_called_once_with(1234)
  252. def test_prints_notice_zero_countdown(self):
  253. """Tests that a notice is printed when the countdown reaches 0."""
  254. self.FileRead.side_effect = [
  255. '{"is-googler": true, "countdown": 0, "opt-in": null, "version": 0}'
  256. ]
  257. with self.assertRaises(SystemExit) as cm:
  258. with self.collector.print_notice_and_exit():
  259. pass
  260. self.assertEqual(cm.exception.code, 0)
  261. self.print_notice.assert_called_once_with(0)
  262. def test_doesnt_print_notice_opted_in(self):
  263. """Tests that a notice is not printed when the user opts-in."""
  264. self.FileRead.side_effect = [
  265. '{"is-googler": true, "countdown": 0, "opt-in": true, "version": 0}'
  266. ]
  267. with self.assertRaises(SystemExit) as cm:
  268. with self.collector.print_notice_and_exit():
  269. pass
  270. self.assertEqual(cm.exception.code, 0)
  271. self.assertFalse(self.print_notice.called)
  272. def test_doesnt_print_notice_opted_out(self):
  273. """Tests that a notice is not printed when the user opts-out."""
  274. self.FileRead.side_effect = [
  275. '{"is-googler": true, "countdown": 0, "opt-in": false, "version": 0}'
  276. ]
  277. with self.assertRaises(SystemExit) as cm:
  278. with self.collector.print_notice_and_exit():
  279. pass
  280. self.assertEqual(cm.exception.code, 0)
  281. self.assertFalse(self.print_notice.called)
  282. @mock.patch('metrics.DISABLE_METRICS_COLLECTION', True)
  283. def test_doesnt_print_notice_disable_metrics_collection(self):
  284. with self.assertRaises(SystemExit) as cm:
  285. with self.collector.print_notice_and_exit():
  286. pass
  287. self.assertEqual(cm.exception.code, 0)
  288. self.assertFalse(self.print_notice.called)
  289. # We shouldn't have tried to read the config file.
  290. self.assertFalse(self.FileRead.called)
  291. def test_print_notice_handles_exceptions(self):
  292. """Tests that exception are caught and we exit with an appropriate code."""
  293. self.FileRead.side_effect = [
  294. '{"is-googler": true, "countdown": 0, "opt-in": null, "version": 0}'
  295. ]
  296. # print_notice should catch the exception, print it and invoke sys.exit()
  297. with self.assertRaises(SystemExit) as cm:
  298. with self.collector.print_notice_and_exit():
  299. raise ValueError
  300. self.assertEqual(cm.exception.code, 1)
  301. self.assertTrue(self.print_notice.called)
  302. def test_print_notice_handles_system_exit(self):
  303. """Tests that the sys.exit code is respected and a notice is displayed."""
  304. self.FileRead.side_effect = [
  305. '{"is-googler": true, "countdown": 0, "opt-in": null, "version": 0}'
  306. ]
  307. # print_notice should catch the exception, print it and invoke sys.exit()
  308. with self.assertRaises(SystemExit) as cm:
  309. with self.collector.print_notice_and_exit():
  310. sys.exit(0)
  311. self.assertEqual(cm.exception.code, 0)
  312. self.assertTrue(self.print_notice.called)
  313. def test_print_notice_handles_system_exit_non_zero(self):
  314. """Tests that the sys.exit code is respected and a notice is displayed."""
  315. self.FileRead.side_effect = [
  316. '{"is-googler": true, "countdown": 0, "opt-in": null, "version": 0}'
  317. ]
  318. # When an exception is raised, we should catch it, update exit-code,
  319. # collect metrics, and re-raise it.
  320. with self.assertRaises(SystemExit) as cm:
  321. with self.collector.print_notice_and_exit():
  322. sys.exit(123)
  323. self.assertEqual(cm.exception.code, 123)
  324. self.assertTrue(self.print_notice.called)
  325. def test_counts_down(self):
  326. """Tests that the countdown works correctly."""
  327. self.FileRead.side_effect = [
  328. '{"is-googler": true, "countdown": 10, "opt-in": null, "version": 0}'
  329. ]
  330. # We define multiple functions to ensure it has no impact on countdown.
  331. @self.collector.collect_metrics('barn')
  332. def _barn():
  333. pass
  334. @self.collector.collect_metrics('fun')
  335. def _fun():
  336. pass
  337. def foo_main():
  338. pass
  339. # Assert that the countdown hasn't decrease yet.
  340. self.assertFalse(self.FileWrite.called)
  341. self.assertEqual(self.collector.config.countdown, 10)
  342. with self.assertRaises(SystemExit) as cm:
  343. with self.collector.print_notice_and_exit():
  344. foo_main()
  345. self.assertEqual(cm.exception.code, 0)
  346. # Assert that the countdown decreased by one, and the config file was
  347. # updated.
  348. self.assertEqual(self.collector.config.countdown, 9)
  349. self.print_notice.assert_called_once_with(10)
  350. self.assert_writes_file(
  351. self.config_file,
  352. {'is-googler': True, 'countdown': 9, 'opt-in': None, 'version': 0})
  353. def test_nested_functions(self):
  354. """Tests that a function can call another function for which metrics are
  355. collected."""
  356. self.FileRead.side_effect = [
  357. '{"is-googler": true, "countdown": 0, "opt-in": true, "version": 0}'
  358. ]
  359. @self.collector.collect_metrics('barn')
  360. def barn():
  361. self.collector.add('barn-metric', 1)
  362. return 1000
  363. @self.collector.collect_metrics('fun')
  364. def fun():
  365. result = barn()
  366. self.collector.add('fun-metric', result + 1)
  367. fun()
  368. # Assert that we collected metrics for fun, but not for barn.
  369. self.assert_collects_metrics({'fun-metric': 1001})
  370. @mock.patch('metrics.metrics_utils.CURRENT_VERSION', 5)
  371. def test_version_change_from_hasnt_decided(self):
  372. # The user has not decided yet, and the countdown hasn't reached 0, so we're
  373. # not collecting metrics.
  374. self.FileRead.side_effect = [
  375. '{"is-googler": true, "countdown": 9, "opt-in": null, "version": 0}'
  376. ]
  377. with self.assertRaises(SystemExit) as cm:
  378. with self.collector.print_notice_and_exit():
  379. self.collector.add('foo-metric', 1)
  380. self.assertEqual(cm.exception.code, 0)
  381. # We display the notice informing the user of the changes.
  382. self.print_version_change.assert_called_once_with(0)
  383. # But the countdown is not reset.
  384. self.assert_writes_file(
  385. self.config_file,
  386. {'is-googler': True, 'countdown': 8, 'opt-in': None, 'version': 0})
  387. # And no metrics are uploaded.
  388. self.assertFalse(self.Popen.called)
  389. @mock.patch('metrics.metrics_utils.CURRENT_VERSION', 5)
  390. def test_version_change_from_opted_in_by_default(self):
  391. # The user has not decided yet, but the countdown has reached 0, and we're
  392. # collecting metrics.
  393. self.FileRead.side_effect = [
  394. '{"is-googler": true, "countdown": 0, "opt-in": null, "version": 0}'
  395. ]
  396. with self.assertRaises(SystemExit) as cm:
  397. with self.collector.print_notice_and_exit():
  398. self.collector.add('foo-metric', 1)
  399. self.assertEqual(cm.exception.code, 0)
  400. # We display the notice informing the user of the changes.
  401. self.print_version_change.assert_called_once_with(0)
  402. # We reset the countdown.
  403. self.assert_writes_file(
  404. self.config_file,
  405. {'is-googler': True, 'countdown': 9, 'opt-in': None, 'version': 0})
  406. # No metrics are uploaded.
  407. self.assertFalse(self.Popen.called)
  408. @mock.patch('metrics.metrics_utils.CURRENT_VERSION', 5)
  409. def test_version_change_from_opted_in(self):
  410. # The user has opted in, and we're collecting metrics.
  411. self.FileRead.side_effect = [
  412. '{"is-googler": true, "countdown": 0, "opt-in": true, "version": 0}'
  413. ]
  414. with self.assertRaises(SystemExit) as cm:
  415. with self.collector.print_notice_and_exit():
  416. self.collector.add('foo-metric', 1)
  417. self.assertEqual(cm.exception.code, 0)
  418. # We display the notice informing the user of the changes.
  419. self.print_version_change.assert_called_once_with(0)
  420. # We reset the countdown.
  421. self.assert_writes_file(
  422. self.config_file,
  423. {'is-googler': True, 'countdown': 9, 'opt-in': None, 'version': 0})
  424. # No metrics are uploaded.
  425. self.assertFalse(self.Popen.called)
  426. @mock.patch('metrics.metrics_utils.CURRENT_VERSION', 5)
  427. def test_version_change_from_opted_out(self):
  428. # The user has opted out and we're not collecting metrics.
  429. self.FileRead.side_effect = [
  430. '{"is-googler": true, "countdown": 0, "opt-in": false, "version": 0}'
  431. ]
  432. with self.assertRaises(SystemExit) as cm:
  433. with self.collector.print_notice_and_exit():
  434. self.collector.add('foo-metric', 1)
  435. self.assertEqual(cm.exception.code, 0)
  436. # We don't display any notice.
  437. self.assertFalse(self.print_version_change.called)
  438. self.assertFalse(self.print_notice.called)
  439. # We don't upload any metrics.
  440. self.assertFalse(self.Popen.called)
  441. # We don't modify the config.
  442. self.assertFalse(self.FileWrite.called)
  443. @mock.patch('metrics.metrics_utils.CURRENT_VERSION', 5)
  444. def test_version_change_non_googler(self):
  445. # The user is not a googler and we're not collecting metrics.
  446. self.FileRead.side_effect = [
  447. '{"is-googler": false, "countdown": 10, "opt-in": null, "version": 0}'
  448. ]
  449. with self.assertRaises(SystemExit) as cm:
  450. with self.collector.print_notice_and_exit():
  451. self.collector.add('foo-metric', 1)
  452. self.assertEqual(cm.exception.code, 0)
  453. # We don't display any notice.
  454. self.assertFalse(self.print_version_change.called)
  455. self.assertFalse(self.print_notice.called)
  456. # We don't upload any metrics.
  457. self.assertFalse(self.Popen.called)
  458. # We don't modify the config.
  459. self.assertFalse(self.FileWrite.called)
  460. @mock.patch('metrics.metrics_utils.CURRENT_VERSION', 5)
  461. def test_opting_in_updates_version(self):
  462. # The user is seeing the notice telling him of the version changes.
  463. self.FileRead.side_effect = [
  464. '{"is-googler": true, "countdown": 8, "opt-in": null, "version": 0}'
  465. ]
  466. self.collector.config.opted_in = True
  467. # We don't display any notice.
  468. self.assertFalse(self.print_version_change.called)
  469. self.assertFalse(self.print_notice.called)
  470. # We don't upload any metrics.
  471. self.assertFalse(self.Popen.called)
  472. # We update the version and opt-in the user.
  473. self.assert_writes_file(
  474. self.config_file,
  475. {'is-googler': True, 'countdown': 8, 'opt-in': True, 'version': 5})
  476. @mock.patch('metrics.metrics_utils.CURRENT_VERSION', 5)
  477. def test_opting_in_by_default_updates_version(self):
  478. # The user will be opted in by default on the next execution.
  479. self.FileRead.side_effect = [
  480. '{"is-googler": true, "countdown": 1, "opt-in": null, "version": 0}'
  481. ]
  482. with self.assertRaises(SystemExit) as cm:
  483. with self.collector.print_notice_and_exit():
  484. self.collector.add('foo-metric', 1)
  485. self.assertEqual(cm.exception.code, 0)
  486. # We display the notices.
  487. self.print_notice.assert_called_once_with(1)
  488. self.print_version_change.assert_called_once_with(0)
  489. # We don't upload any metrics.
  490. self.assertFalse(self.Popen.called)
  491. # We update the version and set the countdown to 0. In subsequent runs,
  492. # we'll start collecting metrics.
  493. self.assert_writes_file(
  494. self.config_file,
  495. {'is-googler': True, 'countdown': 0, 'opt-in': None, 'version': 5})
  496. def test_add_repeated(self):
  497. """Tests that we can add repeated metrics."""
  498. self.FileRead.side_effect = [
  499. '{"is-googler": true, "countdown": 0, "opt-in": true}'
  500. ]
  501. @self.collector.collect_metrics('fun')
  502. def fun():
  503. self.collector.add_repeated('fun', 1)
  504. self.collector.add_repeated('fun', 2)
  505. self.collector.add_repeated('fun', 5)
  506. fun()
  507. # Assert that we collected all metrics for fun.
  508. self.assert_collects_metrics({'fun': [1, 2, 5]})
  509. class MetricsUtilsTest(unittest.TestCase):
  510. def test_extracts_host(self):
  511. """Test that we extract the host from the requested URI."""
  512. # Regular case
  513. http_metrics = metrics_utils.extract_http_metrics(
  514. 'https://chromium-review.googlesource.com/foo/bar?q=baz', '', 0, 0)
  515. self.assertEqual('chromium-review.googlesource.com', http_metrics['host'])
  516. # Unexpected host
  517. http_metrics = metrics_utils.extract_http_metrics(
  518. 'https://foo-review.googlesource.com/', '', 0, 0)
  519. self.assertNotIn('host', http_metrics)
  520. def test_extracts_path(self):
  521. """Test that we extract the matching path from the requested URI."""
  522. # Regular case
  523. http_metrics = metrics_utils.extract_http_metrics(
  524. 'https://review.example.com/changes/1234/revisions/deadbeef/commit',
  525. '', 0, 0)
  526. self.assertEqual('changes/revisions/commit', http_metrics['path'])
  527. # No matching paths
  528. http_metrics = metrics_utils.extract_http_metrics(
  529. 'https://review.example.com/changes/1234/unexpected/path', '', 0, 0)
  530. self.assertNotIn('path', http_metrics)
  531. def test_extracts_path_changes(self):
  532. """Tests that we extract paths for /changes/."""
  533. # /changes/<change-id>
  534. http_metrics = metrics_utils.extract_http_metrics(
  535. 'https://review.example.com/changes/proj%2Fsrc%7Emaster%7EI1234abcd',
  536. '', 0, 0)
  537. self.assertEqual('changes', http_metrics['path'])
  538. # /changes/?q=<something>
  539. http_metrics = metrics_utils.extract_http_metrics(
  540. 'https://review.example.com/changes/?q=owner:me+OR+cc:me',
  541. '', 0, 0)
  542. self.assertEqual('changes', http_metrics['path'])
  543. # /changes/#<something>
  544. http_metrics = metrics_utils.extract_http_metrics(
  545. 'https://review.example.com/changes/#something',
  546. '', 0, 0)
  547. self.assertEqual('changes', http_metrics['path'])
  548. # /changes/<change-id>/<anything> does not map to changes.
  549. http_metrics = metrics_utils.extract_http_metrics(
  550. 'https://review.example.com/changes/12345678/message',
  551. '', 0, 0)
  552. self.assertNotEqual('changes', http_metrics['path'])
  553. def test_extracts_arguments(self):
  554. """Test that we can extract arguments from the requested URI."""
  555. # Regular case
  556. http_metrics = metrics_utils.extract_http_metrics(
  557. 'https://review.example.com/?q=123&foo=bar&o=ALL_REVISIONS', '', 0, 0)
  558. self.assertEqual(['ALL_REVISIONS'], http_metrics['arguments'])
  559. # Some unexpected arguments are filtered out.
  560. http_metrics = metrics_utils.extract_http_metrics(
  561. 'https://review.example.com/?o=ALL_REVISIONS&o=LABELS&o=UNEXPECTED',
  562. '', 0, 0)
  563. self.assertEqual(['ALL_REVISIONS', 'LABELS'], http_metrics['arguments'])
  564. # No valid arguments, so arguments is not present
  565. http_metrics = metrics_utils.extract_http_metrics(
  566. 'https://review.example.com/?o=bar&baz=1', '', 0, 0)
  567. self.assertNotIn('arguments', http_metrics)
  568. # No valid arguments, so arguments is not present
  569. http_metrics = metrics_utils.extract_http_metrics(
  570. 'https://review.example.com/?foo=bar&baz=1', '', 0, 0)
  571. self.assertNotIn('arguments', http_metrics)
  572. def test_validates_method(self):
  573. """Test that we validate the HTTP method used."""
  574. # Regular case
  575. http_metrics = metrics_utils.extract_http_metrics('', 'POST', 0, 0)
  576. self.assertEqual('POST', http_metrics['method'])
  577. # Unexpected method is not reported
  578. http_metrics = metrics_utils.extract_http_metrics('', 'DEMAND', 0, 0)
  579. self.assertNotIn('method', http_metrics)
  580. def test_status(self):
  581. """Tests that the response status we passed is returned."""
  582. http_metrics = metrics_utils.extract_http_metrics('', '', 123, 0)
  583. self.assertEqual(123, http_metrics['status'])
  584. http_metrics = metrics_utils.extract_http_metrics('', '', 404, 0)
  585. self.assertEqual(404, http_metrics['status'])
  586. def test_response_time(self):
  587. """Tests that the response time we passed is returned."""
  588. http_metrics = metrics_utils.extract_http_metrics('', '', 0, 0.25)
  589. self.assertEqual(0.25, http_metrics['response_time'])
  590. http_metrics = metrics_utils.extract_http_metrics('', '', 0, 12345.25)
  591. self.assertEqual(12345.25, http_metrics['response_time'])
  592. if __name__ == '__main__':
  593. unittest.main()