Improve ctest logic

Firstly, as there will be more datastore tests, sysrepo.cpp had to be
changed to something different.

The overall logic for the datastore tests had to be changed. Now, all
datastore tests depend on the start_daemons (using fixtures). This means
that even if one starts the `test_setting_values_sysrepo` test, it
always runs start_daemons (the previous config only ensured, that
start_daemons ran before the datastore tests).

The actual datastore test creation was also refactored
to a function. This ensures that both sysrepo and netconf versions of
the test will get created.

Change-Id: Id74c9671802ed746d44f33ba0026eb81a1594e2a
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 8de23df..e0be51d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -195,6 +195,15 @@
     configure_file(${CMAKE_CURRENT_SOURCE_DIR}/start_daemons.sh.in ${CMAKE_CURRENT_BINARY_DIR}/start_daemons.sh @ONLY)
     configure_file(${CMAKE_CURRENT_SOURCE_DIR}/netopeer_vars.hpp.in ${CMAKE_CURRENT_BINARY_DIR}/netopeer_vars.hpp @ONLY)
 
+    function(setup_datastore_tests)
+        add_test(NAME setup_netopeer COMMAND ${SYSREPOCFG_EXECUTABLE} ietf-netconf-server -i ${CMAKE_CURRENT_SOURCE_DIR}/netopeer-test-config --datastore=startup --format=xml)
+        add_test(NAME start_daemons COMMAND ${FAKEROOT_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/start_daemons.sh)
+        add_test(NAME kill_daemons COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/kill_daemons.sh)
+        set_tests_properties(setup_netopeer start_daemons kill_daemons PROPERTIES RESOURCE_LOCK sysrepo)
+        set_tests_properties(setup_netopeer PROPERTIES FIXTURES_SETUP netopeer_configured)
+        set_tests_properties(start_daemons PROPERTIES FIXTURES_REQUIRED netopeer_configured FIXTURES_SETUP netopeer_running)
+    endfunction()
+
     function(cli_test name)
         if (${ARGC} GREATER 1) # this is how CMake does optional arguments
             add_executable(test_${name}
@@ -212,15 +221,32 @@
         target_include_directories(test_${name} PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
     endfunction()
 
-    function(datastore_test name fname model_path)
-        cli_test(${name} ${fname})
-        target_link_libraries(test_${name} sysreposubscription)
-        add_test(NAME test_${name}_init COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/tests/sysrepoctl-manage-module.sh ${SYSREPOCTL_EXECUTABLE} ${SYSREPOCFG_EXECUTABLE} install ${model_path})
-        add_test(NAME test_${name}_cleanup COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/tests/sysrepoctl-manage-module.sh ${SYSREPOCTL_EXECUTABLE} ${SYSREPOCFG_EXECUTABLE} uninstall ${model_path})
-        set_tests_properties(test_${name}_init PROPERTIES FIXTURES_SETUP ${name}-setup)
-        set_tests_properties(test_${name} PROPERTIES FIXTURES_REQUIRED ${name}-setup RESOURCE_LOCK sysrepo)
-        set_tests_properties(test_${name}_cleanup PROPERTIES FIXTURES_CLEANUP ${name}-setup)
-        target_compile_definitions(test_${name} PRIVATE ${name}_BACKEND)
+    function(datastore_test_impl name model_path backend)
+        set(TESTNAME test_${name}_${backend})
+        cli_test(${name}_${backend} ${name}.cpp)
+        add_test(NAME ${TESTNAME}_init COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/tests/sysrepoctl-manage-module.sh ${SYSREPOCTL_EXECUTABLE} ${SYSREPOCFG_EXECUTABLE} install ${model_path})
+        add_test(NAME ${TESTNAME}_cleanup COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/tests/sysrepoctl-manage-module.sh ${SYSREPOCTL_EXECUTABLE} ${SYSREPOCFG_EXECUTABLE} uninstall ${model_path})
+        set_tests_properties(${TESTNAME}_init PROPERTIES FIXTURES_REQUIRED netopeer_running FIXTURES_SETUP ${TESTNAME}_setup)
+        set_tests_properties(${TESTNAME} PROPERTIES FIXTURES_REQUIRED ${TESTNAME}_setup RESOURCE_LOCK sysrepo)
+        set_tests_properties(${TESTNAME}_cleanup PROPERTIES FIXTURES_CLEANUP ${TESTNAME}_setup)
+        target_include_directories(${TESTNAME} PRIVATE ${PROJECT_SOURCE_DIR}/tests/mock)
+        if (${backend} STREQUAL "sysrepo")
+            target_link_libraries(${TESTNAME} sysrepoaccess)
+        elseif (${backend} STREQUAL "netconf")
+            target_link_libraries(${TESTNAME} netconfaccess)
+        else()
+            message(FATAL_ERROR "Unknown backend ${backend}")
+        endif()
+        target_link_libraries(${TESTNAME} yangschema sysreposubscription)
+
+        target_compile_definitions(${TESTNAME} PRIVATE ${backend}_BACKEND)
+    endfunction()
+
+    function(datastore_test name model_path)
+        set(TESTNAME test_${name})
+        datastore_test_impl(${name} ${CMAKE_CURRENT_SOURCE_DIR}/example-schema.yang sysrepo)
+        datastore_test_impl(${name} ${CMAKE_CURRENT_SOURCE_DIR}/example-schema.yang netconf)
+        set_tests_properties(${TESTNAME}_netconf_init PROPERTIES DEPENDS ${TESTNAME}_sysrepo_cleanup)
     endfunction()
 
     cli_test(cd)
@@ -235,21 +261,12 @@
     cli_test(enum_completion)
     cli_test(list_manipulation)
     cli_test(parser_methods)
-    datastore_test(sysrepo sysrepo.cpp ${CMAKE_CURRENT_SOURCE_DIR}/example-schema.yang)
-    target_link_libraries(test_sysrepo sysrepoaccess yangschema parser)
-    datastore_test(netconf sysrepo.cpp ${CMAKE_CURRENT_SOURCE_DIR}/example-schema.yang)
-    add_test(NAME start_daemons COMMAND ${FAKEROOT_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/start_daemons.sh)
-    add_test(NAME setup_netopeer COMMAND ${SYSREPOCFG_EXECUTABLE} ietf-netconf-server -i ${CMAKE_CURRENT_SOURCE_DIR}/netopeer-test-config --datastore=startup --format=xml)
-    add_test(NAME kill_daemons COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/kill_daemons.sh)
-    set_tests_properties(test_sysrepo_init PROPERTIES DEPENDS start_daemons)
-    set_tests_properties(start_daemons PROPERTIES DEPENDS setup_netopeer)
-    set_tests_properties(test_netconf_init PROPERTIES DEPENDS test_sysrepo_cleanup)
-    set_tests_properties(test_netconf_cleanup PROPERTIES DEPENDS kill_daemons)
-    set_tests_properties(kill_daemons PROPERTIES DEPENDS test_netconf)
 
-    target_include_directories(test_sysrepo PRIVATE ${PROJECT_SOURCE_DIR}/tests/mock)
-    target_link_libraries(test_netconf sysreposubscription netconfaccess yangschema parser)
-    target_include_directories(test_netconf PRIVATE ${PROJECT_SOURCE_DIR}/tests/mock)
+    setup_datastore_tests()
+    datastore_test(setting_values ${CMAKE_CURRENT_SOURCE_DIR}/example-schema.yang)
+    set_tests_properties(kill_daemons PROPERTIES DEPENDS test_setting_values_netconf_cleanup)
+
+
 endif()
 
 if(WITH_DOCS)