diff options
author | Bartek Grzybowski <b.grzybowski@partner.samsung.com> | 2020-06-17 14:54:56 +0200 |
---|---|---|
committer | Bartek Grzybowski <b.grzybowski@partner.samsung.com> | 2020-06-17 16:12:00 +0200 |
commit | d35d3b8c0ca5784e0ffd7cebad0fa68631f767ab (patch) | |
tree | 3ea92ee01fa17ac28439a99b30f4b2fdcac6cc4d /test | |
parent | 90d6d7ff2abc2047d1b95fab651647d9f5ce4d9c (diff) |
Pass options to each lifecycle method separately
This makes the MassPnfSim object instance stateless.
Change-Id: Ic8f7aefa0afb74a15491b5b0e8ba1cb80e3aa3ef
Issue-ID: INT-1629
Signed-off-by: Bartek Grzybowski <b.grzybowski@partner.samsung.com>
Diffstat (limited to 'test')
-rwxr-xr-x | test/mocks/mass-pnf-sim/MassPnfSim.py | 38 | ||||
-rwxr-xr-x | test/mocks/mass-pnf-sim/mass-pnf-sim.py | 4 | ||||
-rw-r--r-- | test/mocks/mass-pnf-sim/test_cli.py | 6 | ||||
-rw-r--r-- | test/mocks/mass-pnf-sim/test_lifecycle.py | 44 |
4 files changed, 53 insertions, 39 deletions
diff --git a/test/mocks/mass-pnf-sim/MassPnfSim.py b/test/mocks/mass-pnf-sim/MassPnfSim.py index d6972d978..dd6509430 100755 --- a/test/mocks/mass-pnf-sim/MassPnfSim.py +++ b/test/mocks/mass-pnf-sim/MassPnfSim.py @@ -87,7 +87,7 @@ class MassPnfSim: class _MassPnfSim_Decorators: @staticmethod def validate_subcommand(method): - def wrapper(self): + def wrapper(self, args): # pylint: disable=W0613 # Validate 'trigger_custom' subcommand options if self.args.subcommand == 'trigger_custom': if (self.args.triggerend + 1) > self._enum_sim_instances(): @@ -107,7 +107,14 @@ class MassPnfSim: if (self.args.subcommand == 'bootstrap') and self._enum_sim_instances(): self.logger.error('Bootstrapped instances detected, not overwiriting, clean first') exit(1) - method(self) + method(self, args) + return wrapper + + @staticmethod + def substitute_instance_args(method): + def wrapper(self, args): + self.args = args + method(self, args) return wrapper log_lvl = logging.INFO @@ -126,8 +133,7 @@ class MassPnfSim: sim_container_name = 'pnf-simulator' rop_script_name = 'ROP_file_creator.sh' - def __init__(self, args): - self.args = args + def __init__(self): self.logger = logging.getLogger(__name__) self.logger.setLevel(self.log_lvl) self.sim_dirname_pattern = "pnf-sim-lw-" @@ -231,8 +237,9 @@ class MassPnfSim: f.write(template) chdir(old_pwd) + @_MassPnfSim_Decorators.substitute_instance_args @_MassPnfSim_Decorators.validate_subcommand - def bootstrap(self): + def bootstrap(self, args): # pylint: disable=W0613 self.logger.info("Bootstrapping PNF instances") start_port = 2000 @@ -306,7 +313,8 @@ class MassPnfSim: self.logger.info(f'Done setting up instance #{i}') - def build(self): + @_MassPnfSim_Decorators.substitute_instance_args + def build(self, args): # pylint: disable=W0613 self.logger.info("Building simulator image") if path.isfile('pnf-sim-lightweight/pom.xml'): self._run_cmd(self.mvn_build_cmd, 'pnf-sim-lightweight') @@ -314,13 +322,15 @@ class MassPnfSim: self.logger.error('POM file was not found, Maven cannot run') exit(1) - def clean(self): + @_MassPnfSim_Decorators.substitute_instance_args + def clean(self, args): # pylint: disable=W0613 self.logger.info('Cleaning simulators workdirs') for sim_id in range(self._enum_sim_instances()): rmtree(f"{self.sim_dirname_pattern}{sim_id}") + @_MassPnfSim_Decorators.substitute_instance_args @_MassPnfSim_Decorators.validate_subcommand - def start(self): + def start(self, args): # pylint: disable=W0613 for i in range(*self._get_iter_range()): # If container is not running if f"{self.sim_container_name}-{i}" not in self._get_docker_containers(): @@ -333,8 +343,9 @@ class MassPnfSim: else: self.logger.warning(f'Instance {self.sim_dirname_pattern}{i} containers are already up') + @_MassPnfSim_Decorators.substitute_instance_args @_MassPnfSim_Decorators.validate_subcommand - def status(self): + def status(self, args): # pylint: disable=W0613 for i in range(*self._get_iter_range()): self.logger.info(f'Getting {self.sim_dirname_pattern}{i} instance status:') if f"{self.sim_container_name}-{i}" in self._get_docker_containers(): @@ -352,8 +363,9 @@ class MassPnfSim: else: self.logger.info(' Simulator containers are down') + @_MassPnfSim_Decorators.substitute_instance_args @_MassPnfSim_Decorators.validate_subcommand - def stop(self): + def stop(self, args): # pylint: disable=W0613 for i in range(*self._get_iter_range()): self.logger.info(f'Stopping {self.sim_dirname_pattern}{i} instance:') self.logger.info(f' PNF-Sim IP: {self._get_sim_instance_data(i)}') @@ -384,8 +396,9 @@ class MassPnfSim: else: self.logger.warning(" Simulator containers are already down") + @_MassPnfSim_Decorators.substitute_instance_args @_MassPnfSim_Decorators.validate_subcommand - def trigger(self): + def trigger(self, args): # pylint: disable=W0613 self.logger.info("Triggering VES sending:") for i in range(*self._get_iter_range()): sim_ip = self._get_sim_instance_data(i) @@ -415,8 +428,9 @@ class MassPnfSim: # Make the 'trigger_custom' an alias to the 'trigger' method trigger_custom = trigger + @_MassPnfSim_Decorators.substitute_instance_args @_MassPnfSim_Decorators.validate_subcommand - def stop_simulator(self): + def stop_simulator(self, args): # pylint: disable=W0613 self.logger.info("Stopping sending PNF registration messages:") for i in range(*self._get_iter_range()): sim_ip = self._get_sim_instance_data(i) diff --git a/test/mocks/mass-pnf-sim/mass-pnf-sim.py b/test/mocks/mass-pnf-sim/mass-pnf-sim.py index fe6062637..ce8cd73a6 100755 --- a/test/mocks/mass-pnf-sim/mass-pnf-sim.py +++ b/test/mocks/mass-pnf-sim/mass-pnf-sim.py @@ -18,7 +18,7 @@ if __name__ == '__main__': MassPnfSim.log_lvl = log_lvl if args.subcommand is not None: - sim_routine = getattr(MassPnfSim(args), args.subcommand) - sim_routine() + sim_routine = getattr(MassPnfSim(), args.subcommand) + sim_routine(args) else: parser.print_usage() diff --git a/test/mocks/mass-pnf-sim/test_cli.py b/test/mocks/mass-pnf-sim/test_cli.py index 70c9b6c4f..06d018f39 100644 --- a/test/mocks/mass-pnf-sim/test_cli.py +++ b/test/mocks/mass-pnf-sim/test_cli.py @@ -26,7 +26,7 @@ def test_validate_trigger_custom(parser, caplog): args = parser.parse_args(['trigger_custom', '--triggerstart', '0', '--triggerend', str(SIM_INSTANCES)]) try: - MassPnfSim(args).trigger_custom() + MassPnfSim().trigger_custom(args) except SystemExit as e: assert e.code == 1 assert "--triggerend value greater than existing instance count" in caplog.text @@ -59,8 +59,8 @@ def test_count_option_bad_value(parser, caplog, subcommand): '''Test case where invalid value passed to '--count' opt''' try: args = parser.parse_args([subcommand, '--count', str(SIM_INSTANCES + 1)]) - m = getattr(MassPnfSim(args), subcommand) - m() + m = getattr(MassPnfSim(), subcommand) + m(args) except SystemExit: pass assert '--count value greater that existing instance count' in caplog.text diff --git a/test/mocks/mass-pnf-sim/test_lifecycle.py b/test/mocks/mass-pnf-sim/test_lifecycle.py index d98ea48a9..0452c3298 100644 --- a/test/mocks/mass-pnf-sim/test_lifecycle.py +++ b/test/mocks/mass-pnf-sim/test_lifecycle.py @@ -14,16 +14,16 @@ from time import sleep @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() + m = getattr(MassPnfSim(), action) + m(eval(f'args_{action}')) except SystemExit as e: assert e.code == 1 assert 'No bootstrapped instance found' in caplog.text caplog.clear() -def test_bootstrap(args_bootstrap, parser, caplog): +def test_bootstrap(args_bootstrap, caplog): # Initial bootstrap - MassPnfSim(args_bootstrap).bootstrap() + MassPnfSim().bootstrap(args_bootstrap) for instance in range(SIM_INSTANCES): assert f'Creating pnf-sim-lw-{instance}' in caplog.text assert f'Done setting up instance #{instance}' in caplog.text @@ -31,14 +31,14 @@ def test_bootstrap(args_bootstrap, parser, caplog): # Verify bootstrap idempotence try: - MassPnfSim(args_bootstrap).bootstrap() + MassPnfSim().bootstrap(args_bootstrap) except SystemExit as e: assert e.code == 1 assert 'Bootstrapped instances detected, not overwiriting, clean first' in caplog.text caplog.clear() # Verify simulator dirs created - sim_dirname_pattern = MassPnfSim(parser.parse_args([])).sim_dirname_pattern + sim_dirname_pattern = MassPnfSim().sim_dirname_pattern assert len(glob(f"{sim_dirname_pattern}*")) == SIM_INSTANCES # Verify ROP_file_creator.sh running @@ -65,14 +65,14 @@ def test_bootstrap(args_bootstrap, parser, caplog): assert stat(cfg).st_uid == 0 def test_bootstrap_status(args_status, caplog): - MassPnfSim(args_status).status() + MassPnfSim().status(args_status) for _ in range(SIM_INSTANCES): assert 'Simulator containers are down' in caplog.text assert 'Simulator response' not in caplog.text caplog.clear() def test_start(args_start, caplog): - MassPnfSim(args_start).start() + MassPnfSim().start(args_start) for instance in range(SIM_INSTANCES): instance_ip_offset = instance * 16 ip_offset = 2 @@ -83,7 +83,7 @@ def test_start(args_start, caplog): def test_start_status(args_status, docker_containers, caplog): sleep(5) # Wait for the simulator to settle - MassPnfSim(args_status).status() + MassPnfSim().status(args_status) for instance in range(SIM_INSTANCES): assert '"simulatorStatus":"NOT RUNNING"' in caplog.text assert '"simulatorStatus":"RUNNING"' not in caplog.text @@ -92,13 +92,13 @@ def test_start_status(args_status, docker_containers, caplog): def test_start_idempotence(args_start, caplog): '''Verify start idempotence''' - MassPnfSim(args_start).start() + MassPnfSim().start(args_start) assert 'containers are already up' in caplog.text assert 'Starting simulator containers' not in caplog.text caplog.clear() def test_trigger(args_trigger, caplog): - MassPnfSim(args_trigger).trigger() + MassPnfSim().trigger(args_trigger) for instance in range(SIM_INSTANCES): instance_ip_offset = instance * 16 ip_offset = 2 @@ -108,7 +108,7 @@ def test_trigger(args_trigger, caplog): caplog.clear() def test_trigger_status(args_status, capfd, caplog): - MassPnfSim(args_status).status() + MassPnfSim().status(args_status) msg = capfd.readouterr() for _ in range(SIM_INSTANCES): assert '"simulatorStatus":"RUNNING"' in caplog.text @@ -118,13 +118,13 @@ def test_trigger_status(args_status, capfd, caplog): caplog.clear() def test_trigger_idempotence(args_trigger, caplog): - MassPnfSim(args_trigger).trigger() + MassPnfSim().trigger(args_trigger) assert "Cannot start simulator since it's already running" in caplog.text assert 'Simulator started' not in caplog.text caplog.clear() def test_stop_simulator(args_stop_simulator, caplog): - MassPnfSim(args_stop_simulator).stop_simulator() + MassPnfSim().stop_simulator(args_stop_simulator) for instance in range(SIM_INSTANCES): instance_ip_offset = instance * 16 ip_offset = 2 @@ -135,7 +135,7 @@ def test_stop_simulator(args_stop_simulator, caplog): caplog.clear() def test_stop_simulator_status(args_status, capfd, caplog): - MassPnfSim(args_status).status() + MassPnfSim().status(args_status) msg = capfd.readouterr() for _ in range(SIM_INSTANCES): assert '"simulatorStatus":"RUNNING"' not in caplog.text @@ -145,7 +145,7 @@ def test_stop_simulator_status(args_status, capfd, caplog): caplog.clear() def test_stop_simulator_idempotence(args_stop_simulator, caplog): - MassPnfSim(args_stop_simulator).stop_simulator() + MassPnfSim().stop_simulator(args_stop_simulator) for instance in range(SIM_INSTANCES): instance_ip_offset = instance * 16 ip_offset = 2 @@ -156,7 +156,7 @@ def test_stop_simulator_idempotence(args_stop_simulator, caplog): caplog.clear() def test_trigger_custom(args_trigger_custom, caplog): - MassPnfSim(args_trigger_custom).trigger_custom() + MassPnfSim().trigger_custom(args_trigger_custom) for instance in range(SIM_INSTANCES): instance_ip_offset = instance * 16 ip_offset = 2 @@ -167,7 +167,7 @@ def test_trigger_custom(args_trigger_custom, caplog): caplog.clear() def test_stop(args_stop, caplog): - MassPnfSim(args_stop).stop() + MassPnfSim().stop(args_stop) for instance in range(SIM_INSTANCES): instance_ip_offset = instance * 16 ip_offset = 2 @@ -178,14 +178,14 @@ def test_stop(args_stop, caplog): caplog.clear() def test_stop_status(args_status, docker_containers, caplog): - MassPnfSim(args_status).status() + MassPnfSim().status(args_status) for instance in range(SIM_INSTANCES): assert f"{PNF_SIM_CONTAINER_NAME}{instance}" not in docker_containers assert 'Simulator containers are down' in caplog.text caplog.clear() def test_stop_idempotence(args_stop, caplog, docker_containers): - MassPnfSim(args_stop).stop() + MassPnfSim().stop(args_stop) for instance in range(SIM_INSTANCES): assert f'Stopping pnf-sim-lw-{instance} instance:' in caplog.text assert f'ROP_file_creator.sh {instance} already not running' in caplog.text @@ -195,6 +195,6 @@ def test_stop_idempotence(args_stop, caplog, docker_containers): caplog.clear() def test_clean(args_clean): - m = MassPnfSim(args_clean) - m.clean() + m = MassPnfSim() + m.clean(args_clean) assert not glob(f"{m.sim_dirname_pattern}*") |