#!/bin/sh

# This file is AUTO GENERATED, do not modify it or your changes might be
# lost!
#
# shell test suite v0.5 for kdb command


#
# VARIABLES
#

# variables to count up errors and tests
nbError=0
nbTest=0
nbSkip=0

# the script itself
scriptName=`basename $0`

MOUNTPOINT="/tests/script"

NAMESPACES="user system"

USER_ROOT="user/tests/script"
SYSTEM_ROOT="system/tests/script"

ROOTS="$USER_ROOT $SYSTEM_ROOT"

IS_INSTALLED=YES
if [ "x$IS_INSTALLED" = "xYES" ]
then
	DATADIR="/usr/share/elektra/test_data/shell"
else
	DATADIR="/home/abuild/rpmbuild/BUILD/elektra-160630.175244gita0ee10f3/tests/shell/shell"
fi

USER="`id -un`"
GROUP="`id -gn`"

SYSTEM_FOLDER=/etc/kdb
if [ -n "$HOME" ]
then
	USER_FOLDER=$HOME/.config
else
	USER_FOLDER=/home/$USER/.config
fi
PLUGINS=`echo "cachefilter;ccode;conditionals;constants;counter;csvstorage;dbus;doc;dpkg;dump;enum;error;filecheck;fstab;glob;hexcode;hidden;hosts;iconv;ini;iterate;keytometa;line;lineendings;list;logchange;mathcheck;network;ni;noresolver;null;path;profile;regexstore;rename;resolver_fm_b_b;resolver_fm_hb_b;resolver_fm_hp_b;resolver_fm_hpu_b;resolver_fm_pb_b;resolver_fm_ub_x;resolver_fm_uhb_xb;resolver_fm_xb_x;resolver_fm_xhp_x;resolver_fm_xp_x;semlock;shell;simpleini;spec;struct;sync;syslog;tcl;template;timeofday;tracer;type;uname;validation;wresolver;xmltool;yajl" | tr ';' ' '`
PLUGINS_NEWLINES=`echo "cachefilter;ccode;conditionals;constants;counter;csvstorage;dbus;doc;dpkg;dump;enum;error;filecheck;fstab;glob;hexcode;hidden;hosts;iconv;ini;iterate;keytometa;line;lineendings;list;logchange;mathcheck;network;ni;noresolver;null;path;profile;regexstore;rename;resolver_fm_b_b;resolver_fm_hb_b;resolver_fm_hp_b;resolver_fm_hpu_b;resolver_fm_pb_b;resolver_fm_ub_x;resolver_fm_uhb_xb;resolver_fm_xb_x;resolver_fm_xhp_x;resolver_fm_xp_x;semlock;shell;simpleini;spec;struct;sync;syslog;tcl;template;timeofday;tracer;type;uname;validation;wresolver;xmltool;yajl" | tr ';' '\n'`

DATE=`date "+%b %d %H:%M"`

KDB_DEFAULT_STORAGE="dump"
KDB_DEFAULT_RESOLVER="resolver_fm_hpu_b"

KDB_GETENV=`kdb elektrify-getenv getenv KDB 2> /dev/null`

if [ -x "${KDB_GETENV}" ]
then
	KDB=${KDB_GETENV};
fi

#
# ASSERTIONS
#

#succeed if the previous command was successful
succeed_if ()
{
	if [ $? != "0" ]
	then
		nbError=$(( $nbError + 1 ))
		echo error: $*
	fi
	nbTest=$(( $nbTest + 1 ))
}

#fails and exits the program if the previous command failed
exit_if_fail ()
{
	if [ $? != "0" ]
	then
		if command -v cleanup > /dev/null
		then
			echo "fatal: $*"
			cleanup
		else
			echo "fatal (no cleanup): $*"
		fi
		exit 1
	fi
	nbTest=$(( $nbTest + 1 ))
}

end_script()
{
	if command -v cleanup > /dev/null
	then
		cleanup
	fi

	echo -n "$scriptName RESULTS: $nbTest test(s) done"
	if [ $nbSkip -ne 0 ]
	then
		echo -n " $nbSkip skipped"
	fi
	echo " $nbError error(s)."
	exit "$nbError"
}


#
# COMMON CHECKS
#
KDB_VERSION="0.8.17"
SO_VERSION="4"

