diff --git a/test/infamy/netns.py b/test/infamy/netns.py index 696f1217..cd3522a7 100644 --- a/test/infamy/netns.py +++ b/test/infamy/netns.py @@ -7,6 +7,7 @@ import subprocess import tempfile import time +import requests from . import env @@ -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", @@ -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)