Validate the subcommands on each instance method invocation 61/109261/1
authorBartek Grzybowski <b.grzybowski@partner.samsung.com>
Wed, 17 Jun 2020 10:28:45 +0000 (12:28 +0200)
committerBartek Grzybowski <b.grzybowski@partner.samsung.com>
Wed, 17 Jun 2020 10:28:45 +0000 (12:28 +0200)
Change-Id: I22d6d6ef47ae99a7b2ad7f3e1407e0a49c48c953
Issue-ID: INT-1629
Signed-off-by: Bartek Grzybowski <b.grzybowski@partner.samsung.com>
test/mocks/mass-pnf-sim/MassPnfSim.py

index c7886d3..d6972d9 100755 (executable)
@@ -83,6 +83,33 @@ def get_parser():
 
 class MassPnfSim:
 
+    # MassPnfSim class actions decorator
+    class _MassPnfSim_Decorators:
+        @staticmethod
+        def validate_subcommand(method):
+            def wrapper(self):
+                # Validate 'trigger_custom' subcommand options
+                if self.args.subcommand == 'trigger_custom':
+                    if (self.args.triggerend + 1) > self._enum_sim_instances():
+                        self.logger.error('--triggerend value greater than existing instance count.')
+                        exit(1)
+
+                # Validate --count option for subcommands that support it
+                if self.args.subcommand in ['start', 'stop', 'trigger', 'status', 'stop_simulator']:
+                    if self.args.count > self._enum_sim_instances():
+                        self.logger.error('--count value greater that existing instance count')
+                        exit(1)
+                    if not self._enum_sim_instances():
+                        self.logger.error('No bootstrapped instance found')
+                        exit(1)
+
+                # Validate 'bootstrap' subcommand
+                if (self.args.subcommand == 'bootstrap') and self._enum_sim_instances():
+                    self.logger.error('Bootstrapped instances detected, not overwiriting, clean first')
+                    exit(1)
+                method(self)
+            return wrapper
+
     log_lvl = logging.INFO
     sim_compose_template = 'docker-compose-template.yml'
     sim_vsftpd_template = 'config/vsftpd_ssl-TEMPLATE.conf'
@@ -107,26 +134,6 @@ class MassPnfSim:
         self.mvn_build_cmd = 'mvn clean package docker:build -Dcheckstyle.skip'
         self.docker_compose_status_cmd = 'docker-compose ps'
 
-        # Validate 'trigger_custom' subcommand options
-        if self.args.subcommand == 'trigger_custom':
-            if (self.args.triggerend + 1) > self._enum_sim_instances():
-                self.logger.error('--triggerend value greater than existing instance count.')
-                exit(1)
-
-        # Validate --count option for subcommands that support it
-        if self.args.subcommand in ['start', 'stop', 'trigger', 'status', 'stop_simulator']:
-            if self.args.count > self._enum_sim_instances():
-                self.logger.error('--count value greater that existing instance count')
-                exit(1)
-            if not self._enum_sim_instances():
-                self.logger.error('No bootstrapped instance found')
-                exit(1)
-
-        # Validate 'bootstrap' subcommand
-        if (self.args.subcommand == 'bootstrap') and self._enum_sim_instances():
-            self.logger.error('Bootstrapped instances detected, not overwiriting, clean first')
-            exit(1)
-
     def _run_cmd(self, cmd, dir_context='.'):
         old_pwd = getcwd()
         try:
@@ -224,6 +231,7 @@ class MassPnfSim:
             f.write(template)
         chdir(old_pwd)
 
+    @_MassPnfSim_Decorators.validate_subcommand
     def bootstrap(self):
         self.logger.info("Bootstrapping PNF instances")
 
@@ -311,6 +319,7 @@ class MassPnfSim:
         for sim_id in range(self._enum_sim_instances()):
             rmtree(f"{self.sim_dirname_pattern}{sim_id}")
 
+    @_MassPnfSim_Decorators.validate_subcommand
     def start(self):
         for i in range(*self._get_iter_range()):
             # If container is not running
@@ -324,6 +333,7 @@ class MassPnfSim:
             else:
                 self.logger.warning(f'Instance {self.sim_dirname_pattern}{i} containers are already up')
 
+    @_MassPnfSim_Decorators.validate_subcommand
     def status(self):
         for i in range(*self._get_iter_range()):
             self.logger.info(f'Getting {self.sim_dirname_pattern}{i} instance status:')
@@ -342,6 +352,7 @@ class MassPnfSim:
             else:
                 self.logger.info(' Simulator containers are down')
 
+    @_MassPnfSim_Decorators.validate_subcommand
     def stop(self):
         for i in range(*self._get_iter_range()):
             self.logger.info(f'Stopping {self.sim_dirname_pattern}{i} instance:')
@@ -373,6 +384,7 @@ class MassPnfSim:
             else:
                 self.logger.warning(" Simulator containers are already down")
 
+    @_MassPnfSim_Decorators.validate_subcommand
     def trigger(self):
         self.logger.info("Triggering VES sending:")
         for i in range(*self._get_iter_range()):
@@ -403,6 +415,7 @@ class MassPnfSim:
     # Make the 'trigger_custom' an alias to the 'trigger' method
     trigger_custom = trigger
 
+    @_MassPnfSim_Decorators.validate_subcommand
     def stop_simulator(self):
         self.logger.info("Stopping sending PNF registration messages:")
         for i in range(*self._get_iter_range()):