Skip to content

Commit fcbac06

Browse files
tests: on_target: add memfault test
Add memfault test. Test check that coredump are uploaded to memfault. Signed-off-by: Giacomo Dematteis <giacomo.dematteis@nordicsemi.no>
1 parent 7cb2c7a commit fcbac06

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
##########################################################################################
2+
# Copyright (c) 2025 Nordic Semiconductor
3+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
4+
##########################################################################################
5+
6+
import pytest
7+
import os
8+
import requests
9+
import datetime
10+
import time
11+
from utils.flash_tools import flash_device, reset_device
12+
from utils.logger import get_logger
13+
14+
MEMFAULT_ORG_TOKEN = os.getenv('MEMFAULT_ORGANIZATION_TOKEN')
15+
MEMFAULT_ORG = os.getenv('MEMFAULT_ORGANIZATION_SLUG')
16+
MEMFAULT_PROJ = os.getenv('MEMFAULT_PROJECT_SLUG')
17+
IMEI = os.getenv('IMEI')
18+
MEMFAULT_TIMEOUT = 5 * 60
19+
20+
logger = get_logger()
21+
22+
url = "https://api.memfault.com/api/v0"
23+
auth = ("", MEMFAULT_ORG_TOKEN)
24+
25+
26+
def get_traces(family, device_id):
27+
r = requests.get(
28+
f"{url}/organizations/{MEMFAULT_ORG}/projects/{MEMFAULT_PROJ}/traces", auth=auth)
29+
r.raise_for_status()
30+
data = r.json()["data"]
31+
latest_traces = [
32+
x
33+
for x in data
34+
if x["device"]["device_serial"] == str(device_id) and x["source_type"] == family
35+
]
36+
return latest_traces
37+
38+
def get_latest_coredump_traces(device_id):
39+
return get_traces("coredump", device_id)
40+
41+
def timestamp(event):
42+
return datetime.datetime.strptime(
43+
event["captured_date"], "%Y-%m-%dT%H:%M:%S.%f%z"
44+
)
45+
46+
47+
def test_memfault(t91x_board, hex_file):
48+
# Save timestamp of latest coredump
49+
coredumps = get_latest_coredump_traces(IMEI)
50+
timestamp_old_coredump = timestamp(coredumps[0]) if coredumps else None
51+
52+
flash_device(os.path.abspath(hex_file))
53+
t91x_board.uart.xfactoryreset()
54+
patterns_memfault = [
55+
"Network connectivity established",
56+
"memfault: memfault_task: Memfault module task started",
57+
"memfault: memfault_task: Cloud status received",
58+
]
59+
60+
t91x_board.uart.flush()
61+
reset_device()
62+
t91x_board.uart.wait_for_str(patterns_memfault, timeout=120)
63+
64+
# Trigger bus fault to generate memfault event
65+
t91x_board.uart.write("mflt test usagefault\r\n")
66+
67+
# Wait for upload to be reported to memfault api
68+
start = time.time()
69+
while time.time() - start < MEMFAULT_TIMEOUT:
70+
time.sleep(5)
71+
coredumps = get_latest_coredump_traces(IMEI)
72+
if not coredumps:
73+
continue
74+
# Check that we have an upload with newer timestamp
75+
if not timestamp_old_coredump:
76+
break
77+
if timestamp(coredumps[0]) > timestamp_old_coredump:
78+
break
79+
else:
80+
raise RuntimeError(f"No new coredump observed")
81+

0 commit comments

Comments
 (0)