Skip to content

Commit

Permalink
add aof py test case
Browse files Browse the repository at this point in the history
  • Loading branch information
bug-superman committed Oct 29, 2023
1 parent 0b8cecb commit 2e840c6
Showing 1 changed file with 150 additions and 27 deletions.
177 changes: 150 additions & 27 deletions tests/cases/aof.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@

import helpers as h
import os
#format aof command
def format_command(*args):
cmd = f"*{len(args)}\r\n"
for a in args:
cmd += f"${len(a)}\r\n{a}\r\n"
return cmd


def append_to_file(write_file, strings):
with open(write_file, "w+") as fp:
for string in strings:
fp.write(string)

def create_aof_dir(dir_path):
os.makedirs(dir_path, exist_ok=True)

def get_aof_file_relative_path():
if h.REDIS_SERVER_VERSION == 7.0:
Expand All @@ -11,7 +26,6 @@ def get_aof_file_relative_path():
return aof_file

def test(src, dst):

cross_slots_cmd = not (src.is_cluster() or dst.is_cluster())
inserter = h.DataInserter()
inserter.add_data(src, cross_slots_cmd=cross_slots_cmd)
Expand All @@ -24,6 +38,36 @@ def test(src, dst):
inserter.check_data(dst, cross_slots_cmd=cross_slots_cmd)
p.ASSERT_EQ(src.dbsize(), dst.dbsize())

def test_base_file(dst):
#creat manifest file
current_directory = p.get_case_context().dir + "_own"
create_aof_dir(current_directory + "/appendonlydir")
manifest_filepath = current_directory + "/appendonlydir/appendonly.aof.manifest"
commands = []
commands += "file appendonly.aof.1.base.aof seq 1 type b\n"
append_to_file(manifest_filepath, commands)

#creat aof file
base_file_path = current_directory + "/appendonlydir/appendonly.aof.1.base.aof"
commands = []
commands += format_command("set", "k1", "v1")
commands += format_command("set", "k2", "v2")
append_to_file(base_file_path, commands)

#start shake redis
opts = h.ShakeOpts.create_aof_opts(f"{current_directory}{get_aof_file_relative_path()}", dst)
p.log(f"opts: {opts}")
h.Shake.run_once(opts)

#check data
pip = dst.pipeline()
pip.get("k1")
pip.get("k2")
ret = pip.execute()
p.ASSERT_EQ(ret, [b"v1", b"v2"])
p.ASSERT_EQ(dst.dbsize(), 2)


def test_error(src, dst):
#set aof
ret = src.do("CONFIG SET", "appendonly", "yes")
Expand Down Expand Up @@ -62,22 +106,6 @@ def test_rm_file(src, dst):
#cant restore
p.ASSERT_EQ(dst.dbsize(), 0)

def test_timestamp(dst):
current_directory = os.getcwd()
opts = h.ShakeOpts.create_aof_opts(f"{current_directory}{get_aof_file_relative_path()}", dst, 1697476302)
h.Shake.run_once(opts)
#checkout data
pip = dst.pipeline()
prefix = "string"
i = 0
pip.get(f"{prefix}_{i}_str")
pip.get(f"{prefix}_{i}_int")
pip.get(f"{prefix}_{i}_int0")
pip.get(f"{prefix}_{i}_int1")
ret = pip.execute()
p.ASSERT_EQ(ret, [b"string", b"0", b"-1", b"123456789"])
p.ASSERT_EQ(dst.dbsize(), 4)

def test_history_file(src, dst):

cross_slots_cmd = not (src.is_cluster() or dst.is_cluster())
Expand All @@ -92,6 +120,87 @@ def test_history_file(src, dst):
inserter.check_data(dst, cross_slots_cmd=cross_slots_cmd)
p.ASSERT_EQ(src.dbsize(), dst.dbsize())


def test_base_file_timestamp(dst): # base file play back all
#creat manifest file
current_directory = p.get_case_context().dir + "_own"
create_aof_dir(current_directory)
manifest_filepath = current_directory + "/appendonlydir/appendonly.aof.manifest"
commands = []
commands += "file appendonly.aof.1.base.aof seq 1 type b\n"
append_to_file(manifest_filepath, commands)

#creat aof file
base_file_path = current_directory + "/appendonlydir/appendonly.aof.1.base.aof"
commands = []
commands += "#TS1233\r\n"
commands += format_command("set", "k1", "v1")
commands += "#TS1234\r\n"
commands += format_command("set", "k2", "v2")
commands += "#TS1235\r\n"
commands += format_command("set", "k3", "v3")
append_to_file(base_file_path, commands)

