Static Utils API¶
The static_utils module provides utility functions for network testing, command execution, and system operations.
Static Utilities and Helper Functions Module.
This module provides a collection of utility functions for network testing, command execution, and system operations. It includes functions for:
- Network operations (ping, packet loss analysis, IP validation)
- File operations (JSON loading, SCP transfers)
- System commands (shell command execution)
- Test utilities (pytest integration, data processing)
The utilities are designed to support router and network device testing scenarios, providing common operations needed across different test cases.
Functions:
| Name | Description |
|---|---|
Network Utilities |
|
- ping |
Execute ping commands and return results |
- get_packet_loss |
Extract packet loss percentage from ping output |
- is_valid_ip |
Validate IP address strings (IPv4/IPv6) |
- get_interface_ips |
Get IP addresses assigned to network interfaces |
System Utilities |
|
- execute_shell_commands_on_host |
Execute shell commands with error handling |
- scp_file_to_home_dir |
Transfer files using SCP |
Data Utilities |
|
- load_json |
Load and parse JSON configuration files |
- print_banner |
Print formatted banner messages |
Test Utilities |
|
- get_tests |
Collect pytest test items |
- TestCollector |
Pytest plugin for test collection |
Example
Common usage patterns:
Note
Many functions in this module execute system commands or access network resources. Ensure proper permissions and network connectivity when using these utilities in test environments.
Classes¶
TestCollector ¶
Functions¶
get_tests ¶
Collect test items using pytest collection.
Source code in src/router_test_kit/static_utils.py
load_json ¶
Load and parse a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
Path to the JSON file |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Parsed JSON data as dictionary |
Source code in src/router_test_kit/static_utils.py
print_banner ¶
Print formatted banner messages with decorative borders.
Creates a visually appealing banner by surrounding the provided messages with asterisk borders. Each message is centered within the banner width.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*messages
|
str
|
Variable number of string messages to display in the banner |
()
|
banner_legth
|
int
|
Width of the banner in characters. Defaults to 80. |
80
|
Example
Output:
Note
Messages are logged using the logger.info() method, so they will appear in both console output and log files based on logging configuration.
Source code in src/router_test_kit/static_utils.py
execute_shell_commands_on_host ¶
execute_shell_commands_on_host(
commands: List[str],
print_response: bool = False,
quiet: bool = False,
) -> Optional[str]
Execute shell commands on the host system.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
commands
|
List[str]
|
List of commands to execute |
required |
print_response
|
bool
|
Whether to log successful command execution |
False
|
quiet
|
bool
|
Whether to suppress error logging |
False
|
Returns:
| Type | Description |
|---|---|
Optional[str]
|
Combined output of all commands, or None if no output |
Source code in src/router_test_kit/static_utils.py
get_interface_ips ¶
Get IPv4 and IPv6 addresses assigned to a network interface.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interface
|
str
|
Name of the network interface |
required |
Returns:
| Type | Description |
|---|---|
Tuple[List[str], List[str]]
|
Tuple of (IPv4 addresses, IPv6 addresses) |
Source code in src/router_test_kit/static_utils.py
reboot_device ¶
reboot_device(
connection: TelnetConnection, timeout: int = 60
) -> TelnetConnection
Reboot a device and wait for it to come back online.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connection
|
TelnetConnection
|
Active telnet connection to device |
required |
timeout
|
int
|
Maximum time to wait for device to come back online |
60
|
Returns:
| Type | Description |
|---|---|
TelnetConnection
|
Renewed connection to the device |
Raises:
| Type | Description |
|---|---|
ConnectionError
|
If connection is not established |
TimeoutError
|
If device doesn't come back online within timeout |
Source code in src/router_test_kit/static_utils.py
ping ¶
Ping a destination and return the result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
destination_ip
|
str
|
IP address to ping |
required |
count
|
int
|
Number of ping packets to send |
1
|
Returns:
| Type | Description |
|---|---|
Optional[str]
|
Ping command output, or None if command failed |
Source code in src/router_test_kit/static_utils.py
get_packet_loss ¶
Extract packet loss percentage from ping command output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response
|
str
|
Output from ping command |
required |
Returns:
| Type | Description |
|---|---|
Optional[str]
|
Packet loss percentage as string, or None if not found |
Example
'2 packets transmitted, 2 received, 0% packet loss, time 11ms' Returns '0'
Source code in src/router_test_kit/static_utils.py
is_valid_ip ¶
Check if a string represents a valid IP address (IPv4 or IPv6).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ip
|
str
|
String to validate as IP address |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if valid IP address, False otherwise |
Source code in src/router_test_kit/static_utils.py
scp_file_to_home_dir ¶
Copy a local file to remote host's home directory using SCP.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
local_file_path
|
str
|
Path to local file to copy |
required |
user_at_ip
|
str
|
Remote destination in format 'user@ip' |
required |
password
|
str
|
Password for remote authentication |
required |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If local file or remote path is invalid |
ValueError
|
If unknown error occurs during transfer |