Skip to content

Commit 9c0671b

Browse files
committed
fix(gapic): mock os.path.exists in mTLS tests to support newer google-auth
1 parent b60bb04 commit 9c0671b

13 files changed

Lines changed: 38 additions & 38 deletions

File tree

packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,13 @@ def test__get_default_mtls_endpoint():
174174

175175
def test__read_environment_variables():
176176
assert {{ service.client_name }}._read_environment_variables() == (False, "auto", None)
177-
177+
178178
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}):
179179
assert {{ service.client_name }}._read_environment_variables() == (True, "auto", None)
180-
180+
181181
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}):
182182
assert {{ service.client_name }}._read_environment_variables() == (False, "auto", None)
183-
183+
184184
with mock.patch.dict(
185185
os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "Unsupported"}
186186
):
@@ -200,10 +200,10 @@ def test__read_environment_variables():
200200

201201
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "never"}):
202202
assert {{ service.client_name }}._read_environment_variables() == (False, "never", None)
203-
203+
204204
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "always"}):
205205
assert {{ service.client_name }}._read_environment_variables() == (False, "always", None)
206-
206+
207207
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "auto"}):
208208
assert {{ service.client_name }}._read_environment_variables() == (False, "auto", None)
209209

@@ -293,7 +293,7 @@ def test_use_client_cert_effective():
293293
assert {{ service.client_name }}._use_client_cert_effective() is False
294294

295295
# Test case 12: Test when `should_use_client_cert` is available and the
296-
# `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is unset. Also,
296+
# `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is unset. Also,
297297
# the GOOGLE_API_CONFIG environment variable is unset.
298298
if hasattr(google.auth.transport.mtls, "should_use_client_cert"):
299299
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": ""}):
@@ -303,7 +303,7 @@ def test_use_client_cert_effective():
303303
def test__get_client_cert_source():
304304
mock_provided_cert_source = mock.Mock()
305305
mock_default_cert_source = mock.Mock()
306-
306+
307307
assert {{ service.client_name }}._get_client_cert_source(None, False) is None
308308
assert {{ service.client_name }}._get_client_cert_source(mock_provided_cert_source, False) is None
309309
assert {{ service.client_name }}._get_client_cert_source(mock_provided_cert_source, True) == mock_provided_cert_source
@@ -867,7 +867,7 @@ def test_{{ service.client_name|snake_case }}_get_mtls_endpoint_and_cert_source(
867867
)
868868
assert api_endpoint == mock_api_endpoint
869869
assert cert_source is None
870-
870+
871871
# Test cases for mTLS enablement when GOOGLE_API_USE_CLIENT_CERTIFICATE is unset.
872872
test_cases = [
873873
(
@@ -900,7 +900,7 @@ def test_{{ service.client_name|snake_case }}_get_mtls_endpoint_and_cert_source(
900900
config_filename = "mock_certificate_config.json"
901901
config_file_content = json.dumps(config_data)
902902
m = mock.mock_open(read_data=config_file_content)
903-
with mock.patch("builtins.open", m):
903+
with mock.patch("builtins.open", m), mock.patch("os.path.exists", return_value=True):
904904
with mock.patch.dict(
905905
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}
906906
):
@@ -929,10 +929,10 @@ def test_{{ service.client_name|snake_case }}_get_mtls_endpoint_and_cert_source(
929929
},
930930
},
931931
mock_client_cert_source,
932-
),
932+
),
933933
(
934934
# With workloads not present in config, mTLS is disabled.
935-
{
935+
{
936936
"version": 1,
937937
"cert_configs": {},
938938
},
@@ -947,7 +947,7 @@ def test_{{ service.client_name|snake_case }}_get_mtls_endpoint_and_cert_source(
947947
config_filename = "mock_certificate_config.json"
948948
config_file_content = json.dumps(config_data)
949949
m = mock.mock_open(read_data=config_file_content)
950-
with mock.patch("builtins.open", m):
950+
with mock.patch("builtins.open", m), mock.patch("os.path.exists", return_value=True):
951951
with mock.patch.dict(
952952
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}
953953
):
@@ -994,7 +994,7 @@ def test_{{ service.client_name|snake_case }}_get_mtls_endpoint_and_cert_source(
994994
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "Unsupported"}):
995995
with pytest.raises(MutualTLSChannelError) as excinfo:
996996
client_class.get_mtls_endpoint_and_cert_source()
997-
997+
998998
assert str(excinfo.value) == "Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`"
999999

10001000
@pytest.mark.parametrize("client_class", [
@@ -1059,7 +1059,7 @@ def test_{{ service.client_name|snake_case }}_client_api_endpoint(client_class):
10591059
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "never"}):
10601060
client = client_class(client_options=options, credentials=ga_credentials.AnonymousCredentials())
10611061
assert client.api_endpoint == default_endpoint
1062-
1062+
10631063

10641064
@pytest.mark.parametrize("client_class,transport_class,transport_name", [
10651065
{% if 'grpc' in opts.transport %}

packages/gapic-generator/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_asset_service.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ def test_asset_service_client_get_mtls_endpoint_and_cert_source(client_class):
700700
config_filename = "mock_certificate_config.json"
701701
config_file_content = json.dumps(config_data)
702702
m = mock.mock_open(read_data=config_file_content)
703-
with mock.patch("builtins.open", m):
703+
with mock.patch("builtins.open", m), mock.patch("os.path.exists", return_value=True):
704704
with mock.patch.dict(
705705
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}
706706
):
@@ -747,7 +747,7 @@ def test_asset_service_client_get_mtls_endpoint_and_cert_source(client_class):
747747
config_filename = "mock_certificate_config.json"
748748
config_file_content = json.dumps(config_data)
749749
m = mock.mock_open(read_data=config_file_content)
750-
with mock.patch("builtins.open", m):
750+
with mock.patch("builtins.open", m), mock.patch("os.path.exists", return_value=True):
751751
with mock.patch.dict(
752752
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}
753753
):

packages/gapic-generator/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_iam_credentials.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ def test_iam_credentials_client_get_mtls_endpoint_and_cert_source(client_class):
690690
config_filename = "mock_certificate_config.json"
691691
config_file_content = json.dumps(config_data)
692692
m = mock.mock_open(read_data=config_file_content)
693-
with mock.patch("builtins.open", m):
693+
with mock.patch("builtins.open", m), mock.patch("os.path.exists", return_value=True):
694694
with mock.patch.dict(
695695
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}
696696
):
@@ -737,7 +737,7 @@ def test_iam_credentials_client_get_mtls_endpoint_and_cert_source(client_class):
737737
config_filename = "mock_certificate_config.json"
738738
config_file_content = json.dumps(config_data)
739739
m = mock.mock_open(read_data=config_file_content)
740-
with mock.patch("builtins.open", m):
740+
with mock.patch("builtins.open", m), mock.patch("os.path.exists", return_value=True):
741741
with mock.patch.dict(
742742
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}
743743
):

