瀏覽代碼

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>
Junji Watanabe 11 月之前
父節點
當前提交
28edf94b2b
共有 2 個文件被更改,包括 19 次插入17 次删除
  1. 11 9
      build_telemetry.py
  2. 8 8
      tests/build_telemetry_test.py

+ 11 - 9
build_telemetry.py

@@ -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(
-        "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 {}
         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 {}

+ 8 - 8
tests/build_telemetry_test.py

@@ -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:
-            run_mock.return_value.returncode = 0
+        with unittest.mock.patch('subprocess.check_output') as run_mock:
             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:
-            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(), {})
             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(), {})
             self.assertEqual(build_telemetry.check_auth(), {})
 
 
     def test_load_and_save_config(self):
     def test_load_and_save_config(self):