This example demonstrates how to use the OpenVPN interface for HIL testing.
Prerequisites
On Debian/Ubuntu systems, install OpenVPN:
sudoapt-getinstallopenvpn
What This Example Does
Sets up an OpenVPN server - Creates CA certificate, server certificate, and configuration
Starts the OpenVPN server - Launches the server process
Generates client configuration - Creates a .ovpn file that clients can use to connect
Use Cases
This OpenVPN interface can be used for:
- Testing network connectivity in isolated environments
- Simulating VPN connections for IoT devices
- Creating secure tunnels for HIL test communication
- Testing VPN client behavior on embedded devices
Security Note
The certificates generated in this example are for testing purposes only and use default test values. Do not use in production environments.
importosimportpytestimportloggingimporttempfileimporttimeimportshutilfromhil_sdk.versionimport__version__fromhil_sdk.interfaces.openvpnimportOpenVPNInterface,OpenVPNInterfaceException@pytest.fixture(autouse=True,scope="session")defhil_version_fixture():logging.info(f"HIL SDK version {__version__}")@pytest.fixture(scope="session")defopenvpn_work_dir():"""Create a temporary directory for OpenVPN test files"""temp_dir=tempfile.mkdtemp(prefix="hil_openvpn_test_")logging.info(f"Using OpenVPN work directory: {temp_dir}")yieldtemp_dir# Cleanup is handled by the OpenVPN interface@pytest.fixture(scope="session")defopenvpn_server_fixture(hil_version_fixture,openvpn_work_dir):""" Set up and start OpenVPN server for testing. This fixture runs once per test session. """logging.info("Setting up OpenVPN server...")# Create OpenVPN interface with test configurationopenvpn=OpenVPNInterface(server_ip="127.0.0.1",server_port=11194,# Use non-standard port to avoid conflictsnetwork="10.9.0.0",netmask="255.255.255.0",work_dir=openvpn_work_dir)try:# Setup server certificates and configurationopenvpn.setup_server()# Start the serveropenvpn.start_server()# Give server time to fully initialize# Poll server status until ready or timeoutmax_attempts=10for_inrange(max_attempts):ifopenvpn.is_running:breaktime.sleep(1)else:raiseOpenVPNInterfaceException("Server failed to start within timeout")logging.info("OpenVPN server started successfully")yieldopenvpnexceptExceptionase:logging.error(f"Failed to setup OpenVPN server: {e}")raisefinally:# Cleanup HIL gatewaylogging.info("Cleaning up HIL gateway...")try:openvpn.cleanup()exceptExceptionase:logging.warning(f"HIL gateway cleanup failed: {e}")@pytest.fixture(scope="session")defclient_config_dir():"""Create a temporary directory for client configuration files"""temp_dir=tempfile.mkdtemp(prefix="hil_openvpn_client_")logging.info(f"Using client config directory: {temp_dir}")yieldtemp_dir# Cleanup client config directory after all teststry:ifos.path.exists(temp_dir):shutil.rmtree(temp_dir,ignore_errors=True)logging.info(f"Removed client config directory: {temp_dir}")exceptExceptionase:logging.warning(f"Client config directory cleanup failed: {e}")
importloggingfrompathlibimportPathdeftest_generate_dut_client_config(request,openvpn_server_fixture,client_config_dir):"""Generate OpenVPN client configuration for DUT using auto-detected IP"""dut_client_name="dut_device"# Save configuration file for DUT deploymentconfig_file_path=openvpn_server_fixture.save_client_config(dut_client_name,client_config_dir)# Verify the configuration is suitable for DUTconfig_file=Path(config_file_path)assertconfig_file.exists()withopen(config_file,'r')asf:content=f.read()# Verify that remote directive exists (IP will be auto-detected)assert"remote "incontentassert"client"incontent# Extract the auto-detected IP for loggingforlineincontent.split('\n'):ifline.startswith('remote '):detected_ip=line.split()[1]logging.info(f"Auto-detected HIL server IP: {detected_ip}")breaklogging.info(f"DUT client config generated: {config_file_path}")# Store the config file path in the test session for other tests to userequest.config.cache.set("dut_config_file_path",config_file_path)returnconfig_file_path