develop/test/bsim.html · develop/test/bsim.rst 공식 원문 ↗

Zephyr 3.7.0 · Develop · Testing · BabbleSim

BabbleSim

BLE·802.15.4 device network를 Linux executable과 PHY simulator로 재현하고 병렬 test합니다.

Official pathdevelop/test/bsim.html
Source filedevelop/test/bsim.rst
Source versionZephyr 3.7.0
TranslationFull · reviewed

본문 기준: Zephyr 3.7.0 LTS3 · 이후 유지보수 패치는 별도입니다.

Part 1

요약·해설

Radio 유무에 따른 Twister·전용 script 선택, self-checking·EDTT 방식, coverage, build/run script와 simulation ID·timeout·filesystem 규칙을 설명합니다.

Radio

BLE · 802.15.4 · PHY

Runner

Twister · EDTT · scripts

CI

parallel · timeout · results

Part 2

접을 수 있는 영어 원문 전체

영어 원문 전체 펼치기
원문 SHA-256 A86DF0B47D3D7A1F586208DEF4A2FA3DC0401489DCC0B04D248D560A7C739BB4
.. _bsim:

BabbleSim
#########

BabbleSim and Zephyr
********************

In the Zephyr project we use the `Babblesim`_ simulator to test some of the Zephyr radio protocols,
including the BLE stack, 802.15.4, and some of the networking stack.

BabbleSim_ is a physical layer simulator, which in combination with the Zephyr
:ref:`bsim boards<bsim boards>`
can be used to simulate a network of BLE and 15.4 devices.
When we build Zephyr targeting a :ref:`bsim board<bsim boards>` we produce a Linux
executable, which includes the application, Zephyr OS, and models of the HW.

When there is radio activity, this Linux executable will connect to the BabbleSim Phy simulation
to simulate the radio channel.

In the BabbleSim documentation you can find more information on how to
`get <https://babblesim.github.io/fetching.html>`_ and
`build <https://babblesim.github.io/building.html>`_ the simulator.
In the :ref:`nrf52_bsim<nrf52_bsim>`, :ref:`nrf5340bsim<nrf5340bsim>`,
and :ref:`nrf54l15bsim<nrf54l15bsim>` boards documentation
you can find more information about how to build Zephyr targeting these particular boards,
and a few examples.

Types of tests
**************

Tests without radio activity: bsim tests with twister
=====================================================

The :ref:`bsim boards<bsim boards>` can be used without radio activity, and in that case, it is not
necessary to connect them to a physical layer simulation. Thanks to this, these target boards can
be used just like :ref:`native_sim<native_sim>` with :ref:`twister <twister_script>`,
to run all standard Zephyr twister tests, but with models of a real SOC HW, and their drivers.

Tests with radio activity
=========================

When there is radio activity, BabbleSim tests require at the very least a physical layer simulation
running, and most, more than 1 simulated device. Due to this, these tests are not build and run
with twister, but with a dedicated set of tests scripts.

These tests are kept in the :code:`tests/bsim/` folder. The ``compile.sh`` and ``run_parallel.sh``
scripts contained in that folder are used by the CI system to build the needed images and execute
these tests in batch.

See sections below for more information about how to build and run them, as well as the conventions
they follow.

There are two main sets of tests:

* Self checking embedded application/tests: In which some of the simulated devices applications are
  built with some checks which decide if the test is passing or failing. These embedded
  applications tests use the :ref:`bs_tests<bsim_boards_bs_tests>` system to report the pass or
  failure, and in many cases to build several tests into the same binary.

* Test using the EDTT_ tool, in which a EDTT (python) test controls the embedded applications over
  an RPC mechanism, and decides if the test passes or not.
  Today these tests include a very significant subset of the BT qualification test suite.

More information about how different tests types relate to BabbleSim and the bsim boards can be
found in the :ref:`bsim boards tests section<bsim_boards_tests>`.

Test coverage and BabbleSim
***************************

As the :ref:`nrf52_bsim<nrf52_bsim>` and :ref:`nrf5340bsim<nrf5340bsim>`, and
:ref:`nrf54l15bsim<nrf54l15bsim>` boards are based on the POSIX architecture, you can easily collect
test coverage information.

You can use the script :zephyr_file:`tests/bsim/generate_coverage_report.sh` to generate an html
coverage report from tests.

