File tree Expand file tree Collapse file tree
google/cloud/sql/connector Expand file tree Collapse file tree Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff line change 1818import asyncio
1919from datetime import datetime
2020import os
21+ import socket
2122
2223import pytest
2324import sqlalchemy
2627from google .cloud .sql .connector import DefaultResolver
2728from 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
3036def create_sqlalchemy_engine (
3137 instance_connection_name : str ,
Original file line number Diff line number Diff line change 1616
1717from datetime import datetime
1818import os
19+ import socket
1920
2021import pytest
2122import sqlalchemy
2223
2324from 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
3239def create_sqlalchemy_engine (
Original file line number Diff line number Diff 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+
391403def 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 )
You can’t perform that action at this time.
0 commit comments