1#!/bin/sh 2# 3# Command library that provides a boilerplate set of functions and variables 4# required for all bourne shell based scripts. 5# 6# Copyright (C) 2009, Cisco Systems Inc. 7# 8# This program is free software; you can redistribute it and/or modify 9# it under the terms of the GNU General Public License as published by 10# the Free Software Foundation; either version 2 of the License, or 11# (at your option) any later version. 12# 13# This program is distributed in the hope that it will be useful, 14# but WITHOUT ANY WARRANTY; without even the implied warranty of 15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16# GNU General Public License for more details. 17# 18# You should have received a copy of the GNU General Public License along 19# with this program; if not, write to the Free Software Foundation, Inc., 20# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21# 22# Ngie Cooper, August 2009 23# 24 25set -u 26 27export SHELL_DEBUG=${SHELL_DEBUG:=0} 28if [ "x$SHELL_DEBUG" = x1 ] ; then 29 set -x 30fi 31 32#============================================================================= 33# FUNCTION: tst_cleanup 34# PURPOSE: Clean up after a testcase. 35#============================================================================= 36tst_cleanup() 37{ 38 # Disable the trap EXIT handler. 39 trap '' EXIT 40 # To ensure set -u passes... 41 TCtmp=${TCtmp:=} 42 tst_resm TINFO "Cleaning up." 43 # Nuke the testcase temporary directory if it exists. 44 [ -d "$TCtmp" ] && rm -rf "$TCtmp" 45} 46 47#============================================================================= 48# FUNCTION: setup 49# PURPOSE: Setup the test environment. 50#============================================================================= 51tst_setup() 52{ 53 54 TST_COUNT=1 55 TST_TOTAL=${TST_TOTAL:=1} 56 export TCID TST_COUNT TST_TOTAL 57 58 for varname in TST_TOTAL; do 59 if eval "test -z \"\$${varname}\""; then 60 end_testcase "You must set ${varname} before calling setup()." 61 fi 62 done 63 64 LTPROOT=${LTPROOT:="../../../../"} 65 TEMPDIR=${TEMPDIR:=/tmp} 66 67 TCtmp=${TCtmp:=$TEMPDIR/$TC$$} 68 # User wants a temporary sandbox to play with. 69 if [ -n "$TCtmp" -a "$TCtmp" != "$TEMPDIR/$$" ] ; then 70 test -d "$TCtmp" || mkdir -p "$TCtmp" 71 # Clean up on exit. 72 trap tst_cleanup EXIT 73 fi 74 75} 76 77#============================================================================= 78# FUNCTION NAME: end_testcase 79# 80# FUNCTION DESCRIPTION: Print out whether or not a test failed. Do not use 81# this when TBROK messages should be applied. 82# 83# PARAMETERS: Failure message, or "" / unset if passed. 84# 85# RETURNS: None. 86#============================================================================= 87end_testcase() 88{ 89 if [ $# -eq 0 ]; then 90 tst_resm TPASS "Test successful" 91 exit 0 92 else 93 tst_resm TFAIL "Test broken: $*" 94 exit 1 95 fi 96} 97 98#============================================================================= 99# FUNCTION: exists 100# PURPOSE: Check if command(s) used by this test script exist. 101#============================================================================= 102exists() 103{ 104 for cmd in $*; do 105 if ! command -v $cmd >/dev/null 2>&1; then 106 end_testcase "$cmd: command not found" 107 exit 1 108 fi 109 done 110} 111 112incr_tst_count() 113{ 114 : $(( TST_COUNT += 1 )) 115} 116 117tst_require_root() 118{ 119 if [ "x$(id -u)" != "x0" ]; then 120 tst_resm TCONF "You must be root to execute this test" 121 exit 0 122 fi 123} 124 125# 126# $0 is maintained by the caller script; tested on FreeBSD (ash) and Gentoo 127# GNU/Linux (bash) -- assuming you aren't calling this from another function 128# or command: 129# 130# foo.sh: 131# echo ${0##*/} 132# . ${$0%%*}/bar.sh 133# bar.sh: 134# echo ${0##*/} 135# echo $SHELL 136# 137# Gentoo: 138# gcooper@orangebox ~/Desktop $ ./foo.sh 139# foo.sh 140# foo.sh 141# /bin/bash 142# gcooper@orangebox ~/Desktop $ uname -sr 143# Linux 2.6.29-gentoo-r5 144# 145# FreeBSD: 146# $ ./foo.sh 147# foo.sh 148# foo.sh 149# /bin/sh 150# $ uname -sr 151# FreeBSD 8.0-BETA2 152# 153TCID=${TCID:=} 154[ -z "$TCID" ] && TCID=${0##*/} 155TC=$(echo "$TCID" | awk '{ sub( /[0-9]+$/,""); print; }') 156 157. daemonlib.sh 158