run-tests.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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_tcp_tests()
  42. {
  43. info "### Running test: TCP ###"
  44. rm -rf "$rundir"
  45. local conf="$testdir/conf/tcp.conf"
  46. info "Starting letmeinfwd..."
  47. "$target/letmeinfwd" \
  48. --test-mode \
  49. --no-systemd \
  50. --rundir "$rundir" \
  51. --config "$conf" &
  52. pid_letmeinfwd=$!
  53. info "Starting letmeind..."
  54. "$target/letmeind" \
  55. --no-systemd \
  56. --rundir "$rundir" \
  57. --config "$conf" &
  58. pid_letmeind=$!
  59. sleep 1
  60. check_pidfiles
  61. info "Knocking..."
  62. "$target/letmein" \
  63. --config "$conf" \
  64. knock \
  65. --user 12345678 \
  66. localhost 42 \
  67. || die "letmein knock failed"
  68. kill_all_and_wait
  69. }
  70. check_pidfile()
  71. {
  72. local name="$1"
  73. local pid="$2"
  74. if [ -r "$rundir/$name/$name.pid" ]; then
  75. if [ "$pid" != "$(cat "$rundir/$name/$name.pid")" ]; then
  76. die "$name: Invalid PID-file."
  77. fi
  78. else
  79. die "$name PID-file is missing. Did $name fail to start?"
  80. fi
  81. }
  82. check_pidfiles()
  83. {
  84. check_pidfile letmeinfwd "$pid_letmeinfwd"
  85. check_pidfile letmeind "$pid_letmeind"
  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. tmpdir="$(mktemp -d --tmpdir=/tmp letmein-test.XXXXXXXXXX)"
  127. [ -d "$tmpdir" ] || die "Failed to create temporary directory"
  128. tmpbin="$tmpdir/bin"
  129. rundir="$tmpdir/run"
  130. target="$basedir/target/debug"
  131. testdir="$basedir/tests"
  132. stubdir="$testdir/stubs"
  133. export PATH="$tmpbin:$PATH"
  134. trap cleanup_and_exit INT TERM
  135. trap cleanup EXIT
  136. info "Temporary directory is: $tmpdir"
  137. build_project
  138. cargo_clippy
  139. build_stubs
  140. run_tcp_tests
  141. info "All tests Ok."
  142. # vim: ts=4 sw=4 expandtab