do_tests.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #!/bin/sh
  2. #
  3. # Copyright (C) 2010 Google Inc.
  4. # Written by David Hendricks for Google Inc.
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. #
  20. # This is a simple test harness which contains setup and cleanup code common to
  21. # all tests. The syntax for this script is:
  22. # do_tests.sh --options <test1> <test2> ... <testN>
  23. #
  24. # Arguments which are files are assumed to be tests which are run after common
  25. # setup routines and before common cleanup routines.
  26. #
  27. # This script exports some global variables intended for use by individual unit
  28. # tests. The important ones are:
  29. # EXIT_SUCCESS & EXIT_FAILURE: Exit codes
  30. # DEBUG: Indicates that extra verbosity should be used.
  31. # BACKUP: The backup firmware image obtained during setup.
  32. . "$(pwd)/common.sh"
  33. if [ -z "$DEBUG" ]; then
  34. DEBUG=0
  35. fi
  36. show_help()
  37. {
  38. echo " \
  39. Usage:
  40. do_test.sh [OPTION] test1.sh test2.sh ... testN.sh
  41. Environment variables:
  42. FLASHROM: Path to the Flashrom binary to test
  43. FLASHROM_PARAM: Parameters to pass in
  44. OPTIONS
  45. -h or --help
  46. Display this message.
  47. Arguments with one or two leading dashes are processed as options.
  48. Arguments which are filenames are processed as test cases. Names of test
  49. cases may not contain spaces.
  50. "
  51. exit $EXIT_SUCCESS
  52. }
  53. msg_dbg() {
  54. if [ ${DEBUG} -eq 1 ]; then
  55. echo "$@"
  56. fi
  57. }
  58. # Parse command-line
  59. OPTIONS=""
  60. TESTS=""
  61. for ARG in $@; do
  62. if echo "${ARG}" | grep "^-.*$" >/dev/null
  63. then
  64. OPTIONS=${OPTIONS}" "${ARG}
  65. elif [ -f ${ARG} ]; then
  66. TESTS=${TESTS}" "${ARG}
  67. fi
  68. done
  69. for ARG in ${OPTIONS}; do
  70. case ${ARG} in
  71. -h|--help)
  72. show_help;
  73. shift;;
  74. esac;
  75. done
  76. #
  77. # Setup Routine
  78. #
  79. # The copy of flashrom to test. If unset, we'll assume the user wants to test
  80. # a newly built flashrom binary in the parent directory (this script should
  81. # reside in flashrom/util).
  82. if [ -z "${FLASHROM}" ] ; then
  83. FLASHROM="./flashrom"
  84. fi
  85. echo "testing flashrom binary: ${FLASHROM}"
  86. olddir=$(pwd)
  87. # test data location
  88. tmpdir=$(mktemp -d -t flashrom_test.XXXXXXXXXX)
  89. if [ $? -ne 0 ] ; then
  90. echo "Could not create temporary directory"
  91. exit ${EXIT_FAILURE}
  92. fi
  93. which flashrom > /dev/null
  94. if [ $? -ne 0 ] ; then
  95. echo "Please install a stable version of flashrom in your path."
  96. echo "This will be used to compare the test flashrom binary and "
  97. echo "restore your firmware image at the end of the test."
  98. exit ${EXIT_FAILURE}
  99. fi
  100. # Copy the test scripts and flashrom to temporary directory.
  101. cp common.sh "${tmpdir}/"
  102. cp "${FLASHROM}" "${tmpdir}/"
  103. for TEST in ${TESTS}; do
  104. cp ${TEST} "${tmpdir}/"
  105. done
  106. cd "${tmpdir}"
  107. echo "Running test in ${tmpdir}"
  108. # Make a backup
  109. echo "Reading firmware image"
  110. export BACKUP="backup.bin"
  111. system_flashrom -r "$BACKUP"
  112. if [ $? -ne 0 ]; then
  113. echo "Failed to create backup image"
  114. exit ${EXIT_FAILURE}
  115. else
  116. echo "Original image saved as ${BACKUP}"
  117. fi
  118. # Attempt to write the backup image to ensure the system's flashrom is
  119. # at least somewhat capable of restoring the image.
  120. # FIXME: this requires a modification to flashrom to force blocks to be
  121. # re-written, even if the content is the same.
  122. #flashrom ${FLASHROM_PARAM} --force -r "$BACKUP" > /dev/null
  123. #if [ $? -ne 0 ]; then
  124. # echo "The installed flashrom binary is not usable for testing"
  125. # exit ${EXIT_FAILURE}
  126. #fi
  127. export BACKUP=${BACKUP}
  128. #
  129. # Execute test cases
  130. #
  131. rc=${EXIT_SUCCESS}
  132. msg_dbg "Test cases: ${TESTS}"
  133. for TEST in ${TESTS}; do
  134. msg_dbg "Running test: \"${TEST}\""
  135. if [ $DEBUG -eq 1 ]; then
  136. sh -x ${TEST}
  137. else
  138. sh ${TEST}
  139. fi
  140. if [ ${?} -ne ${EXIT_SUCCESS} ]; then
  141. rc=${EXIT_FAILURE}
  142. break
  143. fi
  144. done
  145. #
  146. # Cleanup Routine
  147. #
  148. if [ ${rc} -ne ${EXIT_SUCCESS} ] ; then
  149. echo "Result: FAILED"
  150. else
  151. echo "Result: PASSED"
  152. fi
  153. echo "restoring original image using system's flashrom"
  154. system_flashrom -w "$BACKUP"
  155. echo "test files remain in ${tmpdir}"
  156. cd "${olddir}"
  157. exit ${rc}