build_telemetry_test.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2024 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 subprocess
  8. import sys
  9. import tempfile
  10. import unittest
  11. import unittest.mock
  12. ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  13. sys.path.insert(0, ROOT_DIR)
  14. import build_telemetry
  15. class BuildTelemetryTest(unittest.TestCase):
  16. def test_check_auth(self):
  17. with unittest.mock.patch('subprocess.check_output') as run_mock:
  18. auth = {'email': 'bob@google.com'}
  19. run_mock.return_value = json.dumps(auth)
  20. self.assertEqual(build_telemetry.check_auth(), auth)
  21. with unittest.mock.patch('subprocess.check_output') as run_mock:
  22. run_mock.side_effect = subprocess.CalledProcessError(
  23. 1, cmd=['check auth'], stderr='failed')
  24. self.assertEqual(build_telemetry.check_auth(), {})
  25. with unittest.mock.patch('subprocess.check_output') as run_mock:
  26. run_mock.return_value = ''
  27. self.assertEqual(build_telemetry.check_auth(), {})
  28. def test_load_and_save_config(self):
  29. test_countdown = 2
  30. with tempfile.TemporaryDirectory() as tmpdir:
  31. cfg_path = os.path.join(tmpdir, "build_telemetry.cfg")
  32. with unittest.mock.patch(
  33. 'build_telemetry.check_auth') as check_auth:
  34. check_auth.return_value = {'email': 'bob@google.com'}
  35. # Initial config load
  36. cfg = build_telemetry.load_config(cfg_path, test_countdown)
  37. self.assertEqual(cfg.path, cfg_path)
  38. self.assertTrue(cfg.is_googler)
  39. self.assertEqual(cfg.countdown, test_countdown)
  40. self.assertEqual(cfg.version, build_telemetry.VERSION)
  41. cfg.save()
  42. # 2nd config load
  43. cfg = build_telemetry.load_config(cfg_path, test_countdown)
  44. self.assertEqual(cfg.path, cfg_path)
  45. self.assertTrue(cfg.is_googler)
  46. self.assertEqual(cfg.countdown, test_countdown)
  47. self.assertEqual(cfg.version, build_telemetry.VERSION)
  48. # build_telemetry.check_auth() is an expensive call.
  49. # The cached result should be reused.
  50. check_auth.assert_called_once()
  51. def test_enabled(self):
  52. test_countdown = 2
  53. # Googler auto opt-in.
  54. with tempfile.TemporaryDirectory() as tmpdir:
  55. cfg_path = os.path.join(tmpdir, "build_telemetry.cfg")
  56. with unittest.mock.patch(
  57. 'build_telemetry.check_auth') as check_auth:
  58. check_auth.return_value = {'email': 'bob@google.com'}
  59. # Initial config load
  60. cfg = build_telemetry.load_config(cfg_path, test_countdown)
  61. cfg._show_notice = unittest.mock.MagicMock()
  62. self.assertEqual(cfg.countdown, test_countdown)
  63. # 1st enabled() call should print the notice and
  64. # change the countdown.
  65. self.assertTrue(cfg.enabled())
  66. self.assertEqual(cfg.countdown, test_countdown - 1)
  67. cfg._show_notice.assert_called_once()
  68. cfg._show_notice.reset_mock()
  69. # 2nd enabled() call shouldn't print the notice and
  70. # change the countdown.
  71. self.assertTrue(cfg.enabled())
  72. self.assertEqual(cfg.countdown, test_countdown - 1)
  73. cfg._show_notice.assert_not_called()
  74. cfg.save()
  75. # 2nd config load
  76. cfg = build_telemetry.load_config(cfg_path)
  77. cfg._show_notice = unittest.mock.MagicMock()
  78. self.assertTrue(cfg.enabled())
  79. self.assertEqual(cfg.countdown, test_countdown - 2)
  80. cfg._show_notice.assert_called_once()
  81. cfg.save()
  82. # 3rd config load
  83. cfg = build_telemetry.load_config(cfg_path)
  84. cfg._show_notice = unittest.mock.MagicMock()
  85. self.assertTrue(cfg.enabled())
  86. self.assertEqual(cfg.countdown, 0)
  87. cfg._show_notice.assert_not_called()
  88. # Googler opt-in/opt-out.
  89. with tempfile.TemporaryDirectory() as tmpdir:
  90. cfg_path = os.path.join(tmpdir, "build_telemetry.cfg")
  91. with unittest.mock.patch(
  92. 'build_telemetry.check_auth') as check_auth:
  93. check_auth.return_value = {'email': 'bob@google.com'}
  94. # After opt-out, it should not display the notice and
  95. # change the countdown.
  96. cfg = build_telemetry.load_config(cfg_path, test_countdown)
  97. cfg.opt_out()
  98. cfg = build_telemetry.load_config(cfg_path, test_countdown)
  99. cfg._show_notice = unittest.mock.MagicMock()
  100. self.assertFalse(cfg.enabled())
  101. self.assertEqual(cfg.countdown, test_countdown)
  102. cfg._show_notice.assert_not_called()
  103. # After opt-in, it should not display the notice and
  104. # change the countdown.
  105. cfg = build_telemetry.load_config(cfg_path, test_countdown)
  106. cfg.opt_in()
  107. cfg = build_telemetry.load_config(cfg_path, test_countdown)
  108. cfg._show_notice = unittest.mock.MagicMock()
  109. self.assertTrue(cfg.enabled())
  110. self.assertEqual(cfg.countdown, test_countdown)
  111. cfg._show_notice.assert_not_called()
  112. # Non-Googler
  113. with tempfile.TemporaryDirectory() as tmpdir:
  114. cfg_path = os.path.join(tmpdir, "build_telemetry.cfg")
  115. with unittest.mock.patch(
  116. 'build_telemetry.check_auth') as check_auth:
  117. check_auth.return_value = {'email': 'bob@example.com'}
  118. cfg = build_telemetry.load_config(cfg_path)
  119. self.assertFalse(cfg.enabled())
  120. if __name__ == '__main__':
  121. unittest.main()