-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathtest_monitored_cache.py
More file actions
240 lines (222 loc) · 8.14 KB
/
test_monitored_cache.py
File metadata and controls
240 lines (222 loc) · 8.14 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
232
233
234
235
236
237
238
239
240
# Copyright 2025 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
#
# http://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 asyncio
import socket
import ssl
import dns.message
import dns.rdataclass
import dns.rdatatype
import dns.resolver
from mock import patch
import pytest
from google.cloud.sql.connector.client import CloudSQLClient
from google.cloud.sql.connector.connection_name import ConnectionName
from google.cloud.sql.connector.exceptions import CacheClosedError
from google.cloud.sql.connector.lazy import LazyRefreshCache
from google.cloud.sql.connector.monitored_cache import MonitoredCache
from google.cloud.sql.connector.resolver import DefaultResolver
from google.cloud.sql.connector.resolver import DnsResolver
from google.cloud.sql.connector.utils import generate_keys
query_text = """id 1234
opcode QUERY
rcode NOERROR
flags QR AA RD RA
;QUESTION
db.example.com. IN TXT
;ANSWER
db.example.com. 0 IN TXT "test-project:test-region:test-instance"
;AUTHORITY
;ADDITIONAL
"""
async def test_MonitoredCache_properties(fake_client: CloudSQLClient) -> None:
"""
Test that MonitoredCache properties work as expected.
"""
conn_name = ConnectionName("test-project", "test-region", "test-instance")
cache = LazyRefreshCache(
conn_name,
client=fake_client,
keys=asyncio.create_task(generate_keys()),
enable_iam_auth=False,
)
monitored_cache = MonitoredCache(cache, 30, DefaultResolver())
# test that ticker is not set for instance not using domain name
assert monitored_cache.domain_name_ticker is None
# test closed property
assert monitored_cache.closed is False
# close cache and make sure property is updated
await monitored_cache.close()
assert monitored_cache.closed is True
async def test_MonitoredCache_CacheClosedError(fake_client: CloudSQLClient) -> None:
"""
Test that MonitoredCache.connect_info errors when cache is closed.
"""
conn_name = ConnectionName("test-project", "test-region", "test-instance")
cache = LazyRefreshCache(
conn_name,
client=fake_client,
keys=asyncio.create_task(generate_keys()),
enable_iam_auth=False,
)
monitored_cache = MonitoredCache(cache, 30, DefaultResolver())
# test closed property
assert monitored_cache.closed is False
# close cache and make sure property is updated
await monitored_cache.close()
assert monitored_cache.closed is True
# attempt to get connect info from closed cache
with pytest.raises(CacheClosedError):
await monitored_cache.connect_info()
async def test_MonitoredCache_with_DnsResolver(fake_client: CloudSQLClient) -> None:
"""
Test that MonitoredCache with DnsResolver work as expected.
"""
conn_name = ConnectionName(
"test-project", "test-region", "test-instance", "db.example.com"
)
cache = LazyRefreshCache(
conn_name,
client=fake_client,
keys=asyncio.create_task(generate_keys()),
enable_iam_auth=False,
)
# Patch DNS resolution with valid TXT records
with patch("dns.asyncresolver.Resolver.resolve") as mock_connect:
answer = dns.resolver.Answer(
"db.example.com",
dns.rdatatype.TXT,
dns.rdataclass.IN,
dns.message.from_text(query_text),
)
mock_connect.return_value = answer
resolver = DnsResolver()
resolver.port = 5053
monitored_cache = MonitoredCache(cache, 30, resolver)
# test that ticker is set for instance using domain name
assert type(monitored_cache.domain_name_ticker) is asyncio.Task
# test closed property
assert monitored_cache.closed is False
# close cache and make sure property is updated
await monitored_cache.close()
assert monitored_cache.closed is True
# domain name ticker should be set back to None
assert monitored_cache.domain_name_ticker is None
async def test_MonitoredCache_with_disabled_failover(
fake_client: CloudSQLClient,
) -> None:
"""
Test that MonitoredCache disables DNS polling with failover_period=0
"""
conn_name = ConnectionName(
"test-project", "test-region", "test-instance", "db.example.com"
)
cache = LazyRefreshCache(
conn_name,
client=fake_client,
keys=asyncio.create_task(generate_keys()),
enable_iam_auth=False,
)
monitored_cache = MonitoredCache(cache, 0, DnsResolver())
# test that ticker is not set when failover is disabled
assert monitored_cache.domain_name_ticker is None
# test closed property
assert monitored_cache.closed is False
# close cache and make sure property is updated
await monitored_cache.close()
assert monitored_cache.closed is True
@pytest.mark.usefixtures("proxy_server")
async def test_MonitoredCache_check_domain_name(
context: ssl.SSLContext, fake_client: CloudSQLClient
) -> None:
"""
Test that MonitoredCache is closed when _check_domain_name has domain change.
"""
conn_name = ConnectionName(
"my-project", "my-region", "my-instance", "db.example.com"
)
cache = LazyRefreshCache(
conn_name,
client=fake_client,
keys=asyncio.create_task(generate_keys()),
enable_iam_auth=False,
)
# Patch DNS resolution with valid TXT records
with patch("dns.asyncresolver.Resolver.resolve") as mock_connect:
answer = dns.resolver.Answer(
"db.example.com",
dns.rdatatype.TXT,
dns.rdataclass.IN,
dns.message.from_text(query_text),
)
mock_connect.return_value = answer
resolver = DnsResolver()
resolver.port = 5053
# configure a local socket
ip_addr = "127.0.0.1"
sock = context.wrap_socket(
socket.create_connection((ip_addr, 3307)),
server_hostname=ip_addr,
)
# verify socket is open
assert sock.fileno() != -1
# set failover to 0 to disable polling
monitored_cache = MonitoredCache(cache, 0, resolver)
# add socket to cache
monitored_cache.sockets = [sock]
# check cache is not closed
assert monitored_cache.closed is False
# call _check_domain_name and verify cache is closed
await monitored_cache._check_domain_name()
assert monitored_cache.closed is True
# verify socket was closed
assert sock.fileno() == -1
@pytest.mark.usefixtures("proxy_server")
async def test_MonitoredCache_purge_closed_sockets(
context: ssl.SSLContext, fake_client: CloudSQLClient
) -> None:
"""
Test that MonitoredCache._purge_closed_sockets removes closed sockets from
cache.
"""
conn_name = ConnectionName(
"my-project", "my-region", "my-instance", "db.example.com"
)
cache = LazyRefreshCache(
conn_name,
client=fake_client,
keys=asyncio.create_task(generate_keys()),
enable_iam_auth=False,
)
# configure a local socket
ip_addr = "127.0.0.1"
sock = context.wrap_socket(
socket.create_connection((ip_addr, 3307)),
server_hostname=ip_addr,
)
# set failover to 0 to disable polling
monitored_cache = MonitoredCache(cache, 0, DnsResolver())
# verify socket is open
assert sock.fileno() != -1
# add socket to cache
monitored_cache.sockets = [sock]
# call _purge_closed_sockets and verify socket remains
monitored_cache._purge_closed_sockets()
# verify socket is still open
assert sock.fileno() != -1
assert len(monitored_cache.sockets) == 1
# close socket
sock.close()
# call _purge_closed_sockets and verify socket is removed
monitored_cache._purge_closed_sockets()
assert len(monitored_cache.sockets) == 0