Skip to content

Commit

Permalink
Add internet monitoring
Browse files Browse the repository at this point in the history
Signed-off-by: Marius Sincovici <[email protected]>
  • Loading branch information
mariusSincovici authored and keliramu committed Oct 8, 2024
1 parent 158157f commit 3fb8e0c
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
2 changes: 1 addition & 1 deletion test/qa/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ source .venv/bin/activate # this should modify your shell prompt
```
3. How to install Python dependencies from file.
```bash
python3 -m pip install -r <path-to-requirements-txt> # requirements.txt is found in `ci/docker/requirements.txt`
python3 -m pip install -r <path-to-requirements-txt> # requirements.txt is found in `ci/docker/tester/requirements.txt`
```
4. How to install new packages.
```bash
Expand Down
88 changes: 88 additions & 0 deletions test/qa/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import datetime
import io
import socket
import threading
import time

import pytest
import sh

from lib import logging, network

_CHECK_FREQUENCY=5

#TODO: check connectivity outside VPN tunnel!


def print_to_string(*args, **kwargs):
output = io.StringIO()
_original_print(*args, file=output, **kwargs)
contents = output.getvalue()
output.close()
return contents


_original_print = print
def _print_with_timestamp(*args, **kwargs):
# Get the current time and format it
timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
# Prepend the timestamp to the original print arguments
#_original_print(timestamp, *args, **kwargs)
logging.log(data=print_to_string(timestamp, *args, **kwargs))


# Replace the built-in print with our custom version
print = _print_with_timestamp # noqa: A001

@pytest.fixture(scope="function", autouse=True)
def setup_check_internet_connection():
print("~~~setup_check_internet_connection: Check internet connection before starting tests")
assert network.is_available()
print("~~~setup_check_internet_connection: Check internet connection before starting tests SUCCESS")


@pytest.fixture(scope="session", autouse=True)
def start_system_monitoring():
print("~~~start_system_monitoring: Start system monitoring")

connection_check_thread = threading.Thread(target=_check_connection_to_ip, args=("1.1.1.1",), daemon=True)
connection_out_vpn_check_thread = threading.Thread(target=_check_connection_to_ip_outside_vpn, args=("1.1.1.1",), daemon=True)
dns_resolver_thread = threading.Thread(target=_check_dns_resolution, args=("nordvpn.com",), daemon=True)
connection_check_thread.start()
connection_out_vpn_check_thread.start()
dns_resolver_thread.start()

yield


def _check_connection_to_ip(ip_address):
while True:
try:
print(f"~~~_check_connection_to_ip: {ip_address}")
"icmp_seq=" in sh.ping("-c", "1", "-w", "1", ip_address) # noqa: B015
print(f"~~~_check_connection_to_ip: {ip_address} SUCCESS")
except sh.ErrorReturnCode as e:
print(f"~~~_check_connection_to_ip: Failed to connect to {ip_address}: {e}.")
time.sleep(_CHECK_FREQUENCY)


def _check_connection_to_ip_outside_vpn(ip_address):
while True:
try:
print(f"~~~_check_connection_to_ip_outside_vpn: {ip_address}")
"icmp_seq=" in sh.ping("-c", "1", "-w", "1", "-I", "eth0", ip_address) # noqa: B015
print(f"~~~_check_connection_to_ip_outside_vpn: {ip_address} SUCCESS")
except sh.ErrorReturnCode as e:
print(f"~~~_check_connection_to_ip_outside_vpn: Failed to connect to {ip_address}: {e}.")
time.sleep(_CHECK_FREQUENCY)


def _check_dns_resolution(domain):
while True:
try:
print(f"~~~_check_dns_resolution: {domain}")
socket.gethostbyname(domain)
print(f"~~~_check_dns_resolution: {domain} SUCCESS")
except socket.gaierror:
print(f"~~~_check_dns_resolution: DNS resolution for {domain} failed.")
time.sleep(_CHECK_FREQUENCY)
2 changes: 1 addition & 1 deletion test/qa/lib/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def collect():
network_interface_info = os.popen("sudo ip addr").read() #sh.sudo.ip.addr()
routing_info = os.popen("sudo ip route").read() #sh.sudo.ip.route()
firewall_info = os.popen("sudo iptables -S").read() #sh.sudo.iptables("-S")
nameserver_info = os.popen("sudo cat /etc/resolve.conf").read() #sh.sudo.cat("/etc/resolv.conf")
nameserver_info = os.popen("sudo cat /etc/resolv.conf").read() #sh.sudo.cat("/etc/resolv.conf")

# without `ww` we cannot see full process lines, as it is cut off early
processes = sh.ps("-ef", "ww")
Expand Down

0 comments on commit 3fb8e0c

Please sign in to comment.