diff --git a/tests/common/utils.py b/tests/common/utils.py index 69e284e8f8a..44d24123339 100644 --- a/tests/common/utils.py +++ b/tests/common/utils.py @@ -5,6 +5,7 @@ # This source code is licensed under Apache 2.0 License. import os +import re import random import string import time @@ -441,3 +442,11 @@ def get_conn_pool(host: str, port: int): if not pool.init([(host, port)], config): raise Exception("Fail to init connection pool.") return pool + +def parse_service_index(name: str): + name = name.lower() + pattern = r"(graphd|storaged|metad)\[(\d+)\]" + m = re.match(pattern, name) + if m and len(m.groups()) == 2: + return int(m.groups()[1]) + return None diff --git a/tests/tck/conftest.py b/tests/tck/conftest.py index 7aa95a0a9b0..b1e241526f6 100644 --- a/tests/tck/conftest.py +++ b/tests/tck/conftest.py @@ -28,6 +28,7 @@ check_resp, response, resp_ok, + parse_service_index, ) from tests.common.nebula_service import NebulaService from tests.tck.utils.table import dataset, table @@ -292,6 +293,21 @@ def given_nebulacluster_with_param( class_fixture_variables["cluster"] = nebula_svc class_fixture_variables["pool"] = pool +@when(parse("login {graph} with {user} and {password}")) +def when_login_graphd(graph, user, password, class_fixture_variables, pytestconfig): + index = parse_service_index(graph) + assert index is not None, "Invalid graph name, name is {}".format(graph) + nebula_svc = class_fixture_variables.get("cluster") + assert nebula_svc is not None, "Cannot get the cluster" + assert index < len(nebula_svc.graphd_processes) + graphd_process = nebula_svc.graphd_processes[index] + graph_ip, graph_port = graphd_process.host, graphd_process.tcp_port + pool = get_conn_pool(graph_ip, graph_port) + sess = pool.get_session(user, password) + # do not release original session, as we may have cases to test multiple sessions. + # connection could be released after cluster stopped. + class_fixture_variables["session"] = sess + class_fixture_variables["pool"] = pool @when(parse("executing query:\n{query}")) def executing_query(query, graph_spaces, session, request): @@ -562,6 +578,19 @@ def result_should_contain(request, result, graph_spaces): ) +@then(parse("the result should contain, replace the holders with cluster info:\n{result}")) +def then_result_should_contain_replace(request, result, graph_spaces, class_fixture_variables): + result = replace_result_with_cluster_info(result, class_fixture_variables) + cmp_dataset( + request, + graph_spaces, + result, + order=False, + strict=True, + contains=CmpType.CONTAINS, + ) + + @then(parse("the result should not contain:\n{result}")) def result_should_not_contain(request, result, graph_spaces): cmp_dataset( @@ -758,3 +787,18 @@ def check_client_compatible(graph_spaces): assert ( resp.error_code == ErrorCode.E_CLIENT_SERVER_INCOMPATIBLE ), f'The client was not rejected by server: {resp}' + + +def replace_result_with_cluster_info(result, class_fixture_variables): + pattern = r"\$\{.*?\}" + holders = set(re.findall(pattern, result)) + cluster = class_fixture_variables.get("cluster") + assert cluster is not None, "Cannot get the cluster" + for holder in holders: + try: + eval_string = holder[2:-1] + value = eval(eval_string) + result = result.replace(holder, str(value)) + except: + raise + return result diff --git a/tests/tck/features/admin/Sessions.feature b/tests/tck/features/admin/Sessions.feature new file mode 100644 index 00000000000..7fff0de5656 --- /dev/null +++ b/tests/tck/features/admin/Sessions.feature @@ -0,0 +1,33 @@ +# Copyright (c) 2021 vesoft inc. All rights reserved. +# +# This source code is licensed under Apache 2.0 License. +Feature: Test sessions + + Background: + Given a nebulacluster with 3 graphd and 1 metad and 1 storaged + + Scenario: Show sessions + When executing query: + """ + SHOW SESSIONS; + """ + Then the result should contain: + | SessionId | UserName | SpaceName | CreateTime | UpdateTime | GraphAddr | Timezone | ClientIp | + | /\d+/ | "root" | "" | /.*/ | /.*/ | /.*/ | 0 | "127.0.0.1" | + When executing query: + """ + CREATE USER user1 WITH PASSWORD 'nebula1'; + CREATE SPACE s1(vid_type=int); + USE s1; + """ + Then the execution should be successful + And wait 1 seconds + When login "graphd[1]" with "user1" and "nebula1" + And executing query: + """ + SHOW SESSIONS; + """ + Then the result should contain, replace the holders with cluster info: + | SessionId | UserName | SpaceName | CreateTime | UpdateTime | GraphAddr | Timezone | ClientIp | + | /\d+/ | "root" | "s1" | /.*/ | /.*/ | "127.0.0.1:${cluster.graphd_processes[0].tcp_port}" | 0 | "127.0.0.1" | + | /\d+/ | "user1" | "" | /.*/ | /.*/ | "127.0.0.1:${cluster.graphd_processes[1].tcp_port}" | 0 | "127.0.0.1" |