Skip to content

Commit 2dfee66

Browse files
authored
fix(psycopg): add platform guard for platforms without AF_UNIX support (#1466)
1 parent 2b20167 commit 2dfee66

4 files changed

Lines changed: 35 additions & 5 deletions

File tree

‎google/cloud/sql/connector/psycopg.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ def connect(
142142
'Unable to import module "psycopg." Please install and try again.'
143143
)
144144

145+
if not hasattr(socket, "AF_UNIX"):
146+
raise NotImplementedError(
147+
"Unix domain sockets (AF_UNIX) are not supported on this platform"
148+
)
149+
145150
tmpdir = tempfile.mkdtemp()
146151
socket_path = os.path.join(tmpdir, ".s.PGSQL.5432")
147152
logger.debug("psycopg: created Unix socket at %s", socket_path)

‎tests/system/test_psycopg_connection.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import asyncio
1919
from datetime import datetime
2020
import os
21+
import socket
2122

2223
import pytest
2324
import sqlalchemy
@@ -26,6 +27,11 @@
2627
from google.cloud.sql.connector import DefaultResolver
2728
from google.cloud.sql.connector import DnsResolver
2829

30+
pytestmark = pytest.mark.skipif(
31+
not hasattr(socket, "AF_UNIX"),
32+
reason="Unix domain sockets (AF_UNIX) not available on this platform",
33+
)
34+
2935

3036
def create_sqlalchemy_engine(
3137
instance_connection_name: str,

‎tests/system/test_psycopg_iam_auth.py‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,24 @@
1616

1717
from datetime import datetime
1818
import os
19+
import socket
1920

2021
import pytest
2122
import sqlalchemy
2223

2324
from google.cloud.sql.connector import Connector
2425

25-
# Skip all tests in this file if POSTGRES_IAM_USER is not set
26-
pytestmark = pytest.mark.skipif(
27-
not os.environ.get("POSTGRES_IAM_USER"),
28-
reason="POSTGRES_IAM_USER env var not set for IAM Authn tests",
29-
)
26+
# Skip all tests in this file if POSTGRES_IAM_USER is not set or AF_UNIX is not available
27+
pytestmark = [
28+
pytest.mark.skipif(
29+
not os.environ.get("POSTGRES_IAM_USER"),
30+
reason="POSTGRES_IAM_USER env var not set for IAM Authn tests",
31+
),
32+
pytest.mark.skipif(
33+
not hasattr(socket, "AF_UNIX"),
34+
reason="Unix domain sockets (AF_UNIX) not available on this platform",
35+
),
36+
]
3037

3138

3239
def create_sqlalchemy_engine(

‎tests/unit/test_psycopg.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,18 @@ def test_connect_import_error() -> None:
388388
connect("127.0.0.1", mock_remote_sock)
389389

390390

391+
def test_connect_unsupported_platform() -> None:
392+
"""Test that connect raises NotImplementedError when AF_UNIX is not available."""
393+
mock_remote_sock = MagicMock(spec=ssl.SSLSocket)
394+
with (
395+
patch("google.cloud.sql.connector.psycopg.hasattr", return_value=False),
396+
pytest.raises(
397+
NotImplementedError, match="Unix domain sockets \\(AF_UNIX\\)"
398+
),
399+
):
400+
connect("127.0.0.1", mock_remote_sock)
401+
402+
391403
def test_connect_cleanup_errors() -> None:
392404
"""Test that connect ignores OSErrors when removing temp files/dirs during cleanup."""
393405
mock_remote_sock = MagicMock(spec=ssl.SSLSocket)

0 commit comments

Comments
 (0)