]> git.ipfire.org Git - people/ms/network.git/blame - test/networkd/test.sh
test: Collect more information from test environment
[people/ms/network.git] / test / networkd / test.sh
CommitLineData
c920185e
MT
1#!/bin/bash
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2023 IPFire Network Development Team #
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
21
22# Break if anything fails
23set -e
24
25# Turn on job control
26set -o monitor
27
28run_script() {
29 local script="${1}"
30 shift
31
32 if [ -f "${script}" ]; then
33 echo "Launching ${script}..."
34
35 # Launch the script in a separate shell and echo every command
36 if ! ${SHELL} -xe "${script}"; then
37 echo "${script} failed" >&2
38 return 1
39 fi
40 fi
41
42 return 0
43}
44
9ddb7dad
MT
45dump_command() {
46 echo "Output of $@"
47
48 # Run the command
49 $@ 2>&1
50
51 echo "EOF"
52}
53
54dump_status() {
fcc334a2
MT
55 dump_command "printenv"
56 dump_command "ps aux"
9ddb7dad
MT
57 dump_command "ip -d link"
58}
59
c920185e
MT
60# Launches networkd in the background
61launch_networkd() {
62 echo "Launching networkd..."
63
64 # Launch it!
65 coproc networkd { ./networkd "$@"; }
66
67 echo "networkd launched as PID ${networkd_PID}"
f1e43a02 68
5d4157c6
MT
69 # Wait until networkd is initialized
70 # XXX Calling sleep(8) is very racy and should be replaced by something that
71 # waits until networkd has connected to dbus
f1e43a02 72 sleep 1
c920185e
MT
73}
74
75terminate_networkd() {
f1e43a02 76 local seconds=0
c920185e 77
f1e43a02
MT
78 if [ -n "${networkd_PID}" ]; then
79 while [ -n "${networkd_PID}" ] && kill -0 "${networkd_PID}"; do
80 case "${seconds}" in
81 # Send SIGTERM in the beginning
82 0)
83 echo "Sending SIGTERM to networkd"
84 kill -TERM "${networkd_PID}"
85 ;;
86
87 # After 5 seconds, send SIGKILL
88 5)
89 echo "Sending SIGKILL to networkd"
90 kill -KILL "${networkd_PID}"
91
92 # It is an error, if we have to kill networkd
93 exit 1
94 ;;
95 esac
96
97 # Wait for a moment
98 sleep 1
99
100 # Increment seconds
101 (( seconds++ ))
102 done
c920185e
MT
103
104 echo "networkd has terminated"
105 fi
106}
107
5d4157c6
MT
108# Collect some status information
109trap dump_status EXIT
110
c920185e
MT
111main() {
112 local test="${1}"
113 shift
114
115 echo "Running ${test}..."
116
117 # Check if the test exists
118 if [ ! -d "${test}" ]; then
119 echo "Test '${test}' does not exist" >&2
120 return 2
121 fi
122
123 # Run prepare script
124 if ! run_script "${test}/prepare.sh"; then
125 return 1
126 fi
127
128 # Launch networkd
129 launch_networkd --config="${test}/config"
130
131 # Run test script
132 if ! run_script "${test}/test.sh"; then
133 return 1
134 fi
135
136 # Terminate networkd
137 terminate_networkd
138
139 # Run cleanup script
140 if ! run_script "${test}/cleanup.sh"; then
141 return 1
142 fi
143
144 return 0
145}
146
147# Call main()
148main "$@" || exit $?