Skip to content

Commit

Permalink
add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Ami11111 committed Oct 31, 2024
1 parent 520372f commit cec6027
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 30 deletions.
20 changes: 18 additions & 2 deletions python/infinity_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ def set_role_standalone(self, node_name):
r = self.net.request(url, "post", h, d)
self.net.raise_exception(r)

def set_role_admin(self):
url = f"admin/node/current"
h = self.net.set_up_header(["accept", "content-type"])
d = self.net.set_up_data([], {"role": "admin"})
r = self.net.request(url, "post", h, d)
self.net.raise_exception(r)

def set_role_leader(self, node_name):
url = f"admin/node/current"
h = self.net.set_up_header(["accept", "content-type"])
Expand Down Expand Up @@ -206,6 +213,13 @@ def show_database(self, db_name):
self.net.raise_exception(r)
return database_result(database_name=r.json()["database_name"])

def show_node(self, node_name):
url = f"admin/node/{node_name}"
h = self.net.set_up_header(["accept"])
r = self.net.request(url, "get", h, {})
self.net.raise_exception(r)
print(r.json())
return database_result(node_name=r.json()["node"]["name"], node_role=r.json()["node"]["role"], node_status=r.json()["node"]["status"])

####################3####################3####################3####################3####################3####################3####################3####################3

Expand Down Expand Up @@ -804,13 +818,15 @@ def to_arrow(self):


class database_result():
def __init__(self, list=[], database_name: str="", error_code=ErrorCode.OK, columns=[], index_list=[]):
def __init__(self, list=[], database_name: str="", error_code=ErrorCode.OK, columns=[], index_list=[], node_name="", node_role="", node_status=""):
self.db_names = list
self.database_name = database_name # get database
self.error_code = error_code
self.columns = columns
self.index_list = index_list

self.node_name = node_name
self.node_role = node_role
self.node_status = node_status

identifier_limit = 65536

Expand Down
12 changes: 12 additions & 0 deletions python/test_cluster/infinity_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ def init_as_standalone(self, config_path: str | None = None):
self.add_client(f"http://{http_ip}:{http_port}/")
self.__init_cmd(lambda: self.client.set_role_standalone(self.node_name))

def init_as_admin(self, config_path: str | None = None):
#self.init(config_path)
http_ip, http_port = self.http_uri()
self.add_client(f"http://{http_ip}:{http_port}/")
self.__init_cmd(lambda: self.client.set_role_admin())

def init_as_leader(self, config_path: str | None = None):
self.init(config_path)
http_ip, http_port = self.http_uri()
Expand Down Expand Up @@ -199,6 +205,12 @@ def init_standalone(self, node_name: str):
runner = self.runners[node_name]
runner.init_as_standalone()

def init_admin(self, node_name: str):
if node_name not in self.runners:
raise ValueError(f"Node {node_name} not found in the runners.")
runner = self.runners[node_name]
runner.init_as_admin()

