Skip to content

Commit 926a96f

Browse files
committed
fix(gapic): use safer mock for os.path.exists in mTLS tests
1 parent 9c0671b commit 926a96f

13 files changed

Lines changed: 396 additions & 381 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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), mock.patch("os.path.exists", return_value=True):
903+
with mock.patch("builtins.open", m), mock.patch("os.path.exists", side_effect=lambda path: os.path.basename(path) == config_filename):
904904
with mock.patch.dict(
905905
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}
906906
):
@@ -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), mock.patch("os.path.exists", return_value=True):
950+
with mock.patch("builtins.open", m), mock.patch("os.path.exists", side_effect=lambda path: os.path.basename(path) == config_filename):
951951
with mock.patch.dict(
952952
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}
953953
):

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

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,62 +13,60 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
#
16-
import os
1716
import asyncio
17+
import json
18+
import math
19+
import os
20+
from collections.abc import AsyncIterable, Iterable, Mapping, Sequence
1821
from unittest import mock
1922
from unittest.mock import AsyncMock
2023

2124
import grpc
22-
from grpc.experimental import aio
23-
from collections.abc import Iterable, AsyncIterable
24-
from google.protobuf import json_format
25-
import json
26-
import math
2725
import pytest
28-
from collections.abc import Sequence, Mapping
2926
from google.api_core import api_core_version
30-
from proto.marshal.rules.dates import DurationRule, TimestampRule
27+
from google.protobuf import json_format
28+
from grpc.experimental import aio
3129
from proto.marshal.rules import wrappers
32-
from requests import Response
33-
from requests import Request, PreparedRequest
30+
from proto.marshal.rules.dates import DurationRule, TimestampRule
31+
from requests import PreparedRequest, Request, Response
3432
from requests.sessions import Session
35-
from google.protobuf import json_format
3633

3734
try:
3835
from google.auth.aio import credentials as ga_credentials_async
3936
HAS_GOOGLE_AUTH_AIO = True
4037
except ImportError: # pragma: NO COVER
4138
HAS_GOOGLE_AUTH_AIO = False
4239

43-
from google.api_core import client_options
44-
from google.api_core import exceptions as core_exceptions
45-
from google.api_core import future
46-
from google.api_core import gapic_v1
47-
from google.api_core import grpc_helpers
48-
from google.api_core import grpc_helpers_async
49-
from google.api_core import operation
50-
from google.api_core import operations_v1
51-
from google.api_core import path_template
52-
from google.api_core import retry as retries
53-
from google.auth import credentials as ga_credentials
54-
from google.auth.exceptions import MutualTLSChannelError
55-
from google.cloud.asset_v1.services.asset_service import AssetServiceAsyncClient
56-
from google.cloud.asset_v1.services.asset_service import AssetServiceClient
57-
from google.cloud.asset_v1.services.asset_service import pagers
58-
from google.cloud.asset_v1.services.asset_service import transports
59-
from google.cloud.asset_v1.types import asset_service
60-
from google.cloud.asset_v1.types import assets
61-
from google.longrunning import operations_pb2 # type: ignore
62-
from google.oauth2 import service_account
6340
import google.api_core.operation_async as operation_async # type: ignore
6441
import google.auth
6542
import google.protobuf.duration_pb2 as duration_pb2 # type: ignore
6643
import google.protobuf.field_mask_pb2 as field_mask_pb2 # type: ignore
6744
import google.protobuf.timestamp_pb2 as timestamp_pb2 # type: ignore
6845
import google.rpc.status_pb2 as status_pb2 # type: ignore
6946
import google.type.expr_pb2 as expr_pb2 # type: ignore
70-
71-
47+
from google.api_core import (
48+
client_options,
49+
future,
50+
gapic_v1,
51+
grpc_helpers,
52+
grpc_helpers_async,
53+
operation,
54+
operations_v1,
55+
path_template,
56+
)
57+
from google.api_core import exceptions as core_exceptions
58+
from google.api_core import retry as retries
59+
from google.auth import credentials as ga_credentials
60+
from google.auth.exceptions import MutualTLSChannelError
61+
from google.cloud.asset_v1.services.asset_service import (
62+
AssetServiceAsyncClient,
63+
AssetServiceClient,
64+
pagers,
65+
transports,
66+
)
67+
from google.cloud.asset_v1.types import asset_service, assets
68+
from google.longrunning import operations_pb2 # type: ignore
69+
from google.oauth2 import service_account
7270

