]>
Commit | Line | Data |
---|---|---|
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 | |
23 | set -e | |
24 | ||
25 | # Turn on job control | |
26 | set -o monitor | |
27 | ||
28 | run_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 |
45 | dump_command() { |
46 | echo "Output of $@" | |
47 | ||
48 | # Run the command | |
49 | $@ 2>&1 | |
50 | ||
51 | echo "EOF" | |
52 | } | |
53 | ||
54 | dump_status() { | |
55 | dump_command "ip -d link" | |
56 | } | |
57 | ||
c920185e MT |
58 | # Launches networkd in the background |
59 | launch_networkd() { | |
60 | echo "Launching networkd..." | |
61 | ||
62 | # Launch it! | |
63 | coproc networkd { ./networkd "$@"; } | |
64 | ||
65 | echo "networkd launched as PID ${networkd_PID}" | |
66 | } | |
67 | ||
68 | terminate_networkd() { | |
69 | if [ -n "${networkd_PID}" ]; then | |
9ddb7dad MT |
70 | # Collect some status information |
71 | dump_status | |
72 | ||
c920185e MT |
73 | # Send SIGTERM |
74 | kill -TERM "${networkd_PID}" | |
75 | ||
76 | # Wait until networkd has finished | |
77 | echo "Waiting for networkd to terminate..." | |
78 | wait "${networkd_PID}" | |
79 | ||
80 | echo "networkd has terminated" | |
81 | fi | |
82 | } | |
83 | ||
84 | # Make sure networkd has been terminated when this script exits | |
85 | trap "terminate_networkd" EXIT | |
86 | ||
87 | main() { | |
88 | local test="${1}" | |
89 | shift | |
90 | ||
91 | echo "Running ${test}..." | |
92 | ||
93 | # Check if the test exists | |
94 | if [ ! -d "${test}" ]; then | |
95 | echo "Test '${test}' does not exist" >&2 | |
96 | return 2 | |
97 | fi | |
98 | ||
99 | # Run prepare script | |
100 | if ! run_script "${test}/prepare.sh"; then | |
101 | return 1 | |
102 | fi | |
103 | ||
104 | # Launch networkd | |
105 | launch_networkd --config="${test}/config" | |
106 | ||
107 | # Run test script | |
108 | if ! run_script "${test}/test.sh"; then | |
109 | return 1 | |
110 | fi | |
111 | ||
112 | # Terminate networkd | |
113 | terminate_networkd | |
114 | ||
115 | # Run cleanup script | |
116 | if ! run_script "${test}/cleanup.sh"; then | |
117 | return 1 | |
118 | fi | |
119 | ||
120 | return 0 | |
121 | } | |
122 | ||
123 | # Call main() | |
124 | main "$@" || exit $? |