Skip to content

Commit

Permalink
infamy: netns: Add possibility to get url using requests
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiaswal committed Nov 12, 2024
1 parent 2d1f570 commit 5903c74
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions test/infamy/netns.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import subprocess
import tempfile
import time
import requests

from . import env

Expand Down Expand Up @@ -51,6 +52,7 @@ def __init__(self, ifmap, lo=True):
self.sleeper = None
self.ifmap, self.lo = ifmap, lo
self.ping_timeout = env.ENV.attr("ping_timeout", 5)
self.http_timeout = env.ENV.attr("http_timeout", 5)

def start(self):
self.sleeper = subprocess.Popen(["unshare", "-r", "-n", "sh", "-c",
Expand Down Expand Up @@ -241,6 +243,29 @@ def ping(self, daddr, id=None, timeout=None):
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
input=f"while :; do {ping} && break; done")

def get_url(self, url, timeout=None):
"""Get an URL"""
return self.runpy(f"""
import requests
from requests.exceptions import ConnectionError,Timeout
timeout = {timeout} if {timeout} else {self.http_timeout}
session = requests.Session()
try:
response = session.get("{url}", timeout=(2, 1))
print(response.text)
except Exception:
exit(1)
""")

def must_reach_url(self, url, data):
"""Fail if not get the correct data from url"""
response=self.get_url(url)
if response is None:
return False

return data in response.stdout

def must_reach(self, *args, **kwargs):
self.ping(*args, **kwargs)

Expand Down

0 comments on commit 5903c74

Please sign in to comment.