forked from GoogleCloudPlatform/cloud-sql-python-connector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
231 lines (192 loc) · 9.49 KB
/
Copy pathconftest.py
File metadata and controls
231 lines (192 loc) · 9.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
""""
Copyright 2021 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
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 socket
import asyncio
import pytest # noqa F401 Needed to run the tests
from threading import Thread
from typing import Any, Generator, AsyncGenerator, Tuple
from google.auth.credentials import Credentials, with_scopes_if_required
from google.oauth2 import service_account
from aioresponses import aioresponses
from mock import patch
from unit.mocks import FakeCSQLInstance # type: ignore
from google.cloud.sql.connector import Connector
from google.cloud.sql.connector.instance import Instance
from google.cloud.sql.connector.utils import generate_keys
SCOPES = ["https://www.googleapis.com/auth/sqlservice.admin"]
def pytest_addoption(parser: Any) -> None:
parser.addoption(
"--run_private_ip",
action="store_true",
default=False,
help="run tests that need to be running in VPC network",
)
def pytest_configure(config: Any) -> None:
config.addinivalue_line(
"markers", "private_ip: mark test as requiring private IP access"
)
def pytest_collection_modifyitems(config: Any, items: Any) -> None:
if config.getoption("--run_private_ip"):
return
skip_private_ip = pytest.mark.skip(reason="need --run_private_ip option to run")
for item in items:
if "private_ip" in item.keywords:
item.add_marker(skip_private_ip)
@pytest.fixture
def event_loop() -> asyncio.AbstractEventLoop:
"""
Creates an event loop to use for testing.
"""
loop: asyncio.AbstractEventLoop = asyncio.new_event_loop()
return loop
@pytest.fixture
def connect_string() -> str:
"""
Retrieves a valid connection string from the environment and
returns it.
"""
try:
connect_string = os.environ["MYSQL_CONNECTION_NAME"]
except KeyError:
raise KeyError(
"Please set environment variable 'INSTANCE_CONNECTION"
+ "_NAME' to a valid Cloud SQL connection string."
)
return connect_string
@pytest.fixture
def fake_credentials() -> Credentials:
fake_service_account = {
"type": "service_account",
"project_id": "a-project-id",
"private_key_id": "a-private-key-id",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDY3E8o1NEFcjMM\nHW/5ZfFJw29/8NEqpViNjQIx95Xx5KDtJ+nWn9+OW0uqsSqKlKGhAdAo+Q6bjx2c\nuXVsXTu7XrZUY5Kltvj94DvUa1wjNXs606r/RxWTJ58bfdC+gLLxBfGnB6CwK0YQ\nxnfpjNbkUfVVzO0MQD7UP0Hl5ZcY0Puvxd/yHuONQn/rIAieTHH1pqgW+zrH/y3c\n59IGThC9PPtugI9ea8RSnVj3PWz1bX2UkCDpy9IRh9LzJLaYYX9RUd7++dULUlat\nAaXBh1U6emUDzhrIsgApjDVtimOPbmQWmX1S60mqQikRpVYZ8u+NDD+LNw+/Eovn\nxCj2Y3z1AgMBAAECggEAWDBzoqO1IvVXjBA2lqId10T6hXmN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PEJB+dPgNDIaboAsh1nZhVhN5cvdvCWuEYgOGCPQLYQF0zmTLcM+sVxOYgfy8mV\nfbNgPgsP5xmu6dw2COBKdtozw0HrWSRjACd1N4yGu75+wPCcX/gQarcjRcXXZeEa\nNtBLSfcqPULqD+h7br9lEJio\n-----END PRIVATE KEY-----\n",
"client_email": "email@example.com",
"client_id": "12345",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/email%40example.com",
}
fake_credentials = service_account.Credentials.from_service_account_info(
fake_service_account
)
fake_credentials = with_scopes_if_required(fake_credentials, scopes=SCOPES)
# stub refresh of credentials
setattr(fake_credentials, "refresh", lambda *args: None)
return fake_credentials
def mock_server(server_sock: socket.socket) -> None:
"""Create mock server listening on specified ip_address and port."""
ip_address = "127.0.0.1"
port = 3307
server_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_sock.bind((ip_address, port))
server_sock.listen(0)
server_sock.accept()
@pytest.fixture
def server() -> Generator:
"""Create thread with server listening on proper port"""
server_sock = socket.socket()
thread = Thread(target=mock_server, args=(server_sock,), daemon=True)
thread.start()
yield thread
server_sock.close()
thread.join()
@pytest.fixture
def kwargs() -> Any:
"""Database connection keyword arguments."""
kwargs = {"user": "test-user", "db": "test-db", "password": "test-password"}
return kwargs
@pytest.fixture(scope="module")
def mock_instance() -> FakeCSQLInstance:
mock_instance = FakeCSQLInstance("my-project", "my-region", "my-instance")
return mock_instance
@pytest.fixture
async def instance(
mock_instance: FakeCSQLInstance,
fake_credentials: Credentials,
event_loop: asyncio.AbstractEventLoop,
) -> AsyncGenerator[Instance, None]:
"""
Instance with mocked API calls.
"""
# generate client key pair
keys = event_loop.create_task(generate_keys())
_, client_key = await keys
with patch("google.auth.default") as mock_auth:
mock_auth.return_value = fake_credentials, None
# mock Cloud SQL Admin API calls
with aioresponses() as mocked:
mocked.get(
f"https://sqladmin.googleapis.com/sql/v1beta4/projects/{mock_instance.project}/instances/{mock_instance.name}/connectSettings",
status=200,
body=mock_instance.connect_settings(),
repeat=True,
)
mocked.post(
f"https://sqladmin.googleapis.com/sql/v1beta4/projects/{mock_instance.project}/instances/{mock_instance.name}:generateEphemeralCert",
status=200,
body=mock_instance.generate_ephemeral(client_key),
repeat=True,
)
instance = Instance(
f"{mock_instance.project}:{mock_instance.region}:{mock_instance.name}",
"pg8000",
keys,
event_loop,
)
yield instance
await instance.close()
@pytest.fixture
async def connector(fake_credentials: Credentials) -> AsyncGenerator[Connector, None]:
instance_connection_name = "my-project:my-region:my-instance"
project, region, instance_name = instance_connection_name.split(":")
# initialize connector
connector = Connector()
with patch("google.auth.default") as mock_auth:
mock_auth.return_value = fake_credentials, None
# mock Cloud SQL Admin API calls
mock_instance = FakeCSQLInstance(project, region, instance_name)
async def wait_for_keys(future: asyncio.Future) -> Tuple[bytes, str]:
"""
Helper method to await keys of Connector in tests prior to
initializing an Instance object.
"""
return await future
# converting asyncio.Future into concurrent.Future
# await keys in background thread so that .result() is set
# required because keys are needed for mocks, but are not awaited
# in the code until Instance() is initialized
_, client_key = asyncio.run_coroutine_threadsafe(
wait_for_keys(connector._keys), connector._loop
).result()
with aioresponses() as mocked:
mocked.get(
f"https://sqladmin.googleapis.com/sql/v1beta4/projects/{project}/instances/{instance_name}/connectSettings",
status=200,
body=mock_instance.connect_settings(),
repeat=True,
)
mocked.post(
f"https://sqladmin.googleapis.com/sql/v1beta4/projects/{project}/instances/{instance_name}:generateEphemeralCert",
status=200,
body=mock_instance.generate_ephemeral(client_key),
repeat=True,
)
# initialize Instance using mocked API calls
instance = Instance(
instance_connection_name, "pg8000", connector._keys, connector._loop
)
connector._instances[instance_connection_name] = instance
yield connector
connector.close()