Skip to content

Commit

Permalink
Add c8y_Command and c8y_RelayArray system tests with workflow
Browse files Browse the repository at this point in the history
Signed-off-by: Rina Fujino <[email protected]>
  • Loading branch information
rina23q committed Dec 10, 2024
1 parent f041423 commit 53633f8
Show file tree
Hide file tree
Showing 10 changed files with 201 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[exec]
topic = "c8y/devicecontrol/notifications"
on_fragment = "c8y_RelayArray"

[exec.workflow]
operation = "set_relay"
input.relay = "${.payload.c8y_RelayArray}"
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
*** Settings ***
Resource ../../../../resources/common.resource
Library Cumulocity
Library ThinEdgeIO

Suite Setup Custom Setup
Test Teardown Get Logs

Test Tags theme:c8y theme:troubleshooting theme:plugins


*** Variables ***
${PARENT_IP} ${EMPTY}
${PARENT_SN} ${EMPTY}


*** Test Cases ***
Run c8y_RelayArray operation with workflow execution
Symlink Should Exist /etc/tedge/operations/c8y/c8y_RelayArray
Cumulocity.Should Contain Supported Operations c8y_RelayArray

${operation}= Cumulocity.Create Operation
... description=Set relays
... fragments={"c8y_RelayArray":["OPEN", "CLOSED"]}

Should Have MQTT Messages
... c8y/s/us
... message_pattern=^506,[0-9]+,OPEN,CLOSED
... minimum=1
... maximum=1
Cumulocity.Operation Should Be SUCCESSFUL ${operation}
Cumulocity.Managed Object Should Have Fragment Values c8y_RelayArray\=["OPEN", "CLOSED"]


*** Keywords ***
Transfer Configuration Files
Transfer To Device ${CURDIR}/c8y_RelayArray.template /etc/tedge/operations/c8y/
Transfer To Device ${CURDIR}/set_relay.toml /etc/tedge/operations/
Transfer To Device ${CURDIR}/set_relay.sh /etc/tedge/operations/
Execute Command chmod a+x /etc/tedge/operations/set_relay.sh

Custom Setup
# Parent
${parent_sn}= Setup skip_bootstrap=False
Set Suite Variable $PARENT_SN ${parent_sn}

${parent_ip}= Get IP Address
Set Suite Variable $PARENT_IP ${parent_ip}

Set Device Context ${PARENT_SN}
Transfer Configuration Files
Execute Command tedge config set mqtt.external.bind.address ${PARENT_IP}
Execute Command tedge config set mqtt.external.bind.port 1883
Execute Command tedge reconnect c8y

Cumulocity.Device Should Exist ${PARENT_SN}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh

MESSAGE="$1"

echo :::begin-tedge:::
printf '{"result":%s}\n' "$MESSAGE"
echo :::end-tedge:::
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
operation = "set_relay"

[init]
action = "proceed"
on_success = "executing"

[executing]
action = "proceed"
on_success = "run"

[run]
script = "/etc/tedge/operations/set_relay.sh ${.payload.relay}"
on_success = "successful"
on_error = { status = "failed", reason = "Command returned a non-zero exit code" }

[successful]
action = "cleanup"

[failed]
action = "cleanup"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[exec]
topic = "c8y/devicecontrol/notifications"
on_fragment = "c8y_Command"

[exec.workflow]
operation = "shell_execute"
input.command = "${.payload.c8y_Command.text}"
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
*** Settings ***
Resource ../../../../resources/common.resource
Library Cumulocity
Library ThinEdgeIO

Suite Setup Custom Setup
Test Teardown Get Logs

Test Tags theme:c8y theme:troubleshooting theme:plugins


*** Variables ***
${PARENT_IP} ${EMPTY}
${PARENT_SN} ${EMPTY}


*** Test Cases ***
Run c8y_Command operation with workflow execution
Symlink Should Exist /etc/tedge/operations/c8y/c8y_Command
Cumulocity.Should Contain Supported Operations c8y_Command

${operation}= Cumulocity.Create Operation
... description=echo helloworld
... fragments={"c8y_Command":{"text":"echo helloworld"}}

Should Have MQTT Messages
... c8y/s/us
... message_pattern=^506,[0-9]+($|,\\"helloworld\n\\")
... minimum=1
... maximum=1
Cumulocity.Operation Should Be SUCCESSFUL ${operation}


*** Keywords ***
Transfer Configuration Files
Transfer To Device ${CURDIR}/c8y_Command.template /etc/tedge/operations/c8y/
Transfer To Device ${CURDIR}/shell_execute.toml /etc/tedge/operations/
Transfer To Device ${CURDIR}/shell_execute.sh /etc/tedge/operations/
Execute Command chmod a+x /etc/tedge/operations/shell_execute.sh

Custom Setup
# Parent
${parent_sn}= Setup skip_bootstrap=False
Set Suite Variable $PARENT_SN ${parent_sn}

${parent_ip}= Get IP Address
Set Suite Variable $PARENT_IP ${parent_ip}

Set Device Context ${PARENT_SN}
Transfer Configuration Files
Execute Command tedge config set mqtt.external.bind.address ${PARENT_IP}
Execute Command tedge config set mqtt.external.bind.port 1883
Execute Command tedge reconnect c8y

Cumulocity.Device Should Exist ${PARENT_SN}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash

info() {
echo "$(date --iso-8601=seconds 2>/dev/null || date -Iseconds) : INFO : $*" >&2
}

# Parse the message
COMMAND="${1}"

TMP_OUTPUT=$(mktemp)
info "Writing command output to file. path=$TMP_OUTPUT"

EXIT_CODE=0

set +e
bash -c "$COMMAND" >"$TMP_OUTPUT" 2>&1
EXIT_CODE=$?
set -e

if [ "${EXIT_CODE}" -ne 0 ]; then
info "Command returned a non-zero exit code. code=$EXIT_CODE"
fi

echo :::begin-tedge:::
printf '{"result":%s}\n' "$(jq -R -s '.' < "$TMP_OUTPUT")"
echo :::end-tedge:::

exit "$EXIT_CODE"
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
operation = "shell_execute"

[init]
action = "proceed"
on_success = "executing"

[executing]
action = "proceed"
on_success = "run"

[run]
script = "/etc/tedge/operations/shell_execute.sh ${.payload.command}"
on_success = "successful"
on_error = { status = "failed", reason = "Command returned a non-zero exit code" }

[successful]
action = "cleanup"

[failed]
action = "cleanup"
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ Run custom operation with workflow execution
... description=take a picture
... fragments={"c8y_TakePicture":{"parameters": {"duration": "5s", "quality": "HD"}}}
Verify Local Command main take_picture

Should Have MQTT Messages
... c8y/s/us
... minimum=1
... maximum=1
... message_pattern=^506,[0-9]+,(5s HD)
Cumulocity.Operation Should Be SUCCESSFUL ${operation}

Add template and workflow file dynamically
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ ARG1=$1
ARG2=$2

echo ':::begin-tedge:::'
printf '{"result":"%s %s"}\n' "$ARG1" "$ARG2"
printf '{"something":"%s %s"}\n' "$ARG1" "$ARG2"
echo ':::end-tedge:::'

0 comments on commit 53633f8

Please sign in to comment.