|
| 1 | +import grpc |
| 2 | +import re |
| 3 | +import pytest |
| 4 | +from unittest.mock import patch, MagicMock, ANY |
| 5 | + |
| 6 | +from pinecone import Config |
| 7 | +from pinecone.grpc.channel_factory import GrpcChannelFactory, GRPCClientConfig |
| 8 | +from pinecone.utils.constants import MAX_MSG_SIZE |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture |
| 12 | +def config(): |
| 13 | + return Config(ssl_ca_certs=None, proxy_url=None) |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture |
| 17 | +def grpc_client_config(): |
| 18 | + return GRPCClientConfig(secure=True) |
| 19 | + |
| 20 | + |
| 21 | +class TestGrpcChannelFactory: |
| 22 | + def test_create_secure_channel_with_default_settings(self, config, grpc_client_config): |
| 23 | + factory = GrpcChannelFactory( |
| 24 | + config=config, grpc_client_config=grpc_client_config, use_asyncio=False |
| 25 | + ) |
| 26 | + endpoint = "test.endpoint:443" |
| 27 | + |
| 28 | + with patch("grpc.secure_channel") as mock_secure_channel, patch( |
| 29 | + "certifi.where", return_value="/path/to/certifi/cacert.pem" |
| 30 | + ), patch("builtins.open", new_callable=MagicMock) as mock_open: |
| 31 | + # Mock the file object to return bytes when read() is called |
| 32 | + mock_file = MagicMock() |
| 33 | + mock_file.read.return_value = b"mocked_cert_data" |
| 34 | + mock_open.return_value = mock_file |
| 35 | + channel = factory.create_channel(endpoint) |
| 36 | + |
| 37 | + mock_secure_channel.assert_called_once() |
| 38 | + assert mock_secure_channel.call_args[0][0] == endpoint |
| 39 | + assert isinstance(mock_secure_channel.call_args[1]["options"], tuple) |
| 40 | + |
| 41 | + options = dict(mock_secure_channel.call_args[1]["options"]) |
| 42 | + assert options["grpc.ssl_target_name_override"] == "test.endpoint" |
| 43 | + assert options["grpc.max_send_message_length"] == MAX_MSG_SIZE |
| 44 | + assert options["grpc.per_rpc_retry_buffer_size"] == MAX_MSG_SIZE |
| 45 | + assert options["grpc.max_receive_message_length"] == MAX_MSG_SIZE |
| 46 | + assert "grpc.service_config" in options |
| 47 | + assert options["grpc.enable_retries"] is True |
| 48 | + assert ( |
| 49 | + re.search( |
| 50 | + r"python-client\[grpc\]-\d+\.\d+\.\d+", options["grpc.primary_user_agent"] |
| 51 | + ) |
| 52 | + is not None |
| 53 | + ) |
| 54 | + |
| 55 | + assert isinstance(channel, MagicMock) |
| 56 | + |
| 57 | + def test_create_secure_channel_with_proxy(self): |
| 58 | + grpc_client_config = GRPCClientConfig(secure=True) |
| 59 | + config = Config(proxy_url="http://test.proxy:8080") |
| 60 | + factory = GrpcChannelFactory( |
| 61 | + config=config, grpc_client_config=grpc_client_config, use_asyncio=False |
| 62 | + ) |
| 63 | + endpoint = "test.endpoint:443" |
| 64 | + |
| 65 | + with patch("grpc.secure_channel") as mock_secure_channel: |
| 66 | + channel = factory.create_channel(endpoint) |
| 67 | + |
| 68 | + mock_secure_channel.assert_called_once() |
| 69 | + assert "grpc.http_proxy" in dict(mock_secure_channel.call_args[1]["options"]) |
| 70 | + assert ( |
| 71 | + "http://test.proxy:8080" |
| 72 | + == dict(mock_secure_channel.call_args[1]["options"])["grpc.http_proxy"] |
| 73 | + ) |
| 74 | + assert isinstance(channel, MagicMock) |
| 75 | + |
| 76 | + def test_create_insecure_channel(self, config): |
| 77 | + grpc_client_config = GRPCClientConfig(secure=False) |
| 78 | + factory = GrpcChannelFactory( |
| 79 | + config=config, grpc_client_config=grpc_client_config, use_asyncio=False |
| 80 | + ) |
| 81 | + endpoint = "test.endpoint:50051" |
| 82 | + |
| 83 | + with patch("grpc.insecure_channel") as mock_insecure_channel: |
| 84 | + channel = factory.create_channel(endpoint) |
| 85 | + |
| 86 | + mock_insecure_channel.assert_called_once_with(endpoint, options=ANY) |
| 87 | + assert isinstance(channel, MagicMock) |
| 88 | + |
| 89 | + |
| 90 | +class TestGrpcChannelFactoryAsyncio: |
| 91 | + def test_create_secure_channel_with_default_settings(self, config, grpc_client_config): |
| 92 | + factory = GrpcChannelFactory( |
| 93 | + config=config, grpc_client_config=grpc_client_config, use_asyncio=True |
| 94 | + ) |
| 95 | + endpoint = "test.endpoint:443" |
| 96 | + |
| 97 | + with patch("grpc.aio.secure_channel") as mock_secure_aio_channel, patch( |
| 98 | + "certifi.where", return_value="/path/to/certifi/cacert.pem" |
| 99 | + ), patch("builtins.open", new_callable=MagicMock) as mock_open: |
| 100 | + # Mock the file object to return bytes when read() is called |
| 101 | + mock_file = MagicMock() |
| 102 | + mock_file.read.return_value = b"mocked_cert_data" |
| 103 | + mock_open.return_value = mock_file |
| 104 | + channel = factory.create_channel(endpoint) |
| 105 | + |
| 106 | + mock_secure_aio_channel.assert_called_once() |
| 107 | + assert mock_secure_aio_channel.call_args[0][0] == endpoint |
| 108 | + assert isinstance(mock_secure_aio_channel.call_args[1]["options"], tuple) |
| 109 | + |
| 110 | + options = dict(mock_secure_aio_channel.call_args[1]["options"]) |
| 111 | + assert options["grpc.ssl_target_name_override"] == "test.endpoint" |
| 112 | + assert options["grpc.max_send_message_length"] == MAX_MSG_SIZE |
| 113 | + assert options["grpc.per_rpc_retry_buffer_size"] == MAX_MSG_SIZE |
| 114 | + assert options["grpc.max_receive_message_length"] == MAX_MSG_SIZE |
| 115 | + assert "grpc.service_config" in options |
| 116 | + assert options["grpc.enable_retries"] is True |
| 117 | + assert ( |
| 118 | + re.search( |
| 119 | + r"python-client\[grpc\]-\d+\.\d+\.\d+", options["grpc.primary_user_agent"] |
| 120 | + ) |
| 121 | + is not None |
| 122 | + ) |
| 123 | + |
| 124 | + security_credentials = mock_secure_aio_channel.call_args[1]["credentials"] |
| 125 | + assert security_credentials is not None |
| 126 | + assert isinstance(security_credentials, grpc.ChannelCredentials) |
| 127 | + |
| 128 | + assert isinstance(channel, MagicMock) |
| 129 | + |
| 130 | + def test_create_insecure_channel_asyncio(self, config): |
| 131 | + grpc_client_config = GRPCClientConfig(secure=False) |
| 132 | + factory = GrpcChannelFactory( |
| 133 | + config=config, grpc_client_config=grpc_client_config, use_asyncio=True |
| 134 | + ) |
| 135 | + endpoint = "test.endpoint:50051" |
| 136 | + |
| 137 | + with patch("grpc.aio.insecure_channel") as mock_aio_insecure_channel: |
| 138 | + channel = factory.create_channel(endpoint) |
| 139 | + |
| 140 | + mock_aio_insecure_channel.assert_called_once_with(endpoint, options=ANY) |
| 141 | + assert isinstance(channel, MagicMock) |
0 commit comments