From 37e29bb56e6c35b097070ac095f081a38b67ebec Mon Sep 17 00:00:00 2001 From: Reuben Miller Date: Tue, 25 Apr 2023 12:06:43 +0200 Subject: [PATCH] feat(test): mqtt logger reads mqtt host via tedge Signed-off-by: Reuben Miller --- .../requirements/requirements.txt | 2 +- .../tests/mqtt/remote_mqtt_broker.robot | 93 +++++++++++++++++++ tests/images/debian-systemd/files/mqtt-logger | 18 +++- 3 files changed, 110 insertions(+), 3 deletions(-) create mode 100644 tests/RobotFramework/tests/mqtt/remote_mqtt_broker.robot diff --git a/tests/RobotFramework/requirements/requirements.txt b/tests/RobotFramework/requirements/requirements.txt index 5ecd7d7be38..b5a3562068c 100644 --- a/tests/RobotFramework/requirements/requirements.txt +++ b/tests/RobotFramework/requirements/requirements.txt @@ -2,7 +2,7 @@ dateparser~=1.1.7 paho-mqtt~=1.6.1 python-dotenv~=0.20.0 robotframework~=6.0.0 -robotframework-c8y @ git+https://github.com/reubenmiller/robotframework-c8y.git@0.13.4 +robotframework-c8y @ git+https://github.com/reubenmiller/robotframework-c8y.git@0.15.0 robotframework-debuglibrary~=2.3.0 robotframework-jsonlibrary~=0.5 robotframework-pabot~=2.13.0 diff --git a/tests/RobotFramework/tests/mqtt/remote_mqtt_broker.robot b/tests/RobotFramework/tests/mqtt/remote_mqtt_broker.robot new file mode 100644 index 00000000000..fe6dfebc50e --- /dev/null +++ b/tests/RobotFramework/tests/mqtt/remote_mqtt_broker.robot @@ -0,0 +1,93 @@ +*** Settings *** +Resource ../../resources/common.resource +Library ThinEdgeIO +Library Cumulocity + +Test Tags theme:mqtt theme:c8y adapter:docker +Suite Setup Custom Setup +Suite Teardown Get Logs name=${CONTAINER_1} + +*** Variables *** +${CONTAINER_1} +${CONTAINER_2} + +*** Test Cases *** +Check remote mqtt broker #1773 + [Documentation] The test relies on two containers functioning as one logical device. 1 container (CONTAINER_1) + ... runs the mqtt broker and the mapper, and the other container (CONTAINER_2) runs the other tedge components. + ... Together the two containers should function similar to installing everything in one container (more or less). + ... This is the building blocks for enabling a single-process container setup where each thin-edge.io component + ... is running in its own container. + [Tags] \#1773 + ThinEdgeIO.Set Device Context ${CONTAINER_1} + ThinEdgeIO.Service Should Be Running mosquitto + ThinEdgeIO.Service Should Be Running tedge-mapper-c8y + ThinEdgeIO.Service Should Be Stopped tedge-agent + ThinEdgeIO.Service Should Be Stopped c8y-configuration-plugin + ThinEdgeIO.Service Should Be Stopped c8y-firmware-plugin + ThinEdgeIO.Service Should Be Stopped c8y-log-plugin + + ThinEdgeIO.Set Device Context ${CONTAINER_2} + ThinEdgeIO.Service Should Be Stopped mosquitto + ThinEdgeIO.Service Should Be Stopped tedge-mapper-c8y + ThinEdgeIO.Service Should Be Running tedge-agent + ThinEdgeIO.Service Should Be Running c8y-configuration-plugin + ThinEdgeIO.Service Should Be Running c8y-firmware-plugin + ThinEdgeIO.Service Should Be Running c8y-log-plugin + + # Validate the device exists in the cloud + Cumulocity.Device Should Exist ${CONTAINER_1} + + # Cumulocity sanity check + ThinEdgeIO.Set Device Context ${CONTAINER_2} + ThinEdgeIO.Execute Command tedge mqtt pub tedge/measurements '{"temperature": 29.8}' + ${measurements}= Cumulocity.Device Should Have Measurements value=temperature series=temperature + Should Be Equal As Numbers ${measurements[0]["temperature"]["temperature"]["value"]} ${29.8} + + Cumulocity.Should Have Services name=tedge-mapper-c8y status=up + Cumulocity.Should Have Services name=tedge-agent status=up + Cumulocity.Should Have Services name=c8y-configuration-plugin status=up + Cumulocity.Should Have Services name=c8y-firmware-plugin status=up + Cumulocity.Should Have Services name=c8y-log-plugin status=up + +*** Keywords *** + +Custom Setup + # Container 1 running mqtt host and mapper + ${CONTAINER_1}= Setup skip_bootstrap=${True} + Execute Command ./bootstrap.sh --no-connect --bootstrap + Set Suite Variable $CONTAINER_1 + ${CONTAINER_1_IP}= Get IP Address + Disconnect Mapper c8y + Execute Command sudo tedge config set mqtt.client.host ${CONTAINER_1_IP} + Execute Command sudo tedge config set mqtt.client.port 1883 + Execute Command sudo tedge config set mqtt.bind_address ${CONTAINER_1_IP} + Connect Mapper c8y + Restart Service mqtt-logger + + Stop Service tedge-agent + Stop Service c8y-configuration-plugin + Stop Service c8y-log-plugin + Stop Service c8y-firmware-plugin + + # Copy files form one device to another + ${tedge_toml}= Execute Command cat /etc/tedge/tedge.toml + ${pem}= Execute Command cat "$(tedge config get device.cert.path)" + + # container 2 running all other services + ${CONTAINER_2}= Setup skip_bootstrap=${True} + Execute Command ./bootstrap.sh --no-connect --no-bootstrap + Set Suite Variable $CONTAINER_2 + + # Stop services that don't need to be running on the second device + Stop Service mosquitto + Stop Service tedge-mapper-c8y + + Execute Command echo "${tedge_toml}" | sudo tee /etc/tedge/tedge.toml + Execute Command sudo tedge config set mqtt.client.host ${CONTAINER_1_IP} + Execute Command sudo tedge config set mqtt.client.port 1883 + Execute Command echo "${pem}" | sudo tee "$(tedge config get device.cert.path)" + Restart Service c8y-firmware-plugin + Restart Service c8y-log-plugin + Restart Service c8y-configuration-plugin + Restart Service tedge-agent diff --git a/tests/images/debian-systemd/files/mqtt-logger b/tests/images/debian-systemd/files/mqtt-logger index dd0a30350e5..bc15594559a 100755 --- a/tests/images/debian-systemd/files/mqtt-logger +++ b/tests/images/debian-systemd/files/mqtt-logger @@ -1,8 +1,8 @@ #!/bin/sh set -e -MQTT_HOST=${MQTT_HOST:-localhost} -MQTT_PORT=${MQTT_PORT:-1883} +MQTT_HOST=${MQTT_HOST:-} +MQTT_PORT=${MQTT_PORT:-} OUTPUT_LOG=${OUTPUT_LOG:-} WAIT=${WAIT:-1} @@ -25,6 +25,16 @@ usage() { " } +# Use defaults from tedge if they are set +if command -v tedge >/dev/null 2>&1; then + if [ -z "$MQTT_HOST" ]; then + MQTT_HOST=$(tedge config get mqtt.client.host) + fi + if [ -z "$MQTT_PORT" ]; then + MQTT_PORT=$(tedge config get mqtt.client.port) + fi +fi + while [ $# -gt 0 ]; do case "$1" in --host) @@ -56,6 +66,10 @@ while [ $# -gt 0 ]; do shift done +# set defaults +MQTT_HOST=${MQTT_HOST:-localhost} +MQTT_PORT=${MQTT_PORT:-1883} + log() { echo "$*" >&2 }