This standalone module is intended to handle OpenVPN connections to a remote server.
python3 -m venv .venv
. .venv/bin/activate
pip install -r requirements.txt
Invoke from command line
# connect
python3 openvpnclient.py --config <full-path-to-ovpn-file>
# disconnect
python3 openvpnclient.py --disconnect
Use as dependency in script
from openvpnclient import OpenVPNClient
# context handler
with OpenVPNClient(ovpn_file) as vpn:
...
# manual start and stop
vpn = OpenVPNClient(ovpn_file)
vpn.connect()
...
vpn.disconnect()
# do not close the connection despite script exit
vpn = OpenVPNClient(ovpn_file)
vpn.connect(stay_alive_on_exit=True)
# <script end>
Test cases
- Connect and disconnect the OpenVPN client manually
- Connect and disconnect the OpenVPN client automatically using the context manager
- Disconnect OpenVPN client automatically on SIGINT (Ctrl+C)
- Disconnect when not connected
- Connect when already connected
- Invalid client configuration syntax
- Server not reachable (invalid ip)
- Wrong path to ovpn config file
- Connection attempt timeout
It is possible that the host OS/computer is not fast enough to close the sockets
being opened by the repeated OpenVPN client connections and therefore an autouse fixture (await_openvpn_cleanup
) forces a timeout between all tests. Update this timeout if the socket
appears to be busy.
Run with minimalistic output
pytest --tap test_openvpnclient.py
Run with verbose output
pytest -s -v test_openvpnclient.py
Currently the tests achieve ~89% coverage of openvpnclient.py
.
--cov=./
flag is used to specify the directory of files that should be tested for coverage.--cov-report annotate
flag is used to generate an annotated coverage report.
pytest --cov=./ test_openvpnclient.py --cov-report annotate