run.sh 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/bin/bash
  2. set -eo pipefail
  3. # Test basic mosquitto broker and clients operation.
  4. #
  5. # 1. Run the broker.
  6. # 2. Use mosquitto_pub to publish a retained message with a random payload to a
  7. # random topic
  8. # 3. Use mosquitto_sub to subscribe to the random topic, and retrieve the
  9. # payload.
  10. #
  11. # mosquitto_sub times out after two seconds if the message is not delivered.
  12. dir="$(dirname "$(readlink -f "$BASH_SOURCE")")"
  13. image="$1"
  14. # Create an instance of the container-under-test
  15. serverImage="$("$dir/../image-name.sh" librarytest/eclipse-mosquitto-basics "$image")"
  16. "$dir/../docker-build.sh" "$dir" "$serverImage" <<EOD
  17. FROM $image
  18. COPY dir/mosquitto.conf /mosquitto/config/
  19. EOD
  20. cname="eclipse-mosquitto-container-$RANDOM-$RANDOM"
  21. cid="$(docker run -d \
  22. --name "$cname" \
  23. "$serverImage")"
  24. trap "docker rm -vf $cid > /dev/null" EXIT
  25. _publish() {
  26. local topic="${1}"
  27. shift
  28. local payload="${1}"
  29. shift
  30. docker run --rm \
  31. --link "$cid":eclipse-mosquitto \
  32. "$serverImage" \
  33. mosquitto_pub \
  34. -t $topic \
  35. -m ${payload} \
  36. -r \
  37. -h eclipse-mosquitto
  38. }
  39. _subscribe() {
  40. local topic="${1}"
  41. shift
  42. docker run --rm \
  43. --link "$cid":eclipse-mosquitto \
  44. "$serverImage" \
  45. mosquitto_sub \
  46. -t $topic \
  47. -C 1 \
  48. -W 2 \
  49. -h eclipse-mosquitto
  50. }
  51. topic="topic-$RANDOM"
  52. payload="$RANDOM"
  53. . "$dir/../../retry.sh" --tries 20 "_publish $topic $payload"
  54. response="$(_subscribe $topic)"
  55. [[ "$response" == "$payload" ]]