From 142d6939069068a8457555f02be4ea0e4d93e5c3 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Wed, 6 Dec 2023 15:12:59 +0100 Subject: [PATCH] feat: add a raw eth test --- CMakeLists.txt | 6 +- GNUmakefile | 3 +- tests/raweth.py | 142 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 149 insertions(+), 2 deletions(-) create mode 100644 tests/raweth.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 73db9fa37..88312b89c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -107,15 +107,18 @@ set(Z_FEATURE_PUBLICATION 1 CACHE STRING "Toggle publication feature") set(Z_FEATURE_SUBSCRIPTION 1 CACHE STRING "Toggle subscription feature") set(Z_FEATURE_QUERY 1 CACHE STRING "Toggle query feature") set(Z_FEATURE_QUERYABLE 1 CACHE STRING "Toggle queryable feature") +set(Z_FEATURE_RAWETH_TRANSPORT 0 CACHE STRING "Toggle raw ethernet transport feature") add_definition(Z_FEATURE_PUBLICATION=${Z_FEATURE_PUBLICATION}) add_definition(Z_FEATURE_SUBSCRIPTION=${Z_FEATURE_SUBSCRIPTION}) add_definition(Z_FEATURE_QUERY=${Z_FEATURE_QUERY}) add_definition(Z_FEATURE_QUERYABLE=${Z_FEATURE_QUERYABLE}) +add_definition(Z_FEATURE_RAWETH_TRANSPORT=${Z_FEATURE_RAWETH_TRANSPORT}) message(STATUS "Building with feature confing:\n\ * PUBLICATION: ${Z_FEATURE_PUBLICATION}\n\ * SUBSCRIPTION: ${Z_FEATURE_SUBSCRIPTION}\n\ * QUERY: ${Z_FEATURE_QUERY}\n\ -* QUERYABLE: ${Z_FEATURE_QUERYABLE}") +* QUERYABLE: ${Z_FEATURE_QUERYABLE}\n\ +* RAWETH: ${Z_FEATURE_RAWETH_TRANSPORT}") # Print summary of CMAKE configurations message(STATUS "Building in ${CMAKE_BUILD_TYPE} mode") @@ -300,6 +303,7 @@ if(UNIX OR MSVC) target_link_libraries(z_api_double_drop_test ${Libname}) configure_file(${PROJECT_SOURCE_DIR}/tests/modularity.py ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/modularity.py COPYONLY) + configure_file(${PROJECT_SOURCE_DIR}/tests/raweth.py ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/raweth.py COPYONLY) enable_testing() add_test(z_data_struct_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_data_struct_test) diff --git a/GNUmakefile b/GNUmakefile index 67932a3bc..f6ad15055 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -51,6 +51,7 @@ Z_FEATURE_PUBLICATION?=1 Z_FEATURE_SUBSCRIPTION?=1 Z_FEATURE_QUERY?=1 Z_FEATURE_QUERYABLE?=1 +Z_FEATURE_RAWETH_TRANSPORT?=0 # zenoh-pico/ directory ROOT_DIR:=$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST)))) @@ -67,7 +68,7 @@ CROSSIMG_PREFIX=zenoh-pico_ CMAKE_OPT=-DZENOH_DEBUG=$(ZENOH_DEBUG) -DBUILD_EXAMPLES=$(BUILD_EXAMPLES) -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) -DBUILD_TESTING=$(BUILD_TESTING) -DBUILD_MULTICAST=$(BUILD_MULTICAST)\ -DZ_FEATURE_PUBLICATION=$(Z_FEATURE_PUBLICATION) -DZ_FEATURE_SUBSCRIPTION=$(Z_FEATURE_SUBSCRIPTION) -DZ_FEATURE_QUERY=$(Z_FEATURE_QUERY) -DZ_FEATURE_QUERYABLE=$(Z_FEATURE_QUERYABLE)\ - -DBUILD_INTEGRATION=$(BUILD_INTEGRATION) -DBUILD_TOOLS=$(BUILD_TOOLS) -DBUILD_SHARED_LIBS=$(BUILD_SHARED_LIBS) -H. + -DZ_FEATURE_RAWETH_TRANSPORT=$(Z_FEATURE_RAWETH_TRANSPORT) -DBUILD_INTEGRATION=$(BUILD_INTEGRATION) -DBUILD_TOOLS=$(BUILD_TOOLS) -DBUILD_SHARED_LIBS=$(BUILD_SHARED_LIBS) -H. all: make diff --git a/tests/raweth.py b/tests/raweth.py new file mode 100644 index 000000000..8f14e3b78 --- /dev/null +++ b/tests/raweth.py @@ -0,0 +1,142 @@ +import argparse +import subprocess +import sys +import time + +# Specify the directory for the binaries +DIR_EXAMPLES = "build/examples" + +def pub_and_sub(args): + print("*** Pub & sub test ***") + test_status = 0 + + # Expected z_pub output & status + if args.reth == 1: + z_pub_expected_status = 0 + z_pub_expected_output = '''Opening session... +Declaring publisher for 'demo/example/zenoh-pico-pub'... +Putting Data ('demo/example/zenoh-pico-pub': 'Pub from Pico!')... +Putting Data ('demo/example/zenoh-pico-pub': 'Pub from Pico!')... +Putting Data ('demo/example/zenoh-pico-pub': 'Pub from Pico!')... +Putting Data ('demo/example/zenoh-pico-pub': 'Pub from Pico!')... +Putting Data ('demo/example/zenoh-pico-pub': 'Pub from Pico!')... +Putting Data ('demo/example/zenoh-pico-pub': 'Pub from Pico!')... +Putting Data ('demo/example/zenoh-pico-pub': 'Pub from Pico!')... +Putting Data ('demo/example/zenoh-pico-pub': 'Pub from Pico!')... +Putting Data ('demo/example/zenoh-pico-pub': 'Pub from Pico!')... +Putting Data ('demo/example/zenoh-pico-pub': 'Pub from Pico!')...''' + else : + z_pub_expected_status = 255 + z_pub_expected_output = '''Opening session... +Unable to open session!''' + + # Expected z_sub output & status + if args.reth == 1: + z_sub_expected_status = 0 + z_sub_expected_output = '''Opening session... +Declaring Subscriber on 'demo/example/**'... +Enter 'q' to quit... +>> [Subscriber] Received ('demo/example/zenoh-pico-pub': 'Pub from Pico!') +>> [Subscriber] Received ('demo/example/zenoh-pico-pub': 'Pub from Pico!') +>> [Subscriber] Received ('demo/example/zenoh-pico-pub': 'Pub from Pico!') +>> [Subscriber] Received ('demo/example/zenoh-pico-pub': 'Pub from Pico!') +>> [Subscriber] Received ('demo/example/zenoh-pico-pub': 'Pub from Pico!') +>> [Subscriber] Received ('demo/example/zenoh-pico-pub': 'Pub from Pico!') +>> [Subscriber] Received ('demo/example/zenoh-pico-pub': 'Pub from Pico!') +>> [Subscriber] Received ('demo/example/zenoh-pico-pub': 'Pub from Pico!') +>> [Subscriber] Received ('demo/example/zenoh-pico-pub': 'Pub from Pico!') +>> [Subscriber] Received ('demo/example/zenoh-pico-pub': 'Pub from Pico!')''' + else : + z_sub_expected_status = 255 + z_sub_expected_output = '''Opening session... +Unable to open session!''' + + print("Start subscriber") + # Start z_sub in the background + z_sub_command = f"sudo ./{DIR_EXAMPLES}/z_sub -m \"peer\" -l \"reth/0\"s" + z_sub_process = subprocess.Popen(z_sub_command, + shell=True, + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, text=True) + + # Introduce a delay to ensure z_sub starts + time.sleep(2) + + print("Start publisher") + # Start z_pub + z_pub_command = f"sudo ./{DIR_EXAMPLES}/z_pub -m \"peer\" -l \"reth/0\"s" + z_pub_process = subprocess.Popen(z_pub_command, + shell=True, + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True) + + # Wait for z_pub to finish + z_pub_process.wait() + + print("Stop subscriber") + if z_sub_process.poll() is None: + # Send "q" command to z_sub to stop it + z_sub_process.stdin.write("q\n") + z_sub_process.stdin.flush() + + # Wait for z_sub to finish + z_sub_process.wait() + + print("Check publisher status & output") + # Check the exit status of z_pub + z_pub_status = z_pub_process.returncode + if z_pub_status == z_pub_expected_status: + print("z_pub status valid") + else: + print(f"z_pub status invalid, expected: {z_pub_expected_status}, received: {z_pub_status}") + test_status = 1 + + # Check output of z_pub + z_pub_output = z_pub_process.stdout.read() + if z_pub_expected_output in z_pub_output: + print("z_pub output valid") + else: + print("z_pub output invalid:") + print(f"Expected: \"{z_pub_expected_output}\"") + print(f"Received: \"{z_pub_output}\"") + test_status = 1 + + print("Check subscriber status & output") + # Check the exit status of z_sub + z_sub_status = z_sub_process.returncode + if z_sub_status == z_sub_expected_status: + print("z_sub status valid") + else: + print(f"z_sub status invalid, expected: {z_sub_expected_status}, received: {z_sub_status}") + test_status = 1 + + # Check output of z_sub + z_sub_output = z_sub_process.stdout.read() + if z_sub_expected_output in z_sub_output: + print("z_sub output valid") + else: + print("z_sub output invalid:") + print(f"Expected: \"{z_sub_expected_output}\"") + print(f"Received: \"{z_sub_output}\"") + test_status = 1 + # Return value + return test_status + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description="This script runs zenoh-pico examples" + " and checks them according to the given configuration") + parser.add_argument("--reth", type=int, choices=[0, 1], + help="Z_FEATURE_RAWETH_TRANSPORT (0 or 1)") + + EXIT_STATUS = 0 + prog_args = parser.parse_args() + print(f"Args value, reth:{prog_args.reth}") + + # Test pub and sub examples + if pub_and_sub(prog_args) == 1: + EXIT_STATUS = 1 + # Exit + sys.exit(EXIT_STATUS)