1
|
|
|
import pdb |
2
|
|
|
import logging |
3
|
|
|
import socket |
4
|
|
|
import pytest |
5
|
|
|
from utils import gen_unique_str |
6
|
|
|
from milvus import Milvus, 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 binary_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] = {"field": "binary_vector", "type": DataType.BINARY_VECTOR, "params": {"dim": dimension}} |
122
|
|
|
logging.getLogger().info(fields) |
123
|
|
|
try: |
124
|
|
|
connect.create_collection(collection_name, fields) |
125
|
|
|
except Exception as 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 |