From: Michael Tremer Date: Fri, 9 Jun 2023 07:46:40 +0000 (+0000) Subject: networkd: Add a simple test environment X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c920185ee99d0f143946925aba0668785225f6f4;p=network.git networkd: Add a simple test environment Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index f4c22b48..fe1e4d1f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 index 00000000..e69de29b diff --git a/test/networkd/00_launch.t/test.sh b/test/networkd/00_launch.t/test.sh new file mode 100644 index 00000000..f3d7bbc5 --- /dev/null +++ b/test/networkd/00_launch.t/test.sh @@ -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 index 00000000..2af7479f --- /dev/null +++ b/test/networkd/test.sh @@ -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 . # +# # +############################################################################### + +# 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 $?