add precondition check 13/9513/1
authorkubi <jean.gaoliang@huawei.com>
Fri, 5 Feb 2016 08:48:00 +0000 (08:48 +0000)
committerJörgen Karlsson <jorgen.w.karlsson@ericsson.com>
Mon, 8 Feb 2016 11:29:41 +0000 (11:29 +0000)
as we discussed yersterday, for daily jenkins task,
i have a new idea, i add a precondition check and a key parameter in test case,
if environment info(eg. "DEPLOY_SCENARIO") meet the preconditon which was defined in test case ,
this test case will run, if not meet, this test case will skip.

and default is allow all test case to run, so this patch will not influence existing test case.
any comments are welcomed

Change-Id: I4300ac58994d51c0ddb4dd6d58b7191f796ddcee
Signed-off-by: kubi <jean.gaoliang@huawei.com>
(cherry picked from commit ff5cb9501b155e07fe75f03062217270b9745131)

tests/opnfv/test_cases/opnfv_yardstick_tc027.yaml
tests/unit/cmd/commands/test_task.py
yardstick/cmd/commands/task.py

index e360b4b..cf3afc8 100644 (file)
@@ -24,6 +24,9 @@ scenarios:
     max_rtt: 30
     action: monitor
 
+precondition:
+  installer_type: compass
+  deploy_scenarios: os-nosdn
 
 context:
   type: Node
index e785e99..5b404c4 100644 (file)
@@ -56,3 +56,17 @@ class TaskCommandsTestCase(unittest.TestCase):
         mock_base_runner.Runner.get.return_value = runner
         t._run([scenario], False, "yardstick.out")
         self.assertTrue(runner.run.called)
+
+    @mock.patch('yardstick.cmd.commands.task.os')
+    def test_check_precondition(self, mock_os):
+        cfg = \
+            {'precondition':
+                 {'installer_type': 'compass',
+                  'deploy_scenarios': 'os-nosdn'
+                 }
+            }
+
+        t = task.TaskParser('/opt')
+        mock_os.environ.get.side_effect = ['compass', 'os-nosdn']
+        result = t._check_precondition(cfg)
+        self.assertTrue(result)
index 17e8f4c..55898e1 100644 (file)
@@ -84,9 +84,13 @@ class TaskCommands(object):
             one_task_start_time = time.time()
             parser.path = task_files[i]
             task_name = os.path.splitext(os.path.basename(task_files[i]))[0]
-            scenarios, run_in_parallel = parser.parse_task(task_name,
-                                                           task_args[i],
-                                                           task_args_fnames[i])
+            scenarios, run_in_parallel, meet_precondition = parser.parse_task(
+                task_name, task_args[i], task_args_fnames[i])
+
+            if not meet_precondition:
+                LOG.info("meet_precondition is %s, please check envrionment",
+                         meet_precondition)
+                continue
 
             self._run(scenarios, run_in_parallel, args.output_file)
 
@@ -232,6 +236,7 @@ class TaskParser(object):
             sys.exit(ioerror)
 
         self._check_schema(cfg["schema"], "task")
+        meet_precondition = self._check_precondition(cfg)
 
         # TODO: support one or many contexts? Many would simpler and precise
         # TODO: support hybrid context type
@@ -261,7 +266,7 @@ class TaskParser(object):
             scenario["task_id"] = task_id
 
         # TODO we need something better here, a class that represent the file
-        return cfg["scenarios"], run_in_parallel
+        return cfg["scenarios"], run_in_parallel, meet_precondition
 
     def _check_schema(self, cfg_schema, schema_type):
         '''Check if config file is using the correct schema type'''
@@ -270,6 +275,26 @@ class TaskParser(object):
             sys.exit("error: file %s has unknown schema %s" % (self.path,
                                                                cfg_schema))
 
+    def _check_precondition(self, cfg):
+        '''Check if the envrionment meet the preconditon'''
+
+        if "precondition" in cfg:
+            precondition = cfg["precondition"]
+            installer_type = precondition.get("installer_type", None)
+            deploy_scenarios = precondition.get("deploy_scenarios", None)
+            installer_type_env = os.environ.get('INSTALL_TYPE', None)
+            deploy_scenario_env = os.environ.get('DEPLOY_SCENARIO', None)
+            if installer_type and installer_type_env:
+                if installer_type_env not in installer_type:
+                    return False
+            if deploy_scenarios and deploy_scenario_env:
+                deploy_scenarios_list = deploy_scenarios.split(',')
+                for deploy_scenario in deploy_scenarios_list:
+                    if deploy_scenario_env.startswith(deploy_scenario):
+                        return True
+                return False
+        return True
+
 
 def atexit_handler():
     '''handler for process termination'''