def init_leader(self, leader_name: str):
if self.leader_runner is not None:
raise ValueError(
Expand Down
168 changes: 140 additions & 28 deletions python/test_cluster/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
from infinity_cluster import InfinityCluster
from mocked_infinity_cluster import MockInfinityCluster
from docker_infinity_cluster import DockerInfinityCluster, MinioParams

from numpy import dtype
import pandas as pd
import time
from infinity.errors import ErrorCode
from infinity.common import InfinityException
from infinity.common import ConflictType

@pytest.mark.usefixtures("skip_if_docker")
def test_standalone(cluster: InfinityCluster):
Expand Down Expand Up @@ -86,30 +91,137 @@ def test_docker(docker_cluster: DockerInfinityCluster):
print("remove nodes")

cluster.clear()
'''
tc1:
n1: admin
n2: admin
n1->leader
show node->n1 leader;
n2->follower
show node on n1 & n2->n1 leader, n2 follower;
create table t1 on n1;
insert data into t1 on n1;
select data t1 on n2;
try to insert data into t1 on n2; # fail
n1->admin
show node on n2->n1 timeout, n2 follower;
create table t2 on n2; # fail
n1->leader
show node on n1 & n2->n1 leader, n2 follower;
insert data into t1 on n1;
select data t1 on n1 and n2;
n2->admin
show node on n1;
insert data into t1 on n2;
n2->follower
show node on n1 & n2;
select data t1 on n1 and n2;
'''

@pytest.mark.usefixtures("skip_if_docker")
def test_tc1(cluster: InfinityCluster):
'''
tc1:
n1: admin
n2: admin
n1->leader
show node->n1 leader;
n2->follower
show node on n1 & n2->n1 leader, n2 follower;
create table t1 on n1;
insert data into t1 on n1;
select data t1 on n2;
try to insert data into t1 on n2; # fail
n1->admin
show node on n2->n1 timeout, n2 follower;
create table t2 on n2; # fail
n1->leader
show node on n1 & n2->n1 leader, n2 follower;
insert data into t1 on n1;
select data t1 on n1 and n2;
n2->admin
show node on n1;
insert data into t1 on n2;
n2->follower
show node on n1 & n2;
select data t1 on n1 and n2;
'''
cluster.add_node("node1", "conf/leader.toml")
cluster.add_node("node2", "conf/follower.toml")

cluster.init_leader("node1")
infinity1 = cluster.client("node1")
res = infinity1.show_node("node1")
assert(res.node_name == "node1")
assert(res.node_role == "leader")
assert(res.node_status == "alive")

cluster.init_follower("node2")
infinity2 = cluster.client("node2")
res = infinity1.show_node("node1")
assert(res.node_name == "node1")
assert(res.node_role == "leader")
assert(res.node_status == "alive")
res = infinity1.show_node("node2")
assert(res.node_name == "node2")
assert(res.node_role == "follower")
assert(res.node_status == "alive")

table_name = "table1"
db1 = infinity1.get_database("default_db")
res = db1.drop_table(table_name, ConflictType.Ignore)
table1 = db1.create_table(
table_name, {"c1": {"type": "int"}, "c2": {"type": "vector,4,float"}}
)
table1.insert([{"c1": 1, "c2": [1.0, 2.0, 3.0, 4.0]}])

res_gt = pd.DataFrame(
{
"c1": (1),
"c2": ([[1.0, 2.0, 3.0, 4.0]]),
}
).astype({"c1": dtype("int32"), "c2": dtype("object")})

res = table1.output(["*"]).to_df()
pd.testing.assert_frame_equal(res, res_gt)

time.sleep(1)
print("select in node2")

infinity2 = cluster.client("node2")
db2 = infinity2.get_database("default_db")
table2 = db2.get_table(table_name)
res = table2.output(["*"]).to_df()
pd.testing.assert_frame_equal(res, res_gt)

try:
table2.insert([{"c1": 1, "c2": [1.0, 2.0, 3.0, 4.0]}])
except InfinityException as e:
print(e)


infinity1.set_role_admin()
time.sleep(1)
res = infinity2.show_node("node1")
assert(res.node_status == "timeout")
res = infinity2.show_node("node2")
assert(res.node_name == "node2")
assert(res.node_role == "follower")
assert(res.node_status == "alive")

table_name = "table2"
db2 = infinity2.get_database("default_db")
try:
table2 = db1.create_table(
table_name, {"c1": {"type": "int"}, "c2": {"type": "vector,4,float"}}
)
except InfinityException as e:
print(e)

infinity1.set_role_leader("node1")
time.sleep(1)
res = infinity1.show_node("node1")
assert(res.node_name == "node1")
assert(res.node_role == "leader")
assert(res.node_status == "alive")
res = infinity1.show_node("node2")
assert(res.node_name == "node2")
assert(res.node_role == "follower")
assert(res.node_status == "alive")

table_name = "table1"
db1 = infinity1.get_database("default_db")
table1 = db1.get_table(table_name)
res = table1.output(["*"]).to_df()
print(res)
#table1.insert([{"c1": 2, "c2": [1.0, 2.0, 3.0, 4.0]}])
#res_gt = pd.DataFrame(
# {
# "c1": (1, 2),
# "c2": ([[1.0, 2.0, 3.0, 4.0]], [[1.0, 2.0, 3.0, 4.0]]),
# }
#).astype({"c1": dtype("int32"), "c2": dtype("object")})

#res = table1.output(["*"]).to_df()
#pd.testing.assert_frame_equal(res, res_gt)
#res = db1.drop_table(table_name)
#assert res.error_code == ErrorCode.OK

time.sleep(1)
cluster.remove_node("node2")
cluster.remove_node("node1")
cluster.clear()
2 changes: 2 additions & 0 deletions python/test_cluster/test_insert.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pytest
import time
from infinity.errors import ErrorCode
from infinity.common import ConflictType


class TestInsert:
Expand All @@ -26,6 +27,7 @@ def __test_inner_1(self, cluster: InfinityCluster):

table_name = "table1"
db1 = infinity1.get_database("default_db")
res = db1.drop_table(table_name, ConflictType.Ignore)
table1 = db1.create_table(
table_name, {"c1": {"type": "int"}, "c2": {"type": "vector,4,float"}}
)
Expand Down
1 change: 1 addition & 0 deletions tools/run_cluster_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
python_executable,
"-m",
"pytest",
#"-v",
f"{python_test_dir}/test_cluster",
f"--infinity_path={infinity_path}",
"-x",
Expand Down

0 comments on commit cec6027

Please sign in to comment.