|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +DEBUG=false |
| 5 | +LOCAL_RUN=false |
| 6 | + |
| 7 | +DOMAIN='example.org' |
| 8 | +HOST='localhost' |
| 9 | + |
| 10 | +usage() |
| 11 | +{ |
| 12 | + echo "Usage: $0 [-d] [-l] [-i IPADDRESS] [-h HOST] [-s DOMAIN]" |
| 13 | + echo " -d: Enable debug mode. Prints commands, and preserves temp directories if used (default: off)" |
| 14 | + echo " -l: Launch a local Openfire. (default: off)" |
| 15 | + echo " -i: Set a hosts file for the given IP and host (or for example.com if running locally). Reverted at exit." |
| 16 | + echo " -h: The hostname for the Openfire under test (default: localhost)" |
| 17 | + echo " -s: The XMPP domain name for the Openfire under test (default: example.org)" |
| 18 | + exit 2 |
| 19 | +} |
| 20 | + |
| 21 | +while getopts dlh:i:s: OPTION "$@"; do |
| 22 | + case $OPTION in |
| 23 | + d) |
| 24 | + DEBUG=true |
| 25 | + set -x |
| 26 | + ;; |
| 27 | + l) |
| 28 | + LOCAL_RUN=true |
| 29 | + ;; |
| 30 | + h) |
| 31 | + HOST="${OPTARG}" |
| 32 | + ;; |
| 33 | + i) |
| 34 | + IPADDRESS="${OPTARG}" |
| 35 | + ;; |
| 36 | + s) |
| 37 | + DOMAIN="${OPTARG}" |
| 38 | + ;; |
| 39 | + \? ) usage;; |
| 40 | + : ) usage;; |
| 41 | + * ) usage;; |
| 42 | + esac |
| 43 | +done |
| 44 | + |
| 45 | +if [[ $LOCAL_RUN == true ]] && [[ $DOMAIN != "example.org" ]]; then |
| 46 | + echo "Domain is fixed if launching a local instance. If you have an already-running instance to test against, omit the -l flag (and provide -h 127.0.0.1 if necessary)." |
| 47 | + exit 1 |
| 48 | +fi |
| 49 | + |
| 50 | +function setBaseDirectory { |
| 51 | + # Pretty fancy method to get reliable the absolute path of a shell |
| 52 | + # script, *even if it is sourced*. Credits go to GreenFox on |
| 53 | + # stackoverflow: http://stackoverflow.com/a/12197518/194894 |
| 54 | + pushd . > /dev/null |
| 55 | + SCRIPTDIR="${BASH_SOURCE[0]}"; |
| 56 | + while [ -h "${SCRIPTDIR}" ]; do |
| 57 | + cd "$(dirname "${SCRIPTDIR}")" |
| 58 | + SCRIPTDIR="$(readlink "$(basename "${SCRIPTDIR}")")"; |
| 59 | + done |
| 60 | + cd "$(dirname "${SCRIPTDIR}")" > /dev/null |
| 61 | + SCRIPTDIR="$(pwd)"; |
| 62 | + popd > /dev/null |
| 63 | + BASEDIR="${SCRIPTDIR}" |
| 64 | + cd "${BASEDIR}" |
| 65 | +} |
| 66 | + |
| 67 | +function createTempDirectory { |
| 68 | + OFTEMPDIR=$(mktemp -d) |
| 69 | +} |
| 70 | + |
| 71 | +function cleanup { |
| 72 | + if [[ $DEBUG == false && -n "${OFTEMPDIR-}" ]]; then |
| 73 | + echo "Removing temp directories" |
| 74 | + rm -rf "${OFTEMPDIR}" |
| 75 | + fi |
| 76 | + if [[ $LOCAL_RUN == true ]]; then |
| 77 | + echo "Stopping Openfire" |
| 78 | + pkill -f openfire.lib #TODO: Can this be made more future-proof against changes in the start script? |
| 79 | + fi |
| 80 | +} |
| 81 | + |
| 82 | +function setUpMelliumEnvironment { |
| 83 | + MELLIUMDIR="${BASEDIR}/mellium" |
| 84 | + AIOXMPPCONFIG="${MELLIUMDIR}/openfire-config.ini" |
| 85 | + if [ -d "${MELLIUMDIR}" ]; then |
| 86 | + pushd "${MELLIUMDIR}" |
| 87 | + git pull |
| 88 | + else |
| 89 | + git clone -b main https://github.com/mellium/xmpp "${MELLIUMDIR}" |
| 90 | + pushd "${MELLIUMDIR}" |
| 91 | + fi |
| 92 | + popd |
| 93 | +} |
| 94 | + |
| 95 | +function setHostsFile { |
| 96 | + if [[ -n "${IPADDRESS-}" ]]; then |
| 97 | + echo "Setting hosts file for local running. This may prompt for sudo." |
| 98 | + sudo /bin/sh -c "echo \"$IPADDRESS $HOST\" >> /etc/hosts" |
| 99 | + fi |
| 100 | +} |
| 101 | + |
| 102 | +function launchOpenfire { |
| 103 | + declare -r OPENFIRE_SHELL_SCRIPT="${BASEDIR}/distribution/target/distribution-base/bin/openfire.sh" |
| 104 | + |
| 105 | + if [[ ! -f "${OPENFIRE_SHELL_SCRIPT}" ]]; then |
| 106 | + mvn verify -P ci |
| 107 | + fi |
| 108 | + |
| 109 | + rm -f distribution/target/distribution-base/conf/openfire.xml |
| 110 | + cp distribution/target/distribution-base/conf/openfire-demoboot.xml \ |
| 111 | + distribution/target/distribution-base/conf/openfire.xml |
| 112 | + |
| 113 | + echo "Starting Openfire…" |
| 114 | + "${OPENFIRE_SHELL_SCRIPT}" & |
| 115 | + |
| 116 | + # Wait 120 seconds for Openfire to open up the web interface and |
| 117 | + # assume Openfire is fully operational once that happens (not sure if |
| 118 | + # this assumption is correct). |
| 119 | + echo "Waiting for Openfire…" |
| 120 | + timeout 120 bash -c 'until printf "" 2>>/dev/null >>/dev/tcp/$0/$1; do sleep 0.3; done' localhost 7070 |
| 121 | +} |
| 122 | + |
| 123 | +function runTests { |
| 124 | + echo "Starting Integration Tests (using Mellium)…" |
| 125 | + pushd "${MELLIUMDIR}" |
| 126 | + if [ -d output ]; then rm -Rf output; fi |
| 127 | + mkdir output |
| 128 | + |
| 129 | + pushd "${MELLIUMDIR}/examples/echobot" |
| 130 | + go build |
| 131 | + ./echobot -vv 2>&1 | tee "${MELLIUMDIR}/output/mellium.test.output.txt" |
| 132 | + if [ ${PIPESTATUS[0]} -ne 0 ]; then false; fi; |
| 133 | + popd |
| 134 | + |
| 135 | + # Mellium's integration test require hooks that will launch new servers on demand. Not currently supported by this script. |
| 136 | + # go build |
| 137 | + # go test -v -tags "integration" -run Integration ./... 2>&1 | tee output/mellium.test.output.txt |
| 138 | + # if [ ${PIPESTATUS[0]} -ne 0 ]; then false; fi; |
| 139 | + popd |
| 140 | +} |
| 141 | + |
| 142 | +setBaseDirectory |
| 143 | +trap cleanup EXIT |
| 144 | +setUpMelliumEnvironment |
| 145 | +if [[ -n "${IPADDRESS-}" ]]; then |
| 146 | + setHostsFile |
| 147 | +fi |
| 148 | +if [[ $LOCAL_RUN == true ]]; then |
| 149 | + launchOpenfire |
| 150 | +fi |
| 151 | +runTests |
0 commit comments