]> git.ipfire.org Git - people/ms/network.git/commitdiff
networkd: Add a simple test environment
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 9 Jun 2023 07:46:40 +0000 (07:46 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 9 Jun 2023 07:46:40 +0000 (07:46 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
test/networkd/00_launch.t/config/settings [new file with mode: 0644]
test/networkd/00_launch.t/test.sh [new file with mode: 0644]
test/networkd/test.sh [new file with mode: 0644]

index f4c22b4837826f4588367d486b9125a7b954ddc3..fe1e4d1fe719c0c293c6694046e324e320e9c4f3 100644 (file)
@@ -59,6 +59,7 @@ DISTCLEANFILES =
 EXTRA_DIST =
 INSTALL_DIRS =
 INSTALL_EXEC_HOOKS =
+TESTS =
 UNINSTALL_EXEC_HOOKS =
 noinst_DATA =
 network_PROGRAMS =
@@ -690,12 +691,10 @@ TESTS_ENVIRONMENT = \
 
 dist_check_DATA = \
        test/constants.sh \
-       test/test-functions
+       test/test-functions \
+       test/networkd/test.sh
 
 dist_check_SCRIPTS = \
-       $(TESTS)
-
-TESTS = \
        test/load-library \
        test/functions/ip/ip_detect_protocol \
        test/functions/ip/ip_get_prefix \
@@ -706,6 +705,23 @@ TESTS = \
        test/functions/ip/ip_protocol_is_supported \
        test/functions/ip/ip_split_prefix
 
+TESTS += $(dist_check_SCRIPTS)
+
+TEST_EXTENSIONS = .t
+
+NETWORKD_TESTS = \
+       test/networkd/00_launch.t
+
+TESTS += $(NETWORKD_TESTS)
+
+EXTRA_DIST += \
+       test/networkd/test.sh \
+       $(NETWORKD_TESTS)
+
+# Run all networkd tests in their own namespaces
+T_LOG_COMPILER = unshare --net --ipc --uts --user --cgroup --time --pid --fork \
+       $(SHELL) test/networkd/test.sh
+
 # - NITSI tests ----------------------------------------------------------------
 
 # Files for the virtual environment
diff --git a/test/networkd/00_launch.t/config/settings b/test/networkd/00_launch.t/config/settings
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/test/networkd/00_launch.t/test.sh b/test/networkd/00_launch.t/test.sh
new file mode 100644 (file)
index 0000000..f3d7bbc
--- /dev/null
@@ -0,0 +1,4 @@
+#!/bin/bash
+
+# Simply run networkctl to check whether it works
+./networkctl --version
diff --git a/test/networkd/test.sh b/test/networkd/test.sh
new file mode 100644 (file)
index 0000000..2af7479
--- /dev/null
@@ -0,0 +1,108 @@
+#!/bin/bash
+###############################################################################
+#                                                                             #
+# IPFire.org - A linux based firewall                                         #
+# Copyright (C) 2023 IPFire Network Development Team                          #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+###############################################################################
+
+# Break if anything fails
+set -e
+
+# Turn on job control
+set -o monitor
+
+run_script() {
+       local script="${1}"
+       shift
+
+       if [ -f "${script}" ]; then
+               echo "Launching ${script}..."
+
+               # Launch the script in a separate shell and echo every command
+               if ! ${SHELL} -xe "${script}"; then
+                       echo "${script} failed" >&2
+                       return 1
+               fi
+       fi
+
+       return 0
+}
+
+# Launches networkd in the background
+launch_networkd() {
+       echo "Launching networkd..."
+
+       # Launch it!
+       coproc networkd { ./networkd "$@"; }
+
+       echo "networkd launched as PID ${networkd_PID}"
+}
+
+terminate_networkd() {
+       if [ -n "${networkd_PID}" ]; then
+               # Send SIGTERM
+               kill -TERM "${networkd_PID}"
+
+               # Wait until networkd has finished
+               echo "Waiting for networkd to terminate..."
+               wait "${networkd_PID}"
+
+               echo "networkd has terminated"
+       fi
+}
+
+# Make sure networkd has been terminated when this script exits
+trap "terminate_networkd" EXIT
+
+main() {
+       local test="${1}"
+       shift
+
+       echo "Running ${test}..."
+
+       # Check if the test exists
+       if [ ! -d "${test}" ]; then
+               echo "Test '${test}' does not exist" >&2
+               return 2
+       fi
+
+       # Run prepare script
+       if ! run_script "${test}/prepare.sh"; then
+               return 1
+       fi
+
+       # Launch networkd
+       launch_networkd --config="${test}/config"
+
+       # Run test script
+       if ! run_script "${test}/test.sh"; then
+               return 1
+       fi
+
+       # Terminate networkd
+       terminate_networkd
+
+       # Run cleanup script
+       if ! run_script "${test}/cleanup.sh"; then
+               return 1
+       fi
+
+       return 0
+}
+
+# Call main()
+main "$@" || exit $?