[Dbconfig-common-changes] [dbconfig-common] r433 - in trunk/test: . data scripts

Sean Finney seanius at alioth.debian.org
Sun Mar 30 18:29:48 UTC 2008


Author: seanius
Date: 2008-03-30 18:29:48 +0000 (Sun, 30 Mar 2008)
New Revision: 433

Added:
   trunk/test/data/dbc_mysql_check_database.exists.stdout.txt
   trunk/test/data/dbc_mysql_check_database.missing.stdout.txt
   trunk/test/data/dbc_mysql_exec_file.nodb.txt
   trunk/test/data/dbc_mysql_exec_file.withdb.txt
   trunk/test/data/generate_mycnf.txt
   trunk/test/data/mysql_check_connect.txt
   trunk/test/data/mysql_check_connect.txt.old
   trunk/test/data/mysql_exec_command.sql.txt
   trunk/test/functions
   trunk/test/mockdb.ts
   trunk/test/scripts/mockup
   trunk/test/scripts/mysql
Modified:
   trunk/test/basic.ts
   trunk/test/runtests.sh
Log:
another batch of work done on unit testing scripts

Modified: trunk/test/basic.ts
===================================================================
--- trunk/test/basic.ts	2008-03-29 17:38:28 UTC (rev 432)
+++ trunk/test/basic.ts	2008-03-30 18:29:48 UTC (rev 433)
@@ -1,76 +1,43 @@
 #!/bin/sh 
 
-oneTimeSetUp(){
-	local curdir basedir
-	curdir="`pwd`"
-	basedir="`dirname $0`"
-	# make sure we're called from the test dir, even if we weren't
-	if [ "$curdir" != "$basedir" ]; then
-		cd "$basedir"
-		exec "./`basename $0`"
-	fi
-	# override the logfile location
-	_dbc_logfile="`pwd`/dbc.log"
-	mkdir -p ./tmp
-	. ../internal/common
-}
+. `dirname $0`/functions
+. ${_dbc_root}/internal/common
 
