Rework datastore tests

Sysrepo now supports parallelized tests. Use the new environmental
variables to implement this in netconf-cli. All of the tests now get
their own clean environment:

- Fresh repository and separate shm prefix. These get cleaned before and
after tests. The only thing that gets left are some empty directories.

- Its own model to test on.

- Separate Netopeer2 daemon: only for netconf tests - that means no
`sleep 5` for sysrepo-only tests. So no useless waiting. Wow! The daemon
also runs with its argv[0] changed to something recognizable for
`pkill`. That means that if Netopeer2 crashes for some reason, pkill
will notify me.

Side note: These changes somehow changed some of the linking, so hopefully I got
those right.

Change-Id: Ib0e582ef03fc559b24203af8afb2a295a6318ca9
diff --git a/tests/init_datastore.bash.in b/tests/init_datastore.bash.in
new file mode 100755
index 0000000..5aec0c0
--- /dev/null
+++ b/tests/init_datastore.bash.in
@@ -0,0 +1,43 @@
+set -eux
+export UBSAN_OPTIONS=halt_on_error=1 # UBSan doesn't stop on errors by default.
+
+if [[ $(dirname "$(dirname "$(realpath "$SYSREPO_REPOSITORY_PATH")")") != "@CMAKE_CURRENT_BINARY_DIR@" ]]; then
+    echo "\$SYSREPO_REPOSITORY_PATH is not inside the build dir! Aborting. ($SYSREPO_REPOSITORY_PATH)"
+    exit 1
+fi
+
+if [[ -z "$SYSREPO_SHM_PREFIX" ]]; then
+    echo '$SYSREPO_SHM_PREFIX is empty! Aborting.'
+    exit 1
+fi
+
+rm -rf "$SYSREPO_REPOSITORY_PATH"
+rm -rf "/dev/shm/$SYSREPO_SHM_PREFIX"*
+cp -r "@SYSREPO_SR_REPO_PATH@" "$SYSREPO_REPOSITORY_PATH"
+
+SYSREPOCTL="@SYSREPOCTL_EXECUTABLE@"
+SYSREPOCFG="@SYSREPOCFG_EXECUTABLE@"
+
+MODULE="$1"
+YANG_DIR=$(dirname "$1")
+shift
+
+# Install the module
+"$SYSREPOCTL" --search-dirs "$YANG_DIR" --install "$MODULE" -a
+
+BACKEND="$1"
+shift
+if [[ "$BACKEND" = "netconf" ]]; then
+    NETOPEER2="@NETOPEER2_EXECUTABLE@"
+    # Setup netopeer config
+    "$SYSREPOCFG" --import --datastore=running --format=xml --module=ietf-netconf-server <<< ""
+
+    # Disable nacm
+    for datastore in startup running; do
+        "$SYSREPOCFG" --import="@CMAKE_CURRENT_SOURCE_DIR@/tests/disable-nacm.xml" --datastore="$datastore" --format=xml --module=ietf-netconf-acm
+    done
+
+    # Run netopeer. Use exec -a, so that each process has a recognizable name for `pkill`.
+    (exec -a "${SYSREPO_SHM_PREFIX}_netopeer2-server" "$NETOPEER2" -v2 "-U$NETOPEER_SOCKET")
+    sleep 5
+fi