From 626f28619bbfb16fd5a2c23bb7002225886b39ae Mon Sep 17 00:00:00 2001 From: Bartek Grzybowski Date: Thu, 4 Jun 2020 09:55:26 +0200 Subject: Support triggering simulator app directly from MassPnfSim module Change-Id: Ice52848bedf8722aa142de74342190378ea593cd Issue-ID: INT-1611 Signed-off-by: Bartek Grzybowski --- test/mocks/mass-pnf-sim/MassPnfSim.py | 34 +++++++++++++++++++--- .../mass-pnf-sim/pnf-sim-lightweight/simulator.sh | 17 ++--------- test/mocks/mass-pnf-sim/test_lifecycle.py | 15 +++++----- 3 files changed, 40 insertions(+), 26 deletions(-) (limited to 'test/mocks') diff --git a/test/mocks/mass-pnf-sim/MassPnfSim.py b/test/mocks/mass-pnf-sim/MassPnfSim.py index 42424e602..19aee6b15 100755 --- a/test/mocks/mass-pnf-sim/MassPnfSim.py +++ b/test/mocks/mass-pnf-sim/MassPnfSim.py @@ -6,11 +6,11 @@ import ipaddress from sys import exit from os import chdir, getcwd, path, popen, kill from shutil import copytree, rmtree -from json import dumps +from json import loads, dumps from yaml import load, SafeLoader from glob import glob from docker import from_env -from requests import get, codes +from requests import get, codes, post from requests.exceptions import MissingSchema, InvalidSchema, InvalidURL, ConnectionError, ConnectTimeout def validate_url(url): @@ -103,8 +103,11 @@ class MassPnfSim: log_lvl = logging.INFO sim_config = 'config/config.yml' + sim_msg_config = 'config/config.json' sim_port = 5000 sim_base_url = 'http://{}:' + str(sim_port) + '/simulator' + sim_start_url = sim_base_url + '/start' + sim_status_url = sim_base_url + '/status' sim_container_name = 'pnf-simulator' rop_script_name = 'ROP_file_creator.sh' @@ -267,7 +270,7 @@ class MassPnfSim: sim_ip = self._get_sim_instance_data(i) self.logger.info(f' PNF-Sim IP: {sim_ip}') self._run_cmd(self.docker_compose_status_cmd, f"{self.sim_dirname_pattern}{i}") - sim_response = get('{}/status'.format(self.sim_base_url).format(sim_ip)) + sim_response = get('{}'.format(self.sim_status_url).format(sim_ip)) if sim_response.status_code == codes.ok: self.logger.info(sim_response.text) else: @@ -308,9 +311,32 @@ class MassPnfSim: else: self.logger.warning(" Simulator containers are already down") - @_MassPnfSim_Decorators.do_action('Triggering', './simulator.sh trigger-simulator') def trigger(self): self.logger.info("Triggering VES sending:") + for i in range(*self._get_iter_range()): + sim_ip = self._get_sim_instance_data(i) + self.logger.info(f'Triggering {self.sim_dirname_pattern}{i} instance:') + self.logger.info(f' PNF-Sim IP: {sim_ip}') + # setup req headers + req_headers = { + "Content-Type": "application/json", + "X-ONAP-RequestID": "123", + "X-InvocationID": "456" + } + self.logger.debug(f' Request headers: {req_headers}') + try: + # get payload for the request + with open(f'{self.sim_dirname_pattern}{i}/{self.sim_msg_config}') as data: + json_data = loads(data.read()) + self.logger.debug(f' JSON payload for the simulator:\n{json_data}') + # make a http request to the simulator + sim_response = post('{}'.format(self.sim_start_url).format(sim_ip), headers=req_headers, json=json_data) + if sim_response.status_code == codes.ok: + self.logger.info(' Simulator response: ' + sim_response.text) + else: + self.logger.warning(' Simulator response ' + sim_response.text) + except TypeError: + self.logger.error(f' Could not load JSON data from {self.sim_dirname_pattern}{i}/{self.sim_msg_config}') @_MassPnfSim_Decorators.do_action('Triggering', './simulator.sh trigger-simulator') def trigger_custom(self): 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 b957279af..6f2514a05 100755 --- a/test/mocks/mass-pnf-sim/pnf-sim-lightweight/simulator.sh +++ b/test/mocks/mass-pnf-sim/pnf-sim-lightweight/simulator.sh @@ -33,8 +33,6 @@ function main(){ exit 1 fi stop $2;; - "run-simulator") - run_simulator;; "trigger-simulator") trigger_simulator;; "stop-simulator") @@ -170,14 +168,6 @@ $(curl -s -X POST -H "Content-Type: application/json" -H "X-ONAP-RequestID: 123" EndOfMessage } -function run_simulator(){ - 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 @@ -210,8 +200,7 @@ Available options: build - locally builds simulator image from existing code start - starts simulator and netopeer2 containers using remote simulator image and specified model name compose - customize the docker-compose and configuration based on arguments -trigger-simulator - start monitoring the ROP files and report periodically -run-simulator - starts sending PNF registration messages with parameters specified in config.json +trigger-simulator - starts sending PNF registration messages with parameters specified in config.json stop-simulator - stop sending PNF registration messages stop - stops both containers status - prints simulator status @@ -229,10 +218,10 @@ Starting simulation: e.g. ./simulator.sh compose 10.11.0.65 10.11.0.64 3 http://10.11.0.69:10000/eventListener/v7 10.11.0.2 10.11.0.66 ftps 2001 2002 10.11.0.67 10.11.0.68 - Setup environment with "./simulator.sh start". It will download required docker images from the internet and run them on docker machine -- To start the simulation use "./simulator.sh run-simulator", which will start sending PNF registration messages with parameters specified in config.json {TODO, might not be needed} +- To start the simulation use "./simulator.sh trigger-simulator", which will start sending PNF registration messages with parameters specified in config.json To stop simulation use "./simulator.sh stop-simulator" command. To check simulator's status use "./simulator.sh status". -If you want to change message parameters simply edit config.json, then start the simulation with "./simulator.sh run-simulator" again +If you want to change message parameters simply edit config.json, then trigger the simulation with "./simulator.sh trigger-simulator" again Logs are written to logs/pnf-simulator.log. If you change the source code you have to rebuild image with "./simulator.sh build" and run "./simulator.sh start" again diff --git a/test/mocks/mass-pnf-sim/test_lifecycle.py b/test/mocks/mass-pnf-sim/test_lifecycle.py index beaa084a8..5ddb93943 100644 --- a/test/mocks/mass-pnf-sim/test_lifecycle.py +++ b/test/mocks/mass-pnf-sim/test_lifecycle.py @@ -94,15 +94,14 @@ def test_start_idempotence(args_start, capfd): assert 'Simulator containers are already up' in msg.out assert 'Starting simulator containers' not in msg.out -def test_trigger(args_trigger, caplog, capfd): +def test_trigger(args_trigger, caplog): MassPnfSim(args_trigger).trigger() - msg = capfd.readouterr() for instance in range(SIM_INSTANCES): instance_ip_offset = instance * 16 ip_offset = 2 assert f'Triggering pnf-sim-lw-{instance} instance:' in caplog.text - assert f'PNF-Sim IP: {str(ip_address(IPSTART) + ip_offset + instance_ip_offset)}' in msg.out - assert 'Simulator started' in msg.out + assert f'PNF-Sim IP: {str(ip_address(IPSTART) + ip_offset + instance_ip_offset)}' in caplog.text + assert 'Simulator started' in caplog.text caplog.clear() def test_trigger_status(args_status, capfd, caplog): @@ -115,11 +114,11 @@ def test_trigger_status(args_status, capfd, caplog): assert 'Exit' not in msg.out caplog.clear() -def test_trigger_idempotence(args_trigger, capfd): +def test_trigger_idempotence(args_trigger, caplog): MassPnfSim(args_trigger).trigger() - msg = capfd.readouterr() - assert "Cannot start simulator since it's already running" in msg.out - assert 'Simulator started' not in msg.out + assert "Cannot start simulator since it's already running" in caplog.text + assert 'Simulator started' not in caplog.text + caplog.clear() def test_trigger_custom(args_trigger_custom, caplog, capfd): MassPnfSim(args_trigger_custom).trigger_custom() -- cgit 1.2.3-korg