diff --git a/build_telemetry.py b/build_telemetry.py index afe6d8211..668f91f9d 100755 --- a/build_telemetry.py +++ b/build_telemetry.py @@ -149,17 +149,19 @@ def load_config(cfg_path=_DEFAULT_CONFIG_PATH, countdown=_DEFAULT_COUNTDOWN): def check_auth(): """Checks auth information.""" - p = subprocess.run( - "cipd auth-info --json-output -", - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - text=True, - shell=True, - ) - if p.returncode != 0: + try: + out = subprocess.check_output( + "cipd auth-info --json-output -", + text=True, + shell=True, + timeout=3, + ) + except Exception as e: + print("WARNING: depot_tools.build_telemetry: " + f"failed to get auth info: {e}") return {} try: - return json.loads(p.stdout) + return json.loads(out) except json.JSONDecodeError as e: logging.error(e) return {} diff --git a/tests/build_telemetry_test.py b/tests/build_telemetry_test.py index d59fe3e3b..9310fd946 100755 --- a/tests/build_telemetry_test.py +++ b/tests/build_telemetry_test.py @@ -5,6 +5,7 @@ import json import os +import subprocess import sys import tempfile import unittest @@ -19,19 +20,18 @@ import build_telemetry class BuildTelemetryTest(unittest.TestCase): def test_check_auth(self): - with unittest.mock.patch('subprocess.run') as run_mock: - run_mock.return_value.returncode = 0 + with unittest.mock.patch('subprocess.check_output') as run_mock: auth = {'email': 'bob@google.com'} - run_mock.return_value.stdout = json.dumps(auth) + run_mock.return_value = json.dumps(auth) self.assertEqual(build_telemetry.check_auth(), auth) - with unittest.mock.patch('subprocess.run') as run_mock: - run_mock.return_value.returncode = 1 + with unittest.mock.patch('subprocess.check_output') as run_mock: + run_mock.side_effect = subprocess.CalledProcessError( + 1, cmd=['check auth'], stderr='failed') 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 = '' + with unittest.mock.patch('subprocess.check_output') as run_mock: + run_mock.return_value = '' self.assertEqual(build_telemetry.check_auth(), {}) def test_load_and_save_config(self):