Skip to content

Commit

Permalink
examples: zephyr: location: add pytest test for native_sim
Browse files Browse the repository at this point in the history
Rely on playback data to result in accurate location data. Wait for first 3
locations and match logged data with expected position.

Signed-off-by: Marcin Niestroj <[email protected]>
  • Loading branch information
mniestroj committed Jan 16, 2025
1 parent 0463d0f commit e3addef
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
9 changes: 9 additions & 0 deletions examples/zephyr/location/pytest/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright (c) 2025 Golioth, Inc.
#
# SPDX-License-Identifier: Apache-2.0

import pytest

@pytest.fixture(scope='session')
def anyio_backend():
return 'trio'
43 changes: 43 additions & 0 deletions examples/zephyr/location/pytest/test_sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright (c) 2025 Golioth, Inc.
#
# SPDX-License-Identifier: Apache-2.0

from collections import namedtuple
import re

from twister_harness.device.device_adapter import DeviceAdapter
from twister_harness.helpers.shell import Shell

import pytest


pytestmark = pytest.mark.anyio


async def test_location(shell: Shell, dut: DeviceAdapter, device):
# Set Golioth credential

golioth_cred = (await device.credentials.list())[0]
shell.exec_command(f"settings set golioth/psk-id {golioth_cred.identity}")
shell.exec_command(f"settings set golioth/psk {golioth_cred.key}")

# Wait for Golioth connection

dut.readlines_until(regex=".*Golioth CoAP client connected", timeout=90.0)

# Verify position

pattern = re.compile(r".* (?P<lon>\d+\.\d+) (?P<lat>\d+\.\d+) \((?P<acc>\d+)\)")
Position = namedtuple('Position', ('lon', 'lat', 'acc'))

positions = []
for _ in range(3):
lines = dut.readlines_until(regex=pattern, timeout=30.0)
m = pattern.search(lines[-1])
pos = Position(*[float(m[p]) for p in ['lon', 'lat', 'acc']])
positions.append(pos)

for pos in positions:
assert pos.lon == pytest.approx(50.663974800, 1e-3)
assert pos.lat == pytest.approx(17.942322850, 1e-3)
assert pos.acc < 1000
26 changes: 22 additions & 4 deletions examples/zephyr/location/sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,47 @@ sample:
description: Location example
name: location
common:
build_only: true
tags:
- golioth
- location
- socket
tests:
sample.golioth.location:
build_only: true
platform_allow:
- esp32_devkitc_wrover/esp32/procpu
- native_sim
- native_sim/native/64
- nrf52840dk/nrf52840
- nrf9151dk/nrf9151/ns
- nrf9160dk/nrf9160/ns
sample.golioth.location.wifi_only:
sample.golioth.location.playback:
timeout: 100
harness: pytest
extra_args: EXTRA_CONF_FILE="../common/runtime_settings.conf"
extra_configs:
- CONFIG_GOLIOTH_SAMPLE_TWISTER_TEST=y
- arch:posix:CONFIG_NATIVE_UART_0_ON_STDINOUT=y
platform_allow:
- native_sim
- native_sim/native/64
sample.golioth.location.playback.wifi:
harness: pytest
extra_args: EXTRA_CONF_FILE="../common/runtime_settings.conf"
extra_configs:
- CONFIG_GOLIOTH_SAMPLE_TWISTER_TEST=y
- arch:posix:CONFIG_NATIVE_UART_0_ON_STDINOUT=y
- CONFIG_GOLIOTH_CELLULAR_PLAYBACK=n
platform_allow:
- native_sim
- native_sim/native/64
sample.golioth.location.cellular_only:
sample.golioth.location.playback.cellular:
harness: pytest
extra_args: EXTRA_CONF_FILE="../common/runtime_settings.conf"
extra_configs:
- CONFIG_GOLIOTH_WIFI_PLAYBACK=n
- CONFIG_GOLIOTH_SAMPLE_TWISTER_TEST=y
- arch:posix:CONFIG_NATIVE_UART_0_ON_STDINOUT=y
- CONFIG_GOLIOTH_CELLULAR_PLAYBACK=n
platform_allow:
- native_sim
- native_sim/native/64

0 comments on commit e3addef

Please sign in to comment.