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
|
|
|
|
13
|
|
|
|
14
|
|
|
def pytest_addoption(parser): |
15
|
|
|
parser.addoption("--ip", action="store", default="localhost") |
16
|
|
|
parser.addoption("--service", action="store", default="") |
17
|
|
|
parser.addoption("--port", action="store", default=19530) |
18
|
|
|
parser.addoption("--http-port", action="store", default=19121) |
19
|
|
|
parser.addoption("--handler", action="store", default="GRPC") |
20
|
|
|
parser.addoption("--tag", action="store", default="all", help="only run tests matching the tag.") |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
def pytest_configure(config): |
24
|
|
|
# register an additional marker |
25
|
|
|
config.addinivalue_line( |
26
|
|
|
"markers", "tag(name): mark test to run only matching the tag" |
27
|
|
|
) |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
def pytest_runtest_setup(item): |
31
|
|
|
tags = list() |
32
|
|
|
for marker in item.iter_markers(name="tag"): |
33
|
|
|
for tag in marker.args: |
34
|
|
|
tags.append(tag) |
35
|
|
|
if tags: |
36
|
|
|
cmd_tag = item.config.getoption("--tag") |
37
|
|
|
if cmd_tag != "all" and cmd_tag not in tags: |
38
|
|
|
pytest.skip("test requires tag in {!r}".format(tags)) |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
def check_server_connection(request): |
42
|
|
|
ip = request.config.getoption("--ip") |
43
|
|
|
port = request.config.getoption("--port") |
44
|
|
|
|
45
|
|
|
connected = True |
46
|
|
|
if ip and (ip not in ['localhost', '127.0.0.1']): |
47
|
|
|
try: |
48
|
|
|
socket.getaddrinfo(ip, port, 0, 0, socket.IPPROTO_TCP) |
49
|
|
|
except Exception as e: |
50
|
|
|
print("Socket connnet failed: %s" % str(e)) |
51
|
|
|
connected = False |
52
|
|
|
return connected |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
@pytest.fixture(scope="module") |
56
|
|
|
def connect(request): |
57
|
|
|
ip = request.config.getoption("--ip") |
58
|
|
|
service_name = request.config.getoption("--service") |
59
|
|
|
port = request.config.getoption("--port") |
60
|
|
|
http_port = request.config.getoption("--http-port") |
61
|
|
|
handler = request.config.getoption("--handler") |
62
|
|
|
if handler == "HTTP": |
63
|
|
|
port = http_port |
64
|
|
|
try: |
65
|
|
|
milvus = get_milvus(host=ip, port=port, handler=handler) |
66
|
|
|
except Exception as e: |
67
|
|
|
logging.getLogger().error(str(e)) |
68
|
|
|
pytest.exit("Milvus server can not connected, exit pytest ...") |
69
|
|
|
def fin(): |
70
|
|
|
try: |
71
|
|
|
milvus.close() |
72
|
|
|
pass |
73
|
|
|
except Exception as e: |
74
|
|
|
logging.getLogger().info(str(e)) |
75
|
|
|
request.addfinalizer(fin) |
76
|
|
|
return milvus |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
@pytest.fixture(scope="module") |
80
|
|
|
def dis_connect(request): |
81
|
|
|
ip = request.config.getoption("--ip") |
82
|
|
|
service_name = request.config.getoption("--service") |
83
|
|
|
port = request.config.getoption("--port") |
84
|
|
|
http_port = request.config.getoption("--http-port") |
85
|
|
|
handler = request.config.getoption("--handler") |
86
|
|
|
if handler == "HTTP": |
87
|
|
|
port = http_port |
88
|
|
|
milvus = get_milvus(host=ip, port=port, handler=handler) |
89
|
|
|
milvus.close() |
90
|
|
|
return milvus |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
@pytest.fixture(scope="module") |
94
|
|
|
def args(request): |
95
|
|
|
ip = request.config.getoption("--ip") |
96
|
|
|
service_name = request.config.getoption("--service") |
97
|
|
|
port = request.config.getoption("--port") |
98
|
|
|
http_port = request.config.getoption("--http-port") |
99
|
|
|
handler = request.config.getoption("--handler") |
100
|
|
|
if handler == "HTTP": |
101
|
|
|
port = http_port |
102
|
|
|
args = {"ip": ip, "port": port, "handler": handler, "service_name": service_name} |
103
|
|
|
return args |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
@pytest.fixture(scope="module") |
107
|
|
|
def milvus(request): |
108
|
|
|
ip = request.config.getoption("--ip") |
109
|
|
|
port = request.config.getoption("--port") |
110
|
|
|
http_port = request.config.getoption("--http-port") |
111
|
|
|
handler = request.config.getoption("--handler") |
112
|
|
|
if handler == "HTTP": |
113
|
|
|
port = http_port |
114
|
|
|
return get_milvus(host=ip, port=port, handler=handler) |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
@pytest.fixture(scope="function") |
118
|
|
|
def collection(request, connect): |
119
|
|
|
ori_collection_name = getattr(request.module, "collection_id", "test") |
120
|
|
|
collection_name = gen_unique_str(ori_collection_name) |
121
|
|
|
try: |
122
|
|
|
default_fields = gen_default_fields() |
123
|
|
|
connect.create_collection(collection_name, default_fields) |
124
|
|
|
except Exception as e: |
125
|
|
|
pytest.exit(str(e)) |
126
|
|
|
def teardown(): |
127
|
|
|
collection_names = connect.list_collections() |
128
|
|
|
for collection_name in collection_names: |
129
|
|
|
connect.drop_collection(collection_name, timeout=delete_timeout) |
130
|
|
|
request.addfinalizer(teardown) |
131
|
|
|
assert connect.has_collection(collection_name) |
132
|
|
|
return collection_name |
133
|
|
|
|
134
|
|
|
|
135
|
|
View Code Duplication |
@pytest.fixture(scope="function") |
|
|
|
|
136
|
|
|
def id_collection(request, connect): |
137
|
|
|
ori_collection_name = getattr(request.module, "collection_id", "test") |
138
|
|
|
collection_name = gen_unique_str(ori_collection_name) |
139
|
|
|
try: |
140
|
|
|
fields = gen_default_fields(auto_id=True) |
141
|
|
|
connect.create_collection(collection_name, fields) |
142
|
|
|
except Exception as e: |
143
|
|
|
pytest.exit(str(e)) |
144
|
|
|
def teardown(): |
145
|
|
|
collection_names = connect.list_collections() |
146
|
|
|
for collection_name in collection_names: |
147
|
|
|
connect.drop_collection(collection_name, timeout=delete_timeout) |
148
|
|
|
request.addfinalizer(teardown) |
149
|
|
|
assert connect.has_collection(collection_name) |
150
|
|
|
return collection_name |
151
|
|
|
|
152
|
|
|
|
153
|
|
|
@pytest.fixture(scope="function") |
154
|
|
|
def binary_collection(request, connect): |
155
|
|
|
ori_collection_name = getattr(request.module, "collection_id", "test") |
156
|
|
|
collection_name = gen_unique_str(ori_collection_name) |
157
|
|
|
try: |
158
|
|
|
fields = gen_binary_default_fields() |
159
|
|
|
connect.create_collection(collection_name, fields) |
160
|
|
|
except Exception as e: |
161
|
|
|
pytest.exit(str(e)) |
162
|
|
|
def teardown(): |
163
|
|
|
collection_names = connect.list_collections() |
164
|
|
|
for collection_name in collection_names: |
165
|
|
|
connect.drop_collection(collection_name, timeout=delete_timeout) |
166
|
|
|
request.addfinalizer(teardown) |
167
|
|
|
assert connect.has_collection(collection_name) |
168
|
|
|
return collection_name |
169
|
|
|
|
170
|
|
|
|
171
|
|
View Code Duplication |
@pytest.fixture(scope="function") |
|
|
|
|
172
|
|
|
def binary_id_collection(request, connect): |
173
|
|
|
ori_collection_name = getattr(request.module, "collection_id", "test") |
174
|
|
|
collection_name = gen_unique_str(ori_collection_name) |
175
|
|
|
try: |
176
|
|
|
fields = gen_binary_default_fields(auto_id=True) |
177
|
|
|
connect.create_collection(collection_name, fields) |
178
|
|
|
except Exception as e: |
179
|
|
|
pytest.exit(str(e)) |
180
|
|
|
def teardown(): |
181
|
|
|
collection_names = connect.list_collections() |
182
|
|
|
for collection_name in collection_names: |
183
|
|
|
connect.drop_collection(collection_name, timeout=delete_timeout) |
184
|
|
|
request.addfinalizer(teardown) |
185
|
|
|
assert connect.has_collection(collection_name) |
186
|
|
|
return collection_name |
187
|
|
|
|