From ea605d48db5f72870e7e9a42ee70df3d967fe94a Mon Sep 17 00:00:00 2001 From: Pavol Juhas Date: Fri, 13 Dec 2024 13:41:10 -0800 Subject: [PATCH] Disable local Google Cloud credentials in tests (#6851) Prevent tested notebooks from connecting to cloud services with local user credentials. Tell gcloud to use empty configuration directory instead of user configuration within pytest session. This makes the test of docs/tutorials/google/start.ipynb notebook use virtual hardware engine unconditionally. --- dev_tools/conftest.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/dev_tools/conftest.py b/dev_tools/conftest.py index 2e7bb5fc556..708c77a3347 100644 --- a/dev_tools/conftest.py +++ b/dev_tools/conftest.py @@ -11,6 +11,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. + import os import shutil import sys @@ -18,6 +19,7 @@ import uuid from pathlib import Path from typing import Tuple +from unittest import mock import pytest from filelock import FileLock @@ -26,6 +28,14 @@ from dev_tools.env_tools import create_virtual_env +@pytest.fixture(scope="session", autouse=True) +def disable_local_gcloud_credentials(tmp_path_factory): + # Ensure tests cannot authenticate to production servers with user credentials + empty_dir = tmp_path_factory.mktemp("empty_gcloud_config", numbered=False) + with mock.patch.dict(os.environ, {"CLOUDSDK_CONFIG": str(empty_dir)}): + yield + + @pytest.fixture(scope="session") def cloned_env(testrun_uid, worker_id): """Fixture to allow tests to run in a clean virtual env.