run-tests.sh 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #!/bin/sh
  2. # Toprammer regression tests
  3. # Copyright (c) 2010-2012 Michael Buesch <m@bues.ch>
  4. basedir="$(dirname "$0")"
  5. [ "$(echo -n "$basedir" | cut -c1)" = "/" ] || basedir="$PWD/$basedir"
  6. tmpdir="/tmp/toprammer-test-$$"
  7. cleanup_enabled=1
  8. cleanup()
  9. {
  10. [ $cleanup_enabled -ne 0 ] || return
  11. echo "Cleanup..."
  12. rm -Rf "$tmpdir"
  13. }
  14. trap cleanup INT TERM
  15. info()
  16. {
  17. echo "$current_test: $*"
  18. }
  19. warning()
  20. {
  21. echo "WARNING $current_test: $*"
  22. }
  23. error()
  24. {
  25. echo "ERROR $current_test: $*"
  26. }
  27. abort()
  28. {
  29. cleanup
  30. exit 1
  31. }
  32. die()
  33. {
  34. error "$@"
  35. cleanup
  36. exit 1
  37. }
  38. toprammer()
  39. {
  40. local logfile="$tmpdir/toprammer.log"
  41. local args=
  42. [ -n "$current_chipid" ] && args="--chip-id $current_chipid $args"
  43. args="-B -I bin -O bin $args"
  44. echo " toprammer $args $*"
  45. cd "$basedir/.." || die "Failed to chdir"
  46. if [ $verbose -eq 0 ]; then
  47. ./toprammer $args "$@" >$logfile 2>&1
  48. if [ $? -ne 0 ]; then
  49. [ -r "$logfile" ] && cat "$logfile"
  50. die "toprammer $args $* <<<FAILED>>>"
  51. fi
  52. else
  53. ./toprammer $args "$@" -V2
  54. [ $? -eq 0 ] || die "toprammer $args $* <<<FAILED>>>"
  55. fi
  56. }
  57. toprammer_layout_silent()
  58. {
  59. local logfile="$tmpdir/toprammer-layout.log"
  60. echo " toprammer-layout $*"
  61. cd "$basedir/.." || die "Failed to chdir"
  62. ./toprammer-layout "$@" >$logfile 2>&1
  63. if [ $? -ne 0 ]; then
  64. [ -r "$logfile" ] && cat "$logfile"
  65. die "toprammer-layout $* <<<FAILED>>>"
  66. fi
  67. }
  68. toprammer_layout()
  69. {
  70. cd "$basedir/.." || die "Failed to chdir"
  71. ./toprammer-layout "$@"
  72. [ $? -eq 0 ] || die "toprammer-layout $* <<<FAILED>>>"
  73. }
  74. ask()
  75. {
  76. read -p "$* " ok
  77. echo
  78. [ "$ok" = "y" -o "$ok" = "Y" -o \
  79. "$ok" = "1" -o "$ok" = "" ] && return 0
  80. return 1
  81. }
  82. request()
  83. {
  84. read -p "$*" res
  85. echo
  86. [ "$res" = "a" ] && abort
  87. [ "$res" = "x" ] && return 1
  88. return 0
  89. }
  90. request_DUT() # $1=DUT-name
  91. {
  92. local dut="$1"
  93. current_chipid="$dut"
  94. # Init the programmer
  95. toprammer --force-upload
  96. # Show layout
  97. toprammer_layout -d "$current_device" -p "$dut" --only-insert
  98. request "Please insert a $dut into the ZIF socket (x to skip; a to abort)..."
  99. }
  100. request_TOP() # $1=TOPxxxx
  101. {
  102. request "Please connect the $* programmer (x to skip; a to abort)..."
  103. }
  104. create_random_file() # $1=file $2=bs $3=count
  105. {
  106. dd if=/dev/urandom of="$1" bs="$2" count="$3" >/dev/null 2>&1
  107. [ $? -eq 0 ] || die "Failed to create $1"
  108. chmod 444 "$1"
  109. [ $? -eq 0 ] || die "Failed to set $1 read-only"
  110. }
  111. compare_files() # $1=file1 $2=file2
  112. {
  113. [ -r "$1" -a -r "$2" ] || return 1
  114. sum1="$(sha1sum "$1" | cut -d' ' -f1)"
  115. sum2="$(sha1sum "$2" | cut -d' ' -f1)"
  116. [ "$sum1" = "$sum2" ]
  117. }
  118. compare_file_to_hex() # $1=file $2=hex_string
  119. {
  120. local filehex="$(hexdump -v -e '/1 "%02X"' $1)"
  121. local stringhex="$(printf '%s' "$2" | tr -d " \t")"
  122. [ "$filehex" = "$stringhex" ]
  123. }
  124. usage()
  125. {
  126. echo "Usage: run-tests.sh <OPTIONS> <SCRIPTPATH>"
  127. echo
  128. echo "Options:"
  129. echo " -h|--help Show this help text"
  130. echo " -V|--verbose Be verbose"
  131. echo
  132. echo "If the optional scriptpath is specified, only that testscript"
  133. echo "is executed. The scriptpath is DEVICE/TESTSCRIPT. Example:"
  134. echo " top2049/001-atmega32dip40.test"
  135. echo "This will execute the atmega32 test for the TOP2049 and exit."
  136. echo "If no path is specified, all tests will be executed."
  137. }
  138. # Parse commandline
  139. scriptpaths=
  140. verbose=0
  141. while [ $# -gt 0 ]; do
  142. if [ "$1" = "-h" -o "$1" = "--help" ]; then
  143. usage
  144. exit 0
  145. fi
  146. if [ "$1" = "-V" -o "$1" = "--verbose" ]; then
  147. verbose=1
  148. shift
  149. continue
  150. fi
  151. scriptpaths="$scriptpaths $1"
  152. shift
  153. done
  154. current_test=
  155. current_device=
  156. current_chipid=
  157. cleanup
  158. mkdir -p "$tmpdir"
  159. [ $? -eq 0 ] || die "Failed to create $tmpdir"
  160. # Create various test files
  161. tmpfile="$tmpdir/tmpfile"
  162. testfile_64="$tmpdir/testfile_64"
  163. create_random_file "$testfile_64" 64 1
  164. testfile_128="$tmpdir/testfile_128"
  165. create_random_file "$testfile_128" 128 1
  166. testfile_256="$tmpdir/testfile_256"
  167. create_random_file "$testfile_256" 256 1
  168. testfile_512="$tmpdir/testfile_512"
  169. create_random_file "$testfile_512" 512 1
  170. testfile_1k="$tmpdir/testfile_1k"
  171. create_random_file "$testfile_1k" 1024 1
  172. testfile_2k="$tmpdir/testfile_2k"
  173. create_random_file "$testfile_2k" 1024 2
  174. testfile_4k="$tmpdir/testfile_4k"
  175. create_random_file "$testfile_4k" 4096 1
  176. testfile_8k="$tmpdir/testfile_8k"
  177. create_random_file "$testfile_8k" 4096 2
  178. testfile_16k="$tmpdir/testfile_16k"
  179. create_random_file "$testfile_16k" 4096 4
  180. testfile_32k="$tmpdir/testfile_32k"
  181. create_random_file "$testfile_32k" 4096 8
  182. testfile_128k="$tmpdir/testfile_128k"
  183. create_random_file "$testfile_128k" 4096 32
  184. do_run_test() # $1=device, $2=testscript
  185. {
  186. current_device="$1"
  187. current_test="$1/$2"
  188. echo "Running $current_test..."
  189. rm -f "$tmpfile"
  190. # Import the testscript
  191. . "$basedir/defaults.test"
  192. . "$basedir/$current_test"
  193. # And run the tests
  194. while $(true); do
  195. test_init
  196. [ $? -eq 0 ] || break
  197. cleanup_enabled=0
  198. ( test_run )
  199. local res=$?
  200. cleanup_enabled=1
  201. if [ $res -ne 0 ]; then
  202. test_exit
  203. ask "$current_test failed. RETRY?"
  204. [ $? -eq 0 ] && continue
  205. ask "Terminate testsuite?"
  206. [ $? -eq 0 ] && abort
  207. break
  208. fi
  209. test_exit
  210. break
  211. done
  212. current_device=
  213. current_test=
  214. current_chipid=
  215. }
  216. if [ -z "$scriptpaths" ]; then
  217. # Run all scripts
  218. for device in $(ls "$basedir"); do
  219. [ -d "$basedir/$device" ] || continue
  220. [ "$device" = "generic" ] || request_TOP "$device" || continue
  221. for testscript in $(ls "$basedir/$device"); do
  222. do_run_test "$device" "$testscript"
  223. done
  224. done
  225. else
  226. # Only run the specified tests
  227. for scriptpath in $scriptpaths; do
  228. device="$(echo "$scriptpath" | cut -d'/' -f1)"
  229. testscript="$(echo "$scriptpath" | cut -d'/' -f2)"
  230. [ -d "$basedir/$device" -a -f "$basedir/$device/$testscript" ] || \
  231. die "$scriptpath is an invalid scriptpath"
  232. do_run_test "$device" "$testscript"
  233. done
  234. fi
  235. cleanup