7371
CRED_INFO_JSON = {
7472
"credential_source": "/path/to/file",
@@ -700,7 +698,7 @@ def test_asset_service_client_get_mtls_endpoint_and_cert_source(client_class):
700698
config_filename = "mock_certificate_config.json"
701699
config_file_content = json.dumps(config_data)
702700
m = mock.mock_open(read_data=config_file_content)
703-
with mock.patch("builtins.open", m), mock.patch("os.path.exists", return_value=True):
701+
with mock.patch("builtins.open", m), mock.patch("os.path.exists", side_effect=lambda path: os.path.basename(path) == config_filename):
704702
with mock.patch.dict(
705703
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}
706704
):
@@ -747,7 +745,7 @@ def test_asset_service_client_get_mtls_endpoint_and_cert_source(client_class):
747745
config_filename = "mock_certificate_config.json"
748746
config_file_content = json.dumps(config_data)
749747
m = mock.mock_open(read_data=config_file_content)
750-
with mock.patch("builtins.open", m), mock.patch("os.path.exists", return_value=True):
748+
with mock.patch("builtins.open", m), mock.patch("os.path.exists", side_effect=lambda path: os.path.basename(path) == config_filename):
751749
with mock.patch.dict(
752750
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}
753751
):

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

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,52 +13,51 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
#
16-
import os
1716
import asyncio
17+
import json
18+
import math
19+
import os
20+
from collections.abc import AsyncIterable, Iterable, Mapping, Sequence
1821
from unittest import mock
1922
from unittest.mock import AsyncMock
2023

2124
import grpc
22-
from grpc.experimental import aio
23-
from collections.abc import Iterable, AsyncIterable
24-
from google.protobuf import json_format
25-
import json
26-
import math
2725
import pytest
28-
from collections.abc import Sequence, Mapping
2926
from google.api_core import api_core_version
30-
from proto.marshal.rules.dates import DurationRule, TimestampRule
27+
from google.protobuf import json_format
28+
from grpc.experimental import aio
3129
from proto.marshal.rules import wrappers
32-
from requests import Response
33-
from requests import Request, PreparedRequest
30+
from proto.marshal.rules.dates import DurationRule, TimestampRule
31+
from requests import PreparedRequest, Request, Response
3432
from requests.sessions import Session
35-
from google.protobuf import json_format
3633

3734
try:
3835
from google.auth.aio import credentials as ga_credentials_async
3936
HAS_GOOGLE_AUTH_AIO = True
4037
except ImportError: # pragma: NO COVER
4138
HAS_GOOGLE_AUTH_AIO = False
4239

43-
from google.api_core import client_options
40+
import google.auth
41+
import google.protobuf.duration_pb2 as duration_pb2 # type: ignore
42+
import google.protobuf.timestamp_pb2 as timestamp_pb2 # type: ignore
43+
from google.api_core import (
44+
client_options,
45+
gapic_v1,
46+
grpc_helpers,
47+
grpc_helpers_async,
48+
path_template,
49+
)
4450
from google.api_core import exceptions as core_exceptions
45-
from google.api_core import gapic_v1
46-
from google.api_core import grpc_helpers
47-
from google.api_core import grpc_helpers_async
48-
from google.api_core import path_template
4951
from google.api_core import retry as retries
5052
from google.auth import credentials as ga_credentials
5153
from google.auth.exceptions import MutualTLSChannelError
52-
from google.iam.credentials_v1.services.iam_credentials import IAMCredentialsAsyncClient
53-
from google.iam.credentials_v1.services.iam_credentials import IAMCredentialsClient
54-
from google.iam.credentials_v1.services.iam_credentials import transports
54+
from google.iam.credentials_v1.services.iam_credentials import (
55+
IAMCredentialsAsyncClient,
56+
IAMCredentialsClient,
57+
transports,
58+
)
5559
from google.iam.credentials_v1.types import common
5660
from google.oauth2 import service_account
57-
import google.auth
58-
import google.protobuf.duration_pb2 as duration_pb2 # type: ignore
59-
import google.protobuf.timestamp_pb2 as timestamp_pb2 # type: ignore
60-
61-
6261

6362
CRED_INFO_JSON = {
6463
"credential_source": "/path/to/file",
@@ -690,7 +689,7 @@ def test_iam_credentials_client_get_mtls_endpoint_and_cert_source(client_class):
690689
config_filename = "mock_certificate_config.json"
691690
config_file_content = json.dumps(config_data)
692691
m = mock.mock_open(read_data=config_file_content)
693-
with mock.patch("builtins.open", m), mock.patch("os.path.exists", return_value=True):
692+
with mock.patch("builtins.open", m), mock.patch("os.path.exists", side_effect=lambda path: os.path.basename(path) == config_filename):
694693
with mock.patch.dict(
695694
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}
696695
):
@@ -737,7 +736,7 @@ def test_iam_credentials_client_get_mtls_endpoint_and_cert_source(client_class):
737736
config_filename = "mock_certificate_config.json"
738737
config_file_content = json.dumps(config_data)
739738
m = mock.mock_open(read_data=config_file_content)
740-
with mock.patch("builtins.open", m), mock.patch("os.path.exists", return_value=True):
739+
with mock.patch("builtins.open", m), mock.patch("os.path.exists", side_effect=lambda path: os.path.basename(path) == config_filename):
741740
with mock.patch.dict(
742741
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}
743742
):

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

Lines changed: 55 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -13,83 +13,88 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
#
16-
import os
1716
import asyncio
17+
import json
18+
import math
19+
import os
20+
from collections.abc import AsyncIterable, Iterable, Mapping, Sequence
1821
from unittest import mock
1922
from unittest.mock import AsyncMock
2023

2124
import grpc
22-
from grpc.experimental import aio
23-
from collections.abc import Iterable, AsyncIterable
24-
from google.protobuf import json_format
25-
import json
26-
import math
2725
import pytest
28-
from collections.abc import Sequence, Mapping
2926
from google.api_core import api_core_version
30-
from proto.marshal.rules.dates import DurationRule, TimestampRule
27+
from google.protobuf import json_format
28+
from grpc.experimental import aio
3129
from proto.marshal.rules import wrappers
32-
from requests import Response
33-
from requests import Request, PreparedRequest
30+
from proto.marshal.rules.dates import DurationRule, TimestampRule
31+
from requests import PreparedRequest, Request, Response
3432
from requests.sessions import Session
35-
from google.protobuf import json_format
3633

3734
try:
3835
from google.auth.aio import credentials as ga_credentials_async
3936
HAS_GOOGLE_AUTH_AIO = True
4037
except ImportError: # pragma: NO COVER
4138
HAS_GOOGLE_AUTH_AIO = False
4239

