From 53633f8be902a98d1ea76c01029cbd1e17044d9d Mon Sep 17 00:00:00 2001 From: Rina Fujino Date: Tue, 10 Dec 2024 16:30:23 +0000 Subject: [PATCH] Add c8y_Command and c8y_RelayArray system tests with workflow Signed-off-by: Rina Fujino --- .../c8y_RelayArray.template | 7 +++ .../operation_relay_array_workflow.robot | 56 +++++++++++++++++++ .../set_relay.sh | 7 +++ .../set_relay.toml | 20 +++++++ .../c8y_Command.template | 7 +++ .../operation_command_workflow.robot | 55 ++++++++++++++++++ .../shell_execute.sh | 28 ++++++++++ .../shell_execute.toml | 20 +++++++ .../custom_operation_workflow.robot | 6 -- .../custom_operation_workflow/do_something.sh | 2 +- 10 files changed, 201 insertions(+), 7 deletions(-) create mode 100644 tests/RobotFramework/tests/cumulocity/custom_operation/custom_operation_c8y_RelayArray_workflow/c8y_RelayArray.template create mode 100644 tests/RobotFramework/tests/cumulocity/custom_operation/custom_operation_c8y_RelayArray_workflow/operation_relay_array_workflow.robot create mode 100755 tests/RobotFramework/tests/cumulocity/custom_operation/custom_operation_c8y_RelayArray_workflow/set_relay.sh create mode 100644 tests/RobotFramework/tests/cumulocity/custom_operation/custom_operation_c8y_RelayArray_workflow/set_relay.toml create mode 100644 tests/RobotFramework/tests/cumulocity/custom_operation/custom_operation_command_workflow/c8y_Command.template create mode 100644 tests/RobotFramework/tests/cumulocity/custom_operation/custom_operation_command_workflow/operation_command_workflow.robot create mode 100755 tests/RobotFramework/tests/cumulocity/custom_operation/custom_operation_command_workflow/shell_execute.sh create mode 100644 tests/RobotFramework/tests/cumulocity/custom_operation/custom_operation_command_workflow/shell_execute.toml diff --git a/tests/RobotFramework/tests/cumulocity/custom_operation/custom_operation_c8y_RelayArray_workflow/c8y_RelayArray.template b/tests/RobotFramework/tests/cumulocity/custom_operation/custom_operation_c8y_RelayArray_workflow/c8y_RelayArray.template new file mode 100644 index 00000000000..d82fa7e37b5 --- /dev/null +++ b/tests/RobotFramework/tests/cumulocity/custom_operation/custom_operation_c8y_RelayArray_workflow/c8y_RelayArray.template @@ -0,0 +1,7 @@ +[exec] +topic = "c8y/devicecontrol/notifications" +on_fragment = "c8y_RelayArray" + +[exec.workflow] +operation = "set_relay" +input.relay = "${.payload.c8y_RelayArray}" \ No newline at end of file diff --git a/tests/RobotFramework/tests/cumulocity/custom_operation/custom_operation_c8y_RelayArray_workflow/operation_relay_array_workflow.robot b/tests/RobotFramework/tests/cumulocity/custom_operation/custom_operation_c8y_RelayArray_workflow/operation_relay_array_workflow.robot new file mode 100644 index 00000000000..cd51e506f74 --- /dev/null +++ b/tests/RobotFramework/tests/cumulocity/custom_operation/custom_operation_c8y_RelayArray_workflow/operation_relay_array_workflow.robot @@ -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} diff --git a/tests/RobotFramework/tests/cumulocity/custom_operation/custom_operation_c8y_RelayArray_workflow/set_relay.sh b/tests/RobotFramework/tests/cumulocity/custom_operation/custom_operation_c8y_RelayArray_workflow/set_relay.sh new file mode 100755 index 00000000000..98282fa2377 --- /dev/null +++ b/tests/RobotFramework/tests/cumulocity/custom_operation/custom_operation_c8y_RelayArray_workflow/set_relay.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +MESSAGE="$1" + +echo :::begin-tedge::: +printf '{"result":%s}\n' "$MESSAGE" +echo :::end-tedge::: \ No newline at end of file diff --git a/tests/RobotFramework/tests/cumulocity/custom_operation/custom_operation_c8y_RelayArray_workflow/set_relay.toml b/tests/RobotFramework/tests/cumulocity/custom_operation/custom_operation_c8y_RelayArray_workflow/set_relay.toml new file mode 100644 index 00000000000..9b348018225 --- /dev/null +++ b/tests/RobotFramework/tests/cumulocity/custom_operation/custom_operation_c8y_RelayArray_workflow/set_relay.toml @@ -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" \ No newline at end of file diff --git a/tests/RobotFramework/tests/cumulocity/custom_operation/custom_operation_command_workflow/c8y_Command.template b/tests/RobotFramework/tests/cumulocity/custom_operation/custom_operation_command_workflow/c8y_Command.template new file mode 100644 index 00000000000..fe1da3ef0d3 --- /dev/null +++ b/tests/RobotFramework/tests/cumulocity/custom_operation/custom_operation_command_workflow/c8y_Command.template @@ -0,0 +1,7 @@ +[exec] +topic = "c8y/devicecontrol/notifications" +on_fragment = "c8y_Command" + +[exec.workflow] +operation = "shell_execute" +input.command = "${.payload.c8y_Command.text}" \ No newline at end of file diff --git a/tests/RobotFramework/tests/cumulocity/custom_operation/custom_operation_command_workflow/operation_command_workflow.robot b/tests/RobotFramework/tests/cumulocity/custom_operation/custom_operation_command_workflow/operation_command_workflow.robot new file mode 100644 index 00000000000..199119180de --- /dev/null +++ b/tests/RobotFramework/tests/cumulocity/custom_operation/custom_operation_command_workflow/operation_command_workflow.robot @@ -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} diff --git a/tests/RobotFramework/tests/cumulocity/custom_operation/custom_operation_command_workflow/shell_execute.sh b/tests/RobotFramework/tests/cumulocity/custom_operation/custom_operation_command_workflow/shell_execute.sh new file mode 100755 index 00000000000..fbcef8c9f93 --- /dev/null +++ b/tests/RobotFramework/tests/cumulocity/custom_operation/custom_operation_command_workflow/shell_execute.sh @@ -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" diff --git a/tests/RobotFramework/tests/cumulocity/custom_operation/custom_operation_command_workflow/shell_execute.toml b/tests/RobotFramework/tests/cumulocity/custom_operation/custom_operation_command_workflow/shell_execute.toml new file mode 100644 index 00000000000..f0bfbd49f36 --- /dev/null +++ b/tests/RobotFramework/tests/cumulocity/custom_operation/custom_operation_command_workflow/shell_execute.toml @@ -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" \ No newline at end of file diff --git a/tests/RobotFramework/tests/cumulocity/custom_operation/custom_operation_workflow/custom_operation_workflow.robot b/tests/RobotFramework/tests/cumulocity/custom_operation/custom_operation_workflow/custom_operation_workflow.robot index 4efe5a065e5..580973c6634 100644 --- a/tests/RobotFramework/tests/cumulocity/custom_operation/custom_operation_workflow/custom_operation_workflow.robot +++ b/tests/RobotFramework/tests/cumulocity/custom_operation/custom_operation_workflow/custom_operation_workflow.robot @@ -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 diff --git a/tests/RobotFramework/tests/cumulocity/custom_operation/custom_operation_workflow/do_something.sh b/tests/RobotFramework/tests/cumulocity/custom_operation/custom_operation_workflow/do_something.sh index 5fbd937f730..78f7710917c 100644 --- a/tests/RobotFramework/tests/cumulocity/custom_operation/custom_operation_workflow/do_something.sh +++ b/tests/RobotFramework/tests/cumulocity/custom_operation/custom_operation_workflow/do_something.sh @@ -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:::'