diff options
author | Bartek Grzybowski <b.grzybowski@partner.samsung.com> | 2020-05-14 05:52:40 -0700 |
---|---|---|
committer | Bartek Grzybowski <b.grzybowski@partner.samsung.com> | 2020-05-15 13:31:00 +0000 |
commit | b61465f0662d183442eafe7bfed2f1d1061f2c09 (patch) | |
tree | a8671ec08a12a93d545be037251c45cd64d28d34 | |
parent | 633caca8a409fb0685809082b7fc86d433d33f17 (diff) |
Add internal method for shell runner in MassPnfSim module
Since now if module is called in 'debug' mode shell script
is run with trace.
Change-Id: I7bddf6c0583c4b56d4a0c3b5eeb6a78af561891f
Issue-ID: INT-1577
Signed-off-by: Bartek Grzybowski <b.grzybowski@partner.samsung.com>
-rwxr-xr-x | test/mocks/mass-pnf-sim/MassPnfSim.py | 75 | ||||
-rwxr-xr-x | test/mocks/mass-pnf-sim/pnf-sim-lightweight/simulator.sh | 23 |
2 files changed, 40 insertions, 58 deletions
diff --git a/test/mocks/mass-pnf-sim/MassPnfSim.py b/test/mocks/mass-pnf-sim/MassPnfSim.py index 97ebafc77..f782b22fb 100755 --- a/test/mocks/mass-pnf-sim/MassPnfSim.py +++ b/test/mocks/mass-pnf-sim/MassPnfSim.py @@ -4,6 +4,8 @@ import subprocess import time import argparse import ipaddress +from sys import exit +from os import chdir, getcwd from json import dumps from requests import get from requests.exceptions import MissingSchema, InvalidSchema, InvalidURL, ConnectionError, ConnectTimeout @@ -67,7 +69,7 @@ def get_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') + type=str, default='info') return parser class MassPnfSim(): @@ -78,6 +80,20 @@ class MassPnfSim(): self.args = args self.logger = logging.getLogger(__name__) self.logger.setLevel(self.log_lvl) + self.sim_dirname_pattern = "pnf-sim-lw-" + + def _run_cmd(self, cmd, dir_context='.'): + if self.args.verbose == 'debug': + cmd='bash -x ' + cmd + old_pwd = getcwd() + try: + chdir(dir_context) + subprocess.run(cmd, check=True, shell=True) + chdir(old_pwd) + except FileNotFoundError: + self.logger.error(f"Directory {dir_context} not found") + except subprocess.CalledProcessError as e: + exit(e.returncode) def bootstrap(self): self.logger.info("Bootstrapping PNF instances") @@ -160,66 +176,33 @@ class MassPnfSim(): 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}') + self.logger.info('Cleaning simulators workdirs') + self._run_cmd(f"rm -rf {self.sim_dirname_pattern}*") 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}') + self.logger.info(f'Starting {self.sim_dirname_pattern}{i} instance:') + self._run_cmd('./simulator.sh start', f"{self.sim_dirname_pattern}{i}") 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}') + self.logger.info(f'Getting {self.sim_dirname_pattern}{i} status:') + self._run_cmd('./simulator.sh status', f"{self.sim_dirname_pattern}{i}") 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}') + self.logger.info(f'Stopping {self.sim_dirname_pattern}{i} instance:') + self._run_cmd(f'./simulator.sh stop {i}', f"{self.sim_dirname_pattern}{i}") 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}') + self.logger.info(f'Triggering {self.sim_dirname_pattern}{i} instance:') + self._run_cmd(f'./simulator.sh trigger-simulator', f"{self.sim_dirname_pattern}{i}") 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}') + self.logger.info(f'Triggering {self.sim_dirname_pattern}{i} instance:') + self._run_cmd(f'./simulator.sh trigger-simulator', f"{self.sim_dirname_pattern}{i}") diff --git a/test/mocks/mass-pnf-sim/pnf-sim-lightweight/simulator.sh b/test/mocks/mass-pnf-sim/pnf-sim-lightweight/simulator.sh index 1751d07c5..3b229e167 100755 --- a/test/mocks/mass-pnf-sim/pnf-sim-lightweight/simulator.sh +++ b/test/mocks/mass-pnf-sim/pnf-sim-lightweight/simulator.sh @@ -122,8 +122,7 @@ function write_config(){ } function start(){ - - get_pnfsim_ip + get_pnfsim_ip if [[ $(running_containers) ]]; then echo "Simulator containers are already up" else @@ -164,31 +163,31 @@ function stop(){ } function trigger_simulator(){ -get_pnfsim_ip -cat << EndOfMessage + get_pnfsim_ip + cat << EndOfMessage Simulator response: $(curl -s -X POST -H "Content-Type: application/json" -H "X-ONAP-RequestID: 123" -H "X-InvocationID: 456" -d @config/config.json $SIMULATOR_START_URL) EndOfMessage } function run_simulator(){ -get_pnfsim_ip -cat << EndOfMessage + get_pnfsim_ip + cat << EndOfMessage Simulator response: $(curl -s -X POST -H "Content-Type: application/json" -H "X-ONAP-RequestID: 123" -H "X-InvocationID: 456" -d @config/$CONFIG_JSON $SIMULATOR_START_URL) EndOfMessage } function stop_simulator(){ -get_pnfsim_ip -cat << EndOfMessage + get_pnfsim_ip + cat << EndOfMessage Simulator response: $(curl -s -X POST $SIMULATOR_STOP_URL) EndOfMessage } function get_status(){ - get_pnfsim_ip + get_pnfsim_ip if [[ $(running_containers) ]]; then print_status else @@ -197,8 +196,8 @@ function get_status(){ } function print_status(){ -get_pnfsim_ip -cat << EndOfMessage + get_pnfsim_ip + cat << EndOfMessage $(docker-compose -f $RUNNING_COMPOSE_CONFIG ps) Simulator response: @@ -270,7 +269,7 @@ function clear_logs(){ } function timestamp(){ - date "+%Y-%m-%d_%T" + date "+%Y-%m-%d_%T" } main $@ |