-assertEqualFiles(){
-	local msg outfile errfile
-	outfile=./tmp/assertEqualFiles.stdout
-	errfile=./tmp/assertEqualFiles.stderr
-	if [ "$#" -eq 3 ]; then
-		msg="$1"
-		shift
-	else
-		msg="Files not equal"
-	fi
-	diff -u "$1" "$2" > $outfile 2>$errfile
-	assertTrue "$msg" "[ $? -eq 0 ]"
-	if [ -s "$errfile" ]; then
-		cat $errfile
-	fi
-	if [ -s "$outfile" ]; then
-		cat $outfile
-	fi
-}
-
-setUp(){
-	find ./tmp -type f -print0 | xargs --no-run-if-empty -0 rm
-	cat /dev/null > $_dbc_logfile
-}
-
 test_dbc_logline(){
 	local output
 
 	# test echoing one line to user and logfile
 	output=`dbc_logline foo 2>&1 >/dev/null`
+	assertTrue "dbc_logline foo returned error" $?
 	assertEquals foo. "$output"
-	assertEqualFiles $_dbc_logfile ./data/dbc_logline.simple.txt
+	assertFilesEqual $_dbc_logfile ./data/dbc_logline.simple.txt
 
 	# test that a second line is logged appropriately
 	dbc_logline bar >/dev/null 2>&1
-	assertEqualFiles $_dbc_logfile ./data/dbc_logline.twolines.txt
+	assertFilesEqual $_dbc_logfile ./data/dbc_logline.twolines.txt
 
 	# test that multiple arguments are logged appropriately
 	cat /dev/null > $_dbc_logfile
 	output=`dbc_logline foo bar 2>&1 >/dev/null`
+	assertTrue "dbc_logline foo bar returned error" $?
 	assertEquals "foo bar." "$output"
-	assertEqualFiles $_dbc_logfile ./data/dbc_logline.twowords.txt
+	assertFilesEqual $_dbc_logfile ./data/dbc_logline.twowords.txt
 
 	# assure that quoting and escaping are handled properly
 	output=`dbc_logline "\"foobar's\ttab\"" 2>&1 >/dev/null`
+	assertTrue "dbc_logline (with escaping) returned error" $?
 	assertEquals "\"foobar's	tab\"." "$output"
 }
 
 test_dbc_logpart(){
 	# make sure it properly logs partial lines
 	dbc_logpart foo >/dev/null 2>&1
-	assertEqualFiles $_dbc_logfile ./data/dbc_logpart.noeol.txt
+	assertTrue "dbc_logpart foo returned error" $?
+	assertFilesEqual $_dbc_logfile ./data/dbc_logpart.noeol.txt
 
 	# ...and that it completes them with the next logline.
 	dbc_logline bar >/dev/null 2>&1
-	assertEqualFiles $_dbc_logfile ./data/dbc_logline.twowords.txt
+	assertFilesEqual $_dbc_logfile ./data/dbc_logline.twowords.txt
 }
 
 test_dbc_debug(){
@@ -80,23 +47,32 @@
 	# make sure it doesn't do anything by default
 	dbc_debug=""
 	_dbc_debug foo bar >/dev/null 2>&1
-	assertEqualFiles $_dbc_logfile /dev/null
+	assertTrue "_dbc_debug foo bar returned error" $?
+	assertFilesEqual $_dbc_logfile /dev/null
 
 	# ...and that it completes them with the next logline.
 	dbc_debug=1
 	_dbc_debug foo bar >/dev/null 2>&1
-	assertEqualFiles $_dbc_logfile ./data/dbc_logline.twowords.txt
+	assertTrue "_dbc_debug foo bar returned error" $?
+	assertFilesEqual $_dbc_logfile ./data/dbc_logline.twowords.txt
 
 	dbc_debug=$olddebug
 }
 
 test_dbc_mktemp(){
 	local t
+	# try making a temp file w/out arguments
 	t=`dbc_mktemp`
-	assertTrue "file $t nonexistant/nonempty" "[ -f '$t' ] && [ ! -s '$t' ]"
+	assertTrue "dbc_mktemp (no args) failed" $?
+	assertTrue "dbc_mktemp (no args) file $t nonexistant" '[ -f "$t" ]'
+	assertTrue "dbc_mktemp (no args) file $t nonempty" '[ ! -s "$t" ]'
 	rm -f "$t"
+
+	# try making a temp file with arguments
 	t=`dbc_mktemp foo.XXXXXX`
-	assertTrue "file $t nonexistant/nonempty" "[ -f '$t' ] && [ ! -s '$t' ]"
+	assertTrue "dbc_mktemp (no args) failed" $?
+	assertTrue "dbc_mktemp (no args) file $t nonexistant" '[ -f "$t" ]'
+	assertTrue "dbc_mktemp (no args) file $t nonempty" '[ ! -s "$t" ]'
 	rm -f "$t"
 }
 

Added: trunk/test/data/dbc_mysql_check_database.exists.stdout.txt
===================================================================
--- trunk/test/data/dbc_mysql_check_database.exists.stdout.txt	                        (rev 0)
+++ trunk/test/data/dbc_mysql_check_database.exists.stdout.txt	2008-03-30 18:29:48 UTC (rev 433)
@@ -0,0 +1,6 @@
+Database
+foo
+bar
+farsar
+testdbname
+someotherdb

Added: trunk/test/data/dbc_mysql_check_database.missing.stdout.txt
===================================================================
--- trunk/test/data/dbc_mysql_check_database.missing.stdout.txt	                        (rev 0)
+++ trunk/test/data/dbc_mysql_check_database.missing.stdout.txt	2008-03-30 18:29:48 UTC (rev 433)
@@ -0,0 +1,6 @@
+Database
+foo
+bar
+farsar
+nottestdbname
+someotherdb

