Skip to content

Commit

Permalink
feat(test): mqtt logger reads mqtt host via tedge
Browse files Browse the repository at this point in the history
Signed-off-by: Reuben Miller <reuben.d.miller@gmail.com>
  • Loading branch information
reubenmiller committed Apr 25, 2023
1 parent ff0152e commit 37e29bb
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 3 deletions.
2 changes: 1 addition & 1 deletion tests/RobotFramework/requirements/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
93 changes: 93 additions & 0 deletions tests/RobotFramework/tests/mqtt/remote_mqtt_broker.robot
Original file line number Diff line number Diff line change
@@ -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
18 changes: 16 additions & 2 deletions tests/images/debian-systemd/files/mqtt-logger
Original file line number Diff line number Diff line change
@@ -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}

Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 37e29bb

Please sign in to comment.