Skip to content

Commit 86a2776

Browse files
authored
Merge pull request #1899 from pi-hole/improve/check_shutdown
Improve/check shutdown
2 parents 4554d61 + e080b72 commit 86a2776

4 files changed

Lines changed: 41 additions & 28 deletions

File tree

src/bash_functions.sh

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,9 @@ migrate_gravity() {
104104
echo " [i] Gravity migration checks"
105105
gravityDBfile=$(getFTLConfigValue files.gravity)
106106

107-
if [[ -z "${PYTEST}" ]]; then
108-
if [[ ! -f /etc/pihole/adlists.list ]]; then
109-
echo " [i] No adlist file found, creating one with a default blocklist"
110-
echo "https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts" >/etc/pihole/adlists.list
111-
fi
107+
if [[ ! -f /etc/pihole/adlists.list ]]; then
108+
echo " [i] No adlist file found, creating one with a default blocklist"
109+
echo "https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts" >/etc/pihole/adlists.list
112110
fi
113111

114112
if [ ! -f "${gravityDBfile}" ]; then

src/start.sh

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ start() {
7777

7878
# Wait until the FTL log contains the "FTL started" message before continuing, timeout after 10 seconds
7979
# exit if we do not find it
80-
pihole-FTL wait-for '########## FTL started' /var/log/pihole/FTL.log 10 "0 > /dev/null"
80+
pihole-FTL wait-for '########## FTL started' /var/log/pihole/FTL.log 10 0 > /dev/null
8181
if [ $? -ne 0 ]; then
8282
echo " [✗] FTL did not start - stopping container"
8383
exit 1
@@ -146,12 +146,6 @@ stop() {
146146
echo " https://docs.docker.com/engine/containers/start-containers-automatically/#use-a-restart-policy"
147147
echo ""
148148

149-
# If we are running pytest, keep the container alive for a little longer
150-
# to allow the tests to complete
151-
if [[ ${PYTEST} ]]; then
152-
sleep 10
153-
fi
154-
155149
exit "${FTL_EXIT_CODE}"
156150

157151
}

test/tests/conftest.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ def docker(request):
4545
for env_var in env_vars:
4646
cmd.extend(["-e", env_var])
4747

48-
# ensure PYTEST=1 is set
49-
if not any("PYTEST=1" in arg for arg in cmd):
50-
cmd.extend(["-e", "PYTEST=1"])
51-
5248
# add default TZ if not already set
5349
if not any("TZ=" in arg for arg in cmd):
5450
cmd.extend(["-e", 'TZ="Europe/London"'])

test/tests/test_general.py

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,44 @@ def test_pihole_ftl_architecture(docker):
4949
assert platform in func.stdout
5050

5151

52-
# Wait 5 seconds for startup, then kill the start.sh script
53-
# Finally, check the FTL log to see if it has been shut down cleanly
54-
def test_pihole_ftl_clean_shutdown(docker):
55-
func = docker.run(
56-
"""
57-
sleep 5
58-
killall --signal 15 start.sh
59-
sleep 5
60-
grep 'terminated' /var/log/pihole/FTL.log
61-
"""
52+
# Wait for FTL to start up, then stop the container gracefully
53+
# Finally, check the container logs to see if FTL was shut down cleanly
54+
def test_pihole_ftl_starts_and_shuts_down_cleanly(docker):
55+
import subprocess
56+
import time
57+
58+
# Get the container ID from the docker fixture
59+
container_id = docker.backend.name
60+
61+
# Wait for FTL to fully start up by checking logs
62+
max_wait_time = 60 # Maximum wait time in seconds
63+
start_time = time.time()
64+
ftl_started = False
65+
66+
while time.time() - start_time < max_wait_time:
67+
result = subprocess.run(
68+
["docker", "logs", container_id], capture_output=True, text=True
69+
)
70+
71+
if "########## FTL started" in result.stdout:
72+
ftl_started = True
73+
break
74+
75+
time.sleep(1) # Check every second
76+
77+
assert ftl_started, f"FTL did not start within {max_wait_time} seconds"
78+
79+
# Stop the container gracefully (sends SIGTERM)
80+
subprocess.run(["docker", "stop", container_id], check=True)
81+
82+
# Get the container logs
83+
result = subprocess.run(
84+
["docker", "logs", container_id], capture_output=True, text=True
6285
)
63-
assert "INFO: ########## FTL terminated after" in func.stdout
64-
assert "(code 0)" in func.stdout
86+
87+
# Check for clean shutdown messages in the logs
88+
assert "INFO: ########## FTL terminated after" in result.stdout
89+
assert "(code 0)" in result.stdout
6590

6691

6792
def test_cronfile_valid(docker):

0 commit comments

Comments
 (0)