Skip to content

Commit

Permalink
Tests for kickstart script commands
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkankovsky committed Sep 19, 2024
1 parent b75de4b commit 2a2ec60
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 0 deletions.
36 changes: 36 additions & 0 deletions post-script.ks.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Test name: post_install_with_logging_test
%ksappend repos/default.ks

# System configuration for automatic installation
text
bootloader --timeout=1
zerombr
clearpart --all --initlabel
autopart
rootpw --plaintext testpasswords
reboot

# Post-install script (nochroot)
%post --nochroot --log=/mnt/sysroot/root/post-nochroot.log
# Log to the nochroot log file
echo "Post-install (nochroot) script started"
# Write 'SUCCESS' to the RESULT file
echo "SUCCESS" > /mnt/sysroot/root/RESULT
# Log completion
echo "Post-install (nochroot) script finished"
%end

# Post-install script (chroot)
%post --log=/root/post-chroot.log
# Log to the chroot log file
echo "Post-install (chroot) script started"
# Check if the RESULT file contains 'SUCCESS'
if grep -q "SUCCESS" /root/RESULT; then
echo "SUCCESS found in /root/RESULT, everything is good!"
else
echo "ERROR: SUCCESS not found in /root/RESULT" > /root/RESULT
echo "ERROR: SUCCESS not found in /root/RESULT"
fi
# Log completion
echo "Post-install (chroot) script finished"
%end
52 changes: 52 additions & 0 deletions post-script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/bin/bash
#
# Copyright (C) 2024 Red Hat, Inc.
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions of
# the GNU General Public License v.2, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY expressed or implied, including the implied warranties of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details. You should have received a copy of the
# GNU General Public License along with this program; if not, write to the
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA. Any Red Hat trademarks that are incorporated in the
# source code or documentation are not subject to the GNU General Public
# License and may only be used or replicated with the express permission of
# Red Hat, Inc.
#
# Red Hat Author(s): Adam Kankovsky<[email protected]>

# Ignore unused variable parsed out by tooling scripts as test tags metadata
# shellcheck disable=SC2034
TESTTYPE="post-install"

. ${KSTESTDIR}/functions.sh

validate() {
local disksdir=$1
local status=0

# Check if the nochroot log file exists and contains expected output
if [[ ! -f "${disksdir}/root/post-nochroot.log" ]]; then
echo "*** ERROR: nochroot log file does not exist"
status=1
elif ! grep -q "Post-install (nochroot) script finished" "${disksdir}/root/post-nochroot.log"; then
echo "*** ERROR: nochroot log does not contain expected 'finished' message"
status=1
fi

# Check if the chroot log file exists and contains expected output
if [[ ! -f "${disksdir}/root/post-chroot.log" ]]; then
echo "*** ERROR: chroot log file does not exist"
status=1
elif ! grep -q "Post-install (chroot) script finished" "${disksdir}/root/post-chroot.log"; then
echo "*** ERROR: chroot log does not contain expected 'finished' message"
status=1
fi

validate_RESULT $1
return $? || status
}

33 changes: 33 additions & 0 deletions pre-install-script.ks.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Test name: pre_install_interpreter_and_error_test

%ksappend repos/default.ks

# First pre-install script using Python interpreter
%pre-install --interpreter=/usr/bin/python3 --log=/mnt/sysroot/root/preinstall_python.log
print("SUCCESS")
with open("/mnt/sysroot/root/preinstall_python.log", "a") as log_file:
log_file.write("Pre-install script running with Python\n")
%end

# Second pre-install script with intentional error
%pre-install --erroronfail --log=/mnt/sysroot/root/preinstall_error.log
echo "SUCCESS"
echo "Logging from bash pre-install script" >> /mnt/sysroot/root/preinstall_error.log
shutdown +1
exit 1
%end

# Pre-install script that should be unreachable
%pre-install
echo "Unreachable code" >> /mnt/sysroot/root/preinstall_error.log
%end

text
bootloader --timeout=1
zerombr
clearpart --all --initlabel
autopart
rootpw --plaintext testpasswords
timezone UTC
network --bootproto=dhcp
reboot
48 changes: 48 additions & 0 deletions pre-install-script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/bash
#
# Copyright (C) 2024 Red Hat, Inc.
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions of
# the GNU General Public License v.2, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY expressed or implied, including the implied warranties of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details. You should have received a copy of the
# GNU General Public License along with this program; if not, write to the
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA. Any Red Hat trademarks that are incorporated in the
# source code or documentation are not subject to the GNU General Public
# License and may only be used or replicated with the express permission of
# Red Hat, Inc.
#
# Red Hat Author(s): Adam Kankovsky <[email protected]>

# Ignore unused variable parsed out by tooling scripts as test tags metadata
# shellcheck disable=SC2034
TESTTYPE="pre-install"

. ${KSTESTDIR}/functions.sh

validate() {
local disksdir=$1
local status=0

# Check for exactly two "SUCCESS" messages in the log file
local success_count
success_count=$(grep -c "SUCCESS" "${disksdir}/virt-install.log")

if [[ $success_count -ne 2 ]]; then
echo "*** ERROR: Expected 2 SUCCESS messages, but found ${success_count}."
status=1
fi

# Ensure that the "Unreachable code" message is NOT present.
grep -q "Unreachable code" "${disksdir}/virt-install.log"
if [[ $? == 0 ]]; then
echo '*** ERROR: The test failed because unreachable code was executed after "exit 1".'
status=1
fi

return ${status}
}
20 changes: 20 additions & 0 deletions pre-script.ks.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#test name: pre_erroronfail_test

%ksappend repos/default.ks

# Pre-install script with intentional error and --erroronfail
%pre --erroronfail
echo "SUCCESS"

echo "Pre-install script started"

shutdown +1

# raise error
exit 1
%end

%pre
# Write error because this code is unreachable
echo "ERROR: Pre-install script did not fail as expected."
%end
52 changes: 52 additions & 0 deletions pre-script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/bin/bash
#
# Copyright (C) 2024 Red Hat, Inc.
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions of
# the GNU General Public License v.2, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY expressed or implied, including the implied warranties of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details. You should have received a copy of the
# GNU General Public License along with this program; if not, write to the
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA. Any Red Hat trademarks that are incorporated in the
# source code or documentation are not subject to the GNU General Public
# License and may only be used or replicated with the express permission of
# Red Hat, Inc.
#
# Red Hat Author(s): Adam Kankovsky<[email protected]>

# Ignore unused variable parsed out by tooling scripts as test tags metadata
# shellcheck disable=SC2034
TESTTYPE="pre"

. ${KSTESTDIR}/functions.sh

validate() {
# The test is looking for two conditions:
# 1. SUCCESS must be found in the logs to confirm that the pre-script executed until exit 1.
# 2. Ensure that no code after exit 1 was executed by checking if the "ERROR" message was written.

local disksdir=$1
local status=0

# Look for the "SUCCESS" message in the logs to confirm pre-script started correctly.
cat "${disksdir}/virt-install.log" | grep -q "SUCCESS"
if [[ $? != 0 ]]; then
echo '*** The pre-install script did not write "SUCCESS" as expected.'
status=1
fi

# Ensure that the "ERROR: Pre-install script did not fail as expected." message is NOT present.
cat "${disksdir}/virt-install.log" | grep -q "ERROR: Pre-install script did not fail as expected."
if [[ $? == 0 ]]; then
echo '*** The test has failed because code after "exit 1" was executed.'
status=1
fi

# Return the final status
return ${status}
}

0 comments on commit 2a2ec60

Please sign in to comment.