From 1774e4fe6600d06f6091a147f08225cac4b641ea Mon Sep 17 00:00:00 2001 From: Reuben Miller Date: Wed, 24 Jul 2024 20:13:57 +0200 Subject: [PATCH] add SmartREST1.0 system test Signed-off-by: Reuben Miller --- .../libraries/ThinEdgeIO/ThinEdgeIO.py | 27 +++++++ .../RobotFramework/resources/common.resource | 12 ++- .../tests/cumulocity/smartrest_one/README.md | 31 ++++++++ .../smartrest_one/smartrest_one.robot | 78 +++++++++++++++++++ 4 files changed, 146 insertions(+), 2 deletions(-) create mode 100644 tests/RobotFramework/tests/cumulocity/smartrest_one/README.md create mode 100644 tests/RobotFramework/tests/cumulocity/smartrest_one/smartrest_one.robot diff --git a/tests/RobotFramework/libraries/ThinEdgeIO/ThinEdgeIO.py b/tests/RobotFramework/libraries/ThinEdgeIO/ThinEdgeIO.py index bc4765c272d..c7d10cfc008 100644 --- a/tests/RobotFramework/libraries/ThinEdgeIO/ThinEdgeIO.py +++ b/tests/RobotFramework/libraries/ThinEdgeIO/ThinEdgeIO.py @@ -935,6 +935,33 @@ def bridge_should_be_down(self, cloud: str, **kwargs) -> str: self.get_bridge_service_name(cloud), **kwargs ) + def _get_device_sn(self, name): + device = self.current + if name: + if name in self.devices: + device = self.devices.get(name) + + return name or device.get_id() + + @keyword("Delete SmartREST 1.0 Template") + def delete_smartrest_one_template(self, template_id: str): + try: + mo_id = c8y_lib.c8y.identity.get_id( + template_id, "c8y_SmartRestDeviceIdentifier" + ) + log.info( + "Deleting SmartREST 1.0 template. external_id=%s, managed_object_id=%s", + template_id, + mo_id, + ) + c8y_lib.c8y.inventory.delete(mo_id) + except Exception as ex: + log.warning( + "Could not deleted SmartREST 1.0 template. id=%s, ex=%s", + template_id, + ex, + ) + def to_date(value: relativetime_) -> datetime: if isinstance(value, datetime): diff --git a/tests/RobotFramework/resources/common.resource b/tests/RobotFramework/resources/common.resource index e9ef85cbbf7..437c4f5ee33 100644 --- a/tests/RobotFramework/resources/common.resource +++ b/tests/RobotFramework/resources/common.resource @@ -18,7 +18,15 @@ ${DEVICE_ADAPTER} %{DEVICE_ADAPTER=docker} &{LOCAL_CONFIG} skip_bootstrap=False bootstrap_script=%{LOCAL_CONFIG_BOOTSTRAP_SCRIPT= } # Cumulocity settings -&{C8Y_CONFIG} host=%{C8Y_BASEURL= } username=%{C8Y_USER= } password=%{C8Y_PASSWORD= } +&{C8Y_CONFIG} +... host=%{C8Y_BASEURL= } +... username=%{C8Y_USER= } +... password=%{C8Y_PASSWORD= } +... bootstrap_username=%{C8Y_BOOTSTRAP_USER=} +... bootstrap_password=%{C8Y_BOOTSTRAP_PASSWORD=} # AWS settings -&{AWS_CONFIG} access_key_id=%{AWS_ACCESS_KEY_ID= } access_key=%{AWS_SECRET_ACCESS_KEY= } region=%{AWS_REGION= } +&{AWS_CONFIG} +... access_key_id=%{AWS_ACCESS_KEY_ID= } +... access_key=%{AWS_SECRET_ACCESS_KEY= } +... region=%{AWS_REGION= } diff --git a/tests/RobotFramework/tests/cumulocity/smartrest_one/README.md b/tests/RobotFramework/tests/cumulocity/smartrest_one/README.md new file mode 100644 index 00000000000..2820fa2746e --- /dev/null +++ b/tests/RobotFramework/tests/cumulocity/smartrest_one/README.md @@ -0,0 +1,31 @@ +#### PUT - works (at least in PUT mode) + +```sh +curl -XPOST --user "$C8Y_TENANT/${C8Y_USER}:${C8Y_PASSWORD}" -H "Accept: application/json" -H "X-Id: templateXIDexample11" -d '10,107,PUT,/inventory/managedObjects/%%,application/json,application/json,%%,UNSIGNED UNSIGNED,"{""value"":""%%""}"' "https://$(tedge config get c8y.http)/s" +``` + +```sh +tedge mqtt pub c8y/s/ul/templateXIDexample11 "$(printf '107,%s,%s' "9238352676" "1")" +``` + + +### c8y-bridge.conf + +```sh +topic s/ul/# out 2 c8y/ "" +topic t/ul/# out 2 c8y/ "" +topic q/ul/# out 2 c8y/ "" +topic c/ul/# out 2 c8y/ "" +topic s/dl/# in 2 c8y/ "" +topic s/ul/templateXIDexample10 out 2 c8y/ "" +topic s/dl/templateXIDexample10 in 2 c8y/ "" +topic s/ol/templateXIDexample10 in 2 c8y/ "" +``` + + +### Problem with cert based device user when using SmartREST 1.0 + +```sh +[c8y/s/ul/templateXIDexample11] 107,9238352676,3333 +[c8y/s/dl/templateXIDexample11] 50,1,401,Unauthorized +``` diff --git a/tests/RobotFramework/tests/cumulocity/smartrest_one/smartrest_one.robot b/tests/RobotFramework/tests/cumulocity/smartrest_one/smartrest_one.robot new file mode 100644 index 00000000000..f841ca342fd --- /dev/null +++ b/tests/RobotFramework/tests/cumulocity/smartrest_one/smartrest_one.robot @@ -0,0 +1,78 @@ +*** Settings *** +Resource ../../../../resources/common.resource +Library Cumulocity +Library ThinEdgeIO + +Test Teardown Custom Teardown + +Test Tags theme:c8y theme:operation + + +*** Variables *** +${SMART_REST_ONE_TEMPLATES}= +... SEPARATOR=\n +... 10,339,GET,/identity/externalIds/c8y_Serial/%%,,application/vnd.com.nsn.cumulocity.externalId+json,%%,STRING, +... 10,311,GET,/alarm/alarms?source\=%%&status\=%%&pageSize\=100,,,%%,UNSIGNED STRING, +... 11,800,$.managedObject,,$.id +... 11,808,$.alarms,,$.id,$.type + + +*** Test Cases *** +Supports SmartREST 1.0 Templates + [Template] Register and Use SmartREST 1.0. Templates + use_builtin_bridge=true + use_builtin_bridge=false + + +*** Keywords *** +Register and Use SmartREST 1.0. Templates + [Arguments] ${use_builtin_bridge} + Custom Setup use_builtin_bridge=${use_builtin_bridge} + + ${TEMPLATE_XID}= Get Random Name prefix=TST_Template + Set Test Variable $TEMPLATE_XID + Execute Command tedge config set c8y.smartrest1.templates "${TEMPLATE_XID}" + Execute Command tedge connect c8y timeout=10 + ${mo}= Device Should Exist ${DEVICE_SN} + + # register templates + Execute Command + ... curl --max-time 15 -sf -XPOST http://127.0.0.1:8001/c8y/s -H "Content-Type: plain/text" -H "X-Id: ${TEMPLATE_XID}" --data "${SMART_REST_ONE_TEMPLATES}" + + # Use templates + # Get managed object id + Execute Command cmd=tedge mqtt pub c8y/s/ul/${TEMPLATE_XID} '339,${DEVICE_SN}' + Should Have MQTT Messages c8y/s/dl/${TEMPLATE_XID} message_pattern=^800,\\d+,${mo["id"]} timeout=10 + + Execute Command cmd=tedge mqtt pub te/device/main///a/test '{"text":"test alarm","severity":"major"}' -r + Device Should Have Alarm/s type=test expected_text=test alarm + + # Get alarms + Execute Command cmd=tedge mqtt pub c8y/s/ul/${TEMPLATE_XID} '311,${mo["id"]},ACTIVE' + Should Have MQTT Messages c8y/s/dl/${TEMPLATE_XID} message_pattern=^808,\\d+,\\d+,test timeout=10 + + # Operations + ${OPERATION}= Get Configuration tedge-configuration-plugin + Operation Should Be SUCCESSFUL ${OPERATION} + +Register Device + [Arguments] ${SERIAL} + ${CREDENTIALS}= Cumulocity.Bulk Register Device With Basic Auth external_id=${SERIAL} + + Execute Command + ... cmd=printf '[c8y]\nusername = "%s"\npassword = "%s"\n' "${CREDENTIALS.username}" "${CREDENTIALS.password}" > /etc/tedge/credentials + +Custom Setup + [Arguments] ${use_builtin_bridge} + ${DEVICE_SN}= Setup skip_bootstrap=${True} + Execute Command test -f ./bootstrap.sh && ./bootstrap.sh --no-connect || true + Execute Command tedge config set mqtt.bridge.built_in ${use_builtin_bridge} + Execute Command tedge config set c8y.use_basic_auth true + + Set Suite Variable $DEVICE_SN + + Register Device ${DEVICE_SN} + +Custom Teardown + Get Logs + IF $TEMPLATE_XID Delete SmartREST 1.0 Template ${TEMPLATE_XID}