Skip to content

Commit

Permalink
add SmartREST1.0 system test
Browse files Browse the repository at this point in the history
Signed-off-by: Reuben Miller <[email protected]>
  • Loading branch information
reubenmiller authored and rina23q committed Oct 23, 2024
1 parent 3eb62ad commit 1774e4f
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 2 deletions.
27 changes: 27 additions & 0 deletions tests/RobotFramework/libraries/ThinEdgeIO/ThinEdgeIO.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
12 changes: 10 additions & 2 deletions tests/RobotFramework/resources/common.resource
Original file line number Diff line number Diff line change
Expand Up @@ -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= }
31 changes: 31 additions & 0 deletions tests/RobotFramework/tests/cumulocity/smartrest_one/README.md
Original file line number Diff line number Diff line change
@@ -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
```
Original file line number Diff line number Diff line change
@@ -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}

0 comments on commit 1774e4f

Please sign in to comment.