Skip to content

Commit 171e6e1

Browse files
Add unit tests for StaticConnectionInfoCache
1 parent 5d9c473 commit 171e6e1

File tree

2 files changed

+99
-4
lines changed

2 files changed

+99
-4
lines changed

tests/unit/mocks.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def __init__(
149149
cluster: str = "test-cluster",
150150
name: str = "test-instance",
151151
ip_addrs: dict = {
152-
"PRIVATE": "127.0.0.1",
152+
"PRIVATE": "127.0.0.1", # "private" IP is localhost in testing
153153
"PUBLIC": "0.0.0.0",
154154
"PSC": "x.y.alloydb.goog",
155155
},
@@ -443,8 +443,8 @@ def write_static_info(i: FakeInstance) -> io.StringIO:
443443
static[i.uri()] = {
444444
"pemCertificateChain": chain,
445445
"caCert": ca_cert,
446-
"ipAddress": "127.0.0.1", # "private" IP is localhost in testing
447-
"publicIpAddress": "",
448-
"pscInstanceConfig": {"pscDnsName": ""},
446+
"ipAddress": i.ip_addrs["PRIVATE"],
447+
"publicIpAddress": i.ip_addrs["PUBLIC"],
448+
"pscInstanceConfig": {"pscDnsName": i.ip_addrs["PSC"]},
449449
}
450450
return io.StringIO(json.dumps(static))

tests/unit/test_static.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from mocks import FakeInstance, write_static_info
16+
17+
from google.cloud.alloydb.connector.connection_info import ConnectionInfo
18+
from google.cloud.alloydb.connector.static import StaticConnectionInfoCache
19+
20+
21+
def test_StaticConnectionInfoCache_init() -> None:
22+
"""
23+
Test that StaticConnectionInfoCache.__init__ populates its ConnectionInfo
24+
object.
25+
"""
26+
i = FakeInstance()
27+
static_info = write_static_info(i)
28+
cache = StaticConnectionInfoCache(i.uri(), static_info)
29+
assert len(cache._info.cert_chain) == 3
30+
assert cache._info.ca_cert
31+
assert cache._info.key
32+
assert cache._info.ip_addrs == {
33+
"PRIVATE": i.ip_addrs["PRIVATE"],
34+
"PUBLIC": i.ip_addrs["PUBLIC"],
35+
"PSC": i.ip_addrs["PSC"],
36+
}
37+
assert cache._info.expiration
38+
39+
def test_StaticConnectionInfoCache_init_trailing_dot_dns() -> None:
40+
"""
41+
Test that StaticConnectionInfoCache.__init__ populates its ConnectionInfo
42+
object correctly when its PSC DNS name contains a trailing dot.
43+
"""
44+
i = FakeInstance()
45+
no_trailing_dot_dns = i.ip_addrs["PSC"]
46+
i.ip_addrs["PSC"] += "."
47+
static_info = write_static_info(i)
48+
cache = StaticConnectionInfoCache(i.uri(), static_info)
49+
assert len(cache._info.cert_chain) == 3
50+
assert cache._info.ca_cert
51+
assert cache._info.key
52+
assert cache._info.ip_addrs == {
53+
"PRIVATE": i.ip_addrs["PRIVATE"],
54+
"PUBLIC": i.ip_addrs["PUBLIC"],
55+
"PSC": no_trailing_dot_dns,
56+
}
57+
assert cache._info.expiration
58+
59+
async def test_StaticConnectionInfoCache_force_refresh() -> None:
60+
"""
61+
Test that StaticConnectionInfoCache.force_refresh is a no-op.
62+
"""
63+
i = FakeInstance()
64+
static_info = write_static_info(i)
65+
cache = StaticConnectionInfoCache(i.uri(), static_info)
66+
conn_info = cache._info
67+
await cache.force_refresh()
68+
conn_info2 = cache._info
69+
assert conn_info2 == conn_info
70+
71+
async def test_StaticConnectionInfoCache_connect_info() -> None:
72+
"""
73+
Test that StaticConnectionInfoCache.connect_info works as expected.
74+
"""
75+
i = FakeInstance()
76+
static_info = write_static_info(i)
77+
cache = StaticConnectionInfoCache(i.uri(), static_info)
78+
# check that cached connection info is now set
79+
assert isinstance(cache._info, ConnectionInfo)
80+
conn_info = await cache.connect_info()
81+
# check that calling connect_info uses cached info
82+
conn_info2 = await cache.connect_info()
83+
assert conn_info2 == conn_info
84+
85+
async def test_StaticConnectionInfoCache_close() -> None:
86+
"""
87+
Test that StaticConnectionInfoCache.close is a no-op.
88+
"""
89+
i = FakeInstance()
90+
static_info = write_static_info(i)
91+
cache = StaticConnectionInfoCache(i.uri(), static_info)
92+
conn_info = cache._info
93+
await cache.close()
94+
conn_info2 = cache._info
95+
assert conn_info2 == conn_info

0 commit comments

Comments
 (0)