Skip to content

Commit 9d3429d

Browse files
tests: on_target: test for cloud configuration
Add a test that verifies shadow changes are applied c2d and d2c. Signed-off-by: Giacomo Dematteis <giacomo.dematteis@nordicsemi.no>
1 parent 7122e78 commit 9d3429d

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
##########################################################################################
2+
# Copyright (c) 2025 Nordic Semiconductor
3+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
4+
##########################################################################################
5+
6+
import os
7+
import pytest
8+
import time
9+
from utils.flash_tools import flash_device, reset_device
10+
import sys
11+
sys.path.append(os.getcwd())
12+
from utils.logger import get_logger
13+
14+
logger = get_logger()
15+
16+
CLOUD_TIMEOUT = 60 * 3
17+
DEFAULT_UPDATE_INTERVAL = 600
18+
TEST_UPDATE_INTERVAL = 150
19+
20+
def test_config(dut_cloud, hex_file):
21+
'''
22+
Test that verifies shadow changes are applied c2d and d2c.
23+
'''
24+
flash_device(os.path.abspath(hex_file))
25+
dut_cloud.uart.xfactoryreset()
26+
dut_cloud.uart.flush()
27+
reset_device()
28+
dut_cloud.uart.wait_for_str("Connected to Cloud", timeout=120)
29+
30+
dut_cloud.cloud.patch_update_interval(dut_cloud.device_id, interval=TEST_UPDATE_INTERVAL)
31+
dut_cloud.uart.wait_for_str(f"main: Received new interval: {TEST_UPDATE_INTERVAL} seconds", timeout=120)
32+
33+
# # Wait for shadow to be reported to cloud
34+
start = time.time()
35+
try:
36+
while time.time() - start < CLOUD_TIMEOUT:
37+
time.sleep(5)
38+
try:
39+
device = dut_cloud.cloud.get_device(dut_cloud.device_id)
40+
device_state = device["state"]
41+
update_interval = device_state["reported"]["config"]["update_interval"]
42+
except Exception as e:
43+
pytest.skip(f"Unable to retrieve device state from cloud, e: {e}")
44+
45+
logger.debug(f"Device state: {device_state}")
46+
if update_interval == TEST_UPDATE_INTERVAL:
47+
break
48+
else:
49+
logger.debug("No correct interval update reported yet, retrying...")
50+
continue
51+
else:
52+
raise RuntimeError(f"No correct update interval reported back to cloud, desired {TEST_UPDATE_INTERVAL}, reported {update_interval}")
53+
finally:
54+
# Back to default interval no matter what
55+
dut_cloud.cloud.patch_update_interval(dut_cloud.device_id, interval=DEFAULT_UPDATE_INTERVAL)

tests/on_target/tests/test_functional/test_shell.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
def test_shell(dut_cloud, hex_file):
1818
'''
19-
Test that the device is operating normally by checking UART output
19+
Test that the device is operating normally using shell commands
2020
'''
2121
flash_device(os.path.abspath(hex_file))
2222
dut_cloud.uart.xfactoryreset()

tests/on_target/utils/nrfcloud.py

+16
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,22 @@ def check_message_age(self, message: dict, hours: int=0, minutes: int=0, seconds
120120
diff = timedelta(hours=hours, minutes=minutes, seconds=seconds)
121121
return datetime.now(timezone.utc) - message[0].replace(tzinfo=timezone.utc) < diff
122122

123+
def patch_update_interval(self, device_id: str, interval: int) -> None:
124+
"""
125+
Update the device's update interval configuration
126+
127+
:param device_id: Device ID to update
128+
:param interval: New update interval in seconds
129+
"""
130+
data = json.dumps({
131+
"desired": {
132+
"config": {
133+
"update_interval": interval
134+
}
135+
}
136+
})
137+
return self._patch(f"/devices/{device_id}/state", data=data)
138+
123139
class NRFCloudFOTA(NRFCloud):
124140
def upload_firmware(
125141
self, name: str, bin_file: str, version: str, description: str, fw_type: FWType, bin_file_2=None

0 commit comments

Comments
 (0)