| 1 |
#!/bin/bash |
| 2 |
# |
| 3 |
# XMPP E2E test environment setup. |
| 4 |
# |
| 5 |
# Usage: |
| 6 |
# ./setup.sh [OPTIONS] [COMMAND] |
| 7 |
# |
| 8 |
# Commands: |
| 9 |
# start-server Start ejabberd using docker compose |
| 10 |
# stop-server Stop ejabberd |
| 11 |
# status Check ejabberd status |
| 12 |
# setup-all Start the server and wait for readiness (default) |
| 13 |
|
| 14 |
set -euo pipefail |
| 15 |
|
| 16 |
readonly RED='\033[0;31m' |
| 17 |
readonly GREEN='\033[0;32m' |
| 18 |
readonly BLUE='\033[0;34m' |
| 19 |
readonly NC='\033[0m' |
| 20 |
|
| 21 |
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" |
| 22 |
COMPOSE_FILE="${SCRIPT_DIR}/docker-compose.yaml" |
| 23 |
VERBOSE=false |
| 24 |
COMPOSE_CMD="" |
| 25 |
|
| 26 |
error() { |
| 27 |
echo -e "${RED}[ERROR]${NC} $1" >&2 |
| 28 |
exit 1 |
| 29 |
} |
| 30 |
|
| 31 |
info() { |
| 32 |
echo -e "${GREEN}[INFO]${NC} $1" |
| 33 |
} |
| 34 |
|
| 35 |
debug() { |
| 36 |
if [[ "$VERBOSE" == "true" ]]; then |
| 37 |
echo -e "${BLUE}[DEBUG]${NC} $1" |
| 38 |
fi |
| 39 |
} |
| 40 |
|
| 41 |
usage() { |
| 42 |
echo "XMPP E2E Test Environment Setup Script" |
| 43 |
echo "" |
| 44 |
echo "Usage:" |
| 45 |
echo " ./setup.sh [OPTIONS] [COMMAND]" |
| 46 |
echo "" |
| 47 |
echo "Commands:" |
| 48 |
echo " start-server Start ejabberd using docker compose" |
| 49 |
echo " stop-server Stop ejabberd" |
| 50 |
echo " status Check ejabberd status" |
| 51 |
echo " setup-all Start the server and wait for readiness (default)" |
| 52 |
} |
| 53 |
|
| 54 |
check_requirements() { |
| 55 |
if ! command -v openssl &> /dev/null; then |
| 56 |
error "Missing required command: openssl" |
| 57 |
fi |
| 58 |
|
| 59 |
if ! command -v docker &> /dev/null; then |
| 60 |
error "Missing required command: docker" |
| 61 |
fi |
| 62 |
|
| 63 |
if docker compose version &> /dev/null 2>&1; then |
| 64 |
COMPOSE_CMD="docker compose" |
| 65 |
elif command -v docker-compose &> /dev/null; then |
| 66 |
COMPOSE_CMD="docker-compose" |
| 67 |
else |
| 68 |
error "Missing required command: docker compose" |
| 69 |
fi |
| 70 |
} |
| 71 |
|
| 72 |
ensure_certs() { |
| 73 |
if [[ ! -f "${SCRIPT_DIR}/certs/server.pem" ]]; then |
| 74 |
info "Generating TLS certificates..." |
| 75 |
bash "${SCRIPT_DIR}/certs/generate.sh" |
| 76 |
fi |
| 77 |
} |
| 78 |
|
| 79 |
wait_for_server() { |
| 80 |
local max_attempts="${1:-30}" |
| 81 |
local attempt=1 |
| 82 |
|
| 83 |
info "Waiting for ejabberd to be ready..." |
| 84 |
|
| 85 |
while [[ $attempt -le $max_attempts ]]; do |
| 86 |
if docker exec shoutrrr-xmpp-ejabberd ejabberdctl status &> /dev/null; then |
| 87 |
info "ejabberd is ready." |
| 88 |
return 0 |
| 89 |
fi |
| 90 |
|
| 91 |
debug "Attempt ${attempt}/${max_attempts}: not ready yet..." |
| 92 |
sleep 2 |
| 93 |
((attempt++)) |
| 94 |
done |
| 95 |
|
| 96 |
error "ejabberd did not become ready after ${max_attempts} attempts" |
| 97 |
} |
| 98 |
|
| 99 |
start_server() { |
| 100 |
ensure_certs |
| 101 |
|
| 102 |
if [[ ! -f "$COMPOSE_FILE" ]]; then |
| 103 |
error "Docker compose file not found at ${COMPOSE_FILE}" |
| 104 |
fi |
| 105 |
|
| 106 |
cd "$SCRIPT_DIR" |
| 107 |
|
| 108 |
if $COMPOSE_CMD ps --status running 2>/dev/null | grep -q shoutrrr-xmpp-ejabberd; then |
| 109 |
info "Stopping existing ejabberd container..." |
| 110 |
$COMPOSE_CMD down || true |
| 111 |
fi |
| 112 |
|
| 113 |
if $COMPOSE_CMD up -d; then |
| 114 |
info "ejabberd started." |
| 115 |
else |
| 116 |
error "Failed to start ejabberd" |
| 117 |
fi |
| 118 |
} |
| 119 |
|
| 120 |
stop_server() { |
| 121 |
cd "$SCRIPT_DIR" |
| 122 |
if $COMPOSE_CMD down; then |
| 123 |
info "ejabberd stopped." |
| 124 |
else |
| 125 |
error "Failed to stop ejabberd" |
| 126 |
fi |
| 127 |
} |
| 128 |
|
| 129 |
check_status() { |
| 130 |
cd "$SCRIPT_DIR" |
| 131 |
$COMPOSE_CMD ps |
| 132 |
} |
| 133 |
|
| 134 |
setup_all() { |
| 135 |
check_requirements |
| 136 |
start_server |
| 137 |
wait_for_server |
| 138 |
|
| 139 |
info "XMPP E2E environment is ready." |
| 140 |
info "Export these variables before running tests:" |
| 141 |
info " export SHOUTRRR_XMPP_URL='xmpp://sender:senderpass@localhost:5222/?to=recipient@localhost&skiptlsverify=yes'" |
| 142 |
info " export SHOUTRRR_XMPP_TLS_URL='xmpps://sender:senderpass@localhost:5223/?to=recipient@localhost&skiptlsverify=yes'" |
| 143 |
info " export SHOUTRRR_XMPP_PLAIN_URL='xmpp://sender:senderpass@localhost:5224/?to=recipient@localhost&disabletls=yes'" |
| 144 |
info " export SHOUTRRR_XMPP_MUC_URL='xmpp://sender:senderpass@localhost:5222/[email protected]&skiptlsverify=yes'" |
| 145 |
info " export SHOUTRRR_XMPP_MUC_PASSWORD_URL='xmpp://sender:senderpass@localhost:5222/[email protected]&roompassword=roompass&skiptlsverify=yes'" |
| 146 |
info "Then: go test -v ./testing/e2e/xmpp/" |
| 147 |
} |
| 148 |
|
| 149 |
main() { |
| 150 |
local command="setup-all" |
| 151 |
|
| 152 |
while [[ $# -gt 0 ]]; do |
| 153 |
case "$1" in |
| 154 |
--help|-h) |
| 155 |
usage |
| 156 |
exit 0 |
| 157 |
;; |
| 158 |
--verbose|-v) |
| 159 |
VERBOSE=true |
| 160 |
shift |
| 161 |
;; |
| 162 |
start-server|stop-server|status|setup-all) |
| 163 |
command="$1" |
| 164 |
shift |
| 165 |
;; |
| 166 |
*) |
| 167 |
error "Unknown option or command: $1" |
| 168 |
;; |
| 169 |
esac |
| 170 |
done |
| 171 |
|
| 172 |
case "$command" in |
| 173 |
start-server) |
| 174 |
check_requirements |
| 175 |
start_server |
| 176 |
;; |
| 177 |
stop-server) |
| 178 |
check_requirements |
| 179 |
stop_server |
| 180 |
;; |
| 181 |
status) |
| 182 |
check_requirements |
| 183 |
check_status |
| 184 |
;; |
| 185 |
setup-all) |
| 186 |
setup_all |
| 187 |
;; |
| 188 |
esac |
| 189 |
} |
| 190 |
|
| 191 |
main "$@" |