-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Marius Sincovici <[email protected]>
- Loading branch information
1 parent
158157f
commit 9315b44
Showing
3 changed files
with
58 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import datetime | ||
import socket | ||
import threading | ||
import time | ||
|
||
import pytest | ||
import sh | ||
|
||
from lib import network | ||
|
||
_CHECK_FREQUENCY=5 | ||
|
||
_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) | ||
|
||
# Replace the built-in print with our custom version | ||
print = _print_with_timestamp | ||
|
||
@pytest.fixture(scope="function", autouse=True) | ||
def setup_check_internet_connection(): | ||
print("Check internet connection before starting tests") | ||
assert network.is_available() | ||
|
||
|
||
@pytest.fixture(scope="session", autouse=True) | ||
def start_system_monitoring(): | ||
print("Start system monitoring") | ||
|
||
connection_check_thread = threading.Thread(target=_check_connection_to_ip, 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() | ||
dns_resolver_thread.start() | ||
|
||
yield | ||
|
||
|
||
def _check_connection_to_ip(ip_address): | ||
while True: | ||
try: | ||
"icmp_seq=" in sh.ping("-c", "1", "-w", "1", ip_address) # noqa: B015 | ||
except sh.ErrorReturnCode as e: | ||
print(f"Failed to connect to {ip_address}: {e}.") | ||
time.sleep(_CHECK_FREQUENCY) | ||
|
||
|
||
def _check_dns_resolution(domain): | ||
while True: | ||
try: | ||
socket.gethostbyname(domain) | ||
except socket.gaierror: | ||
print(f"DNS resolution for {domain} failed.") | ||
time.sleep(_CHECK_FREQUENCY) | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters