PageRenderTime 120ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/libosinfo-0.1.2/test/test-lib.sh

#
Shell | 228 lines | 184 code | 27 blank | 17 comment | 32 complexity | df9b1c6b0919bc0403748aa302ad99cd MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. # source this file; set up for tests
  2. test -z "$abs_srcdir" && abs_srcdir=$(pwd)
  3. test -z "$abs_builddir" && abs_builddir=$(pwd)
  4. test -z "$abs_top_srcdir" && abs_top_srcdir=$(pwd)/..
  5. test -z "$abs_top_builddir" && abs_top_builddir=$(pwd)/..
  6. test -z "$LC_ALL" && LC_ALL=C
  7. # Skip this test if the shell lacks support for functions.
  8. unset function_test
  9. eval 'function_test() { return 11; }; function_test'
  10. if test $? != 11; then
  11. echo "$0: /bin/sh lacks support for functions; skipping this test." 1>&2
  12. (exit 77); exit 77
  13. fi
  14. test_intro()
  15. {
  16. name=$1
  17. if test "$verbose" = "0" ; then
  18. echo "TEST: $name"
  19. printf " "
  20. fi
  21. }
  22. test_result()
  23. {
  24. counter=$1
  25. name=$2
  26. status=$3
  27. if test "$verbose" = "0" ; then
  28. mod=`expr \( $counter + 40 - 1 \) % 40`
  29. if test "$counter" != 1 && test "$mod" = 0 ; then
  30. printf " %-3d\n" `expr $counter - 1`
  31. printf " "
  32. fi
  33. if test "$status" = "0" ; then
  34. printf "."
  35. else
  36. printf "!"
  37. fi
  38. else
  39. if test "$status" = "0" ; then
  40. printf "%3d) %-60s ... OK\n" "$counter" "$name"
  41. else
  42. printf "%3d) %-60s ... FAILED\n" "$counter" "$name"
  43. fi
  44. fi
  45. }
  46. test_final()
  47. {
  48. counter=$1
  49. status=$2
  50. if test "$verbose" = "0" ; then
  51. len=`expr 39 - \( \( $counter - 1 \) % 40 \)`
  52. printf "%${len}s" ""
  53. if test "$status" = "0" ; then
  54. printf " %-3d OK\n" $counter
  55. else
  56. printf " %-3d FAILED\n" $counter
  57. fi
  58. fi
  59. }
  60. skip_test_()
  61. {
  62. echo "$0: skipping test: $@" 1>&2
  63. (exit 77); exit 77
  64. }
  65. require_acl_()
  66. {
  67. getfacl --version < /dev/null > /dev/null 2>&1 \
  68. && setfacl --version < /dev/null > /dev/null 2>&1 \
  69. || skip_test_ "This test requires getfacl and setfacl."
  70. id -u bin > /dev/null 2>&1 \
  71. || skip_test_ "This test requires a local user named bin."
  72. }
  73. require_ulimit_()
  74. {
  75. ulimit_works=yes
  76. # Expect to be able to exec a program in 10MB of virtual memory,
  77. # but not in 20KB. I chose "date". It must not be a shell built-in
  78. # function, so you can't use echo, printf, true, etc.
  79. # Of course, in coreutils, I could use $top_builddir/src/true,
  80. # but this should be able to work for other projects, too.
  81. ( ulimit -v 10000; date ) > /dev/null 2>&1 || ulimit_works=no
  82. ( ulimit -v 20; date ) > /dev/null 2>&1 && ulimit_works=no
  83. test $ulimit_works = no \
  84. && skip_test_ "this shell lacks ulimit support"
  85. }
  86. require_readable_root_()
  87. {
  88. test -r / || skip_test_ "/ is not readable"
  89. }
  90. # Skip the current test if strace is not available or doesn't work.
  91. require_strace_()
  92. {
  93. strace -V < /dev/null > /dev/null 2>&1 ||
  94. skip_test_ 'no strace program'
  95. strace -qe unlink echo > /dev/null 2>&1 ||
  96. skip_test_ 'strace does not work'
  97. }
  98. require_built_()
  99. {
  100. skip_=no
  101. for i in "$@"; do
  102. case " $built_programs " in
  103. *" $i "*) ;;
  104. *) echo "$i: not built" 1>&2; skip_=yes ;;
  105. esac
  106. done
  107. test $skip_ = yes && skip_test_ "required program(s) not built"
  108. }
  109. uid_is_privileged_()
  110. {
  111. # Make sure id -u succeeds.
  112. my_uid=$(id -u) \
  113. || { echo "$0: cannot run \`id -u'" 1>&2; return 1; }
  114. # Make sure it gives valid output.
  115. case $my_uid in
  116. 0) ;;
  117. *[!0-9]*)
  118. echo "$0: invalid output (\`$my_uid') from \`id -u'" 1>&2
  119. return 1 ;;
  120. *) return 1 ;;
  121. esac
  122. }
  123. skip_if_()
  124. {
  125. case $1 in
  126. root) skip_test_ must be run as root ;;
  127. non-root) skip_test_ must be run as non-root ;;
  128. *) ;; # FIXME?
  129. esac
  130. }
  131. require_selinux_()
  132. {
  133. case `ls -Zd .` in
  134. '? .'|'unlabeled .')
  135. skip_test_ "this system (or maybe just" \
  136. "the current file system) lacks SELinux support"
  137. ;;
  138. esac
  139. }
  140. very_expensive_()
  141. {
  142. if test "$RUN_VERY_EXPENSIVE_TESTS" != yes; then
  143. skip_test_ '
  144. This test is very expensive, so it is disabled by default.
  145. To run it anyway, rerun make check with the RUN_VERY_EXPENSIVE_TESTS
  146. environment variable set to yes. E.g.,
  147. env RUN_VERY_EXPENSIVE_TESTS=yes make check
  148. '
  149. fi
  150. }
  151. require_root_() { uid_is_privileged_ || skip_test_ "must be run as root"; }
  152. skip_if_root_() { uid_is_privileged_ && skip_test_ "must be run as non-root"; }
  153. error_() { echo "$0: $@" 1>&2; (exit 1); exit 1; }
  154. framework_failure() { error_ 'failure in testing framework'; }
  155. mkfifo_or_skip_()
  156. {
  157. test $# = 1 || framework_failure
  158. if ! mkfifo "$1"; then
  159. # Make an exception of this case -- usually we interpret framework-creation
  160. # failure as a test failure. However, in this case, when running on a SunOS
  161. # system using a disk NFS mounted from OpenBSD, the above fails like this:
  162. # mkfifo: cannot make fifo `fifo-10558': Not owner
  163. skip_test_ 'NOTICE: unable to create test prerequisites'
  164. fi
  165. }
  166. test_dir_=$(pwd)
  167. this_test_() { echo "./$0" | sed 's,.*/,,'; }
  168. this_test=$(this_test_)
  169. verbose=0
  170. if test -n "$VIR_TEST_DEBUG" || test -n "$VIR_TEST_VERBOSE" ; then
  171. verbose=1
  172. fi
  173. # This is a stub function that is run upon trap (upon regular exit and
  174. # interrupt). Override it with a per-test function, e.g., to unmount
  175. # a partition, or to undo any other global state changes.
  176. cleanup_() { :; }
  177. mktempd="$abs_top_srcdir/build-aux/mktempd"
  178. t_=$("$SHELL" "$mktempd" "$test_dir_" lv-$this_test.XXXXXXXXXX) \
  179. || error_ "failed to create temporary directory in $test_dir_"
  180. # Run each test from within a temporary sub-directory named after the
  181. # test itself, and arrange to remove it upon exception or normal exit.
  182. trap 'st=$?; cleanup_; d='"$t_"';
  183. cd '"$test_dir_"' && chmod -R u+rwx "$d" && rm -rf "$d" && exit $st' 0
  184. trap '(exit $?); exit $?' 1 2 13 15
  185. cd "$t_" || error_ "failed to cd to $t_"
  186. if ( diff --version < /dev/null 2>&1 | grep GNU ) 2>&1 > /dev/null; then
  187. compare() { diff -u "$@"; }
  188. elif ( cmp --version < /dev/null 2>&1 | grep GNU ) 2>&1 > /dev/null; then
  189. compare() { cmp -s "$@"; }
  190. else
  191. compare() { cmp "$@"; }
  192. fi
  193. # Local Variables:
  194. # indent-tabs-mode: nil
  195. # End: