forked from vesoft-inc/nebula
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
279 lines (220 loc) · 8.76 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# --coding:utf-8--
#
# Copyright (c) 2020 vesoft inc. All rights reserved.
#
# This source code is licensed under Apache 2.0 License.
import json
import os
import pytest
import logging
from tests.common.configs import all_configs
from tests.common.types import SpaceDesc
from tests.common.utils import get_conn_pool, get_ssl_config
from tests.common.constants import NB_TMP_PATH, SPACE_TMP_PATH, BUILD_DIR, NEBULA_HOME
from tests.common.nebula_service import NebulaService
from nebula3.fbthrift.transport import TSocket
from nebula3.fbthrift.transport import TTransport
from nebula3.fbthrift.protocol import TBinaryProtocol
from nebula3.gclient.net import Connection
from nebula3.graph import GraphService
tests_collected = set()
tests_executed = set()
data_dir = os.getenv('NEBULA_DATA_DIR')
# pytest hook to handle test collection when xdist is used (parallel tests)
# https://github.com/pytest-dev/pytest-xdist/pull/35/commits (No official documentation available)
def pytest_xdist_node_collection_finished(node, ids):
tests_collected.update(set(ids))
# link to pytest_collection_modifyitems
# https://docs.pytest.org/en/5.3.2/writing_plugins.html#hook-function-validation-and-execution
@pytest.hookimpl(tryfirst=True)
def pytest_collection_modifyitems(items):
for item in items:
tests_collected.add(item.nodeid)
# link to pytest_runtest_logreport
# https://docs.pytest.org/en/5.3.2/reference.html#_pytest.hookspec.pytest_runtest_logreport
def pytest_runtest_logreport(report):
if report.passed:
tests_executed.add(report.nodeid)
def pytest_addoption(parser):
for config in all_configs:
parser.addoption(
config,
dest=all_configs[config][0],
default=all_configs[config][1],
help=all_configs[config][2],
)
parser.addoption(
"--build_dir",
dest="build_dir",
default=BUILD_DIR,
help="NebulaGraph CMake build directory",
)
parser.addoption(
"--src_dir", dest="src_dir", default=NEBULA_HOME, help="NebulaGraph workspace"
)
def pytest_bdd_step_error(request, feature, scenario, step, step_func, step_func_args):
logging.error("Location: {}:{}".format(feature.filename, step.line_number))
logging.error("Step: {}".format(step.name))
graph_spaces = None
if graph_spaces is None and step_func_args.get("graph_spaces") is not None:
graph_spaces = step_func_args.get("graph_spaces")
if graph_spaces is None and step_func_args.get("exec_ctx") is not None:
graph_spaces = step_func_args.get("exec_ctx")
if graph_spaces is not None and graph_spaces.get("space_desc") is not None:
logging.error("Space: {}".format(graph_spaces.get("space_desc")))
def pytest_configure(config):
pytest.cmdline.address = config.getoption("address")
pytest.cmdline.user = config.getoption("user")
pytest.cmdline.password = config.getoption("password")
pytest.cmdline.replica_factor = config.getoption("replica_factor")
pytest.cmdline.partition_num = config.getoption("partition_num")
if data_dir is None:
pytest.cmdline.data_dir = config.getoption("data_dir")
else:
pytest.cmdline.data_dir = data_dir
pytest.cmdline.build_dir = config.getoption("build_dir")
pytest.cmdline.src_dir = config.getoption("src_dir")
pytest.cmdline.stop_nebula = config.getoption("stop_nebula")
pytest.cmdline.rm_dir = config.getoption("rm_dir")
pytest.cmdline.debug_log = config.getoption("debug_log")
def get_port():
with open(NB_TMP_PATH, "r") as f:
data = json.loads(f.readline())
port = data.get("port", None)
if port is None:
raise Exception(f"Invalid port: {port}")
return port[0]
def get_ports():
with open(NB_TMP_PATH, "r") as f:
data = json.loads(f.readline())
port = data.get("port", None)
if port is None:
raise Exception(f"Invalid port: {port}")
return port
def get_ssl_config_from_tmp():
with open(NB_TMP_PATH, "r") as f:
data = json.loads(f.readline())
is_graph_ssl = (
data.get("enable_ssl", "false").upper() == "TRUE"
or data.get("enable_graph_ssl", "false").upper() == "TRUE"
)
ca_signed = data.get("ca_signed", "false").upper() == "TRUE"
return get_ssl_config(is_graph_ssl, ca_signed)
@pytest.fixture(scope="class")
def class_fixture_variables():
"""save class scope fixture, used for session update."""
# cluster is the instance of NebulaService
# current_session is the session currently using
# sessions is a list of all sessions in the cluster
res = dict(
pool=None,
current_session=None,
cluster=None,
sessions=[],
)
yield res
for sess in res["sessions"]:
if sess is not None:
sess.release()
if res["pool"] is not None:
res["pool"].close()
if res["cluster"] is not None:
_cluster = res["cluster"]
assert isinstance(_cluster, NebulaService)
_cluster.stop()
@pytest.fixture(scope="session")
def conn_pool_to_first_graph_service(pytestconfig):
addr = pytestconfig.getoption("address")
host_addr = addr.split(":") if addr else ["localhost", get_ports()[0]]
assert len(host_addr) == 2
ssl_config = get_ssl_config_from_tmp()
pool = get_conn_pool(host_addr[0], host_addr[1], ssl_config)
yield pool
pool.close()
@pytest.fixture(scope="session")
def conn_pool_to_second_graph_service(pytestconfig):
addr = pytestconfig.getoption("address")
host_addr = ["localhost", get_ports()[1]]
assert len(host_addr) == 2
ssl_config = get_ssl_config_from_tmp()
pool = get_conn_pool(host_addr[0], host_addr[1], ssl_config)
yield pool
pool.close()
@pytest.fixture(scope="session")
def conn_pool(conn_pool_to_first_graph_service):
return conn_pool_to_first_graph_service
@pytest.fixture(scope="class")
def session_from_first_conn_pool(conn_pool_to_first_graph_service, pytestconfig):
user = pytestconfig.getoption("user")
password = pytestconfig.getoption("password")
sess = conn_pool_to_first_graph_service.get_session(user, password)
yield sess
sess.release()
@pytest.fixture(scope="class")
def session_from_second_conn_pool(conn_pool_to_second_graph_service, pytestconfig):
user = pytestconfig.getoption("user")
password = pytestconfig.getoption("password")
sess = conn_pool_to_second_graph_service.get_session(user, password)
yield sess
sess.release()
@pytest.fixture(scope="class")
def session(session_from_first_conn_pool, class_fixture_variables):
if class_fixture_variables.get('current_session', None) is not None:
return class_fixture_variables.get('current_session')
return session_from_first_conn_pool
def load_csv_data_once(space: str):
with open(SPACE_TMP_PATH, "r") as f:
for sp in json.loads(f.readline()):
if sp.get("name", None) == space:
return SpaceDesc.from_json(sp)
raise ValueError(f"Invalid space name: {space}")
@pytest.fixture(scope="session")
def load_nba_data():
yield load_csv_data_once("nba")
@pytest.fixture(scope="session")
def load_nba_int_vid_data():
yield load_csv_data_once("nba_int_vid")
@pytest.fixture(scope="session")
def load_ldbc_v0_3_3():
yield load_csv_data_once("ldbc_v0_3_3")
@pytest.fixture(scope="session")
def load_student_data():
yield load_csv_data_once("student")
@pytest.fixture(scope="session")
def load_ngdata_data():
yield load_csv_data_once("ngdata")
# TODO(yee): Delete this when we migrate all test cases
@pytest.fixture(scope="class")
def workaround_for_class(
request, pytestconfig, conn_pool, session, load_nba_data, load_student_data
):
if request.cls is None:
return
addr = pytestconfig.getoption("address")
if addr:
ss = addr.split(':')
request.cls.host = ss[0]
request.cls.port = ss[1]
else:
request.cls.host = "localhost"
request.cls.port = get_port()
request.cls.data_dir = os.path.dirname(os.path.abspath(__file__))
request.cls.spaces = []
request.cls.user = pytestconfig.getoption("user")
request.cls.password = pytestconfig.getoption("password")
request.cls.replica_factor = pytestconfig.getoption("replica_factor")
request.cls.partition_num = pytestconfig.getoption("partition_num")
request.cls.check_format_str = 'result: {}, expect: {}'
request.cls.data_loaded = False
request.cls.client_pool = conn_pool
request.cls.client = session
request.cls.set_delay()
request.cls.prepare()
yield
if request.cls.client is not None:
request.cls.cleanup()
request.cls.drop_data()
@pytest.fixture(scope="class")
def establish_a_rare_connection(conn_pool, pytestconfig):
conn = conn_pool.get_connection()
return conn._connection