tests: Initialize schema once for the whole test

Installing and uninstalling schemas back-to-back repeatedly causes
issues in sysrepo.

Also try to serialize the tests harder. Concurrent runs of stuff
accessing sysrepo is "velky spatny".

Bug: https://github.com/sysrepo/sysrepo/issues/1792
Co-authored-by: Jan Kundrát <jan.kundrat@cesnet.cz>
Change-Id: Ib9c0c4d9b2943bc882808928a1c51a08d865ac87
diff --git a/CMakeLists.txt b/CMakeLists.txt
index ccf0aeb..a86f288 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -213,10 +213,17 @@
     function(setup_datastore_tests)
         add_test(NAME setup_netopeer COMMAND ${SYSREPOCFG_EXECUTABLE} ietf-netconf-server -i ${CMAKE_CURRENT_SOURCE_DIR}/tests/netopeer-test-config.xml --datastore=startup --format=xml)
         add_test(NAME start_daemons COMMAND ${FAKEROOT_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/start_daemons.sh)
+        add_test(NAME example-schema_init
+            COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/tests/sysrepoctl-manage-module.sh ${SYSREPOCTL_EXECUTABLE} ${SYSREPOCFG_EXECUTABLE} install ${CMAKE_CURRENT_SOURCE_DIR}/tests/example-schema.yang )
+        add_test(NAME example-schema_cleanup
+            COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/tests/sysrepoctl-manage-module.sh ${SYSREPOCTL_EXECUTABLE} ${SYSREPOCFG_EXECUTABLE} uninstall ${CMAKE_CURRENT_SOURCE_DIR}/tests/example-schema.yang )
         add_test(NAME kill_daemons COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/tests/kill_daemons.sh)
-        set_tests_properties(setup_netopeer start_daemons kill_daemons PROPERTIES RESOURCE_LOCK sysrepo)
+        set_tests_properties(example-schema_init PROPERTIES FIXTURES_REQUIRED netopeer_running FIXTURES_SETUP example-schema_setup)
+        set_tests_properties(example-schema_cleanup PROPERTIES FIXTURES_CLEANUP example-schema_setup)
+        set_tests_properties(setup_netopeer start_daemons kill_daemons example-schema_init example-schema_cleanup 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)
+        set_property(TEST kill_daemons APPEND PROPERTY DEPENDS example-schema_cleanup)
     endfunction()
 
     function(cli_test name)
@@ -236,14 +243,11 @@
         target_include_directories(test_${name} PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
     endfunction()
 
-    function(datastore_test_impl name model_path backend)
+    function(datastore_test_impl name model 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)
+        set_tests_properties(${TESTNAME} PROPERTIES FIXTURES_REQUIRED ${model}_setup RESOURCE_LOCK sysrepo)
+        set_property(TEST ${TESTNAME} APPEND PROPERTY FIXTURES_REQUIRED netopeer_running)
         target_include_directories(${TESTNAME} PRIVATE ${PROJECT_SOURCE_DIR}/tests/mock)
         if (${backend} STREQUAL "sysrepo")
             target_link_libraries(${TESTNAME} sysrepoaccess)
@@ -257,13 +261,9 @@
         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}/tests/example-schema.yang sysrepo)
-        datastore_test_impl(${name} ${CMAKE_CURRENT_SOURCE_DIR}/tests/example-schema.yang netconf)
-        set_tests_properties(${TESTNAME}_netconf_init PROPERTIES DEPENDS ${TESTNAME}_sysrepo_cleanup)
-        set_property(TEST kill_daemons APPEND PROPERTY DEPENDS ${TESTNAME}_sysrepo_cleanup)
-        set_property(TEST kill_daemons APPEND PROPERTY DEPENDS ${TESTNAME}_netconf_cleanup)
+    function(datastore_test name model)
+        datastore_test_impl(${name} ${model} sysrepo)
+        datastore_test_impl(${name} ${model} netconf)
     endfunction()
 
     cli_test(cd)
@@ -282,7 +282,7 @@
     target_link_libraries(test_path_utils path)
 
     setup_datastore_tests()
-    datastore_test(datastore_access ${CMAKE_CURRENT_SOURCE_DIR}/tests/example-schema.yang)
+    datastore_test(datastore_access example-schema)
 endif()
 
 option(WITH_PYTHON_BINDINGS "Create and install Python3 bindings for accessing datastores" OFF)
@@ -296,7 +296,7 @@
         configure_file(${CMAKE_CURRENT_SOURCE_DIR}/tests/python_netconfaccess.py
             ${CMAKE_CURRENT_BINARY_DIR}/tests_python_netconfaccess.py @ONLY)
         add_test(NAME test_netconf_cli_py COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/tests_python_netconfaccess.py)
-        set_tests_properties(test_netconf_cli_py PROPERTIES FIXTURES_REQUIRED netopeer_running RESOURCE_LOCK sysrepo DEPENDS test_datastore_access_netconf_cleanup)
+        set_tests_properties(test_netconf_cli_py PROPERTIES FIXTURES_REQUIRED netopeer_running RESOURCE_LOCK sysrepo DEPENDS example-schema_cleanup)
         set_property(TEST kill_daemons APPEND PROPERTY DEPENDS test_netconf_cli_py)
 
         set(sanitizer_active OFF)