Blame tests/0_lib/functions.sh

Karanbir Singh 2dd0be
#!/bin/bash
Karanbir Singh 2dd0be
Grip Firmly 44e7f7
# Human friendly symbols
Grip Firmly 44e7f7
export readonly PASS=0
Grip Firmly 44e7f7
export readonly FAIL=1
Grip Firmly 44e7f7
Steve Barnes e801dc
# Description: call this function whenever you need to log output (preferred to calling echo)
Steve Barnes e801dc
# Arguments: log string to display
Karanbir Singh 2dd0be
function t_Log
Karanbir Singh 2dd0be
{
Steve Barnes e801dc
	printf "[+] `date` -> $*\n"
Karanbir Singh 2dd0be
}
Karanbir Singh 2dd0be
Steve Barnes e801dc
# Description: call this at the end of your script to assess the exit status
Steve Barnes e801dc
# Arguments: the exit status from whatever you want checked (ie, '$?')
Karanbir Singh 2dd0be
function t_CheckExitStatus
Karanbir Singh 2dd0be
{
Steve Barnes e801dc
	[ $1 -eq 0 ] && { t_Log "PASS"; return $PASS; }
Karanbir Singh 2dd0be
Steve Barnes e801dc
	t_Log "FAIL"
Steve Barnes e801dc
	exit $FAIL
Karanbir Singh 2dd0be
}
Karanbir Singh 2dd0be
Steve Barnes e801dc
# Description: call this to perform yum-based installs of packages
Steve Barnes e801dc
# Arguments: a space separated list of package names to install.
Karanbir Singh 2dd0be
function t_InstallPackage
Karanbir Singh 2dd0be
{
Steve Barnes e801dc
	t_Log "Attempting yum install: $*"
Steve Barnes e801dc
	/usr/bin/yum -y -d0 install "$@"
Steve Barnes e801dc
	t_CheckExitStatus $?
Karanbir Singh 2dd0be
}
Karanbir Singh 2dd0be
Steve Barnes e801dc
# Description: call this to perform a yum-based removal of packages
Steve Barnes e801dc
# Arguments: a space separated list of package names to remove.
Karanbir Singh 2dd0be
function t_RemovePackage
Karanbir Singh 2dd0be
{
Steve Barnes e801dc
	t_Log "Attempting yum remove: $*"
Steve Barnes e801dc
	/usr/bin/yum -y -d0 remove "$@"
Steve Barnes e801dc
	t_CheckExitStatus $?
Karanbir Singh 2dd0be
}
Karanbir Singh 2dd0be
Steve Barnes e801dc
# Description: call this to process a list of folders containing test scripts
Grip Firmly 44e7f7
# Arguments: a file handle from which to read the names of paths to process.
Steve Barnes 464547
function t_Process
Karanbir Singh 2dd0be
{
Steve Barnes 464547
	exec 7< $@
Steve Barnes 464547
	
Steve Barnes 464547
	while read -u 7 f
Steve Barnes e801dc
	do
Steve Barnes e801dc
		# skip files named readme or those that start with an _
Steve Barnes e801dc
		[[ "${f}" =~ readme|^_ ]] &&  continue;
Steve Barnes e801dc
		
Steve Barnes 464547
		# handy tip: chmod -x to disable individual test scripts.
Steve Barnes e801dc
		[ -x ${f} ] && ${f}
Steve Barnes e801dc
			
Steve Barnes 464547
	done
Steve Barnes e801dc
Steve Barnes e801dc
	return 0
Steve Barnes e801dc
}
Steve Barnes e801dc
Steve Barnes e801dc
# Description: check to see if one or more packages are installed
Steve Barnes e801dc
# return true if they're all installed, false if not.
Steve Barnes e801dc
# Arguments: one or more package names to check for.
Steve Barnes e801dc
function t_CheckDeps
Steve Barnes e801dc
{
Steve Barnes e801dc
	# TODO
Steve Barnes e801dc
	
Steve Barnes e801dc
	# success, all packages are installed
Steve Barnes e801dc
	return 0
Karanbir Singh 2dd0be
}
Karanbir Singh 2dd0be
Steve Barnes 464547
# Description: perform a service control and sleep for a few seconds to let
Grip Firmly 44e7f7
# the dust settle. Using this function avoids a race condition wherein 
Grip Firmly 44e7f7
# subsequent tests execute (and typically fail) before a service has had a 
Grip Firmly 44e7f7
# chance to fully start/open a network port etc.
Steve Barnes 464547
function t_ServiceControl
Steve Barnes 464547
{
Steve Barnes 464547
	/sbin/service $1 $2
Steve Barnes 464547
Steve Barnes 464547
	# aaaand relax...
Steve Barnes 464547
	sleep 3
Steve Barnes 464547
}
Steve Barnes 464547
Athmane Madjoudj 0a3c81
# Description: Get a package (rpm) release number
Athmane Madjoudj 0a3c81
function t_GetPkgRel
Athmane Madjoudj 0a3c81
{
Athmane Madjoudj 0a3c81
       rpm -q --queryformat '%{RELEASE}' $1 
Athmane Madjoudj 0a3c81
}
Athmane Madjoudj 0a3c81
eefa9e
# Description: return the distro release (returns 5 or 6 now)
eefa9e
function t_DistCheck
eefa9e
{
eefa9e
	rpm -q --queryformat '%{version}\n' centos-release
eefa9e
}
eefa9e
Athmane Madjoudj 549b58
# Description: Get a package (rpm) version number
Athmane Madjoudj 549b58
function t_GetPkgVer
Athmane Madjoudj 549b58
{
Athmane Madjoudj 549b58
       rpm -q --queryformat '%{version}' $1 
Athmane Madjoudj 549b58
}
Athmane Madjoudj 549b58
fbf041
# Description: get the arch 
ccb3fa
function t_GetArch
ccb3fa
{
fbf041
	rpm -q --queryformat '%{arch}\n' centos-release
fbf041
}
fbf041
Karanbir Singh 2dd0be
export -f t_Log
Karanbir Singh 2dd0be
export -f t_CheckExitStatus
Karanbir Singh 2dd0be
export -f t_InstallPackage
Karanbir Singh 2dd0be
export -f t_RemovePackage
Steve Barnes 464547
export -f t_Process
Steve Barnes e801dc
export -f t_CheckDeps
Steve Barnes 464547
export -f t_ServiceControl
Athmane Madjoudj 0a3c81
export -f t_GetPkgRel
eefa9e
export -f t_DistCheck
Athmane Madjoudj 549b58
export -f t_GetPkgVer
fbf041
export -f t_GetArch