Allow importing the script as module
Improves testability (TBD)
Change-Id: I0c4845f85d14b49b4702270ffad537a19c1c7df7
Issue-ID: INT-1577
Signed-off-by: Bartek Grzybowski <b.grzybowski@partner.samsung.com>
diff --git a/test/mocks/mass-pnf-sim/mass-pnf-sim.py b/test/mocks/mass-pnf-sim/mass-pnf-sim.py
index aa371e0..dd2345c 100755
--- a/test/mocks/mass-pnf-sim/mass-pnf-sim.py
+++ b/test/mocks/mass-pnf-sim/mass-pnf-sim.py
@@ -35,192 +35,199 @@
else:
logging.basicConfig(level=logging.INFO, format='[%(levelname)s] %(message)s')
-parser = argparse.ArgumentParser()
-subparsers = parser.add_subparsers(title='Subcommands', dest='subcommand')
-# Bootstrap command parser
-parser_bootstrap = subparsers.add_parser('bootstrap', help='Bootstrap the system')
-parser_bootstrap.add_argument('--count', help='Instance count to bootstrap', type=int, metavar='INT', default=1)
-parser_bootstrap.add_argument('--urlves', help='URL of the VES collector', type=validate_url, metavar='URL', required=True)
-parser_bootstrap.add_argument('--ipfileserver', help='Visible IP of the file server (SFTP/FTPS) to be included in the VES event',
- type=validate_ip, metavar='IP', required=True)
-parser_bootstrap.add_argument('--typefileserver', help='Type of the file server (SFTP/FTPS) to be included in the VES event',
- type=str, choices=['sftp', 'ftps'], required=True)
-parser_bootstrap.add_argument('--ipstart', help='IP address range beginning', type=validate_ip, metavar='IP', required=True)
-# Start command parser
-parser_start = subparsers.add_parser('start', help='Start instances')
-parser_start.add_argument('--count', help='Instance count to start', type=int, metavar='INT', default=1)
-# Stop command parser
-parser_stop = subparsers.add_parser('stop', help='Stop instances')
-parser_stop.add_argument('--count', help='Instance count to stop', type=int, metavar='INT', default=1)
-# Trigger command parser
-parser_trigger = subparsers.add_parser('trigger', help='Trigger one single VES event from each simulator')
-parser_trigger.add_argument('--count', help='Instance count to trigger', type=int, metavar='INT', default=1)
-# Trigger-custom command parser
-parser_triggerstart = subparsers.add_parser('trigger-custom', help='Trigger one single VES event from specific simulators')
-parser_triggerstart.add_argument('--triggerstart', help='First simulator id to trigger', type=int,
- metavar='INT', required=True)
-parser_triggerstart.add_argument('--triggerend', help='Last simulator id to trigger', type=int,
- metavar='INT', required=True)
-# Status command parser
-parser_status = subparsers.add_parser('status', help='Status')
-parser_status.add_argument('--count', help='Instance count to show status for', type=int, metavar='INT', default=1)
-# Clean command parser
-parser_clean = subparsers.add_parser('clean', help='Clean work-dirs')
-# General options parser
-parser.add_argument('--verbose', help='Verbosity level', choices=['info', 'debug'],
- type=str, default='debug')
+def get_parser():
+ '''Process input arguments'''
-args = parser.parse_args()
+ parser = argparse.ArgumentParser()
+ subparsers = parser.add_subparsers(title='Subcommands', dest='subcommand')
+ # Bootstrap command parser
+ parser_bootstrap = subparsers.add_parser('bootstrap', help='Bootstrap the system')
+ parser_bootstrap.add_argument('--count', help='Instance count to bootstrap', type=int, metavar='INT', default=1)
+ parser_bootstrap.add_argument('--urlves', help='URL of the VES collector', type=validate_url, metavar='URL', required=True)
+ parser_bootstrap.add_argument('--ipfileserver', help='Visible IP of the file server (SFTP/FTPS) to be included in the VES event',
+ type=validate_ip, metavar='IP', required=True)
+ parser_bootstrap.add_argument('--typefileserver', help='Type of the file server (SFTP/FTPS) to be included in the VES event',
+ type=str, choices=['sftp', 'ftps'], required=True)
+ parser_bootstrap.add_argument('--ipstart', help='IP address range beginning', type=validate_ip, metavar='IP', required=True)
+ # Start command parser
+ parser_start = subparsers.add_parser('start', help='Start instances')
+ parser_start.add_argument('--count', help='Instance count to start', type=int, metavar='INT', default=1)
+ # Stop command parser
+ parser_stop = subparsers.add_parser('stop', help='Stop instances')
+ parser_stop.add_argument('--count', help='Instance count to stop', type=int, metavar='INT', default=1)
+ # Trigger command parser
+ parser_trigger = subparsers.add_parser('trigger', help='Trigger one single VES event from each simulator')
+ parser_trigger.add_argument('--count', help='Instance count to trigger', type=int, metavar='INT', default=1)
+ # Trigger-custom command parser
+ parser_triggerstart = subparsers.add_parser('trigger-custom', help='Trigger one single VES event from specific simulators')
+ parser_triggerstart.add_argument('--triggerstart', help='First simulator id to trigger', type=int,
+ metavar='INT', required=True)
+ parser_triggerstart.add_argument('--triggerend', help='Last simulator id to trigger', type=int,
+ metavar='INT', required=True)
+ # Status command parser
+ parser_status = subparsers.add_parser('status', help='Status')
+ parser_status.add_argument('--count', help='Instance count to show status for', type=int, metavar='INT', default=1)
+ # Clean command parser
+ subparsers.add_parser('clean', help='Clean work-dirs')
+ # General options parser
+ parser.add_argument('--verbose', help='Verbosity level', choices=['info', 'debug'],
+ type=str, default='debug')
-logger = logging.getLogger(__name__)
-logger.setLevel(getattr(logging, args.verbose.upper()))
+ return parser
-if args.subcommand is None:
- parser.print_usage()
- sys.exit(0)
+if __name__ == '__main__':
+ parser = get_parser()
+ args = parser.parse_args()
-if args.subcommand == 'bootstrap' :
- logger.info("Bootstrapping PNF instances")
+ logger = logging.getLogger(__name__)
+ logger.setLevel(getattr(logging, args.verbose.upper()))
- start_port = 2000
- ftps_pasv_port_start = 8000
- ftps_pasv_port_num_of_ports = 10
+ if args.subcommand is None:
+ parser.print_usage()
+ sys.exit(0)
- ftps_pasv_port_end = ftps_pasv_port_start + ftps_pasv_port_num_of_ports
+ if args.subcommand == 'bootstrap' :
+ logger.info("Bootstrapping PNF instances")
- for i in range(args.count):
- logger.info(f"PNF simulator instance: {i}")
+ start_port = 2000
+ ftps_pasv_port_start = 8000
+ ftps_pasv_port_num_of_ports = 10
- # The IP ranges are in distance of 16 compared to each other.
- # This is matching the /28 subnet mask used in the dockerfile inside.
- instance_ip_offset = i * 16
- ip_properties = [
- 'subnet',
- 'gw',
- 'PnfSim',
- 'ftps',
- 'sftp'
- ]
+ ftps_pasv_port_end = ftps_pasv_port_start + ftps_pasv_port_num_of_ports
- ip_offset = 0
- ip = {}
- for prop in ip_properties:
- ip.update({prop: str(args.ipstart + ip_offset + instance_ip_offset)})
- ip_offset += 1
+ for i in range(args.count):
+ logger.info(f"PNF simulator instance: {i}")
- logger.debug(f'Instance #{i} properties:\n {dumps(ip, indent=4)}')
+ # The IP ranges are in distance of 16 compared to each other.
+ # This is matching the /28 subnet mask used in the dockerfile inside.
+ instance_ip_offset = i * 16
+ ip_properties = [
+ 'subnet',
+ 'gw',
+ 'PnfSim',
+ 'ftps',
+ 'sftp'
+ ]
- PortSftp = start_port + 1
- PortFtps = start_port + 2
- start_port += 2
+ ip_offset = 0
+ ip = {}
+ for prop in ip_properties:
+ ip.update({prop: str(args.ipstart + ip_offset + instance_ip_offset)})
+ ip_offset += 1
- foldername = f"pnf-sim-lw-{i}"
- completed = subprocess.run('mkdir ' + foldername, shell=True)
- logger.info(f'\tCreating folder: {completed.stdout}')
- completed = subprocess.run(
- 'cp -r pnf-sim-lightweight/* ' +
- foldername,
- shell=True)
- logger.info(f'\tCloning folder: {completed.stdout}')
+ logger.debug(f'Instance #{i} properties:\n {dumps(ip, indent=4)}')
- composercmd = " ".join([
- "./simulator.sh compose",
- ip['gw'],
- ip['subnet'],
- str(i),
- args.urlves,
- ip['PnfSim'],
- str(args.ipfileserver),
- args.typefileserver,
- str(PortSftp),
- str(PortFtps),
- ip['ftps'],
- ip['sftp'],
- str(ftps_pasv_port_start),
- str(ftps_pasv_port_end)
- ])
- logger.debug(f"Script cmdline: {composercmd}")
+ PortSftp = start_port + 1
+ PortFtps = start_port + 2
+ start_port += 2
- completed = subprocess.run(
- 'set -x; cd ' +
- foldername +
- '; ' +
- composercmd,
- shell=True)
- logger.info(f'Cloning: {completed.stdout}')
+ foldername = f"pnf-sim-lw-{i}"
+ completed = subprocess.run('mkdir ' + foldername, shell=True)
+ logger.info(f'\tCreating folder: {completed.stdout}')
+ completed = subprocess.run(
+ 'cp -r pnf-sim-lightweight/* ' +
+ foldername,
+ shell=True)
+ logger.info(f'\tCloning folder: {completed.stdout}')
- ftps_pasv_port_start += ftps_pasv_port_num_of_ports + 1
- ftps_pasv_port_end += ftps_pasv_port_num_of_ports + 1
+ composercmd = " ".join([
+ "./simulator.sh compose",
+ ip['gw'],
+ ip['subnet'],
+ str(i),
+ args.urlves,
+ ip['PnfSim'],
+ str(args.ipfileserver),
+ args.typefileserver,
+ str(PortSftp),
+ str(PortFtps),
+ ip['ftps'],
+ ip['sftp'],
+ str(ftps_pasv_port_start),
+ str(ftps_pasv_port_end)
+ ])
+ logger.debug(f"Script cmdline: {composercmd}")
- logger.info(f'Done setting up instance #{i}')
+ completed = subprocess.run(
+ 'set -x; cd ' +
+ foldername +
+ '; ' +
+ composercmd,
+ shell=True)
+ logger.info(f'Cloning: {completed.stdout}')
- completed = subprocess.run('set -x; cd pnf-sim-lightweight; ./simulator.sh build ', shell=True)
- logger.info(f"Build docker image: {completed.stdout}")
+ ftps_pasv_port_start += ftps_pasv_port_num_of_ports + 1
+ ftps_pasv_port_end += ftps_pasv_port_num_of_ports + 1
-if args.subcommand == 'clean':
- completed = subprocess.run('rm -rf ./pnf-sim-lw-*', shell=True)
- logger.info(f'Deleting: {completed.stdout}')
+ logger.info(f'Done setting up instance #{i}')
-if args.subcommand == 'start':
+ completed = subprocess.run('set -x; cd pnf-sim-lightweight; ./simulator.sh build ', shell=True)
+ logger.info(f"Build docker image: {completed.stdout}")
- for i in range(args.count):
- foldername = f"pnf-sim-lw-{i}"
+ if args.subcommand == 'clean':
+ completed = subprocess.run('rm -rf ./pnf-sim-lw-*', shell=True)
+ logger.info(f'Deleting: {completed.stdout}')
- completed = subprocess.run(
- 'set -x ; cd ' +
- foldername +
- "; bash -x ./simulator.sh start",
- shell=True)
- logger.info(f'Starting: {completed.stdout}')
- time.sleep(5)
+ if args.subcommand == 'start':
-if args.subcommand == 'status':
+ for i in range(args.count):
+ foldername = f"pnf-sim-lw-{i}"
- for i in range(args.count):
- foldername = f"pnf-sim-lw-{i}"
+ completed = subprocess.run(
+ 'set -x ; cd ' +
+ foldername +
+ "; bash -x ./simulator.sh start",
+ shell=True)
+ logger.info(f'Starting: {completed.stdout}')
+ time.sleep(5)
- completed = subprocess.run(
- 'cd ' +
- foldername +
- "; ./simulator.sh status",
- shell=True)
- logger.info(f'Status: {completed.stdout}')
+ if args.subcommand == 'status':
-if args.subcommand == 'stop':
- for i in range(args.count):
- foldername = f"pnf-sim-lw-{i}"
+ for i in range(args.count):
+ foldername = f"pnf-sim-lw-{i}"
- completed = subprocess.run(
- 'cd ' +
- foldername +
- f"; ./simulator.sh stop {i}",
- shell=True)
- logger.info(f'Stopping: {completed.stdout}')
+ completed = subprocess.run(
+ 'cd ' +
+ foldername +
+ "; ./simulator.sh status",
+ shell=True)
+ logger.info(f'Status: {completed.stdout}')
+
+ if args.subcommand == 'stop':
+ for i in range(args.count):
+ foldername = f"pnf-sim-lw-{i}"
+
+ completed = subprocess.run(
+ 'cd ' +
+ foldername +
+ f"; ./simulator.sh stop {i}",
+ shell=True)
+ logger.info(f'Stopping: {completed.stdout}')
-if args.subcommand == 'trigger':
- logger.info("Triggering VES sending:")
+ if args.subcommand == 'trigger':
+ logger.info("Triggering VES sending:")
- for i in range(args.count):
- foldername = f"pnf-sim-lw-{i}"
+ for i in range(args.count):
+ foldername = f"pnf-sim-lw-{i}"
- completed = subprocess.run(
- 'cd ' +
- foldername +
- "; ./simulator.sh trigger-simulator",
- shell=True)
- logger.info(f'Status: {completed.stdout}')
+ completed = subprocess.run(
+ 'cd ' +
+ foldername +
+ "; ./simulator.sh trigger-simulator",
+ shell=True)
+ logger.info(f'Status: {completed.stdout}')
-if args.subcommand == 'trigger-custom':
- logger.info("Triggering VES sending by a range of simulators:")
+ if args.subcommand == 'trigger-custom':
+ logger.info("Triggering VES sending by a range of simulators:")
- for i in range(args.triggerstart, args.triggerend+1):
- foldername = f"pnf-sim-lw-{i}"
- logger.info(f"Instance being processed: {i}")
+ for i in range(args.triggerstart, args.triggerend+1):
+ foldername = f"pnf-sim-lw-{i}"
+ logger.info(f"Instance being processed: {i}")
- completed = subprocess.run(
- 'cd ' +
- foldername +
- "; ./simulator.sh trigger-simulator",
- shell=True)
- logger.info(f'Status: {completed.stdout}')
+ completed = subprocess.run(
+ 'cd ' +
+ foldername +
+ "; ./simulator.sh trigger-simulator",
+ shell=True)
+ logger.info(f'Status: {completed.stdout}')