|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""Unit tests for universe domain functionality in ClientOptions.""" |
| 3 | + |
| 4 | +# Copyright 2024 Google LLC |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +# |
| 18 | + |
| 19 | +from unittest import mock |
| 20 | + |
| 21 | +from google.api_core import client_options |
| 22 | +from google.auth import credentials as ga_credentials |
| 23 | +from google.cloud.aiplatform.compat.services import job_service_client |
| 24 | +import pytest |
| 25 | + |
| 26 | + |
| 27 | +@pytest.mark.parametrize( |
| 28 | + "location, universe_domain, expected_host", |
| 29 | + [ |
| 30 | + # Default case |
| 31 | + ( |
| 32 | + "us-central1", |
| 33 | + "googleapis.com", |
| 34 | + "us-central1-aiplatform.googleapis.com:443", |
| 35 | + ), |
| 36 | + # TPC case |
| 37 | + ( |
| 38 | + "u-us-prp1", |
| 39 | + "apis-tpclp.goog", |
| 40 | + "u-us-prp1-aiplatform.apis-tpclp.goog:443", |
| 41 | + ), |
| 42 | + ( |
| 43 | + "u-france-east1", |
| 44 | + "apis-s3ns.fr", |
| 45 | + "u-france-east1-aiplatform.apis-s3ns.fr:443", |
| 46 | + ), |
| 47 | + ], |
| 48 | +) |
| 49 | +@mock.patch("google.auth.default", autospec=True) |
| 50 | +def test_client_options_api_endpoint_with_universe( |
| 51 | + mock_auth_default, location, universe_domain, expected_host |
| 52 | +): |
| 53 | + """Verifies that ClientOptions correctly sets the endpoint for different universes.""" |
| 54 | + |
| 55 | + # Mock credentials to avoid actual authentication attempt |
| 56 | + mock_auth_default.return_value = ( |
| 57 | + ga_credentials.AnonymousCredentials(), |
| 58 | + "test-project", |
| 59 | + ) |
| 60 | + |
| 61 | + api_endpoint = f"{location}-aiplatform.{universe_domain}" |
| 62 | + opts = client_options.ClientOptions(api_endpoint=api_endpoint) |
| 63 | + |
| 64 | + # Instantiate the client with the configured options |
| 65 | + client = job_service_client.JobServiceClient(client_options=opts) |
| 66 | + |
| 67 | + # Check the host that the client's transport is using |
| 68 | + assert client.transport.host == expected_host |
0 commit comments