xref: /aosp_15_r20/external/openthread/tests/toranj/README_CLI.md (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
1# `toranj-cli`
2
3`toranj-cli` is a test framework for OpenThread using its CLI interface.
4
5`toranj` features:
6
7- It is developed in Python.
8- It can be used to simulate multiple nodes forming complex network topologies.
9- It allows testing of network interactions between many nodes.
10- `toranj` in CLI mode runs `ot-cli-ftd` on simulation platform (real-time).
11
12## Setup
13
14To build OpenThread with `toranj` configuration, the `test/toranj/build.sh` script can be used:
15
16```bash
17$ ./tests/toranj/build.sh all
18====================================================================================================
19Building OpenThread (NCP/CLI for FTD/MTD/RCP mode) with simulation platform using cmake
20====================================================================================================
21-- OpenThread Source Directory: /Users/abtink/GitHub/openthread
22-- OpenThread CMake build type: Debug
23-- Package Name: OPENTHREAD
24...
25
26```
27
28The `toranj-cli` tests are included in `tests/toranj/cli` folder. Each test-case has its own script following naming model `test-nnn-name.py` (e.g., `test-001-get-set.py`).
29
30To run a specific test:
31
32```bash
33$ cd tests/toranj/cli
34$ python3 test-001-get-set.py
35```
36
37To run all CLI tests, `start` script can be used. This script will build OpenThread with proper configuration options and starts running all tests.
38
39```bash
40# From OpenThread repo root folder
41$ top_builddir=($pwd) TORANJ_CLI=1 ./tests/toranj/start.sh
42```
43
44## `toranj-cli` Components
45
46`cli` python module defines the `toranj-cli` test components.
47
48### `cli.Node()` Class
49
50`cli.Node()` class creates a Thread node instance. It creates a sub-process to run `ot-cli-ftd` and provides methods to control the node and issue CLI commands.
51
52```python
53>>> import cli
54>>> node1 = cli.Node()
55>>> node1
56Node (index=1)
57>>> node2 = cli.Node()
58>>> node2
59Node (index=2)
60```
61
62Note: You may need to run as `sudo` to allow log file to be written (i.e., use `sudo python` or `sudo python3`).
63
64### `cli.Node` methods
65
66`cli.Node()` provides methods matching different CLI commands, in addition to some helper methods for common operations.
67
68Example:
69
70```python
71>>> node.get_state()
72'disabled'
73>>> node.get_channel()
74'11'
75>>> node.set_channel(12)
76>>> node.get_channel()
77'12'
78>>> node.set_network_key('11223344556677889900aabbccddeeff')
79>>> node.get_network_key()
80'11223344556677889900aabbccddeeff'
81```
82
83Common network operations:
84
85```python
86    # Form a Thread network with all the given parameters.
87    node.form(network_name=None, network_key=None, channel=None, panid=0x1234, xpanid=None):
88
89    # Try to join an existing network as specified by `another_node`.
90    # `type` can be `JOIN_TYPE_ROUTER`, `JOIN_TYPE_END_DEVICE, or `JOIN_TYPE_SLEEPY_END_DEVICE`
91    node.join(another_node, type=JOIN_TYPE_ROUTER):
92```
93
94A direct CLI command can be issued using `node.cli(command)` with a given `command` string.
95
96```python
97>>> node.cli('uptime')
98['00:36:18.778']
99```
100
101Method `allowlist_node()` can be used to add a given node to the allowlist of the device and enables allowlisting:
102
103```python
104    # `node2` is added to the allowlist of `node1` and allowlisting is enabled on `node1`
105    node1.allowlist_node(node2)
106```
107
108#### Example (simple 3-node topology)
109
110Script below shows how to create a 3-node network topology with `node1` and `node2` being routers, and `node3` an end-device connected to `node2`:
111
112```python
113>>> import cli
114>>> node1 = cli.Node()
115>>> node2 = cli.Node()
116>>> node3 = cli.Node()
117
118>>> node1.form('test')
119>>> node1.get_state()
120'leader'
121
122>>> node1.allowlist_node(node2)
123>>> node1.allowlist_node(node3)
124
125>>> node2.join(node1, cli.JOIN_TYPE_ROUTER)
126>>> node2.get_state()
127'router'
128
129>>> node3.join(node1, cli.JOIN_TYPE_END_DEVICE)
130>>> node3.get_state()
131'child'
132
133>>> node1.cli('neighbor list')
134['0x1c01 0x0400 ']
135```
136
137### Logs and Verbose mode
138
139Every `cli.Node()` instance will save its corresponding logs. By default the logs are saved in a file `ot-logs<node_index>.log`.
140
141When `start.sh` script is used to run all test-cases, if any test fails, to help with debugging of the issue, the last 30 lines of logs of every node involved in the test-case are dumped to `stdout`.
142
143A `cli.Node()` instance can also provide additional logs and info as the test-cases are run (verbose mode). It can be enabled for a node instance when it is created:
144
145```python
146>>> import cli
147>>> node = cli.Node(verbose=True)
148$ Node1.__init__() cmd: `../../../examples/apps/cli/ot-cli-ftd --time-speed=1 1`
149
150>>> node.get_state()
151$ Node1.cli('state') -> disabled
152'disabled'
153
154>>> node.form('test')
155$ Node1.cli('networkname test')
156$ Node1.cli('panid 4660')
157$ Node1.cli('ifconfig up')
158$ Node1.cli('thread start')
159$ Node1.cli('state') -> detached
160$ Node1.cli('state') -> detached
161...
162$ Node1.cli('state') -> leader
163```
164
165Alternatively, `cli.Node._VERBOSE` settings can be changed to enable verbose logging for all nodes. The default value of `cli.Node._VERBOSE` is determined from environment variable `TORANJ_VERBOSE` (verbose mode is enabled when env variable is set to any of `1`, `True`, `Yes`, `Y`, `On` (case-insensitive)), otherwise it is disabled.
166
167## `toranj-cli` and `thread-cert` test framework
168
169`toranj-cli` uses CLI commands to test the behavior of OpenThread with simulation platform. `thread-cert` scripts (in `tests/scripts/thread-cert`) also use CLI commands. However, these two test frameworks have certain differences and are intended for different situations. The `toranj` test cases run in real-time (though it is possible to run with a time speed-up factor) while the `thread-cert` scripts use virtual-time and event-based simulation model.
170
171- `toranj` test cases are useful to validate the real-time (non event-based) simulation platform implementation itself.
172- `toranj` test cases can be used in situations where the platform layer may not support event-based model.
173- `toranj` frameworks allows for more interactive testing (e.g., read–eval–print loop (REPL) model in python) and do not need a separate process to run to handle/dispatch events (which is required for the virtual-time simulation model).
174- `thread-cert` test cases can run quickly (due to virtual time emulation), but the test script itself needs to manage the flow and advancement of time.
175