From af9690cf0801222238334e153715e89563f6d882 Mon Sep 17 00:00:00 2001
From: Daniel Agar <daniel@agar.ca>
Date: Wed, 27 Sep 2017 18:25:48 -0400
Subject: [PATCH] add simple posix tests to SITL

---
 Makefile                                      | 16 ++++----
 cmake/configs/posix_sitl_default.cmake        |  6 +++
 posix-configs/SITL/init/test/cmd_template.in  | 37 +++++++++++++++++++
 .../{test_template.in => tests_template.in}   |  2 -
 src/firmware/posix/sitl_tests.cmake           | 31 +++++++++++++++-
 .../posix/tests/hello/hello_start_posix.cpp   |  6 ++-
 .../posix/tests/hrt_test/CMakeLists.txt       |  2 +-
 .../posix/tests/hrt_test/hrt_test.cpp         |  4 +-
 .../tests/hrt_test/hrt_test_start_posix.cpp   |  8 ++--
 .../posix/tests/muorb/muorb_test_example.cpp  |  6 +--
 .../posix/tests/vcdev_test/CMakeLists.txt     |  2 +-
 .../tests/vcdev_test/vcdevtest_example.cpp    | 10 ++---
 .../vcdev_test/vcdevtest_start_posix.cpp      |  6 +--
 .../posix/tests/wqueue/wqueue_start_posix.cpp |  2 +-
 src/systemcmds/tests/tests_main.c             |  3 +-
 15 files changed, 105 insertions(+), 36 deletions(-)
 create mode 100644 posix-configs/SITL/init/test/cmd_template.in
 rename posix-configs/SITL/init/test/{test_template.in => tests_template.in} (98%)

diff --git a/Makefile b/Makefile
index 72210b9984..27c03dba06 100644
--- a/Makefile
+++ b/Makefile
@@ -182,7 +182,9 @@ excelsior_legacy_default: posix_excelsior_legacy qurt_excelsior_legacy
 # Other targets
 # --------------------------------------------------------------------
 
-.PHONY: qgc_firmware px4fmu_firmware misc_qgc_extra_firmware alt_firmware checks_bootloaders sizes check quick_check
+.PHONY: qgc_firmware px4fmu_firmware misc_qgc_extra_firmware alt_firmware
+.PHONY: sizes check quick_check check_format  tests
+.PHONY: check_posix_sitl_default check_px4fmu-v3_default
 
 # QGroundControl flashable NuttX firmware
 qgc_firmware: px4fmu_firmware misc_qgc_extra_firmware sizes
@@ -241,7 +243,7 @@ check_%:
 
 # Documentation
 # --------------------------------------------------------------------
-.PHONY: parameters_metadata airframe_metadata px4_metadata module_documentation
+.PHONY: parameters_metadata airframe_metadata module_documentation px4_metadata
 
 parameters_metadata: posix_sitl_default
 	@python $(SRC_DIR)/Tools/px_process_params.py -s $(SRC_DIR)/src --markdown
@@ -308,13 +310,11 @@ format:
 
 # Testing
 # --------------------------------------------------------------------
-.PHONY: run_tests_posix tests tests_coverage
+.PHONY: tests tests_coverage coveralls_upload codecov_upload
 
-run_tests_posix:
+tests:
 	$(MAKE) --no-print-directory posix_sitl_default test_results
 
-tests: run_tests_posix
-
 tests_coverage:
 	@$(MAKE) --no-print-directory posix_sitl_default test_coverage_genhtml PX4_CMAKE_BUILD_TYPE=Coverage
 
@@ -331,7 +331,7 @@ codecov_upload:
 
 # static analyzers (scan-build, clang-tidy, cppcheck)
 # --------------------------------------------------------------------
-.PHONY: posix_sitl_default-clang scan-build clang-tidy clang-tidy-fix clang-tidy-quiet cppcheck
+.PHONY: posix_sitl_default-clang scan-build clang-tidy clang-tidy-fix clang-tidy-quiet cppcheck check_stack
 
 posix_sitl_default-clang:
 	@mkdir -p $(SRC_DIR)/build_posix_sitl_default-clang
@@ -379,7 +379,7 @@ check_stack: px4fmu-v3_default
 
 # Cleanup
 # --------------------------------------------------------------------
