Improve code testability
A module is created with specific class methods
to perform particular simulator services' lifecycle
operations.
Change-Id: I467360ab304a00f049bf53c3958e42f445f2d75b
Issue-ID: INT-1577
Signed-off-by: Bartek Grzybowski <b.grzybowski@partner.samsung.com>
diff --git a/test/mocks/mass-pnf-sim/.gitignore b/test/mocks/mass-pnf-sim/.gitignore
index 7111c7f..6732c15 100644
--- a/test/mocks/mass-pnf-sim/.gitignore
+++ b/test/mocks/mass-pnf-sim/.gitignore
@@ -2,3 +2,4 @@
.env/
pnf-sim-lightweight/logs/*
files/onap/*
+__pycache__
diff --git a/test/mocks/mass-pnf-sim/MassPnfSim.py b/test/mocks/mass-pnf-sim/MassPnfSim.py
new file mode 100755
index 0000000..1901cb0
--- /dev/null
+++ b/test/mocks/mass-pnf-sim/MassPnfSim.py
@@ -0,0 +1,157 @@
+#!/usr/bin/env python3
+import logging
+import subprocess
+import time
+from json import dumps
+
+class MassPnfSim():
+
+ log_lvl = logging.INFO
+
+ def __init__(self, args):
+ self.args = args
+ self.logger = logging.getLogger(__name__)
+ self.logger.setLevel(self.log_lvl)
+
+ def bootstrap(self):
+ self.logger.info("Bootstrapping PNF instances")
+
+ start_port = 2000
+ ftps_pasv_port_start = 8000
+ ftps_pasv_port_num_of_ports = 10
+
+ ftps_pasv_port_end = ftps_pasv_port_start + ftps_pasv_port_num_of_ports
+
+ for i in range(self.args.count):
+ self.logger.info(f"PNF simulator instance: {i}")
+
+ # 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'
+ ]
+
+ ip_offset = 0
+ ip = {}
+ for prop in ip_properties:
+ ip.update({prop: str(self.args.ipstart + ip_offset + instance_ip_offset)})
+ ip_offset += 1
+
+ self.logger.debug(f'Instance #{i} properties:\n {dumps(ip, indent=4)}')
+
+ PortSftp = start_port + 1
+ PortFtps = start_port + 2
+ start_port += 2
+
+ foldername = f"pnf-sim-lw-{i}"
+ completed = subprocess.run('mkdir ' + foldername, shell=True)
+ self.logger.info(f'\tCreating folder: {completed.stdout}')
+ completed = subprocess.run(
+ 'cp -r pnf-sim-lightweight/* ' +
+ foldername,
+ shell=True)
+ self.logger.info(f'\tCloning folder: {completed.stdout}')
+
+ composercmd = " ".join([
+ "./simulator.sh compose",
+ ip['gw'],
+ ip['subnet'],
+ str(i),
+ self.args.urlves,
+ ip['PnfSim'],
+ str(self.args.ipfileserver),
+ self.args.typefileserver,
+ str(PortSftp),
+ str(PortFtps),
+ ip['ftps'],
+ ip['sftp'],
+ str(ftps_pasv_port_start),
+ str(ftps_pasv_port_end)
+ ])
+ self.logger.debug(f"Script cmdline: {composercmd}")
+
+ completed = subprocess.run(
+ 'set -x; cd ' +
+ foldername +
+ '; ' +
+ composercmd,
+ shell=True)
+ self.logger.info(f'Cloning: {completed.stdout}')
+
+ ftps_pasv_port_start += ftps_pasv_port_num_of_ports + 1
+ ftps_pasv_port_end += ftps_pasv_port_num_of_ports + 1
+
+ self.logger.info(f'Done setting up instance #{i}')
+
+ completed = subprocess.run('set -x; cd pnf-sim-lightweight; ./simulator.sh build ', shell=True)
+ self.logger.info(f"Build docker image: {completed.stdout}")
+
+ def clean(self):
+ completed = subprocess.run('rm -rf ./pnf-sim-lw-*', shell=True)
+ self.logger.info(f'Deleting: {completed.stdout}')
+
+ def start(self):
+ for i in range(self.args.count):
+ foldername = f"pnf-sim-lw-{i}"
+
+ completed = subprocess.run(
+ 'set -x ; cd ' +
+ foldername +
+ "; bash -x ./simulator.sh start",
+ shell=True)
+ self.logger.info(f'Starting: {completed.stdout}')
+ time.sleep(5)
+
+ def status(self):
+ for i in range(self.args.count):
+ foldername = f"pnf-sim-lw-{i}"
+
+ completed = subprocess.run(
+ 'cd ' +
+ foldername +
+ "; ./simulator.sh status",
+ shell=True)
+ self.logger.info(f'Status: {completed.stdout}')
+
+ def stop(self):
+ for i in range(self.args.count):
+ foldername = f"pnf-sim-lw-{i}"
+
+ completed = subprocess.run(
+ 'cd ' +
+ foldername +
+ f"; ./simulator.sh stop {i}",
+ shell=True)
+ self.logger.info(f'Stopping: {completed.stdout}')
+
+ def trigger(self):
+ self.logger.info("Triggering VES sending:")
+
+ for i in range(self.args.count):
+ foldername = f"pnf-sim-lw-{i}"
+
+ completed = subprocess.run(
+ 'cd ' +
+ foldername +
+ "; ./simulator.sh trigger-simulator",
+ shell=True)
+ self.logger.info(f'Status: {completed.stdout}')
+
+ def trigger_custom(self):
+ self.logger.info("Triggering VES sending by a range of simulators:")
+
+ for i in range(self.args.triggerstart, self.args.triggerend+1):
+ foldername = f"pnf-sim-lw-{i}"
+ self.logger.info(f"Instance being processed: {i}")
+
+ completed = subprocess.run(
+ 'cd ' +
+ foldername +
+ "; ./simulator.sh trigger-simulator",
+ shell=True)
+ self.logger.info(f'Status: {completed.stdout}')
diff --git a/test/mocks/mass-pnf-sim/mass-pnf-sim.py b/test/mocks/mass-pnf-sim/mass-pnf-sim.py
index dd2345c..bce0253 100755
--- a/test/mocks/mass-pnf-sim/mass-pnf-sim.py
+++ b/test/mocks/mass-pnf-sim/mass-pnf-sim.py
@@ -1,12 +1,10 @@
#!/usr/bin/env python3
import argparse
import sys
-import subprocess
-import ipaddress
-import time
import logging
+import ipaddress
+from MassPnfSim import MassPnfSim
from requests import get
-from json import dumps
from requests.exceptions import MissingSchema, InvalidSchema, InvalidURL, ConnectionError, ConnectTimeout
def validate_url(url):
@@ -72,162 +70,32 @@
# General options parser
parser.add_argument('--verbose', help='Verbosity level', choices=['info', 'debug'],
type=str, default='debug')
-
return parser
if __name__ == '__main__':
parser = get_parser()
args = parser.parse_args()
+ log_lvl = getattr(logging, args.verbose.upper())
logger = logging.getLogger(__name__)
- logger.setLevel(getattr(logging, args.verbose.upper()))
+ logger.setLevel(log_lvl)
+ MassPnfSim.log_lvl = log_lvl
- if args.subcommand is None:
+ if args.subcommand is not None:
+ sim = MassPnfSim(args)
+ if args.subcommand == 'bootstrap' :
+ sim.bootstrap()
+ if args.subcommand == 'clean':
+ sim.clean()
+ if args.subcommand == 'start':
+ sim.start()
+ if args.subcommand == 'status':
+ sim.status()
+ if args.subcommand == 'stop':
+ sim.stop()
+ if args.subcommand == 'trigger':
+ sim.trigger()
+ if args.subcommand == 'trigger-custom':
+ sim.trigger_custom()
+ else:
parser.print_usage()
- sys.exit(0)
-
- if args.subcommand == 'bootstrap' :
- logger.info("Bootstrapping PNF instances")
-
- start_port = 2000
- ftps_pasv_port_start = 8000
- ftps_pasv_port_num_of_ports = 10
-
- ftps_pasv_port_end = ftps_pasv_port_start + ftps_pasv_port_num_of_ports
-
- for i in range(args.count):
- logger.info(f"PNF simulator instance: {i}")
-
- # 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'
- ]
-
- ip_offset = 0
- ip = {}
- for prop in ip_properties:
- ip.update({prop: str(args.ipstart + ip_offset + instance_ip_offset)})
- ip_offset += 1
-
- logger.debug(f'Instance #{i} properties:\n {dumps(ip, indent=4)}')
-
- PortSftp = start_port + 1
- PortFtps = start_port + 2
- start_port += 2
-
- 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}')
-
- 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}")
-
- completed = subprocess.run(
- 'set -x; cd ' +
- foldername +
- '; ' +
- composercmd,
- shell=True)
- logger.info(f'Cloning: {completed.stdout}')
-
- ftps_pasv_port_start += ftps_pasv_port_num_of_ports + 1
- ftps_pasv_port_end += ftps_pasv_port_num_of_ports + 1
-
- logger.info(f'Done setting up instance #{i}')
-
- completed = subprocess.run('set -x; cd pnf-sim-lightweight; ./simulator.sh build ', shell=True)
- logger.info(f"Build docker image: {completed.stdout}")
-
- if args.subcommand == 'clean':
- completed = subprocess.run('rm -rf ./pnf-sim-lw-*', shell=True)
- logger.info(f'Deleting: {completed.stdout}')
-
- if args.subcommand == 'start':
-
- 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)
-
- if args.subcommand == 'status':
-
- for i in range(args.count):
- foldername = f"pnf-sim-lw-{i}"
-
- 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:")
-
- 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}')
-
- 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}")
-
- completed = subprocess.run(
- 'cd ' +
- foldername +
- "; ./simulator.sh trigger-simulator",
- shell=True)
- logger.info(f'Status: {completed.stdout}')