Added: trunk/test/data/dbc_mysql_exec_file.nodb.txt
===================================================================
--- trunk/test/data/dbc_mysql_exec_file.nodb.txt	                        (rev 0)
+++ trunk/test/data/dbc_mysql_exec_file.nodb.txt	2008-03-30 18:29:48 UTC (rev 433)
@@ -0,0 +1 @@
+'mysql' '--defaults-extra-file=<TMPDIR>/dbconfig-common_my.cnf.XXXXXX'

Added: trunk/test/data/dbc_mysql_exec_file.withdb.txt
===================================================================
--- trunk/test/data/dbc_mysql_exec_file.withdb.txt	                        (rev 0)
+++ trunk/test/data/dbc_mysql_exec_file.withdb.txt	2008-03-30 18:29:48 UTC (rev 433)
@@ -0,0 +1 @@
+'mysql' '--defaults-extra-file=<TMPDIR>/dbconfig-common_my.cnf.XXXXXX' 'testdbname'

Added: trunk/test/data/generate_mycnf.txt
===================================================================
--- trunk/test/data/generate_mycnf.txt	                        (rev 0)
+++ trunk/test/data/generate_mycnf.txt	2008-03-30 18:29:48 UTC (rev 433)
@@ -0,0 +1,20 @@
+# temporary my.cnf generated for usage by dbconfig-common 
+# generated on Sat Mar 29 16:29:44 CET 2008
+# if you're reading this, it probably means something went wrong and
+# for some strange reason dbconfig-common was not able to clean up after itself.
+# you can safely delete this file
+
+[client]
+user            = 'testadmin'
+password        = 'testadmpass'
+host            = 'testserver'
+port            = 'testport'
+socket    = '/var/run/mysqld/mysqld.sock'
+
+[mysqldump]
+user            = 'testadmin'
+password        = 'testadmpass'
+host            = 'testserver'
+port            = 'testport'
+socket    = '/var/run/mysqld/mysqld.sock'
+

Added: trunk/test/data/mysql_check_connect.txt
===================================================================
--- trunk/test/data/mysql_check_connect.txt	                        (rev 0)
+++ trunk/test/data/mysql_check_connect.txt	2008-03-30 18:29:48 UTC (rev 433)
@@ -0,0 +1 @@
+'mysql' '--defaults-extra-file=<TMPDIR>/dbconfig-common_my.cnf.XXXXXX'

Added: trunk/test/data/mysql_check_connect.txt.old
===================================================================
--- trunk/test/data/mysql_check_connect.txt.old	                        (rev 0)
+++ trunk/test/data/mysql_check_connect.txt.old	2008-03-30 18:29:48 UTC (rev 433)
@@ -0,0 +1 @@
+'mysql' '--defaults-extra-file=/home/sean/tmp/``it'\''s a '\''$funny\'\'' path!"/dbconfig-common_my.cnf.XXXXXX'

Added: trunk/test/data/mysql_exec_command.sql.txt
===================================================================
--- trunk/test/data/mysql_exec_command.sql.txt	                        (rev 0)
+++ trunk/test/data/mysql_exec_command.sql.txt	2008-03-30 18:29:48 UTC (rev 433)
@@ -0,0 +1 @@
+select * from foo where bar = 1