43-
from google.api_core import client_options
40+
import google.api_core.operation_async as operation_async # type: ignore
41+
import google.auth
42+
import google.protobuf.duration_pb2 as duration_pb2 # type: ignore
43+
import google.protobuf.field_mask_pb2 as field_mask_pb2 # type: ignore
44+
import google.protobuf.timestamp_pb2 as timestamp_pb2 # type: ignore
45+
import google.rpc.code_pb2 as code_pb2 # type: ignore
46+
from google.api_core import (
47+
client_options,
48+
future,
49+
gapic_v1,
50+
grpc_helpers,
51+
grpc_helpers_async,
52+
operation,
53+
operations_v1,
54+
path_template,
55+
)
4456
from google.api_core import exceptions as core_exceptions
45-
from google.api_core import future
46-
from google.api_core import gapic_v1
47-
from google.api_core import grpc_helpers
48-
from google.api_core import grpc_helpers_async
49-
from google.api_core import operation
50-
from google.api_core import operations_v1
51-
from google.api_core import path_template
5257
from google.api_core import retry as retries
5358
from google.auth import credentials as ga_credentials
5459
from google.auth.exceptions import MutualTLSChannelError
55-
from google.cloud.eventarc_v1.services.eventarc import EventarcAsyncClient
56-
from google.cloud.eventarc_v1.services.eventarc import EventarcClient
57-
from google.cloud.eventarc_v1.services.eventarc import pagers
58-
from google.cloud.eventarc_v1.services.eventarc import transports
59-
from google.cloud.eventarc_v1.types import channel
60+
from google.cloud.eventarc_v1.services.eventarc import (
61+
EventarcAsyncClient,
62+
EventarcClient,
63+
pagers,
64+
transports,
65+
)
66+
from google.cloud.eventarc_v1.types import (
67+
channel,
68+
channel_connection,
69+
discovery,
70+
enrollment,
71+
eventarc,
72+
google_api_source,
73+
google_channel_config,
74+
logging_config,
75+
message_bus,
76+
network_config,
77+
pipeline,
78+
trigger,
79+
)
6080
from google.cloud.eventarc_v1.types import channel as gce_channel
61-
from google.cloud.eventarc_v1.types import channel_connection
6281
from google.cloud.eventarc_v1.types import channel_connection as gce_channel_connection
63-
from google.cloud.eventarc_v1.types import discovery
64-
from google.cloud.eventarc_v1.types import enrollment
6582
from google.cloud.eventarc_v1.types import enrollment as gce_enrollment
66-
from google.cloud.eventarc_v1.types import eventarc
67-
from google.cloud.eventarc_v1.types import google_api_source
6883
from google.cloud.eventarc_v1.types import google_api_source as gce_google_api_source
69-
from google.cloud.eventarc_v1.types import google_channel_config
70-
from google.cloud.eventarc_v1.types import google_channel_config as gce_google_channel_config
71-
from google.cloud.eventarc_v1.types import logging_config
72-
from google.cloud.eventarc_v1.types import message_bus
84+
from google.cloud.eventarc_v1.types import (
85+
google_channel_config as gce_google_channel_config,
86+
)
7387
from google.cloud.eventarc_v1.types import message_bus as gce_message_bus
74-
from google.cloud.eventarc_v1.types import network_config
75-
from google.cloud.eventarc_v1.types import pipeline
7688
from google.cloud.eventarc_v1.types import pipeline as gce_pipeline
77-
from google.cloud.eventarc_v1.types import trigger
7889
from google.cloud.eventarc_v1.types import trigger as gce_trigger
7990
from google.cloud.location import locations_pb2
80-
from google.iam.v1 import iam_policy_pb2 # type: ignore
81-
from google.iam.v1 import options_pb2 # type: ignore
82-
from google.iam.v1 import policy_pb2 # type: ignore
83-
from google.longrunning import operations_pb2 # type: ignore
91+
from google.iam.v1 import (
92+
iam_policy_pb2, # type: ignore
93+
options_pb2, # type: ignore
94+
policy_pb2, # type: ignore
95+
)
96+
from google.longrunning import operations_pb2 # type: ignore
8497
from google.oauth2 import service_account
85-
import google.api_core.operation_async as operation_async # type: ignore
86-
import google.auth
87-
import google.protobuf.duration_pb2 as duration_pb2 # type: ignore
88-
import google.protobuf.field_mask_pb2 as field_mask_pb2 # type: ignore
89-
import google.protobuf.timestamp_pb2 as timestamp_pb2 # type: ignore
90-
import google.rpc.code_pb2 as code_pb2 # type: ignore
91-
92-
9398

9499
CRED_INFO_JSON = {
95100
"credential_source": "/path/to/file",
@@ -721,7 +726,7 @@ def test_eventarc_client_get_mtls_endpoint_and_cert_source(client_class):
721726
config_filename = "mock_certificate_config.json"
722727
config_file_content = json.dumps(config_data)
723728
m = mock.mock_open(read_data=config_file_content)
724-
with mock.patch("builtins.open", m), mock.patch("os.path.exists", return_value=True):
729+
with mock.patch("builtins.open", m), mock.patch("os.path.exists", side_effect=lambda path: os.path.basename(path) == config_filename):
725730
with mock.patch.dict(
726731
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}
727732
):
@@ -768,7 +773,7 @@ def test_eventarc_client_get_mtls_endpoint_and_cert_source(client_class):
768773
config_filename = "mock_certificate_config.json"
769774
config_file_content = json.dumps(config_data)
770775
m = mock.mock_open(read_data=config_file_content)
771-
with mock.patch("builtins.open", m), mock.patch("os.path.exists", return_value=True):
776+
with mock.patch("builtins.open", m), mock.patch("os.path.exists", side_effect=lambda path: os.path.basename(path) == config_filename):
772777
with mock.patch.dict(
773778
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}
774779
):

0 commit comments

Comments
 (0)