Skip to content

Network Address Translation

This example demonstrates how to use the HIL SDK's NetworkInterface class to configure a Network Address Translation (NAT) for Hardware-in-the-Loop testing.

It consists of two files:

conftest.py - Implements a fixture that returns a NetworkInterface() interface.

test_network_interface.py - Contains tests that set up and test various network configurations.

import pytest

from hil_sdk.interfaces.network import NetworkInterface

logging.basicConfig(level=logging.INFO)

@pytest.fixture
def network_interface_fixture():
    """
    Pytest fixture to provide a NetworkInterface instance for testing.

    This fixture will attempt to auto-discover a USB-to-Ethernet interface,
    or skip the test if none is available.
    """
    # Discover available interfaces
    interfaces = NetworkInterface.discover_usb_ethernet_interfaces()

    if not interfaces:
        pytest.skip("No USB-to-Ethernet interfaces available for testing")

    # Use the first available interface
    interface_name = interfaces[0]

    # Create the NetworkInterface with context manager for automatic cleanup
    with NetworkInterface(interface_name) as net_interface:
        yield net_interface
import time
import traceback

from hil_sdk.interfaces.network import NetworkInterface, NetworkInterfaceException

def test_nat_configuration(network_interface_fixture):
    """Test Network Address Translation (NAT) configuration."""
    net_interface = network_interface_fixture
    print(f"\n=== Testing NAT Configuration on {net_interface.get_interface_name()} ===")

    try:    
        # Test getting initial status
        print("\n--- Getting Interface Status ---")
        status = net_interface.get_interface_status()
        print(f"Interface: {status['interface_name']}")
        print(f"Current mode: {status['current_mode']}")

        # Test NAT mode with single IP allocation
        print("\n--- Testing NAT Mode ---")
        net_interface.configure_nat_mode(ip_pool_start="192.168.100.90", pool_size=1)
        time.sleep(3)  # Wait for configuration to take effect

        status = net_interface.get_interface_status()
        print(f"Mode after NAT config: {status['current_mode']}")

        # Test connectivity in NAT mode
        print("\n--- Testing Connectivity ---")
        connectivity = net_interface.test_connectivity(count=2)
        assert connectivity['success'], "⚠️  NAT mode connectivity test failed (may be expected in test environment)"
        print("✓ NAT mode connectivity test passed")

        # Test getting network statistics
        print("\n--- Getting Network Statistics ---")
        try:
            stats = net_interface.get_network_statistics()
            print(f"✓ Statistics retrieved for {stats['interface_name']}")
            print(f"  Mode: {stats['current_mode']}")
            if 'rx_packets' in stats:
                print(f"  RX packets: {stats['rx_packets']}")
            if 'tx_packets' in stats:
                print(f"  TX packets: {stats['tx_packets']}")
        except Exception as e:
            assert False, f"⚠️  Could not get statistics: {e}"

        print("✓ All network configuration tests completed")

    except Exception as e:
        print(f"✗ Network configuration test failed: {e}")
        traceback.print_exc()
        assert False