Skip to content

Commit 2612eb8

Browse files
fix: modify get_preferred_ip to check for empty values (#479)
getConnectionInfo RPC returns the IP type values as strings. The current approach only checks if the value is None, which is not a return type. This PR's change allows checking for None and empty string types.
1 parent 7ae1ab7 commit 2612eb8

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

google/cloud/alloydbconnector/connection_info.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def get_preferred_ip(self, ip_type: IPTypes) -> str:
7979
supplied by ip_type. If no IP addressess with the given preference are found,
8080
an error is raised."""
8181
ip_address = self.ip_addrs.get(ip_type.value)
82-
if ip_address is None:
82+
if not ip_address:
8383
raise IPTypeNotFoundError(
8484
"AlloyDB instance does not have an IP addresses matching "
8585
f"type: '{ip_type.value}'"

tests/unit/test_connection_info.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ async def test_ConnectionInfo_get_preferred_ip(ip_type: IPTypes, expected: str)
105105
assert ip_address == expected
106106

107107

108-
async def test_ConnectionInfo_get_preferred_ip_IPTypeNotFoundError() -> None:
108+
async def test_ConnectionInfo_get_preferred_ip_IPTypeNotFoundError_when_ip_type_not_exist() -> None:
109109
"""Test that ConnectionInfo.get_preferred_ip throws IPTypeNotFoundError"""
110110
conn_info = ConnectionInfo(
111111
["cert"],
@@ -117,3 +117,17 @@ async def test_ConnectionInfo_get_preferred_ip_IPTypeNotFoundError() -> None:
117117
# check error is thrown
118118
with pytest.raises(IPTypeNotFoundError):
119119
conn_info.get_preferred_ip(ip_type=IPTypes.PUBLIC)
120+
121+
122+
async def test_ConnectionInfo_get_preferred_ip_IPTypeNotFoundError_when_empty_value() -> None:
123+
"""Test that ConnectionInfo.get_preferred_ip throws IPTypeNotFoundError"""
124+
conn_info = ConnectionInfo(
125+
["cert"],
126+
"cert",
127+
"key",
128+
{"PUBLIC": ""},
129+
datetime.now(timezone.utc),
130+
)
131+
# check error is thrown
132+
with pytest.raises(IPTypeNotFoundError):
133+
conn_info.get_preferred_ip(ip_type=IPTypes.PUBLIC)

0 commit comments

Comments
 (0)