-.PHONY: clean submodulesclean distclean
+.PHONY: clean submodulesclean submodulesupdate gazeboclean distclean
 
 clean:
 	@rm -rf $(SRC_DIR)/build_*/
diff --git a/cmake/configs/posix_sitl_default.cmake b/cmake/configs/posix_sitl_default.cmake
index 1075fd6797..477e55f6a0 100644
--- a/cmake/configs/posix_sitl_default.cmake
+++ b/cmake/configs/posix_sitl_default.cmake
@@ -60,6 +60,12 @@ set(config_module_list
 	modules/uORB/uORB_tests
 	systemcmds/tests
 
+	platforms/posix/tests/hello
+	platforms/posix/tests/hrt_test
+	platforms/posix/tests/muorb
+	platforms/posix/tests/vcdev_test
+	platforms/posix/tests/wqueue
+
 	#
 	# General system control
 	#
diff --git a/posix-configs/SITL/init/test/cmd_template.in b/posix-configs/SITL/init/test/cmd_template.in
new file mode 100644
index 0000000000..dc2d46a702
--- /dev/null
+++ b/posix-configs/SITL/init/test/cmd_template.in
@@ -0,0 +1,37 @@
+uorb start
+
+param load
+param set SYS_RESTART_TYPE 0
+
+dataman start
+
+simulator start -t
+tone_alarm start
+gyrosim start
+accelsim start
+barosim start
+adcsim start
+gpssim start
+measairspeedsim start
+pwm_out_sim mode_pwm
+
+ver all
+
+list_tasks
+list_devices
+list_topics
+list_files
+
+mavlink start -x -u 14556 -r 2000000
+mavlink boot_complete
+
+@cmd_name@ start
+sleep 1
+@cmd_name@ start
+sleep 1
+@cmd_name@ stop
+
+dataman status
+dataman stop
+
+shutdown
diff --git a/posix-configs/SITL/init/test/test_template.in b/posix-configs/SITL/init/test/tests_template.in
similarity index 98%
rename from posix-configs/SITL/init/test/test_template.in
rename to posix-configs/SITL/init/test/tests_template.in
index a89b301ddc..51e0bdc75b 100644
--- a/posix-configs/SITL/init/test/test_template.in
+++ b/posix-configs/SITL/init/test/tests_template.in
@@ -15,8 +15,6 @@ gpssim start
 measairspeedsim start
 pwm_out_sim mode_pwm
 
-help
-
 ver all
 
 list_tasks
diff --git a/src/firmware/posix/sitl_tests.cmake b/src/firmware/posix/sitl_tests.cmake
index b67d2f3dae..3154a6878d 100644
--- a/src/firmware/posix/sitl_tests.cmake
+++ b/src/firmware/posix/sitl_tests.cmake
@@ -40,7 +40,7 @@ if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
 endif()
 
 foreach(test_name ${tests})
-	configure_file(${PX4_SOURCE_DIR}/posix-configs/SITL/init/test/test_template.in ${PX4_SOURCE_DIR}/posix-configs/SITL/init/test/test_${test_name}_generated)
+	configure_file(${PX4_SOURCE_DIR}/posix-configs/SITL/init/test/tests_template.in ${PX4_SOURCE_DIR}/posix-configs/SITL/init/test/tests_${test_name}_generated)
 
 	add_test(NAME ${test_name}
 		COMMAND ${PX4_SOURCE_DIR}/Tools/sitl_run.sh
@@ -48,7 +48,7 @@ foreach(test_name ${tests})
 			posix-configs/SITL/init/test
 			none
 			none
-			test_${test_name}_generated
+			tests_${test_name}_generated
 			${PX4_SOURCE_DIR}
 			${PX4_BINARY_DIR}
 			WORKING_DIRECTORY ${SITL_WORKING_DIR})
@@ -57,6 +57,33 @@ foreach(test_name ${tests})
 	set_tests_properties(${test_name} PROPERTIES PASS_REGULAR_EXPRESSION "${test_name} PASSED")
 endforeach()
 
+# run arbitrary commands
+set(test_cmds
+	hello
+	hrt_test
+	muorb_test
+	vcdev_test
+	wqueue_test
+	)
+
+foreach(cmd_name ${test_cmds})
+	configure_file(${PX4_SOURCE_DIR}/posix-configs/SITL/init/test/cmd_template.in ${PX4_SOURCE_DIR}/posix-configs/SITL/init/test/cmd_${cmd_name}_generated)
+
+	add_test(NAME posix_${cmd_name}
+		COMMAND ${PX4_SOURCE_DIR}/Tools/sitl_run.sh
+			$<TARGET_FILE:px4>
+			posix-configs/SITL/init/test
+			none
+			none
+			cmd_${cmd_name}_generated
+			${PX4_SOURCE_DIR}
+			${PX4_BINARY_DIR}
+			WORKING_DIRECTORY ${SITL_WORKING_DIR})
+
+	set_tests_properties(posix_${cmd_name} PROPERTIES PASS_REGULAR_EXPRESSION "Shutting down")
+endforeach()
+
+
 add_custom_target(test_results
 		COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure -T Test
 		DEPENDS px4
diff --git a/src/platforms/posix/tests/hello/hello_start_posix.cpp b/src/platforms/posix/tests/hello/hello_start_posix.cpp
index 583baaf56a..f553b985ea 100644
--- a/src/platforms/posix/tests/hello/hello_start_posix.cpp
+++ b/src/platforms/posix/tests/hello/hello_start_posix.cpp
@@ -38,6 +38,8 @@
  * @author Mark Charlebois <mcharleb@gmail.com>
  */
 #include "hello_example.h"
+
+#include <px4_log.h>
 #include <px4_app.h>
 #include <px4_tasks.h>
 #include <stdio.h>
@@ -46,7 +48,7 @@
 
 #define SCHED_DEFAULT	SCHED_FIFO
 #define SCHED_PRIORITY_MAX sched_get_priority_max(SCHED_FIFO)
-#define SCHED_PRIORITY_DEFAULT sched_get_priority_max(SCHED_FIFO)
+//#define SCHED_PRIORITY_DEFAULT sched_get_priority_max(SCHED_FIFO)
 
 static int daemon_task;             /* Handle of deamon task / thread */
 
@@ -74,7 +76,7 @@ int hello_main(int argc, char *argv[])
 						 SCHED_PRIORITY_MAX - 5,
 						 2000,
 						 PX4_MAIN,
-						 (argv) ? (char *const *)&argv[2] : (char *const *)NULL);
+						 (argv) ? (char *const *)&argv[2] : (char *const *)nullptr);
 
 		return 0;
 	}
