From 8200d2a58fcfcd2c4d1e445cfe61ebfdd183fd2c Mon Sep 17 00:00:00 2001 From: Bartek Grzybowski Date: Tue, 5 May 2020 03:50:38 -0700 Subject: Add helper functions for input options validation Change-Id: I0c8cd452fbf740f3b1c7988bf7aa7341a747c75e Issue-ID: INT-1577 Signed-off-by: Bartek Grzybowski --- test/mocks/mass-pnf-sim/mass-pnf-sim.py | 21 +++++++++++++++++++++ test/mocks/mass-pnf-sim/requirements.txt | 1 + 2 files changed, 22 insertions(+) (limited to 'test/mocks') diff --git a/test/mocks/mass-pnf-sim/mass-pnf-sim.py b/test/mocks/mass-pnf-sim/mass-pnf-sim.py index c331d24bb..c70c351ae 100755 --- a/test/mocks/mass-pnf-sim/mass-pnf-sim.py +++ b/test/mocks/mass-pnf-sim/mass-pnf-sim.py @@ -4,6 +4,27 @@ import sys import subprocess import ipaddress import time +from requests import get +from requests.exceptions import MissingSchema, InvalidSchema, InvalidURL, ConnectionError, ConnectTimeout + +def validate_url(url): + '''Helper function to perform --urlves input param validation''' + try: + get(url, timeout=0.001) + except (MissingSchema, InvalidSchema, InvalidURL): + raise argparse.ArgumentTypeError(f'{url} is not a valid URL') + except (ConnectionError, ConnectTimeout): + pass + return url + +def validate_ip(ip): + '''Helper function to validate input param is a vaild IP address''' + try: + ip_valid = ipaddress.ip_address(ip) + except ValueError: + raise argparse.ArgumentTypeError(f'{ip} is not a valid IP address') + else: + return ip_valid parser = argparse.ArgumentParser() parser.add_argument('--bootstrap', help='Bootstrap the system') diff --git a/test/mocks/mass-pnf-sim/requirements.txt b/test/mocks/mass-pnf-sim/requirements.txt index 442e1c37b..7e9b64bd9 100644 --- a/test/mocks/mass-pnf-sim/requirements.txt +++ b/test/mocks/mass-pnf-sim/requirements.txt @@ -1,2 +1,3 @@ argparse ipaddress +requests -- cgit 1.2.3-korg