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 <jwata@google.com>
Commit-Queue: Junji Watanabe <jwata@google.com>
Reviewed-by: Philipp Wollermann <philwo@google.com>
changes/66/5842066/6
Junji Watanabe 10 months ago committed by LUCI CQ
parent 4250face7b
commit 28edf94b2b

@ -149,17 +149,19 @@ def load_config(cfg_path=_DEFAULT_CONFIG_PATH, countdown=_DEFAULT_COUNTDOWN):
def check_auth(): def check_auth():
"""Checks auth information.""" """Checks auth information."""
p = subprocess.run( try:
"cipd auth-info --json-output -", out = subprocess.check_output(
stdout=subprocess.PIPE, "cipd auth-info --json-output -",
stderr=subprocess.PIPE, text=True,
text=True, shell=True,
shell=True, timeout=3,
) )
if p.returncode != 0: except Exception as e:
print("WARNING: depot_tools.build_telemetry: "
f"failed to get auth info: {e}")
return {} return {}
try: try:
return json.loads(p.stdout) return json.loads(out)
except json.JSONDecodeError as e: except json.JSONDecodeError as e:
logging.error(e) logging.error(e)
return {} return {}

@ -5,6 +5,7 @@
import json import json
import os import os
import subprocess
import sys import sys
import tempfile import tempfile
import unittest import unittest
@ -19,19 +20,18 @@ import build_telemetry
class BuildTelemetryTest(unittest.TestCase): class BuildTelemetryTest(unittest.TestCase):
def test_check_auth(self): def test_check_auth(self):
with unittest.mock.patch('subprocess.run') as run_mock: with unittest.mock.patch('subprocess.check_output') as run_mock:
run_mock.return_value.returncode = 0
auth = {'email': 'bob@google.com'} 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) self.assertEqual(build_telemetry.check_auth(), auth)
with unittest.mock.patch('subprocess.run') as run_mock: with unittest.mock.patch('subprocess.check_output') as run_mock:
run_mock.return_value.returncode = 1 run_mock.side_effect = subprocess.CalledProcessError(
1, cmd=['check auth'], stderr='failed')
self.assertEqual(build_telemetry.check_auth(), {}) self.assertEqual(build_telemetry.check_auth(), {})
with unittest.mock.patch('subprocess.run') as run_mock: with unittest.mock.patch('subprocess.check_output') as run_mock:
run_mock.return_value.returncode = 0 run_mock.return_value = ''
run_mock.return_value.stdout = ''
self.assertEqual(build_telemetry.check_auth(), {}) self.assertEqual(build_telemetry.check_auth(), {})
def test_load_and_save_config(self): def test_load_and_save_config(self):

Loading…
Cancel
Save