packages/gapic-generator/tests/integration/goldens/eventarc/tests/unit/gapic/eventarc_v1/test_eventarc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ def test_eventarc_client_get_mtls_endpoint_and_cert_source(client_class):
721721
config_filename = "mock_certificate_config.json"
722722
config_file_content = json.dumps(config_data)
723723
m = mock.mock_open(read_data=config_file_content)
724-
with mock.patch("builtins.open", m):
724+
with mock.patch("builtins.open", m), mock.patch("os.path.exists", return_value=True):
725725
with mock.patch.dict(
726726
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}
727727
):
@@ -768,7 +768,7 @@ def test_eventarc_client_get_mtls_endpoint_and_cert_source(client_class):
768768
config_filename = "mock_certificate_config.json"
769769
config_file_content = json.dumps(config_data)
770770
m = mock.mock_open(read_data=config_file_content)
771-
with mock.patch("builtins.open", m):
771+
with mock.patch("builtins.open", m), mock.patch("os.path.exists", return_value=True):
772772
with mock.patch.dict(
773773
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}
774774
):

packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_config_service_v2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ def test_config_service_v2_client_get_mtls_endpoint_and_cert_source(client_class
678678
config_filename = "mock_certificate_config.json"
679679
config_file_content = json.dumps(config_data)
680680
m = mock.mock_open(read_data=config_file_content)
681-
with mock.patch("builtins.open", m):
681+
with mock.patch("builtins.open", m), mock.patch("os.path.exists", return_value=True):
682682
with mock.patch.dict(
683683
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}
684684
):
@@ -725,7 +725,7 @@ def test_config_service_v2_client_get_mtls_endpoint_and_cert_source(client_class
725725
config_filename = "mock_certificate_config.json"
726726
config_file_content = json.dumps(config_data)
727727
m = mock.mock_open(read_data=config_file_content)
728-
with mock.patch("builtins.open", m):
728+
with mock.patch("builtins.open", m), mock.patch("os.path.exists", return_value=True):
729729
with mock.patch.dict(
730730
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}
731731
):

packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_logging_service_v2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ def test_logging_service_v2_client_get_mtls_endpoint_and_cert_source(client_clas
679679
config_filename = "mock_certificate_config.json"
680680
config_file_content = json.dumps(config_data)
681681
m = mock.mock_open(read_data=config_file_content)
682-
with mock.patch("builtins.open", m):
682+
with mock.patch("builtins.open", m), mock.patch("os.path.exists", return_value=True):
683683
with mock.patch.dict(
684684
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}
685685
):
@@ -726,7 +726,7 @@ def test_logging_service_v2_client_get_mtls_endpoint_and_cert_source(client_clas
726726
config_filename = "mock_certificate_config.json"
727727
config_file_content = json.dumps(config_data)
728728
m = mock.mock_open(read_data=config_file_content)
729-
with mock.patch("builtins.open", m):
729+
with mock.patch("builtins.open", m), mock.patch("os.path.exists", return_value=True):
730730
with mock.patch.dict(
731731
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}
732732
):

packages/gapic-generator/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_metrics_service_v2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ def test_metrics_service_v2_client_get_mtls_endpoint_and_cert_source(client_clas
677677
config_filename = "mock_certificate_config.json"
678678
config_file_content = json.dumps(config_data)
679679
m = mock.mock_open(read_data=config_file_content)
680-
with mock.patch("builtins.open", m):
680+
with mock.patch("builtins.open", m), mock.patch("os.path.exists", return_value=True):
681681
with mock.patch.dict(
682682
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}
683683
):
@@ -724,7 +724,7 @@ def test_metrics_service_v2_client_get_mtls_endpoint_and_cert_source(client_clas
724724
config_filename = "mock_certificate_config.json"
725725
config_file_content = json.dumps(config_data)
726726
m = mock.mock_open(read_data=config_file_content)
727-
with mock.patch("builtins.open", m):
727+
with mock.patch("builtins.open", m), mock.patch("os.path.exists", return_value=True):
728728
with mock.patch.dict(
729729
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}
730730
):

packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_config_service_v2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ def test_base_config_service_v2_client_get_mtls_endpoint_and_cert_source(client_
678678
config_filename = "mock_certificate_config.json"
679679
config_file_content = json.dumps(config_data)
680680
m = mock.mock_open(read_data=config_file_content)
681-
with mock.patch("builtins.open", m):
681+
with mock.patch("builtins.open", m), mock.patch("os.path.exists", return_value=True):
682682
with mock.patch.dict(
683683
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}
684684
):
@@ -725,7 +725,7 @@ def test_base_config_service_v2_client_get_mtls_endpoint_and_cert_source(client_
725725
config_filename = "mock_certificate_config.json"
726726
config_file_content = json.dumps(config_data)
727727
m = mock.mock_open(read_data=config_file_content)
728-
with mock.patch("builtins.open", m):
728+
with mock.patch("builtins.open", m), mock.patch("os.path.exists", return_value=True):
729729
with mock.patch.dict(
730730
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}
731731
):

packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_logging_service_v2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ def test_logging_service_v2_client_get_mtls_endpoint_and_cert_source(client_clas
679679
config_filename = "mock_certificate_config.json"
680680
config_file_content = json.dumps(config_data)
681681
m = mock.mock_open(read_data=config_file_content)
682-
with mock.patch("builtins.open", m):
682+
with mock.patch("builtins.open", m), mock.patch("os.path.exists", return_value=True):
683683
with mock.patch.dict(
684684
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}
685685
):
@@ -726,7 +726,7 @@ def test_logging_service_v2_client_get_mtls_endpoint_and_cert_source(client_clas
726726
config_filename = "mock_certificate_config.json"
727727
config_file_content = json.dumps(config_data)
728728
m = mock.mock_open(read_data=config_file_content)
729-
with mock.patch("builtins.open", m):
729+
with mock.patch("builtins.open", m), mock.patch("os.path.exists", return_value=True):
730730
with mock.patch.dict(
731731
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}
732732
):

packages/gapic-generator/tests/integration/goldens/logging_internal/tests/unit/gapic/logging_v2/test_metrics_service_v2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ def test_base_metrics_service_v2_client_get_mtls_endpoint_and_cert_source(client
677677
config_filename = "mock_certificate_config.json"
678678
config_file_content = json.dumps(config_data)
679679
m = mock.mock_open(read_data=config_file_content)
680-
with mock.patch("builtins.open", m):
680+
with mock.patch("builtins.open", m), mock.patch("os.path.exists", return_value=True):
681681
with mock.patch.dict(
682682
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}
683683
):
@@ -724,7 +724,7 @@ def test_base_metrics_service_v2_client_get_mtls_endpoint_and_cert_source(client
724724
config_filename = "mock_certificate_config.json"
725725
config_file_content = json.dumps(config_data)
726726
m = mock.mock_open(read_data=config_file_content)
727-
with mock.patch("builtins.open", m):
727+
with mock.patch("builtins.open", m), mock.patch("os.path.exists", return_value=True):
728728
with mock.patch.dict(
729729
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}
730730
):

0 commit comments

Comments
 (0)