Added: trunk/test/functions
===================================================================
--- trunk/test/functions	                        (rev 0)
+++ trunk/test/functions	2008-03-30 18:29:48 UTC (rev 433)
@@ -0,0 +1,132 @@
+# override the installation root
+_dbc_root="`dirname \"$0\"`/.."
+
+# takes a single argument and escapes it literally in a fashion that
+# allows it to be used without further quoting
+escape(){
+	#echo "$1" | sed -e 's/\([$`\!]\)/\\\1/g'
+	echo "$1" | sed -e "s/'/'\\\''/g" -e "s/^/'/" -e "s/\$/'/"
+}
+
+dq_escape(){
+	echo "$1" | sed -e 's/\([$`\!]\)/\\\1/g'
+}
+
+# replaces any occurrence of a tmpfile made from a mktemp-like template
+# to the "template" form, i.e. foo.A5jfN4 -> foo.XXXXXX
+# also will call normalize_tmpdir on the logfile
+#
+# helpful for comparing testcase output that includes these testfiles
+subst_tmpfile(){
+	local template match logfile
+	template="$1"
+	logfile="$2"
+	match="`echo \"$template\" | sed -e 's/XXXXXX/....../'`"
+	sed -i -e "s,$match,$template,g" "$logfile"
+}
+
+assertFileExists(){
+	local f msg
+	if [ $# -eq 2 ]; then
+		msg="$1"
+		shift
+	fi
+	f="$1"
+	msg="${msg:-File $f does not exist}"
+
+	assertTrue "$msg" '[ -f "$f" ]'	
+}
+
+assertFileEmpty(){
+	local f msg
+	if [ $# -eq 2 ]; then
+		msg="$1"
+		shift
+	fi
+	f="$1"
+	msg="${msg:-File $f does not exist or is nonempty}"
+	[ -f "$f" ] && [ ! -s "$f" ]
+	ret=$?
+
+	assertTrue "$msg" $ret
+	[ $ret -eq 0 ] || head "$f" >&2
+}
+
+assertDirectoryEmpty(){
+	local d msg
+	if [ $# -eq 2 ]; then
+		msg="$1"
+		shift
+	fi
+	d="$1"
+	msg="${msg:-Directory $d is not empty}"
+
+	[ -d "$d" ] && [ -z "`ls \"$d\"`" ]
+	ret=$?
+	assertTrue "$msg" $ret
+	[ $ret -eq 0 ] || ls "$d" >&2
+}
+
+assertFilesEqual(){
+	local msg outfile errfile oldargs f1 f2
+	outfile=./tmp/assertFilesEqual.stdout
+	errfile=./tmp/assertFilesEqual.stderr
+	if [ $# -eq 3 ]; then
+		msg="$1"
+		shift
+	else
+		msg="Files not equal"
+	fi
+	f1=$1
+	f2=$2
+	eval set ${DIFF:-diff -u}
+	"$@" "$f1" "$f2" > $outfile 2>$errfile
+	assertTrue "$msg" "[ $? -eq 0 ]"
+	if [ -s "$errfile" ]; then
+		cat $errfile
+	fi
+	if [ -s "$outfile" ]; then
+		cat $outfile
+	fi
+}
+
+oneTimeSetUp(){
+    local curdir basedir
+    curdir="`pwd`"
+    basedir="`dirname $0`"
+	
+    # make sure we're called from the test dir, even if we weren't
+    if [ "$curdir" != "$basedir" ]; then
+        cd "$basedir"
+        exec "./`basename $0`"
+    fi
+
+	# set a really strange TMPDIR to see if it causes problems
+	export TMPDIR="${curdir}/tmp/\`\`it's a '\$funny\\' path!\""
+
+	# override the dbc logfile location
+	_dbc_logfile="${curdir}/tmp/dbc.log"
+
+	# specify where the mockup logs should go
+	mockup_logdir="${curdir}/tmp"
+	export mockup_cmdline="${mockup_logdir}/mockup.cmdline"
+	export mockup_errfile="${mockup_logdir}/mockup.errors"
+	export mockup_inlog="${mockup_logdir}/mockup.in.log"
+
+	# splice in the mockup script path
+	export ORIG_PATH=$PATH
+	export MOCKUP_PATH=${curdir}/scripts:$PATH
+	export PATH=$MOCKUP_PATH
+}
+
+setUp(){
+	rm -rf ./tmp; 
+	mkdir -p "$TMPDIR"
+	# for convenience, since we make a really ugly TMPDIR for tests
+	ln -s "$TMPDIR" ./tmp/tmpdir
+	touch $_dbc_logfile
+}
+
+tearDown(){ 
+	assertDirectoryEmpty "cruft files left behind" "$TMPDIR" 
+} 

Added: trunk/test/mockdb.ts
===================================================================
--- trunk/test/mockdb.ts	                        (rev 0)
+++ trunk/test/mockdb.ts	2008-03-30 18:29:48 UTC (rev 433)
@@ -0,0 +1,82 @@
+#!/bin/sh 
+
+dbc_dbname="testdbname"
+dbc_dbadmin="testadmin"
+dbc_dbadmpass="testadmpass"
+dbc_dbserver="testserver"
+dbc_dbport="testport"
+
+# get some common functions
+. `dirname "$0"`/functions
+. ${_dbc_root}/internal/mysql
+
+test_generate_mycnf(){
+	local cnf DIFF
+
+	DIFF='diff -b -u -I "^# generated on"'
+
+	cnf=`_dbc_generate_mycnf`
+	assertTrue "_dbc_generate_mycnf returned error" $?
+	assertFilesEqual ./data/generate_mycnf.txt "$cnf"
+	rm -f "$cnf"
+}
+
+test_mysql_check_connect(){
+	_dbc_mysql_check_connect
+	assertTrue "mysql_check_connect failed" $?
+	subst_tmpfile dbconfig-common_my.cnf.XXXXXX "$mockup_cmdline"
+	assertFilesEqual ./data/mysql_check_connect.txt "$mockup_cmdline"
+	assertFileEmpty "unexpected input to mysql_check_connect" "$mockup_inlog"
+}
+
+test_dbc_mysql_exec_file(){
+	local _dbc_nodb sql
+
+	sql=`mktemp -t`
+
+	# basic test
+	dbc_mysql_exec_file "$sql" >/dev/null
+	assertTrue "dbc_mysql_exec_file (withdb) failed" $?
+	subst_tmpfile dbconfig-common_my.cnf.XXXXXX "$mockup_cmdline"
+	assertFilesEqual ./data/dbc_mysql_exec_file.withdb.txt "$mockup_cmdline"
+
+	# test with non-existing sqlfile
+	dbc_mysql_exec_file "does not exist.sql" >/dev/null
+	assertFalse "dbc_mysql_exec_file (withdb) should have failed" $?
+	subst_tmpfile dbconfig-common_my.cnf.XXXXXX "$mockup_cmdline"
+	assertFilesEqual ./data/dbc_mysql_exec_file.withdb.txt "$mockup_cmdline"
+
+	# same as before, but with _dbc_nodb set
+	_dbc_nodb=yes
+	dbc_mysql_exec_file "$sql" >/dev/null 
+	assertTrue "dbc_mysql_exec_file (nodb) failed" $?
+	subst_tmpfile dbconfig-common_my.cnf.XXXXXX "$mockup_cmdline"
+	assertFilesEqual ./data/dbc_mysql_exec_file.nodb.txt "$mockup_cmdline"
+
+	dbc_mysql_exec_file "does not exist.sql" >/dev/null
+	assertFalse "dbc_mysql_exec_file (nodb) should have failed" $?
+	subst_tmpfile dbconfig-common_my.cnf.XXXXXX "$mockup_cmdline"
+	assertFilesEqual ./data/dbc_mysql_exec_file.nodb.txt "$mockup_cmdline"
+
+	rm -f "$sql"
+}
+
+test_mysql_exec_command(){
+	dbc_mysql_exec_command "select * from foo where bar = 1"
+	assertTrue "dbc_mysql_exec_command failed" $?
+	assertFilesEqual ./data/mysql_exec_command.sql.txt "$mockup_inlog"
+}
+
+test_dbc_mysql_check_database(){
+	local mockup_stdout
+	export mockup_stdout=./data/dbc_mysql_check_database.exists.stdout.txt
+
+	_dbc_mysql_check_database "$dbc_dbname"
+	assertTrue "_dbc_mysql_check_database failed to find db" $?
+
+	mockup_stdout=./data/dbc_mysql_check_database.missing.stdout.txt
+	_dbc_mysql_check_database "$dbc_dbname"
+	assertFalse "_dbc_mysql_check_database shouldn't have found db" $?
+}
+
+. /usr/share/shunit2/shunit2


Property changes on: trunk/test/mockdb.ts
___________________________________________________________________
Name: svn:executable
   + *

Modified: trunk/test/runtests.sh
===================================================================
--- trunk/test/runtests.sh	2008-03-29 17:38:28 UTC (rev 432)
+++ trunk/test/runtests.sh	2008-03-30 18:29:48 UTC (rev 433)
@@ -1,7 +1,7 @@
 #! /bin/sh
 
 cd "`dirname $0`"
-for t in basic.ts; do
+for t in basic.ts mockdb.ts; do
 	printf "# # #\n# # # Test: $t\n# # #\n"
 	printf "# #\n# # Shell: bash\n# #\n"
 	bash $t

Added: trunk/test/scripts/mockup
===================================================================
--- trunk/test/scripts/mockup	                        (rev 0)
+++ trunk/test/scripts/mockup	2008-03-30 18:29:48 UTC (rev 433)
@@ -0,0 +1,54 @@
+#!/bin/sh
+# database client mockup implementation
+set -eu
+
+mockup=`basename $0`
+# where mockup should print its cmdline
+cmdfile="${mockup_cmdline}"
+# where mockup should print internal errors
+errfile="${mockup_errfile}"
+
+stdout="${mockup_stdout:-/dev/null}"
+stderr="${mockup_stderr:-/dev/null}"
+stdin="${mockup_stdin:-}"
+retcode="${mockup_returncode:-0}"
+
+inputlog="$mockup_inlog"
+
+cmdline=''
+needs_input=''
+
+# replace commonly used variables in the cmdline
+replace_variables(){
+	local esc_tmpdir
+	esc_tmpdir=`echo $TMPDIR | sed -e 's,\\\\,\\\\\\\\,g'`
+	sed -e "s,$esc_tmpdir,<TMPDIR>,g" 2>"$errfile"
+}
+
+set $mockup "$@"
+while [ $# -gt 0 ]; do
+	escaped="`echo $1 | replace_variables | sed -e \"s/'/'\\\\\\''/g\"`"
+	if [ "$cmdline" ]; then
+		cmdline="$cmdline '$escaped'"
+	else
+		cmdline="'$escaped'"
+	fi
+	shift
+done 2>"$errfile"
+set "$cmdline" 2>"$errfile"
+echo "$@" > "$cmdfile"
+
+if [ "$stdin" ]; then 
+	exec 0<&"$stdin"
+fi
+cat > "$inputlog" 2>"$errfile"
+cat "$stdout" 2>"$errfile"
+cat "$stderr" >&2
+
+# if we had internal errors we print them and exit 127 (i.e. command not found)
+if [ -s "$errfile" ]; then
+	cat "$errfile" >&2
+	retcode=127
+fi
+
+exit $retcode


Property changes on: trunk/test/scripts/mockup
___________________________________________________________________
Name: svn:executable
   + *

Added: trunk/test/scripts/mysql
===================================================================
--- trunk/test/scripts/mysql	                        (rev 0)
+++ trunk/test/scripts/mysql	2008-03-30 18:29:48 UTC (rev 433)
@@ -0,0 +1 @@
+link mockup
\ No newline at end of file


Property changes on: trunk/test/scripts/mysql
___________________________________________________________________
Name: svn:special
   + *




More information about the Dbconfig-common-changes mailing list