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 dcc5d33
Show file tree
Hide file tree
Showing 4 changed files with 158 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
}

18 changes: 18 additions & 0 deletions pre-script.ks.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#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

# 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 dcc5d33

Please sign in to comment.