Check :ref:`the page on coverage generation <coverage_posix>` for more info.

.. _BabbleSim:
   https://BabbleSim.github.io

.. _EDTT:
   https://github.com/EDTTool/EDTT

Building and running the tests
******************************

See the :ref:`nrf52_bsim` page for setting up the simulator.

The scripts also expect a few environment variables to be set.
For example, from Zephyr's root folder, you can run:

.. code-block:: bash

   # Build all the tests
   ${ZEPHYR_BASE}/tests/bsim/compile.sh

   # Run them (in parallel)
   RESULTS_FILE=${ZEPHYR_BASE}/myresults.xml \
      SEARCH_PATH=${ZEPHYR_BASE}/tests/bsim \
         ${ZEPHYR_BASE}/tests/bsim/run_parallel.sh

Or to build and run only a specific subset, e.g. host advertising tests:

.. code-block:: bash

   # Build the Bluetooth host advertising tests
   ${ZEPHYR_BASE}/tests/bsim/bluetooth/host/adv/compile.sh

   # Run them (in parallel)
   RESULTS_FILE=${ZEPHYR_BASE}/myresults.xml \
      SEARCH_PATH=${ZEPHYR_BASE}/tests/bsim/bluetooth/host/adv \
         ${ZEPHYR_BASE}/tests/bsim/run_parallel.sh

Check the ``run_parallel.sh`` help for more options and examples on how to use this script to run
the tests in batch.

After building the tests' required binaries you can run a test directly using its individual test
script.

For example you can build the required binaries for the networking tests with

.. code-block:: bash

   WORK_DIR=${ZEPHYR_BASE}/bsim_out ${ZEPHYR_BASE}/tests/bsim/net/compile.sh

and then directly run one of the tests:

.. code-block:: bash

   ${ZEPHYR_BASE}/tests/bsim/net/sockets/echo_test/tests_scripts/echo_test_802154.sh

Conventions
===========

Test code
---------

See the :zephyr_file:`Bluetooth sample test <tests/bsim/bluetooth/host/misc/sample_test/README.rst>` for conventions that apply to test
code.

Build scripts
-------------

The build scripts ``compile.sh`` simply build all the required test and sample applications
for the tests' scripts placed in the subfolders below.

This build scripts use the common compile.source which provide a function (compile) which calls
cmake and ninja with the provided application, configuration and overlay files.

To speed up compilation for users interested only in a subset of tests, several compile scripts
exist in several subfolders, where the upper ones call into the lower ones.

Note that cmake and ninja are used directly instead of the ``west build`` wrapper as west is not
required, and some Zephyr users do not use or have west, but still use the build and tests scripts.

Test scripts
------------

Please follow the existing conventions and do not design one-off bespoke runners (e.g. a python
script, or another shell abstraction).

The rationale is that it is easier and faster for the maintainers to perform tree-wide updates for
build system or compatibility changes if the tests are run in the same manner, with the same
variables, etc..

If you have a good idea for improving your test script, please make a PR changing *all* the test
scripts in order to benefit everyone and conserve homogeneity. You can of course discuss it first in
an RFC issue or on the babblesim discord channel.

Scripts starting with an underscore (``_``) are not automatically discovered and run. They can serve
as either helper functions for the main script, or can be used for local development utilities, e.g.
building and running tests locally, debugging, etc..

Here are the conventions:

- Each test is defined by a shell script with the extension ``.sh``, in a subfolder called
  ``test_scripts/``.
- It is recommended to run a single test per script file. It allows for better parallelization of
  the runs in CI.
- Scripts expect that the binaries they require are already built. They should not compile binaries.
- Scripts will spawn the processes for every simulated device and the physical layer simulation.
- Scripts must return 0 to the invoking shell if the test passes, and not 0 if the test fails.
- Each test must have a unique simulation id, to enable running different tests in parallel.
- Neither the scripts nor the images should modify the workstation filesystem content beyond the
  ``${BSIM_OUT_PATH}/results/<simulation_id>/`` or ``/tmp/`` folders.
  That is, they should not leave stray files behind.
- Tests that require several consecutive simulations (e.g, if simulating a device pairing, powering
  off, and powering up after as a new simulation) should use separate simulation ids for each
  simulation segment, ensuring that the radio activity of each segment can be inspected a
  posteriori.
