1
|
|
|
import pdb |
2
|
|
|
import logging |
3
|
|
|
import socket |
4
|
|
|
import pytest |
5
|
|
|
from utils import gen_unique_str |
6
|
|
|
from milvus import Milvus, IndexType, MetricType, DataType |
7
|
|
|
from utils import * |
8
|
|
|
|
9
|
|
|
timeout = 60 |
10
|
|
|
dimension = 128 |
11
|
|
|
delete_timeout = 60 |
12
|
|
|
default_fields = gen_default_fields() |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
def pytest_addoption(parser): |
16
|
|
|
parser.addoption("--ip", action="store", default="localhost") |
17
|
|
|
parser.addoption("--service", action="store", default="") |
18
|
|
|
parser.addoption("--port", action="store", default=19530) |
19
|
|
|
parser.addoption("--http-port", action="store", default=19121) |
20
|
|
|
parser.addoption("--handler", action="store", default="GRPC") |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
def check_server_connection(request): |
24
|
|
|
ip = request.config.getoption("--ip") |
25
|
|
|
port = request.config.getoption("--port") |
26
|
|
|
|
27
|
|
|
connected = True |
28
|
|
|
if ip and (ip not in ['localhost', '127.0.0.1']): |
29
|
|
|
try: |
30
|
|
|
socket.getaddrinfo(ip, port, 0, 0, socket.IPPROTO_TCP) |
31
|
|
|
except Exception as e: |
32
|
|
|
print("Socket connnet failed: %s" % str(e)) |
33
|
|
|
connected = False |
34
|
|
|
return connected |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
@pytest.fixture(scope="module") |
38
|
|
|
def connect(request): |
39
|
|
|
ip = request.config.getoption("--ip") |
40
|
|
|
service_name = request.config.getoption("--service") |
41
|
|
|
port = request.config.getoption("--port") |
42
|
|
|
http_port = request.config.getoption("--http-port") |
43
|
|
|
handler = request.config.getoption("--handler") |
44
|
|
|
if handler == "HTTP": |
45
|
|
|
port = http_port |
46
|
|
|
try: |
47
|
|
|
milvus = get_milvus(host=ip, port=port, handler=handler) |
48
|
|
|
except Exception as e: |
49
|
|
|
logging.getLogger().error(str(e)) |
50
|
|
|
pytest.exit("Milvus server can not connected, exit pytest ...") |
51
|
|
|
def fin(): |
52
|
|
|
try: |
53
|
|
|
milvus.close() |
54
|
|
|
pass |
55
|
|
|
except Exception as e: |
56
|
|
|
logging.getLogger().info(str(e)) |
57
|
|
|
request.addfinalizer(fin) |
58
|
|
|
return milvus |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
@pytest.fixture(scope="module") |
62
|
|
|
def dis_connect(request): |
63
|
|
|
ip = request.config.getoption("--ip") |
64
|
|
|
service_name = request.config.getoption("--service") |
65
|
|
|
port = request.config.getoption("--port") |
66
|
|
|
http_port = request.config.getoption("--http-port") |
67
|
|
|
handler = request.config.getoption("--handler") |
68
|
|
|
if handler == "HTTP": |
69
|
|
|
port = http_port |
70
|
|
|
milvus = get_milvus(host=ip, port=port, handler=handler) |
71
|
|
|
milvus.close() |
72
|
|
|
return milvus |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
@pytest.fixture(scope="module") |
76
|
|
|
def args(request): |
77
|
|
|
ip = request.config.getoption("--ip") |
78
|
|
|
service_name = request.config.getoption("--service") |
79
|
|
|
port = request.config.getoption("--port") |
80
|
|
|
http_port = request.config.getoption("--http-port") |
81
|
|
|
handler = request.config.getoption("--handler") |
82
|
|
|
if handler == "HTTP": |
83
|
|
|
port = http_port |
84
|
|
|
args = {"ip": ip, "port": port, "handler": handler, "service_name": service_name} |
85
|
|
|
return args |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
@pytest.fixture(scope="module") |
89
|
|
|
def milvus(request): |
90
|
|
|
ip = request.config.getoption("--ip") |
91
|
|
|
port = request.config.getoption("--port") |
92
|
|
|
http_port = request.config.getoption("--http-port") |
93
|
|
|
handler = request.config.getoption("--handler") |
94
|
|
|
if handler == "HTTP": |
95
|
|
|
port = http_port |
96
|
|
|
return get_milvus(host=ip, port=port, handler=handler) |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
@pytest.fixture(scope="function") |
100
|
|
|
def collection(request, connect): |
101
|
|
|
ori_collection_name = getattr(request.module, "collection_id", "test") |
102
|
|
|
collection_name = gen_unique_str(ori_collection_name) |
103
|
|
|
try: |
104
|
|
|
connect.create_collection(collection_name, default_fields) |
105
|
|
|
except Exception as e: |
106
|
|
|
pytest.exit(str(e)) |
107
|
|
|
def teardown(): |
108
|
|
|
collection_names = connect.list_collections() |
109
|
|
|
for collection_name in collection_names: |
110
|
|
|
connect.drop_collection(collection_name, timeout=delete_timeout) |
111
|
|
|
request.addfinalizer(teardown) |
112
|
|
|
assert connect.has_collection(collection_name) |
113
|
|
|
return collection_name |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
@pytest.fixture(scope="function") |
117
|
|
|
def ip_collection(request, connect): |
118
|
|
|
ori_collection_name = getattr(request.module, "collection_id", "test") |
119
|
|
|
collection_name = gen_unique_str(ori_collection_name) |
120
|
|
|
fields = gen_default_fields() |
121
|
|
|
fields["fields"][-1]["params"]["metric_type"] = "IP" |
122
|
|
|
try: |
123
|
|
|
connect.create_collection(collection_name, fields) |
124
|
|
|
except Exception as e: |
125
|
|
|
logging.getLogger().info(str(e)) |
126
|
|
|
pytest.exit(str(e)) |
127
|
|
|
def teardown(): |
128
|
|
|
collection_names = connect.list_collections() |
129
|
|
|
for collection_name in collection_names: |
130
|
|
|
connect.drop_collection(collection_name, timeout=delete_timeout) |
131
|
|
|
request.addfinalizer(teardown) |
132
|
|
|
assert connect.has_collection(collection_name) |
133
|
|
|
return collection_name |
134
|
|
|
|
135
|
|
|
|
136
|
|
View Code Duplication |
@pytest.fixture(scope="function") |
|
|
|
|
137
|
|
|
def jac_collection(request, connect): |
138
|
|
|
ori_collection_name = getattr(request.module, "collection_id", "test") |
139
|
|
|
collection_name = gen_unique_str(ori_collection_name) |
140
|
|
|
fields = gen_default_fields() |
141
|
|
|
fields["fields"][-1] = {"field": "binary_vector", "type": DataType.BINARY_VECTOR, "params": {"dimension": dimension, "metric_type": "JACCARD"}} |
142
|
|
|
logging.getLogger().info(fields) |
143
|
|
|
try: |
144
|
|
|
connect.create_collection(collection_name, fields) |
145
|
|
|
except Exception as e: |
146
|
|
|
pytest.exit(str(e)) |
147
|
|
|
def teardown(): |
148
|
|
|
collection_names = connect.list_collections() |
149
|
|
|
for collection_name in collection_names: |
150
|
|
|
connect.drop_collection(collection_name, timeout=delete_timeout) |
151
|
|
|
request.addfinalizer(teardown) |
152
|
|
|
assert connect.has_collection(collection_name) |
153
|
|
|
return collection_name |
154
|
|
|
|
155
|
|
|
|
156
|
|
|
@pytest.fixture(scope="function") |
157
|
|
|
def ham_collection(request, connect): |
158
|
|
|
ori_collection_name = getattr(request.module, "collection_id", "test") |
159
|
|
|
collection_name = gen_unique_str(ori_collection_name) |
160
|
|
|
fields = gen_default_fields() |
161
|
|
|
fields["fields"][-1] = {"field": "binary_vector", "type": DataType.BINARY_VECTOR, "params": {"dimension": dimension, "metric_type": "HAMMING"}} |
162
|
|
|
try: |
163
|
|
|
connect.create_collection(collection_name, fields) |
164
|
|
|
except Exception as e: |
165
|
|
|
pytest.exit(str(e)) |
166
|
|
|
def teardown(): |
167
|
|
|
collection_names = connect.list_collections() |
168
|
|
|
for collection_name in collection_names: |
169
|
|
|
connect.drop_collection(collection_name, timeout=delete_timeout) |
170
|
|
|
request.addfinalizer(teardown) |
171
|
|
|
assert connect.has_collection(collection_name) |
172
|
|
|
return collection_name |
173
|
|
|
|
174
|
|
|
|
175
|
|
|
@pytest.fixture(scope="function") |
176
|
|
|
def tanimoto_collection(request, connect): |
177
|
|
|
ori_collection_name = getattr(request.module, "collection_id", "test") |
178
|
|
|
collection_name = gen_unique_str(ori_collection_name) |
179
|
|
|
fields = gen_default_fields() |
180
|
|
|
fields["fields"][-1] = {"field": "binary_vector", "type": DataType.BINARY_VECTOR, "params": {"dimension": dimension, "metric_type": "TANIMOTO"}} |
181
|
|
|
try: |
182
|
|
|
connect.create_collection(collection_name, fields) |
183
|
|
|
except Exception as e: |
184
|
|
|
pytest.exit(str(e)) |
185
|
|
|
def teardown(): |
186
|
|
|
collection_names = connect.list_collections() |
187
|
|
|
for collection_name in collection_names: |
188
|
|
|
connect.drop_collection(collection_name, timeout=delete_timeout) |
189
|
|
|
request.addfinalizer(teardown) |
190
|
|
|
assert connect.has_collection(collection_name) |
191
|
|
|
return collection_name |
192
|
|
|
|
193
|
|
|
|
194
|
|
|
@pytest.fixture(scope="function") |
195
|
|
|
def substructure_collection(request, connect): |
196
|
|
|
ori_collection_name = getattr(request.module, "collection_id", "test") |
197
|
|
|
collection_name = gen_unique_str(ori_collection_name) |
198
|
|
|
fields = gen_default_fields() |
199
|
|
|
fields["fields"][-1] = {"field": "binary_vector", "type": DataType.BINARY_VECTOR, "params": {"dimension": dimension, "metric_type": "SUBSTRUCTURE"}} |
200
|
|
|
try: |
201
|
|
|
connect.create_collection(collection_name, fields) |
202
|
|
|
except Exception as e: |
203
|
|
|
pytest.exit(str(e)) |
204
|
|
|
def teardown(): |
205
|
|
|
collection_names = connect.list_collections() |
206
|
|
|
for collection_name in collection_names: |
207
|
|
|
connect.drop_collection(collection_name, timeout=delete_timeout) |
208
|
|
|
request.addfinalizer(teardown) |
209
|
|
|
assert connect.has_collection(collection_name) |
210
|
|
|
return collection_name |
211
|
|
|
|
212
|
|
|
|
213
|
|
View Code Duplication |
@pytest.fixture(scope="function") |
|
|
|
|
214
|
|
|
def superstructure_collection(request, connect): |
215
|
|
|
dim = getattr(request.module, "dim", "128") |
216
|
|
|
ori_collection_name = getattr(request.module, "collection_id", "test") |
217
|
|
|
collection_name = gen_unique_str(ori_collection_name) |
218
|
|
|
fields = gen_default_fields() |
219
|
|
|
fields["fields"][-1] = {"field": "binary_vector", "type": DataType.BINARY_VECTOR, "params": {"dimension": dimension, "metric_type": MetricType.SUPERSTRUCTURE}} |
220
|
|
|
try: |
221
|
|
|
connect.create_collection(collection_name, fields) |
222
|
|
|
except Exception as e: |
223
|
|
|
pytest.exit(str(e)) |
224
|
|
|
def teardown(): |
225
|
|
|
collection_names = connect.list_collections() |
226
|
|
|
for collection_name in collection_names: |
227
|
|
|
connect.drop_collection(collection_name, timeout=delete_timeout) |
228
|
|
|
request.addfinalizer(teardown) |
229
|
|
|
assert connect.has_collection(collection_name) |
230
|
|
|
return collection_name |
231
|
|
|
|