diff --git a/src/platforms/posix/tests/hrt_test/CMakeLists.txt b/src/platforms/posix/tests/hrt_test/CMakeLists.txt
index 3a67f630ea..59c967247d 100644
--- a/src/platforms/posix/tests/hrt_test/CMakeLists.txt
+++ b/src/platforms/posix/tests/hrt_test/CMakeLists.txt
@@ -32,7 +32,7 @@
 ############################################################################
 px4_add_module(
 	MODULE platforms__posix__tests__hrt_test
-	MAIN hrttest
+	MAIN hrt_test
 	SRCS
 		hrt_test_main.cpp
 		hrt_test_start_posix.cpp
diff --git a/src/platforms/posix/tests/hrt_test/hrt_test.cpp b/src/platforms/posix/tests/hrt_test/hrt_test.cpp
index 9f34ae264a..1bd5db1e68 100644
--- a/src/platforms/posix/tests/hrt_test/hrt_test.cpp
+++ b/src/platforms/posix/tests/hrt_test/hrt_test.cpp
@@ -59,7 +59,7 @@ static void timer_expired(void *arg)
 
 	if (i < 5) {
 		i++;
-		hrt_call_after(&t1, update_interval, timer_expired, (void *)0);
+		hrt_call_after(&t1, update_interval, timer_expired, (void *)nullptr);
 	}
 }
 
@@ -83,7 +83,7 @@ int HRTTest::main()
 
 	PX4_INFO("HRT_CALL %d\n", hrt_called(&t1));
 
-	hrt_call_after(&t1, update_interval, timer_expired, (void *)0);
+	hrt_call_after(&t1, update_interval, timer_expired, (void *)nullptr);
 	sleep(2);
 	PX4_INFO("HRT_CALL - %d\n", hrt_called(&t1));
 	hrt_cancel(&t1);
