test: enable mosquitto password auth; add mqttping endpoint; use credentials in subscriber; publish defaults

This commit is contained in:
2025-12-28 15:24:12 +01:00
parent 8fd9353068
commit c835fb7a98
5 changed files with 86 additions and 41 deletions
+6
View File
@@ -14,6 +14,8 @@ services:
- MQTT_BROKER=tcp://mosquitto:1883
- MQTT_CLIENT_ID=lambdaiot-server
- MQTT_TOPIC=lambdaiot
- MQTT_USERNAME=testuser
- MQTT_PASSWORD=testpass
depends_on:
- mosquitto
# server image now waits for MQTT broker itself via entrypoint
@@ -27,6 +29,8 @@ services:
- mosquitto_data:/mosquitto/data
- mosquitto_log:/mosquitto/log
- ./mosquitto.conf:/mosquitto/config/mosquitto.conf:ro
# ensure a password file exists and start mosquitto with our config
command: sh -c "mosquitto_passwd -b /mosquitto/config/passwordfile testuser testpass >/dev/null 2>&1 || true; /usr/sbin/mosquitto -c /mosquitto/config/mosquitto.conf"
healthcheck:
test: ["CMD", "sh", "-c", "nc -z localhost 1883 || exit 1"]
interval: 2s
@@ -41,6 +45,8 @@ services:
environment:
- MQTT_BROKER=mosquitto:1883
- MQTT_TOPIC=lambdaiot
- MQTT_USERNAME=testuser
- MQTT_PASSWORD=testpass
command: sh -c "pip install paho-mqtt && python subscribe.py"
depends_on:
- mosquitto
+4 -1
View File
@@ -1,6 +1,9 @@
# Allow external connections on port 1883
# listen on all interfaces
listener 1883 0.0.0.0
allow_anonymous true
# do not allow anonymous in this test stack; require password_file
allow_anonymous false
password_file /mosquitto/config/passwordfile
# Increase persistence location so container can map volume if needed
persistence true
persistence_location /mosquitto/data/
+4
View File
@@ -6,6 +6,8 @@ import paho.mqtt.client as mqtt
broker = os.getenv('MQTT_BROKER', 'localhost:1883')
topic = os.getenv('MQTT_TOPIC', 'lambdaiot')
username = os.getenv('MQTT_USERNAME')
password = os.getenv('MQTT_PASSWORD')
if broker.startswith('tcp://'):
broker = broker[len('tcp://'):]
@@ -28,6 +30,8 @@ def on_message(client, userdata, msg):
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
if username:
client.username_pw_set(username, password)
try:
client.connect(host, port, 60)