- Avoid overly long tests. If the test takes over 20 seconds of runtime, consider if it is possible
  to split it in several separate tests.
- If the test takes over 5 seconds, set ``EXECUTE_TIMEOUT`` to a value that is at least 5 times
  bigger than the measured run-time.
- Do not set ``EXECUTE_TIMEOUT`` to a value lower than the default.
- Tests should not be overly verbose: less than a hundred lines are expected on the outputs. Do make
  use of ``LOG_DBG()`` extensively, but don't enable the ``DBG`` log level by default.

Part 3

한국어 전문 번역

BabbleSim

BabbleSim과 Zephyr

Zephyr project는 BabbleSim simulator를 사용해 BLE stack, 802.15.4와 networking stack 일부를 포함한 Zephyr radio protocol을 test합니다.

BabbleSim은 physical-layer simulator입니다. Zephyr bsim board와 결합하면 BLE와 802.15.4 device network를 simulation할 수 있습니다. Bsim board를 target으로 Zephyr를 build하면 application, Zephyr OS, hardware model을 포함하는 Linux executable이 생성됩니다.

Radio activity가 생기면 Linux executable이 BabbleSim PHY simulation에 연결되어 radio channel을 simulation합니다.

BabbleSim 문서에서 simulator를 가져오는 방법과 build하는 방법을 확인할 수 있습니다. nrf52_bsim, nrf5340bsim, nrf54l15bsim board 문서에는 각 board를 target으로 Zephyr를 build하는 방법과 예제가 있습니다.

Test 유형

Radio activity가 없는 test: Twister를 사용하는 bsim test

Bsim board는 radio activity 없이 사용할 수 있으며 이때 physical-layer simulation에 연결할 필요가 없습니다. 따라서 실제 SoC hardware와 driver model을 사용하면서 native_sim과 마찬가지로 Twister에서 일반 Zephyr test를 실행할 수 있습니다.

Radio activity가 있는 test

Radio activity가 있으면 BabbleSim test에는 최소한 실행 중인 physical-layer simulation이 필요하며 대부분 simulation device도 둘 이상 필요합니다. 따라서 Twister가 아니라 전용 test script 집합으로 build하고 실행합니다.

tests/bsim/ folder에 test가 있습니다. 이 folder의 compile.sh와 run_parallel.sh를 CI system이 사용해 필요한 image를 build하고 test를 batch로 실행합니다.

아래 절에서는 build·실행 방법과 관례를 자세히 설명합니다.

주요 test 집합은 두 가지입니다.

  • Self-checking embedded application/test: simulation device application 일부에 pass/fail을 결정하는 검사를 build합니다. Embedded application test는 bs_tests system으로 결과를 보고하며, 하나의 binary에 여러 test를 build하는 경우가 많습니다.
  • EDTT tool을 사용하는 test: EDTT Python test가 RPC mechanism으로 embedded application을 제어하고 결과를 결정합니다. 현재 BT qualification test suite의 매우 큰 subset을 포함합니다.

Test type과 BabbleSim·bsim board의 관계는 bsim board test 문서를 참조하십시오.

Test coverage와 BabbleSim

nrf52_bsim, nrf5340bsim, nrf54l15bsim board는 POSIX architecture 기반이므로 test coverage 정보를 쉽게 수집할 수 있습니다.

tests/bsim/generate_coverage_report.sh script로 test에서 HTML coverage report를 생성할 수 있습니다. 자세한 내용은 coverage report 생성을 참조하십시오.

Test build와 실행

Simulator 설정은 nrf52_bsim 문서를 참조하십시오.

Script는 몇 가지 environment variable도 설정되어 있기를 기대합니다. Zephyr root folder에서 모든 test를 build하고 병렬 실행하는 예:

# Build all the tests
${ZEPHYR_BASE}/tests/bsim/compile.sh

# Run them (in parallel)
RESULTS_FILE=${ZEPHYR_BASE}/myresults.xml \
   SEARCH_PATH=${ZEPHYR_BASE}/tests/bsim \
      ${ZEPHYR_BASE}/tests/bsim/run_parallel.sh

Bluetooth host advertising test 같은 특정 subset만 build하고 실행하는 예:

# Build the Bluetooth host advertising tests
${ZEPHYR_BASE}/tests/bsim/bluetooth/host/adv/compile.sh

