runUnittests.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #!/usr/bin/env bash
  2. CIDIR=$(dirname $(readlink -fn $0))
  3. NO_EXPECT=0
  4. source $CIDIR/ci.functions
  5. ASTETCDIR=$DESTDIR/etc/asterisk
  6. asterisk_corefile_glob() {
  7. local pattern=$(/sbin/sysctl -n kernel.core_pattern)
  8. # If core_pattern is a pipe there isn't much we can do
  9. if [[ ${pattern:0:1} == "|" ]] ; then
  10. echo "core*"
  11. else
  12. echo "${pattern%%%*}*"
  13. fi
  14. }
  15. run_tests_expect() {
  16. $EXPECT <<-EOF
  17. spawn sudo $ASTERISK ${USER_GROUP:+-U ${USER_GROUP%%:*} -G ${USER_GROUP##*:}} -fcng -C $CONFFILE
  18. match_max 512
  19. set timeout 600
  20. expect -notransfer "Asterisk Ready."
  21. send "core show settings\r"
  22. expect -notransfer "CLI>"
  23. send "${UNITTEST_COMMAND:-test execute all}\r"
  24. expect -notransfer -ex "Test(s) Executed"
  25. expect -notransfer "CLI>"
  26. send "test show results failed\r"
  27. expect -notransfer "CLI>"
  28. send "test generate results xml ${OUTPUTFILE}\r"
  29. expect -notransfer "CLI>"
  30. send "core stop now\r"
  31. expect -notransfer "Executing last minute cleanups"
  32. wait
  33. EOF
  34. }
  35. run_tests_socket() {
  36. sudo $ASTERISK ${USER_GROUP:+-U ${USER_GROUP%%:*} -G ${USER_GROUP##*:}} -gn -C $CONFFILE
  37. for n in {1..5} ; do
  38. sleep 3
  39. $ASTERISK -rx "core waitfullybooted" -C $CONFFILE && break
  40. done
  41. sleep 1
  42. $ASTERISK -rx "core show settings" -C $CONFFILE
  43. $ASTERISK -rx "${UNITTEST_COMMAND:-test execute all}" -C $CONFFILE
  44. $ASTERISK -rx "test show results failed" -C $CONFFILE
  45. $ASTERISK -rx "test generate results xml $OUTPUTFILE" -C $CONFFILE
  46. $ASTERISK -rx "core stop now" -C $CONFFILE
  47. }
  48. # If DESTDIR is used to install and run asterisk from non standard locations,
  49. # the directory entries in asterisk.conf need to be munged to prepend DESTDIR.
  50. ALTERED=$(head -10 ../tmp/DESTDIR/etc/asterisk/asterisk.conf | grep -q "DESTDIR" && echo yes)
  51. if [ x"$ALTERED" = x ] ; then
  52. # In the section that starts with [directories and ends with a blank line,
  53. # replace "=> " with "=> ${DESTDIR}"
  54. sed -i -r -e "/^\[directories/,/^$/ s@=>\s+@=> ${DESTDIR}@" "$ASTETCDIR/asterisk.conf"
  55. fi
  56. cat <<-EOF > "$ASTETCDIR/logger.conf"
  57. [logfiles]
  58. full => notice,warning,error,debug,verbose
  59. console => notice,warning,error
  60. EOF
  61. echo "[default]" > "$ASTETCDIR/extensions.conf"
  62. cat <<-EOF > "$ASTETCDIR/manager.conf"
  63. [general]
  64. enabled=yes
  65. bindaddr=127.0.0.1
  66. port=5038
  67. [test]
  68. secret=test
  69. read = system,call,log,verbose,agent,user,config,dtmf,reporting,cdr,dialplan
  70. write = system,call,agent,user,config,command,reporting,originate
  71. EOF
  72. cat <<-EOF > "$ASTETCDIR/http.conf"
  73. [general]
  74. enabled=yes
  75. bindaddr=127.0.0.1
  76. bindport=8088
  77. EOF
  78. cat <<-EOF > "$ASTETCDIR/modules.conf"
  79. [modules]
  80. autoload=yes
  81. noload=res_mwi_external.so
  82. noload=res_mwi_external_ami.so
  83. noload=res_ari_mailboxes.so
  84. noload=res_stasis_mailbox.so
  85. EOF
  86. cat <<-EOF >> "$ASTETCDIR/sorcery.conf"
  87. [res_pjsip_pubsub]
  88. resource_list=memory
  89. EOF
  90. ASTERISK="$DESTDIR/usr/sbin/asterisk"
  91. CONFFILE=$ASTETCDIR/asterisk.conf
  92. OUTPUTDIR=${OUTPUT_DIR:-tests/CI/output/}
  93. OUTPUTFILE=${OUTPUT_XML:-${OUTPUTDIR}/unittests-results.xml}
  94. EXPECT="$(which expect 2>/dev/null || : )"
  95. [ ! -d ${OUTPUTDIR} ] && mkdir -p $OUTPUTDIR
  96. [ x"$USER_GROUP" != x ] && sudo chown -R $USER_GROUP $OUTPUTDIR
  97. rm -rf $ASTETCDIR/extensions.{ael,lua} || :
  98. set -x
  99. if [ x"$EXPECT" != x -a $NO_EXPECT -eq 0 ] ; then
  100. run_tests_expect
  101. else
  102. run_tests_socket
  103. fi
  104. # Cleanup "just in case"
  105. sudo killall -qe -ABRT $ASTERISK
  106. runner rsync -vaH $DESTDIR/var/log/asterisk/. $OUTPUTDIR
  107. set +x
  108. [ x"$USER_GROUP" != x ] && sudo chown -R $USER_GROUP $OUTPUTDIR
  109. for core in $(asterisk_corefile_glob)
  110. do
  111. if [ -f $core ]
  112. then
  113. echo "*** Found a core file ($core) after running unit tests ***"
  114. set -x
  115. sudo OUTPUTDIR=$OUTPUTDIR $DESTDIR/var/lib/asterisk/scripts/ast_coredumper --no-default-search $core
  116. fi
  117. done
  118. exit 0