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
Signed-off-by: Coiby Xu <coxu(a)redhat.com>
---
kdump-lib.sh | 7 +++++--
kdumpctl | 24 +++++++++++++++++-------
2 files changed, 22 insertions(+), 9 deletions(-)
diff --git a/kdump-lib.sh b/kdump-lib.sh
index 4ed5035..50b69e1 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 271c60c..aa3ad69 100755
--- a/kdumpctl
+++ b/kdumpctl
@@ -33,8 +33,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
@@ -1727,11 +1733,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
@@ -1804,6 +1805,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
--
2.34.1