run_tests.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/usr/bin/env bash
  2. # Copyright 2021 gRPC authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # Test structure borrowed with gratitude from
  16. # https://github.com/grpc/grpc-go/tree/master/examples/features
  17. set -eux
  18. # execute in root dir
  19. SCRIPTPATH="$(
  20. cd -- "$(dirname "$0")" >/dev/null 2>&1
  21. pwd -P
  22. )"
  23. cd ${SCRIPTPATH}/../../..
  24. SERVER_PORT=50051
  25. export TMPDIR=$(mktemp -d)
  26. trap "rm -rf ${TMPDIR}" EXIT
  27. clean() {
  28. # loop a handful of times in case job shutdown is not immediate
  29. for i in {1..5}; do
  30. # kill all jobs
  31. jobs -p | xargs kill &>/dev/null || true
  32. # wait for all jobs to exit
  33. sleep 0.3
  34. if ! jobs | read; then
  35. return
  36. fi
  37. done
  38. echo "ERROR: clean failed to kill tests"
  39. jobs
  40. exit 1
  41. }
  42. fail() {
  43. echo "$@" >&2
  44. clean
  45. exit 1
  46. }
  47. pass() {
  48. echo "SUCCESS: $1"
  49. }
  50. wait_for_server() {
  51. feature=$1
  52. wait_command=${SERVER_WAIT_COMMAND[$feature]:-${SERVER_WAIT_COMMAND["default"]}}
  53. echo "INFO: waiting for server to start"
  54. declare -i i=0
  55. while ! eval "$wait_command"; do
  56. ((++i < 10)) || fail "cannot determine if server started"
  57. lsof -U
  58. sleep 1
  59. done
  60. pass "server started"
  61. }
  62. FEATURES=(
  63. "unix_abstract"
  64. )
  65. declare -A SERVER_WAIT_COMMAND=(
  66. ["unix_abstract"]="lsof -U | grep '@grpc@abstract'"
  67. ["default"]="lsof -i :$SERVER_PORT | grep $SERVER_PORT"
  68. )
  69. declare -A EXPECTED_SERVER_OUTPUT=(
  70. ["unix_abstract"]="Server listening on unix-abstract:grpc%00abstract ... Echoing: arst"
  71. )
  72. declare -A EXPECTED_CLIENT_OUTPUT=(
  73. ["unix_abstract"]="Sending 'arst' to unix-abstract:grpc%00abstract ... Received: arst"
  74. )
  75. for feature in ${FEATURES[@]}; do
  76. echo "TESTING: $feature"
  77. bazel build --define=use_strict_warning=true //examples/cpp/features/${feature}:all || fail "failed to build $feature"
  78. SERVER_LOG="$(mktemp)"
  79. ./bazel-bin/examples/cpp/features/$feature/server &>$SERVER_LOG &
  80. wait_for_server $feature
  81. # TODO(hork): add a timeout to abort client?
  82. CLIENT_LOG="$(mktemp)"
  83. ./bazel-bin/examples/cpp/features/$feature/client &>$CLIENT_LOG
  84. if [ -n "${EXPECTED_SERVER_OUTPUT[$feature]}" ]; then
  85. if ! grep -q "${EXPECTED_SERVER_OUTPUT[$feature]}" $SERVER_LOG; then
  86. fail "server log missing output: ${EXPECTED_SERVER_OUTPUT[$feature]}
  87. got server log:
  88. $(cat $SERVER_LOG)
  89. got client log:
  90. $(cat $CLIENT_LOG)
  91. "
  92. else
  93. pass "server log contains expected output: ${EXPECTED_SERVER_OUTPUT[$feature]}"
  94. fi
  95. fi
  96. if [ -n "${EXPECTED_CLIENT_OUTPUT[$feature]}" ]; then
  97. if ! grep -q "${EXPECTED_CLIENT_OUTPUT[$feature]}" $CLIENT_LOG; then
  98. fail "client log missing output: ${EXPECTED_CLIENT_OUTPUT[$feature]}
  99. got server log:
  100. $(cat $SERVER_LOG)
  101. got client log:
  102. $(cat $CLIENT_LOG)
  103. "
  104. else
  105. pass "client log contains expected output: ${EXPECTED_CLIENT_OUTPUT[$feature]}"
  106. fi
  107. fi
  108. clean
  109. done