Skip to content

Commit 661d55d

Browse files
authored
Merge pull request #3854 from reubenmiller/test-support-custom-mqtt-endpoints
tests: allow using a custom Cumulocity mqtt endpoint in tests
2 parents 82e42e2 + fbfaeeb commit 661d55d

File tree

15 files changed

+107
-27
lines changed

15 files changed

+107
-27
lines changed

tests/RobotFramework/devdata/env.template

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ PYTHON_KEYRING_BACKEND=keyring.backends.null.Keyring
1919
C8Y_BASEURL=
2020
C8Y_USER=
2121
C8Y_PASSWORD=
22+
# Custom MQTT endpoint, e.g. example.cumulocity:9883
23+
#C8Y_MQTT=
2224

2325
#
2426
# AWS IoT

tests/RobotFramework/libraries/ThinEdgeIO/ThinEdgeIO.py

Lines changed: 81 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
from paho.mqtt import matcher
2424
from robot.api.deco import keyword, library
25+
from robot.libraries.BuiltIn import BuiltIn
2526
from DeviceLibrary import DeviceLibrary, DeviceAdapter
2627
from DeviceLibrary.DeviceLibrary import timestamp
2728
from Cumulocity import Cumulocity, retry
@@ -81,6 +82,14 @@ def date(self) -> datetime:
8182
# {"tst":"2022-12-27T16:55:44.923776Z+0000","topic":"c8y/s/us","qos":0,"retain":0,"payloadlen":99,"payload":"119,c8y-bridge.conf,c8y-configuration-plugin,example,mosquitto.conf,tedge-mosquitto.conf,tedge.toml"}
8283

8384

