unit tests: prepare for kdumpctl and kdump-lib.sh to be unit-tested

Currently there are two issues with unit-testing the functions defined
in kdumpctl and other shell scripts after sourcing them,
  - kdumpctl would call main which requires root permission and would
    create single instance lock (/var/lock/kdump)
  - kdumpctl and other shell scripts directly source files under /usr/lib/kdump/

When ShellSpec load a script via "Include", it defines the__SOURCED__
variable. By making use of __SOURCED__, we can
1. let kdumpctl not call main when kdumpctl is "Include"d by ShellSpec
2. instruct kdumpctl and kdump-lib.sh to source the files in the repo
   when running ShelSpec tests

Note coverage/ is added to .gitignore because ShellSpec generates code
coverage results in this folder.

Reviewed-by: Philipp Rudo <prudo@redhat.com>
Signed-off-by: Coiby Xu <coxu@redhat.com>
This commit is contained in:
Coiby Xu 2022-01-04 14:04:38 +08:00
parent 11140c28a2
commit b97310428f
3 changed files with 23 additions and 9 deletions

1
.gitignore vendored
View File

@ -21,3 +21,4 @@
/kexec-tools-2.0.11.tar.xz
/makedumpfile-1.5.9.tar.gz
/kexec-tools-2.0.12.tar.xz
coverage/

View File

@ -2,8 +2,11 @@
#
# Kdump common variables and functions
#
. /usr/lib/kdump/kdump-lib-initramfs.sh
if [[ ${__SOURCED__:+x} ]]; then
. ./kdump-lib-initramfs.sh
else
. /lib/kdump/kdump-lib-initramfs.sh
fi
FADUMP_ENABLED_SYS_NODE="/sys/kernel/fadump_enabled"

View File

@ -32,8 +32,14 @@ fi
[[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
. $dracutbasedir/dracut-functions.sh
. /lib/kdump/kdump-lib.sh
. /lib/kdump/kdump-logger.sh
if [[ ${__SOURCED__:+x} ]]; then
KDUMP_LIB_PATH=.
else
KDUMP_LIB_PATH=/lib/kdump
fi
. $KDUMP_LIB_PATH/kdump-lib.sh
. $KDUMP_LIB_PATH/kdump-logger.sh
#initiate the kdump logger
if ! dlog_init; then
@ -1676,11 +1682,6 @@ reset_crashkernel_for_installed_kernel()
fi
}
if [[ ! -f $KDUMP_CONFIG_FILE ]]; then
derror "Error: No kdump config file found!"
exit 1
fi
main()
{
# Determine if the dump mode is kdump or fadump
@ -1753,6 +1754,15 @@ main()
esac
}
if [[ ${__SOURCED__:+x} ]]; then
return
fi
if [[ ! -f $KDUMP_CONFIG_FILE ]]; then
derror "Error: No kdump config file found!"
exit 1
fi
# Other kdumpctl instances will block in queue, until this one exits
single_instance_lock