check_version()
{
	echo "Check if script tests the correct version"

	if [ "x`kdb elektrify-getenv getenv CHECK_VERSION 2> /dev/null`" = "xNO" ]
	then
		return 0
	fi

	#if no kdb elektrify-getenv getenv is installed:
	if [ "x$CHECK_VERSION" = "xNO" ]
	then
		return 0
	fi

	REAL_KDB_VERSION="`"$KDB" get system/elektra/version/constants/KDB_VERSION 2> /dev/null`"
	[ "x$REAL_KDB_VERSION" = "x$KDB_VERSION" ]
	exit_if_fail "Script was not compiled ($KDB_VERSION) with this elektra version ($REAL_KDB_VERSION): KDB_VERSION mismatch use CHECK_VERSION=NO to disable"

	REAL_SO_VERSION="`"$KDB" get system/elektra/version/constants/SO_VERSION 2> /dev/null`"
	[ "x$REAL_SO_VERSION" = "x$SO_VERSION" ]
	exit_if_fail "Script was not compiled ($SO_VERSION) with this elektra version ($REAL_SO_VERSION): SO_VERSION mismatch"
}

check_remaining_files()
{
	USER_REMAINING="`find $USER_FOLDER -maxdepth 1 -name $1'*' -print -quit`"
	test -z "$USER_REMAINING"
	exit_if_fail "files $USER_REMAINING in $USER_FOLDER would be removed during tests, so test is aborted"

	SYSTEM_REMAINING="`find $SYSTEM_FOLDER -maxdepth 1 -name $1'*' -print -quit`"
	test -z "$SYSTEM_REMAINING"
	exit_if_fail "files $SYSTEM_REMAINING in $SYSTEM_FOLDER would be removed during tests, so test is aborted"
}

check_set_rm()
{
	"$KDB" set "$1" "$2" 1>/dev/null
	succeed_if "could not set $1 with $2"

	[ "x`"$KDB" get "$1" 2> /dev/null`" = "x$2" ]
	succeed_if "cant get $1 (expected $2)"

	"$KDB" rm "$1" 1>/dev/null
	succeed_if "could not rm $1 (having value $2)"

	[ "x`"$KDB" sget $1 defvalue 2> /dev/null`" = "xdefvalue" ]
	succeed_if "Value still there after remove"
}


check_set_mv_rm()
{
	"$KDB" set "$1" "$3" 1>/dev/null
	succeed_if "could not set $1 with $3"

	[ "x`"$KDB" get "$1" 2> /dev/null`" = "x$3" ]
	succeed_if "cant get $1 (expected $3)"

	"$KDB" mv "$1" "$2" 1>/dev/null
	succeed_if "could not mv $1 to $2"

	[ "x`"$KDB" sget $1 defvalue 2> /dev/null`" = "xdefvalue" ]
	succeed_if "Value still there after move"

	[ "x`"$KDB" get "$2" 2> /dev/null`" = "x$3" ]
	succeed_if "cant get $2 (expected $3)"

	"$KDB" rm "$2" 1>/dev/null
	succeed_if "could not rm $2 (having value $3)"

	[ "x`"$KDB" sget $2 defvalue 2> /dev/null`" = "xdefvalue" ]
	succeed_if "Value still there after remove"
}



#
# COMMON UTILITIES
#


is_not_rw_storage()
{
	test ! "x`"$KDB" info $PLUGIN provides 2>/dev/null`" = "xstorage" \
	-o "x$PLUGIN" = "xhosts" \
	-o "x$PLUGIN" = "xfstab" \
	-o "x$PLUGIN" = "xline" \
	-o "x$PLUGIN" = "xuname" \
	-o "x$PLUGIN" = "xconstants" \
	-o "x$PLUGIN" = "xaugeas" \
	-o "x$PLUGIN" = "xcsvstorage" \
	-o "x$PLUGIN" = "xdpkg" \
	-o "x$PLUGIN" = "xregexstore"
}

is_plugin_available()
{
	for PLUGIN in $PLUGINS
	do
		if [ "x$1" = "x$PLUGIN" ]
		then
			return 0
		fi
	done
	return 1
}

mktempdir_elektra()
{
	mktemp -d 2>/dev/null || mktemp -d -t 'libelektra-test'
}

mktempfile_elektra()
{
	mktemp -t elektraenv.XXXXXXXXX 2>/dev/null || mktemp -t 'elektraenv'
}































































































































































































































































































































































































































































































































































































































































































































































































# empty lines up to 1000 so that line numbers are more useful
if [ -z "$KDB" ]; then KDB=kdb; fi

echo
echo ELEKTRA MOUNT TESTS
echo

check_version

echo "Test basic mounting"

ROOT_FILE=${FILE_SUFFIX}_root.ecf
ROOT_MOUNTPOINT=/test/script/mount
ROOT_MOUNTPOINTN=\\/test\\/script\\/mount
ROOT_MOUNTPOINT2=/test/script/remount
ROOT_MOUNTPOINT2N=\\/test\\/script\\/remount

if is_plugin_available dump
then
	"$KDB" mount $ROOT_FILE $ROOT_MOUNTPOINT dump 1>/dev/null
	succeed_if "could not mount root: $ROOT_FILE at $ROOT_MOUNTPOINT"

	"$KDB" umount $ROOT_MOUNTPOINT
	succeed_if "could not unmount previously mounted mountpoint"
