Blame tests/0_lib/functions.sh

Karanbir Singh 2dd0be
#!/bin/bash
Karanbir Singh 2dd0be
Christoph Galuschka b3d99b
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
a78f80
	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: $*"
Christoph Galuschka b3d99b
	/usr/bin/yum -y -d${YUMDEBUG} install "$@"
Karanbir Singh ba03ff
  # TODO: add a hook here, to make sure all binary files have ldd run
Karanbir Singh ba03ff
  # against them, and that there are no missing linker targets
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 _
iaind 79f00f
		[[ "$(basename ${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
Karanbir Singh ba03ff
#   the dust settle. Using this function avoids a race condition wherein 
Karanbir Singh ba03ff
#   subsequent tests execute (and typically fail) before a service has had a 
Karanbir Singh ba03ff
#   chance to fully start/open a network port etc.
Karanbir Singh ba03ff
# Call it with cycle instead of start, and it will stop+start
Karanbir Singh ba03ff
#   handy, if you dont know the service might already be running
Steve Barnes 464547
function t_ServiceControl
Steve Barnes 464547
{
Karanbir Singh b6d758
  if [ $2 = "cycle" ]; then
Karanbir Singh ba03ff
    /sbin/service $1 stop > /dev/null 2>&1
Karanbir Singh ba03ff
    sleep 3
Karanbir Singh ba03ff
    /sbin/service $1 start
Karanbir Singh ba03ff
  else
Karanbir Singh ba03ff
  	/sbin/service $1 $2
Karanbir Singh ba03ff
  fi
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
{
Christoph Galuschka 7ec557
	rpm -q $(rpm -qf /etc/redhat-release) --queryformat '%{version}\n'|cut -f 1 -d '.'
eefa9e
}
Christoph Galuschka a95f5f
# Additionally set distro release to $centos_ver
Christoph Galuschka 6810bd
centos_ver=$(t_DistCheck)
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
{
dc928e
	rpm -q $(rpm -qf /etc/redhat-release) --queryformat '%{arch}\n'
fbf041
}
fbf041
a17f9b
# Set the arch
a17f9b
arch=$(t_GetArch)
a17f9b
c86d2c
function t_CheckForPort
c86d2c
{
c86d2c
        while true
c86d2c
        do
c86d2c
        sleep 1
c86d2c
          >/dev/null 2>&1 >/dev/tcp/localhost/$1
c86d2c
        if [ "$?" = "0" ] ; then
c86d2c
          t_Log "Waiting for tcp port $1 to be listening ..."
c86d2c
          break
c86d2c
        fi
c86d2c
        done
c86d2c
c86d2c
}
c86d2c
Ranjib Dey da7c39
function t_Assert
Ranjib Dey da7c39
{
Ranjib Dey da7c39
 $@ >/dev/null 2>&1
Ranjib Dey da7c39
 t_CheckExitStatus $?
Ranjib Dey da7c39
}
Ranjib Dey da7c39
root a59260
function t_Assert_Equals
root a59260
{
root a59260
 [ $1 -eq $2 ] 
root a59260
 t_CheckExitStatus $?
root a59260
}
e52a98
function t_Select_Alternative
e52a98
{
e52a98
	name=$1
e52a98
	search=$2
e52a98
	option=$(/bin/echo|/usr/sbin/alternatives --config "$name"|/bin/grep -E "$search"|/usr/bin/head -n1|sed 's/    .*//g;s/[^0-9]//g')
e52a98
	if [ -z "$option" ];then
e52a98
		t_Log "Option not found for altenative $search of $name"
e52a98
		t_CheckExitStatus 1
e52a98
	fi
e52a98
	t_Log "Selecing alternative $option for $name--$search"
e52a98
	/bin/echo "$option"|/usr/sbin/alternatives --config "$name" >/dev/null 2>&1
e52a98
}
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
c86d2c
export -f t_CheckForPort
Ranjib Dey da7c39
export -f t_Assert
root a59260
export -f t_Assert_Equals
e52a98
export -f t_Select_Alternative
Christoph Galuschka 89cd74
export centos_ver
a17f9b
export arch