From 28edf94b2becfe0adbac7d38a89d5d1c404b8075 Mon Sep 17 00:00:00 2001 From: Junji Watanabe Date: Fri, 6 Sep 2024 11:01:14 +0000 Subject: [PATCH] build_telemetry: Set timeout to 'cipd auth-info' command. When it's offline or the internet connection is unstable, build_telemetry.enabled() may fail, which end up with blocking autoninja command. Bug: 364611323 Change-Id: Ie0d26e7b950e29af1392e452a0cd7e986494cdcf Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5842066 Auto-Submit: Junji Watanabe Commit-Queue: Junji Watanabe Reviewed-by: Philipp Wollermann --- build_telemetry.py | 20 +++++++++++--------- tests/build_telemetry_test.py | 16 ++++++++-------- 2 files changed, 19 insertions(+), 17 deletions(-) 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):