Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion scripts/fast-reboot
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/bash -e

REBOOT_USER=$(logname)
# reboot DBus service, which uses this script, doesn't use a login shell
REBOOT_USER="$(logname 2> /dev/null || whoami)"
REBOOT_TIME=$(date)
REBOOT_CAUSE_FILE="/host/reboot-cause/reboot-cause.txt"
WARM_DIR=/host/warmboot
Expand Down
28 changes: 28 additions & 0 deletions tests/fast_reboot_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import os
import pytest
import shutil
import subprocess
import tempfile


class TestFastReboot:
@pytest.mark.parametrize(
"working_script,failing_script", [
pytest.param('whoami', 'logname', id='without_logname'),
pytest.param('logname', 'whoami', id='without_whoami'),
]
)
def test_fast_reboot_user(self, working_script, failing_script):
test_path = os.path.dirname(os.path.abspath(__file__))
fast_reboot = os.path.join(test_path, '..', 'scripts', 'fast-reboot')
with tempfile.TemporaryDirectory() as tmp_dir:
working_script_path = os.path.join(tmp_dir, working_script)
with open(working_script_path, 'w') as f:
f.write('echo root')
os.chmod(working_script_path, 0o700)
shutil.copy('/bin/false', os.path.join(tmp_dir, failing_script))
shutil.copy('/bin/true', os.path.join(tmp_dir, 'sonic-cfggen'))
env = os.environ.copy()
env['PATH'] = f"{tmp_dir}:{env.get('PATH', '')}"
res = subprocess.run([fast_reboot, '-h'], env=env)
assert res.returncode == 0
Loading