|
|
@ -3,6 +3,7 @@
|
|
|
|
# Use of this source code is governed by a BSD-style license that can be
|
|
|
|
# Use of this source code is governed by a BSD-style license that can be
|
|
|
|
# found in the LICENSE file.
|
|
|
|
# found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import sys
|
|
|
|
import tempfile
|
|
|
|
import tempfile
|
|
|
@ -17,33 +18,29 @@ import build_telemetry
|
|
|
|
|
|
|
|
|
|
|
|
class BuildTelemetryTest(unittest.TestCase):
|
|
|
|
class BuildTelemetryTest(unittest.TestCase):
|
|
|
|
|
|
|
|
|
|
|
|
def test_is_googler(self):
|
|
|
|
def test_check_auth(self):
|
|
|
|
with unittest.mock.patch('subprocess.run') as run_mock:
|
|
|
|
with unittest.mock.patch('subprocess.run') as run_mock:
|
|
|
|
run_mock.return_value.returncode = 0
|
|
|
|
run_mock.return_value.returncode = 0
|
|
|
|
run_mock.return_value.stdout = 'Logged in as foo@google.com.\n'
|
|
|
|
auth = {'email': 'bob@google.com'}
|
|
|
|
self.assertTrue(build_telemetry.is_googler())
|
|
|
|
run_mock.return_value.stdout = json.dumps(auth)
|
|
|
|
|
|
|
|
self.assertEqual(build_telemetry.check_auth(), auth)
|
|
|
|
|
|
|
|
|
|
|
|
with unittest.mock.patch('subprocess.run') as run_mock:
|
|
|
|
with unittest.mock.patch('subprocess.run') as run_mock:
|
|
|
|
run_mock.return_value.returncode = 1
|
|
|
|
run_mock.return_value.returncode = 1
|
|
|
|
self.assertFalse(build_telemetry.is_googler())
|
|
|
|
self.assertEqual(build_telemetry.check_auth(), {})
|
|
|
|
|
|
|
|
|
|
|
|
with unittest.mock.patch('subprocess.run') as run_mock:
|
|
|
|
with unittest.mock.patch('subprocess.run') as run_mock:
|
|
|
|
run_mock.return_value.returncode = 0
|
|
|
|
run_mock.return_value.returncode = 0
|
|
|
|
run_mock.return_value.stdout = ''
|
|
|
|
run_mock.return_value.stdout = ''
|
|
|
|
self.assertFalse(build_telemetry.is_googler())
|
|
|
|
self.assertEqual(build_telemetry.check_auth(), {})
|
|
|
|
|
|
|
|
|
|
|
|
with unittest.mock.patch('subprocess.run') as run_mock:
|
|
|
|
|
|
|
|
run_mock.return_value.returncode = 0
|
|
|
|
|
|
|
|
run_mock.return_value.stdout = 'Logged in as foo@example.com.\n'
|
|
|
|
|
|
|
|
self.assertFalse(build_telemetry.is_googler())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_load_and_save_config(self):
|
|
|
|
def test_load_and_save_config(self):
|
|
|
|
test_countdown = 2
|
|
|
|
test_countdown = 2
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
cfg_path = os.path.join(tmpdir, "build_telemetry.cfg")
|
|
|
|
cfg_path = os.path.join(tmpdir, "build_telemetry.cfg")
|
|
|
|
with unittest.mock.patch(
|
|
|
|
with unittest.mock.patch(
|
|
|
|
'build_telemetry.is_googler') as is_googler:
|
|
|
|
'build_telemetry.check_auth') as check_auth:
|
|
|
|
is_googler.return_value = True
|
|
|
|
check_auth.return_value = {'email': 'bob@google.com'}
|
|
|
|
|
|
|
|
|
|
|
|
# Initial config load
|
|
|
|
# Initial config load
|
|
|
|
cfg = build_telemetry.load_config(cfg_path, test_countdown)
|
|
|
|
cfg = build_telemetry.load_config(cfg_path, test_countdown)
|
|
|
@ -61,9 +58,9 @@ class BuildTelemetryTest(unittest.TestCase):
|
|
|
|
self.assertEqual(cfg.countdown, test_countdown)
|
|
|
|
self.assertEqual(cfg.countdown, test_countdown)
|
|
|
|
self.assertEqual(cfg.version, build_telemetry.VERSION)
|
|
|
|
self.assertEqual(cfg.version, build_telemetry.VERSION)
|
|
|
|
|
|
|
|
|
|
|
|
# build_telemetry.is_googler() is an expensive call.
|
|
|
|
# build_telemetry.check_auth() is an expensive call.
|
|
|
|
# The cached result should be reused.
|
|
|
|
# The cached result should be reused.
|
|
|
|
is_googler.assert_called_once()
|
|
|
|
check_auth.assert_called_once()
|
|
|
|
|
|
|
|
|
|
|
|
def test_enabled(self):
|
|
|
|
def test_enabled(self):
|
|
|
|
test_countdown = 2
|
|
|
|
test_countdown = 2
|
|
|
@ -72,8 +69,8 @@ class BuildTelemetryTest(unittest.TestCase):
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
cfg_path = os.path.join(tmpdir, "build_telemetry.cfg")
|
|
|
|
cfg_path = os.path.join(tmpdir, "build_telemetry.cfg")
|
|
|
|
with unittest.mock.patch(
|
|
|
|
with unittest.mock.patch(
|
|
|
|
'build_telemetry.is_googler') as is_googler:
|
|
|
|
'build_telemetry.check_auth') as check_auth:
|
|
|
|
is_googler.return_value = True
|
|
|
|
check_auth.return_value = {'email': 'bob@google.com'}
|
|
|
|
|
|
|
|
|
|
|
|
# Initial config load
|
|
|
|
# Initial config load
|
|
|
|
cfg = build_telemetry.load_config(cfg_path, test_countdown)
|
|
|
|
cfg = build_telemetry.load_config(cfg_path, test_countdown)
|
|
|
@ -115,8 +112,8 @@ class BuildTelemetryTest(unittest.TestCase):
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
cfg_path = os.path.join(tmpdir, "build_telemetry.cfg")
|
|
|
|
cfg_path = os.path.join(tmpdir, "build_telemetry.cfg")
|
|
|
|
with unittest.mock.patch(
|
|
|
|
with unittest.mock.patch(
|
|
|
|
'build_telemetry.is_googler') as is_googler:
|
|
|
|
'build_telemetry.check_auth') as check_auth:
|
|
|
|
is_googler.return_value = True
|
|
|
|
check_auth.return_value = {'email': 'bob@google.com'}
|
|
|
|
# After opt-out, it should not display the notice and
|
|
|
|
# After opt-out, it should not display the notice and
|
|
|
|
# change the countdown.
|
|
|
|
# change the countdown.
|
|
|
|
cfg = build_telemetry.load_config(cfg_path, test_countdown)
|
|
|
|
cfg = build_telemetry.load_config(cfg_path, test_countdown)
|
|
|
@ -143,8 +140,8 @@ class BuildTelemetryTest(unittest.TestCase):
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
cfg_path = os.path.join(tmpdir, "build_telemetry.cfg")
|
|
|
|
cfg_path = os.path.join(tmpdir, "build_telemetry.cfg")
|
|
|
|
with unittest.mock.patch(
|
|
|
|
with unittest.mock.patch(
|
|
|
|
'build_telemetry.is_googler') as is_googler:
|
|
|
|
'build_telemetry.check_auth') as check_auth:
|
|
|
|
is_googler.return_value = False
|
|
|
|
check_auth.return_value = {'email': 'bob@example.com'}
|
|
|
|
cfg = build_telemetry.load_config(cfg_path)
|
|
|
|
cfg = build_telemetry.load_config(cfg_path)
|
|
|
|
self.assertFalse(cfg.enabled())
|
|
|
|
self.assertFalse(cfg.enabled())
|
|
|
|
|
|
|
|
|
|
|
|