From e6ff1d75eb6fd731ad3ebf3ddcca3fe9c5c1e9f0 Mon Sep 17 00:00:00 2001 From: Chaoguang Lin Date: Fri, 4 Jun 2021 00:37:25 +0000 Subject: [PATCH 1/5] Add interactive tests for fdbcli commands using a python script --- bindings/python/CMakeLists.txt | 9 +++ bindings/python/tests/fdbcli_tests.py | 99 +++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100755 bindings/python/tests/fdbcli_tests.py diff --git a/bindings/python/CMakeLists.txt b/bindings/python/CMakeLists.txt index 2596d0fd8c..bd3aa67720 100644 --- a/bindings/python/CMakeLists.txt +++ b/bindings/python/CMakeLists.txt @@ -74,3 +74,12 @@ add_custom_command(OUTPUT ${package_file} add_custom_target(python_package DEPENDS ${package_file}) add_dependencies(python_package python_binding) add_dependencies(packages python_package) + +if (NOT WIN32) + add_fdbclient_test( + NAME fdbcli_tests + COMMAND ${CMAKE_SOURCE_DIR}/bindings/python/tests/fdbcli_tests.py + @CLUSTER_FILE@ + ${CMAKE_BINARY_DIR}/bin/fdbcli + ) +endif() diff --git a/bindings/python/tests/fdbcli_tests.py b/bindings/python/tests/fdbcli_tests.py new file mode 100755 index 0000000000..3813fc40b9 --- /dev/null +++ b/bindings/python/tests/fdbcli_tests.py @@ -0,0 +1,99 @@ +#!/usr/bin/env python3 + +import os +import sys +import subprocess +import logging +import fdb +import functools + + +def enable_logging(level=logging.ERROR): + """Enable logging in the function with the specified logging level + + Args: + level (logging., optional): logging level for the decorated function. Defaults to logging.ERROR. + """ + def func_decorator(func): + @functools.wraps(func) + def wrapper(*args,**kwargs): + # initialize logger + logger = logging.getLogger(func.__name__) + logger.setLevel(level) + # set logging format + handler = logging.StreamHandler() + handler_format = logging.Formatter('[%(asctime)s] - %(filename)s:%(lineno)d - %(levelname)s - %(name)s - %(message)s') + handler.setFormatter(handler_format) + handler.setLevel(level) + logger.addHandler(handler) + # pass the logger to the decorated function + result = func(logger, *args,**kwargs) + return result + return wrapper + return func_decorator + +def run_fdbcli_command(*args): + """run through commanline: fdbcli --exec ' ... '. + + Returns: + string: Console output from fdbcli + """ + commands = command_template + ["{}".format(' '.join(args))] + return subprocess.run(commands, stdout=subprocess.PIPE).stdout.decode('utf-8').strip() + +@enable_logging() +def advanceversion(logger): + # get current read version + version1 = int(run_fdbcli_command('getversion')) + logger.debug("Read version: {}".format(version1)) + # advance version to a much larger value to the present version + version2 = version1 * 10000 + logger.debug("Advanced to version: " + str(version2)) + run_fdbcli_command('advanceversion', str(version2)) + # after running the advanceversion command + # check the read version is advanced to the specified value + version3 = int(run_fdbcli_command('getversion')) + logger.debug("Read version: {}".format(version3)) + assert version3 >= version2 + # advance version to a smaller value compared to the current version + # this should be a no-op + run_fdbcli_command('advanceversion', str(version1)) + # get the current version to make sure the version did not decrease + version4 = int(run_fdbcli_command('getversion')) + logger.debug("Read version: {}".format(version4)) + assert version4 >= version3 + +@enable_logging(logging.DEBUG) +def maintenance(logger): + # fdbcli output when there's no ongoing maintenance + no_maintenance_output = 'No ongoing maintenance.' + # no ongoing maintenance + output1 = run_fdbcli_command('maintenance') + assert output1 == no_maintenance_output + # set maintenance on a fake zone id for 10 seconds + run_fdbcli_command('maintenance', 'on', 'fake_zone', '10') + # show current maintenance status + output2 = run_fdbcli_command('maintenance') + logger.debug("Maintenance status: " + output2) + items = output2.split(' ') + assert 'fake_zone' in items + logger.debug("Remaining time(seconds): " + items[-2]) + assert 0 < int(items[-2]) < 10 + # turn off maintenance + run_fdbcli_command('maintenance', 'off') + # check maintenance status + output3 = run_fdbcli_command('maintenance') + assert output3 == no_maintenance_output + +if __name__ == '__main__': + # specify fdb version + fdb.api_version(710) + # fdbcli_tests.py + assert len(sys.argv) == 3, "Please pass arguments: " + # open the existing database + db = fdb.open(sys.argv[1]) + command_template = [sys.argv[2], '--exec'] + # tests for fdbcli commands + # assertions will fail if fdbcli does not work as expected + advanceversion() + maintenance() \ No newline at end of file From 6eff38ebf76e741fdb33515b3bcab26c59a1c293 Mon Sep 17 00:00:00 2001 From: Chaoguang Lin Date: Fri, 4 Jun 2021 00:58:23 +0000 Subject: [PATCH 2/5] Remove some unnecessary code --- bindings/python/CMakeLists.txt | 2 +- bindings/python/tests/fdbcli_tests.py | 17 ++++++----------- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/bindings/python/CMakeLists.txt b/bindings/python/CMakeLists.txt index bd3aa67720..91fc9324ef 100644 --- a/bindings/python/CMakeLists.txt +++ b/bindings/python/CMakeLists.txt @@ -79,7 +79,7 @@ if (NOT WIN32) add_fdbclient_test( NAME fdbcli_tests COMMAND ${CMAKE_SOURCE_DIR}/bindings/python/tests/fdbcli_tests.py - @CLUSTER_FILE@ ${CMAKE_BINARY_DIR}/bin/fdbcli + @CLUSTER_FILE@ ) endif() diff --git a/bindings/python/tests/fdbcli_tests.py b/bindings/python/tests/fdbcli_tests.py index 3813fc40b9..3dd6fbe39f 100755 --- a/bindings/python/tests/fdbcli_tests.py +++ b/bindings/python/tests/fdbcli_tests.py @@ -4,10 +4,8 @@ import os import sys import subprocess import logging -import fdb import functools - def enable_logging(level=logging.ERROR): """Enable logging in the function with the specified logging level @@ -63,7 +61,7 @@ def advanceversion(logger): logger.debug("Read version: {}".format(version4)) assert version4 >= version3 -@enable_logging(logging.DEBUG) +@enable_logging() def maintenance(logger): # fdbcli output when there's no ongoing maintenance no_maintenance_output = 'No ongoing maintenance.' @@ -86,14 +84,11 @@ def maintenance(logger): assert output3 == no_maintenance_output if __name__ == '__main__': - # specify fdb version - fdb.api_version(710) - # fdbcli_tests.py - assert len(sys.argv) == 3, "Please pass arguments: " - # open the existing database - db = fdb.open(sys.argv[1]) - command_template = [sys.argv[2], '--exec'] + # fdbcli_tests.py + assert len(sys.argv) == 3, "Please pass arguments: " + # shell command template + command_template = [sys.argv[1], '-C', sys.argv[2], '--exec'] # tests for fdbcli commands # assertions will fail if fdbcli does not work as expected advanceversion() - maintenance() \ No newline at end of file + maintenance() From 4cc90fb0e93c5eb37e4959ad21f9817d873b7170 Mon Sep 17 00:00:00 2001 From: Chaoguang Lin Date: Fri, 4 Jun 2021 01:17:09 +0000 Subject: [PATCH 3/5] Update comments, fix typos --- bindings/python/tests/fdbcli_tests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bindings/python/tests/fdbcli_tests.py b/bindings/python/tests/fdbcli_tests.py index 3dd6fbe39f..2d6f688d11 100755 --- a/bindings/python/tests/fdbcli_tests.py +++ b/bindings/python/tests/fdbcli_tests.py @@ -31,7 +31,7 @@ def enable_logging(level=logging.ERROR): return func_decorator def run_fdbcli_command(*args): - """run through commanline: fdbcli --exec ' ... '. + """run the fdbcli statement: fdbcli --exec ' ... '. Returns: string: Console output from fdbcli From b57ed906c4ebdedaab8874137c02c779c2636660 Mon Sep 17 00:00:00 2001 From: Chaoguang Lin Date: Fri, 4 Jun 2021 01:36:03 +0000 Subject: [PATCH 4/5] Update comments --- bindings/python/tests/fdbcli_tests.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/bindings/python/tests/fdbcli_tests.py b/bindings/python/tests/fdbcli_tests.py index 2d6f688d11..55492885ae 100755 --- a/bindings/python/tests/fdbcli_tests.py +++ b/bindings/python/tests/fdbcli_tests.py @@ -44,11 +44,11 @@ def advanceversion(logger): # get current read version version1 = int(run_fdbcli_command('getversion')) logger.debug("Read version: {}".format(version1)) - # advance version to a much larger value to the present version + # advance version to a much larger value compared to the current version version2 = version1 * 10000 logger.debug("Advanced to version: " + str(version2)) run_fdbcli_command('advanceversion', str(version2)) - # after running the advanceversion command + # after running the advanceversion command, # check the read version is advanced to the specified value version3 = int(run_fdbcli_command('getversion')) logger.debug("Read version: {}".format(version3)) @@ -63,18 +63,18 @@ def advanceversion(logger): @enable_logging() def maintenance(logger): - # fdbcli output when there's no ongoing maintenance + # expected fdbcli output when running 'maintenance' while there's no ongoing maintenance no_maintenance_output = 'No ongoing maintenance.' - # no ongoing maintenance output1 = run_fdbcli_command('maintenance') assert output1 == no_maintenance_output # set maintenance on a fake zone id for 10 seconds - run_fdbcli_command('maintenance', 'on', 'fake_zone', '10') + run_fdbcli_command('maintenance', 'on', 'fake_zone_id', '10') # show current maintenance status output2 = run_fdbcli_command('maintenance') logger.debug("Maintenance status: " + output2) items = output2.split(' ') - assert 'fake_zone' in items + # make sure this specific zone id is under maintenance + assert 'fake_zone_id' in items logger.debug("Remaining time(seconds): " + items[-2]) assert 0 < int(items[-2]) < 10 # turn off maintenance From a5e69c269a765050a4966f009634a4c554dfc3f1 Mon Sep 17 00:00:00 2001 From: Chaoguang Lin Date: Fri, 4 Jun 2021 20:44:49 +0000 Subject: [PATCH 5/5] remove unused header, fix the CMake rule --- bindings/python/CMakeLists.txt | 2 +- bindings/python/tests/fdbcli_tests.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/bindings/python/CMakeLists.txt b/bindings/python/CMakeLists.txt index 91fc9324ef..0893b08aa8 100644 --- a/bindings/python/CMakeLists.txt +++ b/bindings/python/CMakeLists.txt @@ -75,7 +75,7 @@ add_custom_target(python_package DEPENDS ${package_file}) add_dependencies(python_package python_binding) add_dependencies(packages python_package) -if (NOT WIN32) +if (NOT WIN32 AND NOT OPEN_FOR_IDE) add_fdbclient_test( NAME fdbcli_tests COMMAND ${CMAKE_SOURCE_DIR}/bindings/python/tests/fdbcli_tests.py diff --git a/bindings/python/tests/fdbcli_tests.py b/bindings/python/tests/fdbcli_tests.py index 55492885ae..abdaf4b876 100755 --- a/bindings/python/tests/fdbcli_tests.py +++ b/bindings/python/tests/fdbcli_tests.py @@ -1,6 +1,5 @@ #!/usr/bin/env python3 -import os import sys import subprocess import logging