#start shake redis
opts = h.ShakeOpts.create_aof_opts(f"{current_directory}{get_aof_file_relative_path()}", dst, 1234)
p.log(f"opts: {opts}")
h.Shake.run_once(opts)

#check data
pip = dst.pipeline()
pip.get("k1")
pip.get("k2")
pip.get("k3")
ret = pip.execute()
p.ASSERT_EQ(ret, [b"v1",b"v2",b"v3",])
p.ASSERT_EQ(dst.dbsize(), 3)

def test_base_and_incr_timestamp(dst):

#creat manifest file
current_directory = p.get_case_context().dir + "_own"
create_aof_dir(current_directory + + "/appendonlydir")
manifest_filepath = current_directory + "/appendonlydir/appendonly.aof.manifest"
commands = []
commands += "file appendonly.aof.1.base.aof seq 1 type b\n"
commands += "file appendonly.aof.1.incr.aof seq 1 type i\n"
commands += "file appendonly.aof.2.incr.aof seq 2 type i\n"
append_to_file(manifest_filepath, commands)

#creat aof base file
base_file_path = current_directory + "/appendonlydir/appendonly.aof.1.base.aof"
commands = []
commands += format_command("set", "k1", "v1")
append_to_file(base_file_path, commands)

commands = []
#create aof incr file
incr1_file_path = current_directory + "/appendonlydir/appendonly.aof.1.incr.aof"
commands += "#TS1233\r\n"
commands += format_command("set", "k2", "v2")
append_to_file(incr1_file_path , commands)

commands = []
incr2_file_path = current_directory + "/appendonlydir/appendonly.aof.2.incr.aof"
commands += "#TS1235\r\n"
commands += format_command("set", "k3", "v3")
append_to_file(incr2_file_path , commands)
#start shake redis
opts = h.ShakeOpts.create_aof_opts(f"{current_directory}{get_aof_file_relative_path()}", dst, 1234)
p.log(f"opts: {opts}")
h.Shake.run_once(opts)

#check data
pip = dst.pipeline()
pip.get("k1")
pip.get("k2")
ret = pip.execute()
p.ASSERT_EQ(ret, [b"v1",b"v2"])
p.ASSERT_EQ(dst.dbsize(), 2)




@p.subcase()
def aof_to_standalone():
if h.REDIS_SERVER_VERSION < 7.0:
Expand All @@ -105,7 +214,13 @@ def aof_to_standalone():
p.log(f"aof_ret: {ret}")
dst = h.Redis()
test(src, dst)

@p.subcase()
def aof_to_standalone_base_file():
if h.REDIS_SERVER_VERSION < 7.0:
return
dst = h.Redis()
test_base_file(dst)


@p.subcase()
def aof_to_standalone_rm_file():
Expand Down Expand Up @@ -157,9 +272,14 @@ def aof_to_standalone_timestamp():
if h.REDIS_SERVER_VERSION < 7.0:
return
dst = h.Redis()
test_timestamp(dst)

def aof_to_standalone_hsitory_file():
ret = dst.do("FLUSHALL")
test_base_file_timestamp(dst)
ret = dst.do("FLUSHALL")
test_base_and_incr_timestamp(dst)
ret = dst.do("FLUSHALL")

def aof_to_standalone_history_file():
if h.REDIS_SERVER_VERSION < 7.0:
return
src = h.Redis()
Expand All @@ -179,12 +299,15 @@ def aof_to_standalone_hsitory_file():

@p.case(tags=["sync"])
def main():
aof_to_standalone()
aof_to_standalone_single()
aof_to_standalone_error()
aof_to_standalone_rm_file()
aof_to_cluster()
aof_to_standalone_timestamp()
aof_to_standalone_hsitory_file()
aof_to_standalone() # base + incr aof-multi
aof_to_standalone_base_file() # base file aof-multi
aof_to_standalone_single() #single aof
aof_to_standalone_error() # error aof file
aof_to_standalone_rm_file() # rm aof file
aof_to_standalone_history_file() # history + incr aof-multi
aof_to_cluster() #test cluster
aof_to_standalone_timestamp() #set timestamp aof-multi


if __name__ == '__main__':
main()

0 comments on commit 2e840c6

Please sign in to comment.