# Run them (in parallel)
RESULTS_FILE=${ZEPHYR_BASE}/myresults.xml \
   SEARCH_PATH=${ZEPHYR_BASE}/tests/bsim/bluetooth/host/adv \
      ${ZEPHYR_BASE}/tests/bsim/run_parallel.sh

run_parallel.sh help에서 batch 실행 option과 예제를 더 확인할 수 있습니다.

필요한 test binary를 build한 뒤 개별 test script로 test 하나를 직접 실행할 수 있습니다. Networking test에 필요한 binary를 build하는 예:

WORK_DIR=${ZEPHYR_BASE}/bsim_out ${ZEPHYR_BASE}/tests/bsim/net/compile.sh

그 뒤 test 하나를 직접 실행합니다.

${ZEPHYR_BASE}/tests/bsim/net/sockets/echo_test/tests_scripts/echo_test_802154.sh

관례

Test code

tests/bsim/bluetooth/host/misc/sample_test/README.rst의 Bluetooth sample test에서 test code 관례를 확인하십시오.

Build script

compile.sh build script는 하위 folder의 test script에 필요한 모든 test·sample application을 build합니다.

이 script는 application, configuration, overlay file을 받아 CMake와 Ninja를 호출하는 compile function을 제공하는 공통 compile.source를 사용합니다.

일부 test subset만 필요한 사용자의 compile 시간을 줄이기 위해 여러 하위 folder에 compile script가 있고 상위 script가 하위 script를 호출합니다.

West가 필수가 아니고 West 없이 build·test script를 사용하는 Zephyr 사용자도 있으므로 west build wrapper 대신 CMake와 Ninja를 직접 사용합니다.

Test script

기존 관례를 따르고 일회성 custom runner(Python script나 다른 shell abstraction)를 새로 설계하지 마십시오.

Test가 같은 variable과 같은 방식으로 실행되면 maintainer가 build system 또는 compatibility 변경을 tree 전체에 더 쉽고 빠르게 적용할 수 있기 때문입니다.

Test script를 개선할 좋은 아이디어가 있다면 모든 test script를 바꾸는 PR을 만들어 모두가 이점을 얻고 일관성을 유지하게 하십시오. 먼저 RFC issue나 BabbleSim Discord channel에서 논의할 수 있습니다.

Underscore(_)로 시작하는 script는 자동으로 발견·실행되지 않습니다. Main script helper function 또는 local build·실행·debug utility로 사용할 수 있습니다.

관례:

  • 각 test는 test_scripts/ subfolder의 .sh shell script 하나로 정의합니다.
  • CI 병렬 실행을 개선하려면 script file 하나에서 test 하나를 실행하는 것이 좋습니다.
  • Script는 필요한 binary가 이미 build되어 있다고 가정하며 binary를 compile하면 안 됩니다.
  • Script는 모든 simulation device process와 physical-layer simulation을 생성합니다.
  • Test가 통과하면 호출 shell에 0, 실패하면 0이 아닌 값을 return해야 합니다.
  • 서로 다른 test를 병렬 실행할 수 있도록 각 test에 unique simulation ID가 있어야 합니다.
  • ${BSIM_OUT_PATH}/results/<simulation_id>/와 /tmp/ 밖의 workstation filesystem content를 script나 image가 변경하면 안 되며 stray file을 남기지 않아야 합니다.
  • Device pairing 후 power-off와 새 simulation power-up처럼 연속 simulation이 필요한 test는 segment마다 다른 simulation ID를 사용해 각 radio activity를 사후 조사할 수 있게 해야 합니다.
  • 지나치게 긴 test를 피하십시오. Runtime이 20초를 넘으면 여러 test로 분리할 수 있는지 검토합니다.
  • Runtime이 5초를 넘으면 EXECUTE_TIMEOUT을 측정 runtime의 최소 5배로 설정합니다.
  • EXECUTE_TIMEOUT을 기본값보다 작게 설정하지 않습니다.
  • 출력이 지나치게 많으면 안 됩니다. 100줄 미만이 기대됩니다. LOG_DBG()를 적극 사용하되 DBG log level을 기본 활성화하지 않습니다.

Source

출처

영어 원문에는 문단, 문서 지시문, 표, 코드, 코드 심벌, 경로를 그대로 보존했습니다.