diff --git a/src/platforms/posix/tests/hrt_test/hrt_test_start_posix.cpp b/src/platforms/posix/tests/hrt_test/hrt_test_start_posix.cpp
index ed718b2cc4..0519367169 100644
--- a/src/platforms/posix/tests/hrt_test/hrt_test_start_posix.cpp
+++ b/src/platforms/posix/tests/hrt_test/hrt_test_start_posix.cpp
@@ -46,11 +46,11 @@
 
 static int daemon_task;             /* Handle of deamon task / thread */
 
-extern "C" __EXPORT int hrttest_main(int argc, char *argv[]);
-int hrttest_main(int argc, char *argv[])
+extern "C" __EXPORT int hrt_test_main(int argc, char *argv[]);
+int hrt_test_main(int argc, char *argv[])
 {
 	if (argc < 2) {
-		PX4_WARN("usage: hrttest_main {start|stop|status}\n");
+		PX4_WARN("usage: hrt_test_main {start|stop|status}\n");
 		return 1;
 	}
 
@@ -67,7 +67,7 @@ int hrttest_main(int argc, char *argv[])
 						 SCHED_PRIORITY_MAX - 5,
 						 2000,
 						 PX4_MAIN,
-						 (argv) ? (char *const *)&argv[2] : (char *const *)NULL);
+						 (argv) ? (char *const *)&argv[2] : (char *const *)nullptr);
 
 		return 0;
 	}
diff --git a/src/platforms/posix/tests/muorb/muorb_test_example.cpp b/src/platforms/posix/tests/muorb/muorb_test_example.cpp
index 4547c6db57..d8af5f81e5 100644
--- a/src/platforms/posix/tests/muorb/muorb_test_example.cpp
+++ b/src/platforms/posix/tests/muorb/muorb_test_example.cpp
@@ -60,14 +60,14 @@ int  MuorbTestExample::DefaultTest()
 	int i = 0;
 	orb_advert_t pub_id = orb_advertise(ORB_ID(esc_status), & m_esc_status);
 