85+
def strip_scheme(url: Optional[str]) -> Optional[str]:
86+
"""Strip the scheme from a URL"""
87+
if isinstance(url, str):
88+
# strip any scheme if present
89+
return url.strip().split("://", 1)[-1]
90+
return url
91+
92+
8493
@library(scope="SUITE", auto_keywords=False)
8594
class ThinEdgeIO(DeviceLibrary):
8695
"""ThinEdgeIO Library"""
@@ -102,6 +111,26 @@ def __init__(
102111
# Configure retries
103112
retry.configure_retry_on_members(self, "^_assert_")
104113

114+
# Cumulocity configuration cache
115+
self._c8y_config = {}
116+
117+
@property
118+
def c8y_config(self) -> Dict[str, Any]:
119+
"""Get the Cumulocity configuration"""
120+
if not self._c8y_config:
121+
self._c8y_config = BuiltIn().get_variable_value(r"&{C8Y_CONFIG}", {}) or {}
122+
return self._c8y_config
123+
124+
@property
125+
def c8y_host(self) -> str:
126+
"""Get the Cumulocity domain/host"""
127+
return c8y_lib.get_domain()
128+
129+
@property
130+
def c8y_mqtt(self) -> Optional[str]:
131+
"""Get the Cumulocity MQTT broker URL"""
132+
return strip_scheme(self.c8y_config.get("mqtt"))
133+
105134
def end_suite(self, _data: Any, result: Any):
106135
"""End suite hook which is called by Robot Framework
107136
when the test suite has finished
@@ -201,6 +230,8 @@ def register_device_with_cumulocity_ca(
201230
device_type: str = "thin-edge.io",
202231
device_name: Optional[str] = None,
203232
csr_path: Optional[str] = None,
233+
http: Optional[str] = None,
234+
mqtt: Optional[str] = None,
204235
**kwargs,
205236
):
206237
"""Register a device with Cumulocity using the Cumulocity Certificate Authority feature
@@ -217,7 +248,9 @@ def register_device_with_cumulocity_ca(
217248
device_type=device_type,
218249
)
219250

220-
cmd = f"""tedge config set c8y.url '{credentials.url}' && tedge cert download c8y --device-id '{external_id}' --one-time-password '{credentials.one_time_password}' --retry-every 5s --max-timeout 60s"""
251+
self.set_cumulocity_urls(http=http, mqtt=mqtt, device_name=device_name)
252+
253+
cmd = f"tedge cert download c8y --device-id '{external_id}' --one-time-password '{credentials.one_time_password}' --retry-every 5s --max-timeout 60s"
221254
if csr_path:
222255
cmd += f" --csr-path '{csr_path}'"
223256

@@ -226,12 +259,57 @@ def register_device_with_cumulocity_ca(
226259
device_name=device_name,
227260
)
228261

262+
@keyword("Set Cumulocity URLs")
263+
def set_cumulocity_urls(
264+
self,
265+
http: Optional[str] = None,
266+
mqtt: Optional[str] = None,
267+
profile: Optional[str] = None,
268+
device_name: Optional[str] = None,
269+
):
270+
"""Set the Cumulocity URLs. If an mqtt url is defined then the individual urls will be set
271+
otherwise only the single c8y.url will be set.
272+
273+
Args:
274+
http (Optional[str]): The HTTP URL to set. If set to None, then the default host will be used.
275+
mqtt (Optional[str]): The MQTT URL to set. If set to None, then the default mqtt broker will be used (based on the host).
276+
device_name (Optional[str]): The device name to set.
277+
profile (Optional[str]): Cloud profile. Defaults to None.
278+
"""
279+
280+
if mqtt is None:
281+
mqtt = self.c8y_mqtt
282+
283+
if http is None:
284+
http = self.c8y_host
285+
286+
def _append_command(cmd):
287+
if profile:
288+
commands.append(f"{cmd} --profile {profile}")
289+
else:
290+
commands.append(cmd)
291+
292+
commands = []
293+
if not mqtt:
294+
_append_command(f"tedge config set c8y.url '{http}'")
295+
else:
296+
_append_command(f"tedge config set c8y.mqtt '{mqtt}'")
297+
_append_command(f"tedge config set c8y.http '{http}'")
298+
299+
cmd = " && ".join(commands)
300+
self.execute_command(
301+
cmd,
302+
device_name=device_name,
303+
)
304+
229305
@keyword("Register Device With Self-Signed Certificate")
230306
def register_device_with_self_signed_certificate(
231307
self,
232308
external_id: str,
233309
device_name: Optional[str] = None,
234310
auto_registration_enabled: bool = True,
311+
http: Optional[str] = None,
312+
mqtt: Optional[str] = None,
235313
**kwargs,
236314
):
237315
"""Register a device with Cumulocity using a self-signed certificate. The self-signed
@@ -241,10 +319,8 @@ def register_device_with_self_signed_certificate(
241319
242320
| Register Device With Self-Signed Certificate | external_id=tedge001 |
243321
"""
244-
domain = c8y_lib.get_domain()
245-
self.execute_command(
246-
f"tedge config set c8y.url '{domain}' && tedge cert create --device-id '{external_id}'"
247-
)
322+
self.set_cumulocity_urls(http=http, mqtt=mqtt, device_name=device_name)
323+
self.execute_command(f"tedge cert create --device-id '{external_id}'")
248324
pem_contents = self.execute_command(
249325
'cat "$(tedge config get device.cert_path)"'
250326
)

tests/RobotFramework/resources/common.resource

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ ${DEVICE_ADAPTER} %{DEVICE_ADAPTER=docker}
2020
# Cumulocity settings
2121
&{C8Y_CONFIG}
2222
... host=%{C8Y_BASEURL= }
23+
... mqtt=%{C8Y_MQTT= }
2324
... tenant=%{C8Y_TENANT= }
2425
... username=%{C8Y_USER= }
2526
... password=%{C8Y_PASSWORD= }

tests/RobotFramework/tests/cumulocity/configuration/configuration_operation_multi_cloud.robot

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ Setup Second Device
8181
... tedge config set c8y.device.key_path --profile second /etc/tedge/device-certs/[email protected]
8282
Execute Command tedge config set c8y.proxy.bind.port --profile second 8002
8383
Execute Command tedge config set c8y.bridge.topic_prefix --profile second c8y-second
84-
Execute Command tedge config set c8y.url --profile second "$(tedge config get c8y.url)"
84+
85+
Set Cumulocity URLs profile=second
8586

8687
Execute Command tedge cert create --device-id ${second_device_sn} c8y --profile second
8788
Register Certificate For Cleanup cloud_profile=second common_name=${second_device_sn}

tests/RobotFramework/tests/cumulocity/http_proxy/http_proxy_support.robot

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ Setup Device Without thin-edge.io
6262
Start Service gost-http-proxy
6363

6464
Configure and Connect to Cumulocity
65-
${domain}= Cumulocity.Get Domain
66-
Execute Command tedge config set c8y.url "${domain}"
65+
Set Cumulocity URLs
6766
Execute Command tedge cert create --device-id "${DEVICE_SN}"
6867
Execute Command
6968
... cmd=sudo env C8Y_USER='${C8Y_CONFIG.username}' C8Y_PASSWORD='${C8Y_CONFIG.password}' tedge cert upload c8y

tests/RobotFramework/tests/cumulocity/registration/cumulocity_ca.robot

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ Test Tags theme:c8y
1111
Register Device Using Cumulocity CA
1212
[Setup] Custom Setup
1313
${credentials}= Bulk Register Device With Cumulocity CA ${DEVICE_SN}
14-
${DOMAIN}= Cumulocity.Get Domain
15-
Execute Command tedge config set c8y.url "${DOMAIN}"
14+
Set Cumulocity URLs
1615
Execute Command
1716
... tedge cert download c8y --device-id "${DEVICE_SN}" --one-time-password '${credentials.one_time_password}' --retry-every 5s --max-timeout 30s
1817
Execute Command tedge connect c8y
@@ -23,7 +22,7 @@ Register Device Using Cumulocity CA with url flag
2322
${DOMAIN}= Cumulocity.Get Domain
2423
Execute Command
2524
... tedge cert download c8y --device-id "${DEVICE_SN}" --one-time-password '${credentials.one_time_password}' --url ${DOMAIN} --retry-every 5s --max-timeout 30s
26-
Execute Command tedge config set c8y.url "${DOMAIN}"
25+
Set Cumulocity URLs
2726
Execute Command tedge connect c8y
2827

2928
Certificate Renewal Service Using Cumulocity Certificate Authority
@@ -52,9 +51,8 @@ Certificate Renewal with Cloud Profiles
5251
${DEVICE_SN_2}= Set Variable ${DEVICE_SN}_2
5352
ThinEdgeIO.Register Device With Cumulocity CA external_id
5453
${credentials}= Cumulocity.Bulk Register Device With Cumulocity CA external_id=${DEVICE_SN_2}
55-
${DOMAIN}= Cumulocity.Get Domain
5654

57-
Execute Command tedge config set c8y.url "${DOMAIN}" --profile customer
55+
Set Cumulocity URLs profile=customer
5856
Execute Command
5957
... tedge config set c8y.device.cert_path /etc/tedge/device-certs/tedge-certificate-customer.pem --profile customer
6058
Execute Command tedge config set c8y.bridge.topic_prefix c8y-customer --profile customer

tests/RobotFramework/tests/cumulocity/self-update/tedge_self_update.robot

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,9 @@ Update tedge Using a Custom Software Update Workflow
160160

161161
# Register device (using already installed version)
162162
# Use self-signed certificate as the older thin-edge.io version does not support Cumulocity CA
163-
Register Device With Self-Signed Certificate external_id=${DEVICE_SN}
163+
# and don't explicitly set the mqtt url so that the default value is used (e.g. Core MQTT)
164+
Register Device With Self-Signed Certificate external_id=${DEVICE_SN} mqtt=${EMPTY}
165+
164166
Execute Command cmd=tedge connect c8y
165167
Device Should Exist ${DEVICE_SN}
166168

@@ -231,8 +233,10 @@ Unpin thin-edge.io APT Packages
231233

232234
Register Legacy thin-edge.io
233235
[Arguments] ${external_id}
236+
# Don't explicitly set the mqtt url so that the default value is used (e.g. Core MQTT)
237+
# as older versions of thin-edge.io don't support the mqtt service
234238
${domain}= Cumulocity.Get Domain
235-
Execute Command cmd=tedge config set c8y.url ${domain}
239+
ThinEdgeIO.Set Cumulocity URLs http=${domain} mqtt=${EMPTY}
236240
Execute Command cmd=tedge cert create --device-id '${external_id}'
237241
${pem_contents}= Execute Command cmd=cat "$(tedge config get device.cert.path)" strip=${True}
238242
Cumulocity.Upload Certificate

tests/RobotFramework/tests/cumulocity/smartrest_one/smartrest_one.robot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ SmartREST1 Template Should Exist
8989
Custom Setup
9090
[Arguments] ${use_builtin_bridge}
9191
${DEVICE_SN}= Setup register=${False}
92-
Execute Command cmd=tedge config set c8y.url "${C8Y_CONFIG.host}"
92+
Set Cumulocity URLs
9393
Execute Command tedge config set mqtt.bridge.built_in ${use_builtin_bridge}
9494
Execute Command tedge config set c8y.auth_method basic
9595

tests/RobotFramework/tests/pkcs11/compatibility.robot

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,7 @@ Custom Setup
182182
Execute Command sudo -u tedge /usr/bin/init_softhsm.sh --device-id "${DEVICE_SN}" --pin 123456
183183

184184
# configure tedge
185-
${domain}= Cumulocity.Get Domain
186-
Execute Command tedge config set c8y.url "${domain}"
185+
Set Cumulocity URLs
187186
Execute Command tedge config set mqtt.bridge.built_in true
188187
Execute Command tedge config set device.cryptoki.mode socket
189188

tests/RobotFramework/tests/pkcs11/compatibility_16x.robot

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ Custom Setup
4949
Execute Command sudo -u tedge /usr/bin/init_softhsm.sh --device-id "${DEVICE_SN}" --pin 123456
5050

5151
# configure tedge
52-
${domain}= Cumulocity.Get Domain
53-
Execute Command tedge config set c8y.url "${domain}"
52+
Set Cumulocity URLs
5453
Execute Command tedge config set mqtt.bridge.built_in true
5554
Execute Command tedge config set device.cryptoki.mode socket
5655

0 commit comments

Comments
 (0)