From b97310428f336d58028d34f3f3c7a551e47b5c32 Mon Sep 17 00:00:00 2001 From: Coiby Xu Date: Tue, 4 Jan 2022 14:04:38 +0800 Subject: [PATCH] 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 Signed-off-by: Coiby Xu --- .gitignore | 1 + kdump-lib.sh | 7 +++++-- kdumpctl | 24 +++++++++++++++++------- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 7a38a39..e9c2dd8 100644 --- a/.gitignore +++ b/.gitignore @@ -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/ diff --git a/kdump-lib.sh b/kdump-lib.sh index 5b1656e..557eff6 100755 --- a/kdump-lib.sh +++ b/kdump-lib.sh @@ -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" diff --git a/kdumpctl b/kdumpctl index 4d819c2..6188d47 100755 --- a/kdumpctl +++ b/kdumpctl @@ -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