-	if (pub_id == 0) {
+	if (pub_id == nullptr) {
 		PX4_ERR("error publishing esc_status");
 		return -1;
 	}
 
 	orb_advert_t pub_id_vc = orb_advertise(ORB_ID(vehicle_command), & m_vc);
 
-	if (pub_id_vc == 0) {
+	if (pub_id_vc == nullptr) {
 		PX4_ERR("error publishing vehicle_command");
 		return -1;
 	}
@@ -129,7 +129,7 @@ int MuorbTestExample::PingPongTest()
 	int i = 0;
 	orb_advert_t pub_id_vc = orb_advertise(ORB_ID(vehicle_command), & m_vc);
 
-	if (pub_id_vc == 0) {
+	if (pub_id_vc == nullptr) {
 		PX4_ERR("error publishing vehicle_command");
 		return -1;
 	}
diff --git a/src/platforms/posix/tests/vcdev_test/CMakeLists.txt b/src/platforms/posix/tests/vcdev_test/CMakeLists.txt
index b7ff2cc9e2..ea28bb36c6 100644
--- a/src/platforms/posix/tests/vcdev_test/CMakeLists.txt
+++ b/src/platforms/posix/tests/vcdev_test/CMakeLists.txt
@@ -32,7 +32,7 @@
 ############################################################################
 px4_add_module(
 	MODULE platforms__posix__tests__vcdev_test
-	MAIN vcdevtest
+	MAIN vcdev_test
 	SRCS
 		vcdevtest_main.cpp
 		vcdevtest_start_posix.cpp
diff --git a/src/platforms/posix/tests/vcdev_test/vcdevtest_example.cpp b/src/platforms/posix/tests/vcdev_test/vcdevtest_example.cpp
index 5e4ea85aa4..ad9e974a6f 100644
--- a/src/platforms/posix/tests/vcdev_test/vcdevtest_example.cpp
+++ b/src/platforms/posix/tests/vcdev_test/vcdevtest_example.cpp
@@ -66,7 +66,7 @@ static int writer_main(int argc, char *argv[])
 		return -px4_errno;
 	}
 
-	int ret;
+	int ret = 0;
 	int i = 0;
 
 	while (!g_exit) {
@@ -188,7 +188,7 @@ VCDevExample::~VCDevExample()
 {
 	if (_node) {
 		delete _node;
-		_node = 0;
+		_node = nullptr;
 	}
 }
 
@@ -272,7 +272,7 @@ int VCDevExample::main()
 
 	_node = new VCDevNode();
 
-	if (_node == 0) {
+	if (_node == nullptr) {
 		PX4_INFO("Failed to allocate VCDevNode");
 		return -ENOMEM;
 	}
@@ -289,7 +289,7 @@ int VCDevExample::main()
 		return -px4_errno;
 	}
 
-	void *p = 0;
+	void *p = nullptr;
 	int ret = px4_ioctl(fd, DIOC_GETPRIV, (unsigned long)&p);
 
 	if (ret < 0) {
@@ -318,7 +318,7 @@ int VCDevExample::main()
 				 SCHED_PRIORITY_MAX - 6,
 				 2000,
 				 writer_main,
-				 (char *const *)NULL);
+				 (char *const *)nullptr);
 
 	ret = 0;
 
diff --git a/src/platforms/posix/tests/vcdev_test/vcdevtest_start_posix.cpp b/src/platforms/posix/tests/vcdev_test/vcdevtest_start_posix.cpp
index 135ca8f3f7..b6e7681e15 100644
--- a/src/platforms/posix/tests/vcdev_test/vcdevtest_start_posix.cpp
+++ b/src/platforms/posix/tests/vcdev_test/vcdevtest_start_posix.cpp
@@ -47,8 +47,8 @@ static int daemon_task;             /* Handle of deamon task / thread */
 
 //using namespace px4;
 
-extern "C" __EXPORT int vcdevtest_main(int argc, char *argv[]);
-int vcdevtest_main(int argc, char *argv[])
+extern "C" __EXPORT int vcdev_test_main(int argc, char *argv[]);
+int vcdev_test_main(int argc, char *argv[])
 {
 
 	if (argc < 2) {
@@ -69,7 +69,7 @@ int vcdevtest_main(int argc, char *argv[])
 						 SCHED_PRIORITY_MAX - 5,
 						 2000,
 						 PX4_MAIN,
-						 (argv) ? (char *const *)&argv[2] : (char *const *)NULL);
+						 (argv) ? (char *const *)&argv[2] : (char *const *)nullptr);
 
 		return 0;
 	}
diff --git a/src/platforms/posix/tests/wqueue/wqueue_start_posix.cpp b/src/platforms/posix/tests/wqueue/wqueue_start_posix.cpp
index d4652f8e62..126a57fbc9 100644
--- a/src/platforms/posix/tests/wqueue/wqueue_start_posix.cpp
+++ b/src/platforms/posix/tests/wqueue/wqueue_start_posix.cpp
@@ -71,7 +71,7 @@ int wqueue_test_main(int argc, char *argv[])
 						 SCHED_PRIORITY_MAX - 5,
 						 2000,
 						 PX4_MAIN,
-						 (argv) ? (char *const *)&argv[2] : (char *const *)NULL);
+						 (argv) ? (char *const *)&argv[2] : (char *const *)nullptr);
 
 		return 0;
 	}
diff --git a/src/systemcmds/tests/tests_main.c b/src/systemcmds/tests/tests_main.c
index f3769e77de..7c5cfacc01 100644
--- a/src/systemcmds/tests/tests_main.c
+++ b/src/systemcmds/tests/tests_main.c
@@ -96,10 +96,9 @@ const struct {
 	{"mavlink",		mavlink_tests_main,	0},
 	{"mc_pos_control",	mc_pos_control_tests_main,	0},
 	{"sf0x",		sf0x_tests_main,	0},
-#ifndef __PX4_DARWIN
 	{"uorb",		uorb_tests_main,	0},
 	{"hysteresis",		test_hysteresis,	0},
-#endif /* __PX4_DARWIN */
+
 	{"mixer",		test_mixer,	OPT_NOJIGTEST},
 	{"autodeclination",	test_autodeclination,	0},
 	{"bson",		test_bson,	0},
-- 
GitLab