Skip to content

Commit 1921e2c

Browse files
Add integration test
Signed-off-by: Kate Goldenring <[email protected]>
1 parent 586daa1 commit 1921e2c

File tree

2 files changed

+222
-1
lines changed

2 files changed

+222
-1
lines changed

Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,9 @@ clean:
3636
cargo clean --manifest-path ./examples/mqtt-app/Cargo.toml
3737
rm -f trigger-mqtt-*.tar.gz
3838
rm -f trigger-mqtt.json
39-
spin plugin uninstall trigger-mqtt
39+
spin plugin uninstall trigger-mqtt
40+
41+
.PHONY: test
42+
test:
43+
@echo "Running integration test..."
44+
bash tests/integration_test.sh

tests/integration_test.sh

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6+
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
7+
MQTT_CONTAINER_NAME="emqx-test"
8+
MQTT_HOST="localhost"
9+
MQTT_PORT="1883"
10+
MQTT_USERNAME="admin"
11+
MQTT_PASSWORD="public"
12+
TEST_TOPIC="messages-in01"
13+
TEST_MESSAGE="Hello to MQTT Spin Component!"
14+
15+
# Colors for output
16+
RED='\033[0;31m'
17+
GREEN='\033[0;32m'
18+
YELLOW='\033[1;33m'
19+
NC='\033[0m' # No Color
20+
21+
log() {
22+
echo -e "${GREEN}[$(date +'%Y-%m-%d %H:%M:%S')] $1${NC}"
23+
}
24+
25+
warn() {
26+
echo -e "${YELLOW}[$(date +'%Y-%m-%d %H:%M:%S')] WARNING: $1${NC}"
27+
}
28+
29+
error() {
30+
echo -e "${RED}[$(date +'%Y-%m-%d %H:%M:%S')] ERROR: $1${NC}"
31+
}
32+
33+
cleanup() {
34+
log "Cleaning up test environment..."
35+
36+
# Kill spin process if running
37+
if [ ! -z "${SPIN_PID:-}" ]; then
38+
if kill -0 "$SPIN_PID" 2>/dev/null; then
39+
log "Stopping Spin application (PID: $SPIN_PID)..."
40+
kill "$SPIN_PID"
41+
wait "$SPIN_PID" 2>/dev/null || true
42+
log "Spin application stopped"
43+
fi
44+
fi
45+
46+
# Stop and remove MQTT broker container
47+
docker stop "$MQTT_CONTAINER_NAME" 2>/dev/null || true
48+
docker rm "$MQTT_CONTAINER_NAME" 2>/dev/null || true
49+
50+
log "Cleanup completed"
51+
}
52+
53+
# Set up cleanup trap
54+
trap cleanup EXIT
55+
56+
check_dependencies() {
57+
log "Checking dependencies..."
58+
59+
# Check if docker is available
60+
if ! command -v docker &> /dev/null; then
61+
error "Docker is required but not installed"
62+
exit 1
63+
fi
64+
65+
# Check if mqttx is available
66+
if ! command -v mqttx &> /dev/null; then
67+
error "mqttx CLI is required but not installed. Run 'brew install emqx/mqttx/mqttx-cli' or see installation instructions: https://mqttx.app/docs/get-started"
68+
exit 1
69+
fi
70+
71+
# Check if spin is available
72+
if ! command -v spin &> /dev/null; then
73+
error "Spin CLI is required but not installed"
74+
exit 1
75+
fi
76+
77+
log "Dependencies check completed"
78+
}
79+
80+
start_mqtt_broker() {
81+
log "Starting MQTT broker..."
82+
83+
# Stop existing container if running
84+
docker stop "$MQTT_CONTAINER_NAME" 2>/dev/null || true
85+
docker rm "$MQTT_CONTAINER_NAME" 2>/dev/null || true
86+
87+
# Start EMQX broker
88+
docker run -d \
89+
--name "$MQTT_CONTAINER_NAME" \
90+
-p 1883:1883 \
91+
-p 8083:8083 \
92+
-p 8883:8883 \
93+
-p 8084:8084 \
94+
-p 18083:18083 \
95+
emqx/emqx
96+
97+
log "Waiting for MQTT broker to be ready..."
98+
99+
# Wait for broker to be ready (max 30 seconds)
100+
for i in {1..30}; do
101+
if mqttx pub -t "testing" -h "$MQTT_HOST" -p "$MQTT_PORT" -u "$MQTT_USERNAME" -P "$MQTT_PASSWORD" -m "test message" &>/dev/null; then
102+
log "MQTT broker is ready"
103+
return 0
104+
fi
105+
sleep 1
106+
done
107+
108+
error "MQTT broker failed to start or is not accessible"
109+
docker logs "$MQTT_CONTAINER_NAME"
110+
exit 1
111+
}
112+
113+
build_and_install_plugin() {
114+
log "Building and installing MQTT plugin..."
115+
116+
cd "$PROJECT_DIR"
117+
118+
# Run make to build and install plugin
119+
make clean || true
120+
make
121+
122+
log "Plugin built and installed successfully"
123+
}
124+
125+
start_spin_app() {
126+
log "Starting Spin application..."
127+
128+
cd "$PROJECT_DIR"
129+
130+
# Create log file for spin output (overwrite if exists)
131+
SPIN_LOG_DIR="$PROJECT_DIR/logs"
132+
SPIN_LOGS_STDOUT="$SPIN_LOG_DIR/mqtt-c01_stdout.txt"
133+
134+
# Build and start the example app in background, capturing output
135+
spin build --up --from examples/mqtt-app/spin.toml --log-dir "$SPIN_LOG_DIR" &
136+
SPIN_PID=$!
137+
138+
log "Waiting for Spin application to start..."
139+
140+
# Wait for spin app to be ready (max 30 seconds)
141+
for i in {1..30}; do
142+
if kill -0 "$SPIN_PID" 2>/dev/null; then
143+
sleep 2 # Give it a bit more time to fully initialize
144+
log "Spin application is running (PID: $SPIN_PID)"
145+
log "Spin logs being written to: $SPIN_LOG_DIR"
146+
return 0
147+
fi
148+
sleep 1
149+
done
150+
151+
error "Spin application failed to start"
152+
exit 1
153+
}
154+
155+
test_mqtt_message_flow() {
156+
log "Testing MQTT message flow..."
157+
158+
# Give the system a moment to stabilize
159+
sleep 3
160+
161+
log "Publishing test message to topic '$TEST_TOPIC'..."
162+
163+
# Publish message to MQTT broker
164+
mqttx pub \
165+
-t "$TEST_TOPIC" \
166+
-h "$MQTT_HOST" \
167+
-p "$MQTT_PORT" \
168+
-u "$MQTT_USERNAME" \
169+
-P "$MQTT_PASSWORD" \
170+
-m "$TEST_MESSAGE"
171+
172+
log "Message published successfully"
173+
174+
# Wait a bit for message processing
175+
sleep 5
176+
177+
# Check if spin process is still running (it should be)
178+
if ! kill -0 "$SPIN_PID" 2>/dev/null; then
179+
error "Spin application stopped unexpectedly"
180+
exit 1
181+
fi
182+
183+
# Check if the test message appears in the spin logs
184+
log "Checking if Spin application received the message..."
185+
if grep -q "$TEST_MESSAGE" "$SPIN_LOGS_STDOUT"; then
186+
log "✅ SUCCESS: Test message found in Spin application output!"
187+
else
188+
error "❌ FAILURE: Test message '$TEST_MESSAGE' not found in Spin output"
189+
log "Full Spin output:"
190+
cat "$SPIN_LOGS_STDOUT"
191+
exit 1
192+
fi
193+
194+
rm -rf "$SPIN_LOG_DIR" || true
195+
196+
log "MQTT message flow test completed successfully"
197+
}
198+
199+
run_integration_test() {
200+
log "Starting MQTT Trigger Plugin Integration Test"
201+
log "=============================================="
202+
203+
check_dependencies
204+
start_mqtt_broker
205+
build_and_install_plugin
206+
start_spin_app
207+
test_mqtt_message_flow
208+
209+
log "=============================================="
210+
log "Integration test completed successfully!"
211+
}
212+
213+
# Run the test if script is executed directly
214+
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
215+
run_integration_test
216+
fi

0 commit comments

Comments
 (0)