Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests for kickstart script commands #1302

Merged
merged 1 commit into from
Nov 4, 2024
Merged
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
32 changes: 32 additions & 0 deletions script-post.ks.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Test name: post_install_with_logging_test
%ksappend repos/default.ks
%ksappend common/common_no_payload.ks
%ksappend payload/default_packages.ks

# System configuration for automatic installation
text

# 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 script-post.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="ksscript"

. ${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
}

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

%ksappend repos/default.ks
%ksappend common/common_no_payload.ks
%ksappend payload/default_packages.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
48 changes: 48 additions & 0 deletions script-pre-install.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="ksscript"

. ${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}
}
19 changes: 19 additions & 0 deletions script-pre.ks.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#test name: pre_install_interpreter_test

%ksappend repos/default.ks

# Pre-install script using Bash interpreter
%pre --interpreter=/bin/bash
echo "SUCCESS from Bash"
%end

# Pre-install script using Python interpreter
%pre --interpreter=/usr/bin/python3
print("SUCCESS from Python")
%end

# Pre script just for confirmation
%pre
echo "SUCCESS Pre"
poweroff
%end
41 changes: 41 additions & 0 deletions script-pre.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/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="ksscript"

. ${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 3 ]]; then
echo "*** ERROR: Expected 2 SUCCESS messages, but found ${success_count}."
status=1
fi

return ${status}
}