fi

if is_plugin_available tracer
then
	if is_plugin_available sync
	then
		"$KDB" mount $ROOT_FILE $ROOT_MOUNTPOINT tracer sync 1>/dev/null 2>/dev/null
		[ $? != 0 ]
		succeed_if "could mount the backend, even though tracer+sync conflict"

		"$KDB" umount $ROOT_MOUNTPOINT
		[ $? != 0 ]
		succeed_if "could unmount what should not be mounted"
	fi
fi

echo "Test mounting plugin stack"

if is_plugin_available hosts
then
	if is_plugin_available glob
	then
		"$KDB" mount $ROOT_FILE $ROOT_MOUNTPOINT glob hosts 1>/dev/null
		succeed_if "could not mount glob and hosts plugin together"

		"$KDB" mount $ROOT_FILE $ROOT_MOUNTPOINT glob hosts 1>/dev/null 2>/dev/null
		[ $? != 0 ]
		succeed_if "could remount the same backend"

		"$KDB" mount $ROOT_FILE dir$ROOT_MOUNTPOINT glob hosts 1>/dev/null 2>/dev/null
		[ $? != 0 ]
		succeed_if "could remount the dir backend, even though cascading already mounted"

		"$KDB" mount $ROOT_FILE user$ROOT_MOUNTPOINT glob hosts 1>/dev/null 2>/dev/null
		[ $? != 0 ]
		succeed_if "could remount the user backend, even though cascading already mounted"

		"$KDB" mount $ROOT_FILE system$ROOT_MOUNTPOINT glob hosts 1>/dev/null 2>/dev/null
		[ $? != 0 ]
		succeed_if "could remount the system backend, even though cascading already mounted"

		"$KDB" umount $ROOT_MOUNTPOINT
		succeed_if "could not unmount previously mounted mountpoint"

		echo "Test simple mount configuration"

		"$KDB" mount -c "test1=testvalue1" $ROOT_FILE $ROOT_MOUNTPOINT glob "test1=testvalue1" hosts 1>/dev/null
		succeed_if "could not mount glob and hosts plugin together"

		#"$KDB" ls "system/elektra/mountpoints/$ROOT_MOUNTPOINTN"

		# TODO: check correcly + reenable
		configvalue=$("$KDB" get "system/elektra/mountpoints/$ROOT_MOUNTPOINTN/config/test1")
		test "$configvalue" = "testvalue1"
		succeed_if "config key was not set correctly"

		"$KDB" umount $ROOT_MOUNTPOINT
		succeed_if "could not unmount previously mounted mountpoint"

		echo "Test multiple mount configurations"

		"$KDB" mount -c "test1=testvalue1,test2=test value2" $ROOT_FILE $ROOT_MOUNTPOINT glob "test1=testvalue1" hosts "test2=test value2" 1>/dev/null
		succeed_if "could not mount glob and hosts plugin together"

		# TODO: reenable
		configvalue=$("$KDB" get "system/elektra/mountpoints/$ROOT_MOUNTPOINTN/config/test2")
		test "$configvalue" = "test value2"
		succeed_if "config key was not set correctly"

		echo "Test remounting the existing mount"

		"$KDB" remount "testfile" $ROOT_MOUNTPOINT2 $ROOT_MOUNTPOINT
		succeed_if "could not remount previous mountpoint"

		#$KDB ls "system/elektra/mountpoints"
		#$KDB mount

		"$KDB" umount $ROOT_MOUNTPOINT
		succeed_if "could not unmount previously mounted mountpoint"

		"$KDB" ls "system/elektra/mountpoints/$ROOT_MOUNTPOINT2N/" | grep glob 1> /dev/null
		succeed_if "glob plugin does not exist in the remounted mountpoint"

		"$KDB" ls "system/elektra/mountpoints/$ROOT_MOUNTPOINT2N/" | grep hosts 1> /dev/null
		succeed_if "hosts plugin does not exist in the remounted mountpoint"

		# TODO: reenable
		#configvalue=$($KDB get "system/elektra/mountpoints/$ROOT_MOUNTPOINT2N/config/test2")
		#test "$configvalue" = "test value2"
		#succeed_if "config key was not copied correctly"

		#configvalue=$($KDB get "system/elektra/mountpoints/$ROOT_MOUNTPOINT2N/config/path")
		#test "$configvalue" = "testfile"
		#succeed_if "path was not set correctly"

		echo "Testing unmount via path"

		"$KDB" umount $ROOT_MOUNTPOINT2
		succeed_if "unable to unmount $ROOT_MOUNTPOINT2"

	fi
fi

end_script
