runtests.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #!/bin/bash
  2. # Really simple test runner.
  3. # Runs a bunch of python unit tests as standalone programs
  4. # and collects the results.
  5. ###########################
  6. # Test definitions
  7. ###########################
  8. # We define the test names here. Given a name NAME, the python file should be NAME.py.
  9. # The tests will be run in order, unless a subset is provided on the command line.
  10. TESTS="test_base test_iterable_as_output_thing test_external_event_stream test_multiple_output_ports test_linq test_transducer test_scheduler_cancel test_fatal_error_handling test_fatal_error_in_private_loop test_blocking_output_thing test_solar_heater_scenario test_timeout test_blocking_input_thing test_postgres_adapters test_mqtt test_mqtt_async test_csv_adapters test_functional_api test_tracing test_pandas test_rpi_adapters test_influxdb test_descheduling test_predix"
  11. ###########################
  12. # Environment setup
  13. ###########################
  14. # Try to guess which python points to python 3
  15. if [[ "`which python3`" == "" ]]; then
  16. PYTHON=`which python`
  17. else
  18. PYTHON=`which python3`
  19. fi
  20. if [[ "$PYTHON" == "" ]]; then
  21. echo "Could not find python!"
  22. exit 1
  23. fi
  24. # verify that it is a python3
  25. $PYTHON -c "import sys; sys.exit(0 if sys.version.startswith('3.') else 1)"
  26. if [[ "$?" == 1 ]]; then
  27. echo "Wrong version of python, need python 3.x, got `$PYTHON --version`"
  28. exit 1
  29. fi
  30. echo "Using python at $PYTHON"
  31. # set python path if necessary
  32. if [[ "$PYTHONPATH" == "" ]]; then
  33. export PYTHONPATH=`cd ..; pwd`
  34. echo "Set PYTHONPATH to $PYTHONPATH"
  35. fi
  36. # Counts of each test result type
  37. OK=0
  38. SKIPPED=0
  39. FAILED=0
  40. ERROR=0
  41. # Escape codes for color text
  42. RED='\033[0;31m'
  43. GREEN='\033[0;32m'
  44. YELLOW='\033[0;33m' # yellow
  45. NC='\033[0m' # No Color
  46. # Run a single test and update the counts. Takes one argument: the test name.
  47. # A .py will be appended to get the python filename. The standard output
  48. # goes into $TEST.out and the standard error to $TEST.err. These are not
  49. # kept unless the test fails.
  50. function runtest {
  51. TEST=$1
  52. echo -n "Running $TEST"
  53. $PYTHON ${TEST}.py >${TEST}.out 2>${TEST}.err
  54. rc=$?
  55. if [[ "$rc" == 0 ]]; then
  56. # got a success. Now check whether skipped.
  57. tail -1 $TEST.err | grep -q 'OK (skipped'
  58. skiprc=$?
  59. if [[ "$skiprc" == "0" ]]; then
  60. echo -e " ${YELLOW}SKIPPED${NC}"
  61. SKIPPED=$((SKIPPED+1))
  62. rm $TEST.err $TEST.out
  63. else
  64. tail -1 $TEST.err | grep -q 'OK'
  65. okrc=$?
  66. if [[ "$okrc" == "0" ]]; then
  67. echo -e " ${GREEN}OK${NC}"
  68. OK=$((OK+1))
  69. rm $TEST.err $TEST.out
  70. else
  71. # did not find the OK
  72. echo -e " ${RED}UNKNOWN!${NC}"
  73. ERROR=$((ERROR+1))
  74. fi # okrc
  75. fi # skiprc
  76. else # non-zero return code
  77. tail -1 $TEST.err | grep -q 'FAILED'
  78. failrc=$?
  79. if [[ "$failrc" == "0" ]]; then
  80. echo -e " ${RED}FAILED${NC}"
  81. FAILED=$((FAILED+1))
  82. else
  83. echo -e " ${RED}ERROR${NC}"
  84. ERROR=$((ERROR+1))
  85. fi # failrc
  86. fi # rc
  87. }
  88. ###########################
  89. # validate command line arguments
  90. ###########################
  91. if [[ "$#" == "0" ]]; then
  92. TESTS_TO_RUN=$TESTS
  93. else
  94. ARGS=${@:1}
  95. for tst in $ARGS; do
  96. found=0
  97. for chktst in $TESTS; do
  98. if [[ "$chktst" == "$tst" ]]; then
  99. found=1
  100. break
  101. fi
  102. done
  103. if [[ $found != 1 ]]; then
  104. echo -e "${RED}ERROR: $tst is not a known test.${NC}"
  105. echo "Valid tests are: $TESTS"
  106. exit 1
  107. fi
  108. done
  109. TESTS_TO_RUN=$ARGS
  110. fi
  111. ###########################
  112. # Run the tests
  113. ###########################
  114. rm -f *.err *.out
  115. echo ">>>>>>>>>>>>>>>>>>>> Starting Tests"
  116. for tst in $TESTS_TO_RUN; do
  117. runtest ${tst}
  118. done
  119. echo ">>>>>>>>>>>>>>>>>>>> Finished Tests"
  120. if [[ "$OK" == "0" ]]; then
  121. echo -e "${RED}0 Tests successful.${NC}"
  122. else
  123. echo -e "${GREEN}$OK Tests successful.${NC}"
  124. fi
  125. if [[ "$SKIPPED" == "0" ]]; then
  126. echo -e "${GREEN}0 Tests skipped.${NC}"
  127. else
  128. echo -e "${YELLOW}$SKIPPED Tests skipped.${NC}"
  129. fi
  130. if [[ "$FAILED" == "0" ]]; then
  131. echo -e "${GREEN}0 Tests failed.${NC}"
  132. else
  133. echo -e "${RED}$FAILED Tests failed.${NC}"
  134. fi
  135. if [[ "$ERROR" == "0" ]]; then
  136. echo -e "${GREEN}0 Tests had errors.${NC}"
  137. else
  138. echo -e "${RED}$ERROR Tests had errors.${NC}"
  139. fi
  140. exit $((FAILED + ERROR))