|
| 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