run-tests.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #!/bin/sh
  2. # -*- coding: utf-8 -*-
  3. basedir="$(realpath "$0" | xargs dirname)"
  4. basedir="$basedir/.."
  5. info()
  6. {
  7. echo "--- $*"
  8. }
  9. error()
  10. {
  11. echo "=== ERROR: $*" >&2
  12. }
  13. warning()
  14. {
  15. echo "=== WARNING: $*" >&2
  16. }
  17. die()
  18. {
  19. error "$*"
  20. exit 1
  21. }
  22. build_project()
  23. {
  24. info "Building project..."
  25. cd "$basedir" || die "cd failed"
  26. ./build.sh || die "Build failed"
  27. }
  28. cargo_clippy()
  29. {
  30. cargo clippy -- --deny warnings || die "cargo clippy failed"
  31. cargo clippy --tests -- --deny warnings || die "cargo clippy --tests failed"
  32. }
  33. build_stubs()
  34. {
  35. info "Building stubs..."
  36. mkdir -p "$tmpbin" \
  37. || die "Failed to create tmpbin directory"
  38. rustc --edition 2021 -o "$tmpbin/nft" "$stubdir/nft.rs" \
  39. || die "Failed to build nft stub"
  40. }
  41. run_tests()
  42. {
  43. local test_type="$1"
  44. info "### Running test: $test_type ###"
  45. rm -rf "$rundir"
  46. local conf="$testdir/conf/$test_type.conf"
  47. info "Starting letmeinfwd..."
  48. "$target/letmeinfwd" \
  49. --test-mode \
  50. --no-systemd \
  51. --rundir "$rundir" \
  52. --config "$conf" &
  53. pid_letmeinfwd=$!
  54. info "Starting letmeind..."
  55. "$target/letmeind" \
  56. --no-systemd \
  57. --rundir "$rundir" \
  58. --config "$conf" &
  59. pid_letmeind=$!
  60. wait_for_pidfile letmeinfwd "$pid_letmeinfwd"
  61. wait_for_pidfile letmeind "$pid_letmeind"
  62. info "Knocking..."
  63. "$target/letmein" \
  64. --verbose \
  65. --config "$conf" \
  66. knock \
  67. --user 12345678 \
  68. localhost 42 \
  69. || die "letmein knock failed"
  70. kill_all_and_wait
  71. }
  72. wait_for_pidfile()
  73. {
  74. local name="$1"
  75. local pid="$2"
  76. for i in $(seq 0 29); do
  77. if [ -r "$rundir/$name/$name.pid" ]; then
  78. if [ "$pid" != "$(cat "$rundir/$name/$name.pid")" ]; then
  79. die "$name: Invalid PID-file."
  80. fi
  81. return
  82. fi
  83. sleep 0.1
  84. done
  85. die "$name PID-file is missing. Did $name fail to start?"
  86. }
  87. kill_all()
  88. {
  89. kill_letmeind
  90. kill_letmeinfwd
  91. }
  92. kill_all_and_wait()
  93. {
  94. kill_all
  95. wait
  96. }
  97. kill_letmeinfwd()
  98. {
  99. if [ -n "$pid_letmeinfwd" ]; then
  100. kill -TERM "$pid_letmeinfwd" >/dev/null 2>&1
  101. pid_letmeinfwd=
  102. fi
  103. }
  104. kill_letmeind()
  105. {
  106. if [ -n "$pid_letmeind" ]; then
  107. kill -TERM "$pid_letmeind" >/dev/null 2>&1
  108. pid_letmeind=
  109. fi
  110. }
  111. cleanup()
  112. {
  113. kill_all
  114. if [ -n "$tmpdir" ]; then
  115. rm -rf "$tmpdir"
  116. tmpdir=
  117. fi
  118. }
  119. cleanup_and_exit()
  120. {
  121. cleanup
  122. exit 1
  123. }
  124. pid_letmeinfwd=
  125. pid_letmeind=
  126. [ -n "$TMPDIR" ] || export TMPDIR=/tmp
  127. tmpdir="$(mktemp --tmpdir="$TMPDIR" -d letmein-test.XXXXXXXXXX)"
  128. [ -d "$tmpdir" ] || die "Failed to create temporary directory"
  129. tmpbin="$tmpdir/bin"
  130. rundir="$tmpdir/run"
  131. target="$basedir/target/debug"
  132. testdir="$basedir/tests"
  133. stubdir="$testdir/stubs"
  134. export PATH="$tmpbin:$PATH"
  135. trap cleanup_and_exit INT TERM
  136. trap cleanup EXIT
  137. info "Temporary directory is: $tmpdir"
  138. build_project
  139. cargo_clippy
  140. build_stubs
  141. run_tests tcp
  142. run_tests udp
  143. info "All tests Ok."
  144. # vim: ts=4 sw=4 expandtab