Support stopping sending PNF registration messages in Python module 24/108824/2
authorBartek Grzybowski <b.grzybowski@partner.samsung.com>
Fri, 5 Jun 2020 13:07:56 +0000 (15:07 +0200)
committerMorgan Richomme <morgan.richomme@orange.com>
Fri, 5 Jun 2020 13:47:18 +0000 (13:47 +0000)
Change-Id: Iae7d300e6e09d152b6e7daaebd9c85ba763fc077
Issue-ID: INT-1613
Signed-off-by: Bartek Grzybowski <b.grzybowski@partner.samsung.com>
test/mocks/mass-pnf-sim/MassPnfSim.py
test/mocks/mass-pnf-sim/conftest.py
test/mocks/mass-pnf-sim/test_cli.py
test/mocks/mass-pnf-sim/test_lifecycle.py

index 77076a6..e4b19fd 100755 (executable)
@@ -59,6 +59,9 @@ def get_parser():
     # 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=0)
+    # Stop-simulator command parser
+    parser_stopsimulator = subparsers.add_parser('stop_simulator', help='Stop sending PNF registration messages')
+    parser_stopsimulator.add_argument('--count', help='Instance count to stop', type=int, metavar='INT', default=0)
     # 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,
@@ -108,6 +111,7 @@ class MassPnfSim:
     sim_base_url = 'http://{}:' + str(sim_port) + '/simulator'
     sim_start_url = sim_base_url + '/start'
     sim_status_url = sim_base_url + '/status'
+    sim_stop_url = sim_base_url + '/stop'
     sim_container_name = 'pnf-simulator'
     rop_script_name = 'ROP_file_creator.sh'
 
@@ -127,7 +131,7 @@ class MassPnfSim:
                 exit(1)
 
         # Validate --count option for subcommands that support it
-        if self.args.subcommand in ['start', 'stop', 'trigger', 'status']:
+        if self.args.subcommand in ['start', 'stop', 'trigger', 'status', 'stop_simulator']:
             if self.args.count > self.existing_sim_instances:
                 self.logger.error('--count value greater that existing instance count')
                 exit(1)
@@ -345,3 +349,15 @@ class MassPnfSim:
 
     # Make the 'trigger_custom' an alias to the 'trigger' method
     trigger_custom = trigger
+
+    def stop_simulator(self):
+        self.logger.info("Stopping sending PNF registration messages:")
+        for i in range(*self._get_iter_range()):
+            sim_ip = self._get_sim_instance_data(i)
+            self.logger.info(f'Stopping {self.sim_dirname_pattern}{i} instance:')
+            self.logger.info(f' PNF-Sim IP: {sim_ip}')
+            sim_response = post('{}'.format(self.sim_stop_url).format(sim_ip))
+            if sim_response.status_code == codes.ok:
+                self.logger.info(' Simulator response: ' + sim_response.text)
+            else:
+                self.logger.warning(' Simulator response ' + sim_response.text)
index 2658133..0665810 100644 (file)
@@ -34,6 +34,10 @@ def args_trigger(parser):
 def args_trigger_custom(parser):
     return parser.parse_args(['trigger_custom', '--triggerstart', '0', '--triggerend', str(SIM_INSTANCES-1)])
 
+@pytest.fixture(scope="module")
+def args_stop_simulator(parser):
+    return parser.parse_args(['stop_simulator'])
+
 @pytest.fixture
 def args_clean(parser):
     return parser.parse_args(['clean'])
index 0f46416..3e4a60e 100644 (file)
@@ -36,7 +36,8 @@ def test_validate_trigger_custom(parser, caplog):
     'start',
     'stop',
     'trigger',
-    'status'
+    'status',
+    'stop_simulator'
     ])
 def test_count_option(parser, capsys, subcommand):
     '''Test case where no arg passed to '--count' opt'''
@@ -50,7 +51,8 @@ def test_count_option(parser, capsys, subcommand):
     'start',
     'stop',
     'trigger',
-    'status'
+    'status',
+    'stop_simulator'
     ])
 def test_count_option_bad_value(parser, caplog, subcommand):
     '''Test case where invalid value passed to '--count' opt'''
index d3b452b..0f77c96 100644 (file)
@@ -11,8 +11,8 @@ from time import sleep
 # thus they require that no simulator instances are bootstrapped and running
 # prior to running tests
 
-@pytest.mark.parametrize("action", ['start', 'stop', 'trigger', 'status'])
-def test_not_bootstrapped(action, caplog, args_start, args_stop, args_trigger, args_status): # pylint: disable=W0613
+@pytest.mark.parametrize("action", ['start', 'stop', 'trigger', 'status', 'stop_simulator'])
+def test_not_bootstrapped(action, caplog, args_start, args_stop, args_trigger, args_status, args_stop_simulator): # pylint: disable=W0613
     try:
         m = getattr(MassPnfSim(eval(f'args_{action}')), action)
         m()
@@ -120,6 +120,38 @@ def test_trigger_idempotence(args_trigger, caplog):
     assert 'Simulator started' not in caplog.text
     caplog.clear()
 
+def test_stop_simulator(args_stop_simulator, caplog):
+    MassPnfSim(args_stop_simulator).stop_simulator()
+    for instance in range(SIM_INSTANCES):
+        instance_ip_offset = instance * 16
+        ip_offset = 2
+        assert f'Stopping pnf-sim-lw-{instance} instance:' in caplog.text
+        assert f'PNF-Sim IP: {str(ip_address(IPSTART) + ip_offset + instance_ip_offset)}' in caplog.text
+        assert "Simulator successfully stopped" in caplog.text
+        assert "not running" not in caplog.text
+    caplog.clear()
+
+def test_stop_simulator_status(args_status, capfd, caplog):
+    MassPnfSim(args_status).status()
+    msg = capfd.readouterr()
+    for _ in range(SIM_INSTANCES):
+        assert '"simulatorStatus":"RUNNING"' not in caplog.text
+        assert '"simulatorStatus":"NOT RUNNING"' in caplog.text
+        assert 'Up' in msg.out
+        assert 'Exit' not in msg.out
+    caplog.clear()
+
+def test_stop_simulator_idempotence(args_stop_simulator, caplog):
+    MassPnfSim(args_stop_simulator).stop_simulator()
+    for instance in range(SIM_INSTANCES):
+        instance_ip_offset = instance * 16
+        ip_offset = 2
+        assert f'Stopping pnf-sim-lw-{instance} instance:' in caplog.text
+        assert f'PNF-Sim IP: {str(ip_address(IPSTART) + ip_offset + instance_ip_offset)}' in caplog.text
+        assert "Cannot stop simulator, because it's not running" in caplog.text
+        assert "Simulator successfully stopped" not in caplog.text
+    caplog.clear()
+
 def test_trigger_custom(args_trigger_custom, caplog):
     MassPnfSim(args_trigger_custom).trigger_custom()
     for instance in range(SIM_INSTANCES):
@@ -127,8 +159,8 @@ def test_trigger_custom(args_trigger_custom, caplog):
         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 caplog.text
-        assert 'Simulator started' not in caplog.text
-        assert "Cannot start simulator since it's already running" in caplog.text
+        assert 'Simulator started' in caplog.text
+        assert "Cannot start simulator since it's already running" not in caplog.text
     caplog.clear()
 
 def test_stop(args_stop, caplog):