1
0
mirror of https://github.com/pi-hole/pi-hole synced 2024-12-21 06:18:06 +00:00

Tweak tests to allow systemd as init system

Signed-off-by: Christian König <ckoenig@posteo.de>
This commit is contained in:
Christian König 2022-11-29 22:32:51 +01:00
parent 95e799ed6e
commit 74b0904af4
No known key found for this signature in database
3 changed files with 91 additions and 82 deletions

View File

@ -39,7 +39,7 @@ testinfra.backend.docker.DockerBackend.run = run_bash
def host(): def host():
# run a container # run a container
docker_id = ( docker_id = (
subprocess.check_output(["docker", "run", "-t", "-d", "--cap-add=ALL", IMAGE]) subprocess.check_output(["docker", "run", "-t", "-d", "--privileged", IMAGE])
.decode() .decode()
.strip() .strip()
) )

View File

@ -168,20 +168,23 @@ def test_installPihole_fresh_install_readableFiles(host):
mock_command("dialog", {"*": ("", "0")}, host) mock_command("dialog", {"*": ("", "0")}, host)
# mock git pull # mock git pull
mock_command_passthrough("git", {"pull": ("", "0")}, host) mock_command_passthrough("git", {"pull": ("", "0")}, host)
# mock systemctl to not start lighttpd and FTL # if systemd is not PID 1 mock systemctl to not start lighttpd and FTL
mock_command_2( init_system = host.run("cat /proc/1/comm")
"systemctl", print(init_system.stdout)
{ if "systemd" not in init_system.stdout:
"enable lighttpd": ("", "0"), mock_command_2(
"restart lighttpd": ("", "0"), "systemctl",
"start lighttpd": ("", "0"), {
"enable pihole-FTL": ("", "0"), "enable lighttpd": ("", "0"),
"restart pihole-FTL": ("", "0"), "restart lighttpd": ("", "0"),
"start pihole-FTL": ("", "0"), "start lighttpd": ("", "0"),
"*": ('echo "systemctl call with $@"', "0"), "enable pihole-FTL": ("", "0"),
}, "restart pihole-FTL": ("", "0"),
host, "start pihole-FTL": ("", "0"),
) "*": ('echo "systemctl call with $@"', "0"),
},
host,
)
# try to install man # try to install man
host.run("command -v apt-get > /dev/null && apt-get install -qq man") host.run("command -v apt-get > /dev/null && apt-get install -qq man")
host.run("command -v dnf > /dev/null && dnf install -y man") host.run("command -v dnf > /dev/null && dnf install -y man")
@ -278,13 +281,14 @@ def test_installPihole_fresh_install_readableFiles(host):
) )
actual_rc = host.run(check_dnsmasqconf).rc actual_rc = host.run(check_dnsmasqconf).rc
assert exit_status_success == actual_rc assert exit_status_success == actual_rc
# check readable and executable /etc/init.d/pihole-FTL if "systemd" not in init_system.stdout:
check_init = test_cmd.format("x", "/etc/init.d/pihole-FTL", piholeuser) # check readable and executable /etc/init.d/pihole-FTL
actual_rc = host.run(check_init).rc check_init = test_cmd.format("x", "/etc/init.d/pihole-FTL", piholeuser)
assert exit_status_success == actual_rc actual_rc = host.run(check_init).rc
check_init = test_cmd.format("r", "/etc/init.d/pihole-FTL", piholeuser) assert exit_status_success == actual_rc
actual_rc = host.run(check_init).rc check_init = test_cmd.format("r", "/etc/init.d/pihole-FTL", piholeuser)
assert exit_status_success == actual_rc actual_rc = host.run(check_init).rc
assert exit_status_success == actual_rc
# check readable /etc/lighttpd/lighttpd.conf # check readable /etc/lighttpd/lighttpd.conf
check_lighttpd = test_cmd.format("r", "/etc/lighttpd/lighttpd.conf", piholeuser) check_lighttpd = test_cmd.format("r", "/etc/lighttpd/lighttpd.conf", piholeuser)
actual_rc = host.run(check_lighttpd).rc actual_rc = host.run(check_lighttpd).rc
@ -362,67 +366,69 @@ def test_installPihole_fresh_install_readableBlockpage(host, test_webpage):
# mock git pull # mock git pull
mock_command_passthrough("git", {"pull": ("", "0")}, host) mock_command_passthrough("git", {"pull": ("", "0")}, host)
# mock systemctl to start lighttpd and FTL init_system = host.run("cat /proc/1/comm")
ligthttpdcommand = dedent( if "systemd" not in init_system.stdout:
r'''\"\" # if systemd is not PID 1 mock systemctl to not start lighttpd and FTL
echo 'starting lighttpd with {}' ligthttpdcommand = dedent(
if [ command -v "apt-get" >/dev/null 2>&1 ]; then r'''\"\"
LIGHTTPD_USER="www-data" echo 'starting lighttpd with {}'
LIGHTTPD_GROUP="www-data" if [ command -v "apt-get" >/dev/null 2>&1 ]; then
else LIGHTTPD_USER="www-data"
LIGHTTPD_USER="lighttpd" LIGHTTPD_GROUP="www-data"
LIGHTTPD_GROUP="lighttpd" else
fi LIGHTTPD_USER="lighttpd"
mkdir -p "{run}" LIGHTTPD_GROUP="lighttpd"
chown {usergroup} "{run}" fi
mkdir -p "{cache}" mkdir -p "{run}"
chown {usergroup} "/var/cache" chown {usergroup} "{run}"
chown {usergroup} "{cache}" mkdir -p "{cache}"
mkdir -p "{compress}" chown {usergroup} "/var/cache"
chown {usergroup} "{compress}" chown {usergroup} "{cache}"
mkdir -p "{uploads}" mkdir -p "{compress}"
chown {usergroup} "{uploads}" chown {usergroup} "{compress}"
chmod 0777 /var mkdir -p "{uploads}"
chmod 0777 /var/cache chown {usergroup} "{uploads}"
chmod 0777 "{cache}" chmod 0777 /var
find "{run}" -type d -exec chmod 0777 {chmodarg} \;; chmod 0777 /var/cache
find "{run}" -type f -exec chmod 0666 {chmodarg} \;; chmod 0777 "{cache}"
find "{compress}" -type d -exec chmod 0777 {chmodarg} \;; find "{run}" -type d -exec chmod 0777 {chmodarg} \;;
find "{compress}" -type f -exec chmod 0666 {chmodarg} \;; find "{run}" -type f -exec chmod 0666 {chmodarg} \;;
find "{uploads}" -type d -exec chmod 0777 {chmodarg} \;; find "{compress}" -type d -exec chmod 0777 {chmodarg} \;;
find "{uploads}" -type f -exec chmod 0666 {chmodarg} \;; find "{compress}" -type f -exec chmod 0666 {chmodarg} \;;
/usr/sbin/lighttpd -tt -f '{config}' find "{uploads}" -type d -exec chmod 0777 {chmodarg} \;;
/usr/sbin/lighttpd -f '{config}' find "{uploads}" -type f -exec chmod 0666 {chmodarg} \;;
echo \"\"'''.format( /usr/sbin/lighttpd -tt -f '{config}'
"{}", /usr/sbin/lighttpd -f '{config}'
usergroup="${{LIGHTTPD_USER}}:${{LIGHTTPD_GROUP}}", echo \"\"'''.format(
chmodarg="{{}}", "{}",
config="/etc/lighttpd/lighttpd.conf", usergroup="${{LIGHTTPD_USER}}:${{LIGHTTPD_GROUP}}",
run="/var/run/lighttpd", chmodarg="{{}}",
cache="/var/cache/lighttpd", config="/etc/lighttpd/lighttpd.conf",
uploads="/var/cache/lighttpd/uploads", run="/var/run/lighttpd",
compress="/var/cache/lighttpd/compress", cache="/var/cache/lighttpd",
uploads="/var/cache/lighttpd/uploads",
compress="/var/cache/lighttpd/compress",
)
)
FTLcommand = dedent(
'''\"\"
set -x
/etc/init.d/pihole-FTL restart
echo \"\"'''
)
mock_command_run(
"systemctl",
{
"enable lighttpd": ("", "0"),
"restart lighttpd": (ligthttpdcommand.format("restart"), "0"),
"start lighttpd": (ligthttpdcommand.format("start"), "0"),
"enable pihole-FTL": ("", "0"),
"restart pihole-FTL": (FTLcommand, "0"),
"start pihole-FTL": (FTLcommand, "0"),
"*": ('echo "systemctl call with $@"', "0"),
},
host,
) )
)
FTLcommand = dedent(
'''\"\"
set -x
/etc/init.d/pihole-FTL restart
echo \"\"'''
)
mock_command_run(
"systemctl",
{
"enable lighttpd": ("", "0"),
"restart lighttpd": (ligthttpdcommand.format("restart"), "0"),
"start lighttpd": (ligthttpdcommand.format("start"), "0"),
"enable pihole-FTL": ("", "0"),
"restart pihole-FTL": (FTLcommand, "0"),
"start pihole-FTL": (FTLcommand, "0"),
"*": ('echo "systemctl call with $@"', "0"),
},
host,
)
# create configuration file # create configuration file
setup_var_file = "cat <<EOF> /etc/pihole/setupVars.conf\n" setup_var_file = "cat <<EOF> /etc/pihole/setupVars.conf\n"
for k, v in SETUPVARS.items(): for k, v in SETUPVARS.items():

View File

@ -137,6 +137,9 @@ def test_getFTLPIDFile_and_getFTLPID_custom(host):
host.run( host.run(
""" """
tmpfile=$(mktemp) tmpfile=$(mktemp)
# adding a small delay here because otherwise there is some kind of
# race condition with systemd deleting files in /tmp
sleep 1
echo "PIDFILE=${tmpfile}" > /etc/pihole/pihole-FTL.conf echo "PIDFILE=${tmpfile}" > /etc/pihole/pihole-FTL.conf
echo "1234" > ${tmpfile} echo "1234" > ${tmpfile}
""" """