dovetail tool: Add cli mechanism for changing envs at run-time 95/23495/6
authorxudan <xudan16@huawei.com>
Fri, 21 Oct 2016 08:03:43 +0000 (08:03 +0000)
committerxudan <xudan16@huawei.com>
Wed, 2 Nov 2016 09:05:23 +0000 (09:05 +0000)
JIRA: DOVETAIL-23

1. Add file cmd_config.yml for defining arguments and options.
2. Using python click module to add and parse cmds in cmd_config.yml
3. Using envs options to update envs in yardstick/functest config file

Change-Id: Ie513c0f0a1ed76f29408419a4bcbbcf21334bb3f
Signed-off-by: xudan <xudan16@huawei.com>
dovetail/conf/cmd_config.yml [new file with mode: 0644]
dovetail/conf/dovetail_config.py
dovetail/conf/dovetail_config.yml
dovetail/container.py
dovetail/run.py

diff --git a/dovetail/conf/cmd_config.yml b/dovetail/conf/cmd_config.yml
new file mode 100644 (file)
index 0000000..63d51ed
--- /dev/null
@@ -0,0 +1,45 @@
+cli:
+  arguments:
+    envs:
+      # This is a simple example of arguments.
+      # Dovetail has no need of this kind of parameters currently.
+      # The arguments must be given orderly at the run-time.
+      #
+      # docker_tag:
+      #   flags: 'docker_tag'
+    non-envs:
+
+  options:
+    envs:
+      SUT_TYPE:
+        flags:
+          - '--SUT_TYPE'
+          - '-t'
+        help: 'Installer type of the system under test (SUT).'
+      SUT_IP:
+        flags:
+          - '--SUT_IP'
+          - '-i'
+        help: 'IP of the system under test (SUT).'
+      DEPLOY_SCENARIO:
+        flags:
+          - '--DEPLOY_SCENARIO'
+          - '-S'
+        help: 'DEPLOY_SCENARIO of the system under test (SUT).'
+      DEPLOY_TYPE:
+        flags:
+          - '--DEPLOY_TYPE'
+          - '-T'
+        help: 'DEPLOY_TYPE of the system under test (SUT).'
+      CI_DEBUG:
+        flags:
+          - '--CI_DEBUG'
+          - '-d'
+        help: 'CI_DEBUG for showing debug log.'
+    non-envs:
+      scenario:
+        flags:
+          - '--scenario'
+          - '-s'
+        default: 'basic'
+        help: 'certification scenario.'
index 6f3eebf..03d0bb7 100644 (file)
@@ -9,6 +9,7 @@
 
 import yaml
 import os
+import re
 
 CERT_PATH = './cert/'
 TESTCASE_PATH = './testcase/'
@@ -27,3 +28,38 @@ container_config = {}
 
 container_config['functest'] = dovetail_config['functest']
 container_config['yardstick'] = dovetail_config['yardstick']
+
+
+with open(os.path.join(curr_path, dovetail_config['cli_file_name'])) as f:
+    cmd_yml = yaml.safe_load(f)
+    dovetail_config['cli'] = cmd_yml[cmd_yml.keys()[0]]
+
+
+def cmd_name_trans(cmd_name):
+    key = cmd_name.upper()
+    if key == 'SUT_TYPE':
+        key = 'INSTALLER_TYPE'
+    if key == 'SUT_IP':
+        key = 'INSTALLER_IP'
+    return key
+
+
+def update_envs(options):
+    for item in options:
+        if options[item] is not None:
+            key = cmd_name_trans(item)
+            os.environ[key] = options[item]
+            update_config_envs('functest', key)
+            update_config_envs('yardstick', key)
+
+
+def update_config_envs(script_type, key):
+    old_value = re.findall(r'\s+%s=(.*?)(\s+|$)' % key,
+                           dovetail_config[script_type]['envs'])
+    if old_value == []:
+        dovetail_config[script_type]['envs'] += \
+            ' -e ' + key + '=' + os.environ[key]
+    else:
+        dovetail_config[script_type]['envs'] = \
+            dovetail_config[script_type]['envs'].replace(old_value[0][0],
+                                                         os.environ[key])
index 901988f..1f5de67 100644 (file)
@@ -2,6 +2,7 @@
 work_dir: /home/opnfv/dovetail
 result_dir: /home/opnfv/dovetail/results
 report_file: 'dovetail_report.txt'
+cli_file_name: 'cmd_config.yml'
 
 # used for testcase cmd template in jinja2 format
 # we have two variables available now
index 6ff3980..6d7ac94 100644 (file)
@@ -7,7 +7,7 @@
 # http://www.apache.org/licenses/LICENSE-2.0
 #
 
-import os
+
 import utils.dovetail_logger as dt_logger
 import utils.dovetail_utils as dt_utils
 from conf.dovetail_config import dovetail_config
@@ -39,18 +39,12 @@ class Container:
         # sshkey="-v /root/.ssh/id_rsa:/root/.ssh/id_rsa "
         docker_image = cls.get_docker_image(type)
         envs = dovetail_config[type]['envs']
-        import re
-        for i in ['INSTALLER_TYPE', 'DEPLOY_SCENARIO',
-                  'DEPLOY_TYPE', 'CI_DEBUG']:
-            envs = re.sub("%s=(\w+)" % i, '%s=%s' % (i, os.getenv(i)), envs)
-        envs = re.sub("INSTALLER_IP=\d+\.?\d+\.?\d+\.?\d+",
-                      'INSTALLER_IP=' + os.getenv('INSTALLER_IP'), envs)
         opts = dovetail_config[type]['opts']
         sshkey = ''
         result_volume = ' -v %s:%s ' % (dovetail_config['result_dir'],
                                         dovetail_config[type]['result']['dir'])
         cmd = 'sudo docker run %s %s %s %s %s /bin/bash' % \
-              (opts, envs, sshkey, result_volume, docker_image)
+            (opts, envs, sshkey, result_volume, docker_image)
         dt_utils.exec_cmd(cmd, logger)
         ret, container_id = \
             dt_utils.exec_cmd("sudo docker ps | grep " + docker_image +
index ddeb311..39dec07 100755 (executable)
@@ -18,6 +18,8 @@ from testcase import Testcase
 from testcase import Scenario
 from report import Report
 from conf.dovetail_config import SCENARIO_NAMING_FMT
+from conf.dovetail_config import dovetail_config
+from conf.dovetail_config import update_envs
 
 logger = dt_logger.Logger('run.py').getLogger()
 
@@ -69,17 +71,49 @@ def run_test(scenario):
         Report.check_result(testcase, db_result)
 
 
-@click.command()
-@click.option('--scenario', default='basic', help='certification scenario')
-def main(scenario):
+def filter_env_options(input_dict):
+    envs_options = {}
+    for key, value in input_dict.items():
+        key = key.upper()
+        if key in dovetail_config['cli']['options']['envs']:
+            envs_options[key] = value
+    return envs_options
+
+
+def main(*args, **kwargs):
     """Dovetail certification test entry!"""
     logger.info('=======================================')
-    logger.info('Dovetail certification: %s!' % scenario)
+    logger.info('Dovetail certification: %s!' % (kwargs['scenario']))
     logger.info('=======================================')
+    envs_options = filter_env_options(kwargs)
+    update_envs(envs_options)
+    logger.info('Your new envs for functest: %s' %
+                dovetail_config['functest']['envs'])
+    logger.info('Your new envs for yardstick: %s' %
+                dovetail_config['yardstick']['envs'])
     load_testcase()
-    scenario_yaml = load_scenario(scenario)
+    scenario_yaml = load_scenario(kwargs['scenario'])
     run_test(scenario_yaml)
     Report.generate(scenario_yaml)
 
+
+CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
+if dovetail_config['cli']['options'] is not None:
+    for key, value in dovetail_config['cli']['options'].items():
+        if value is not None:
+            for k, v in value.items():
+                flags = v['flags']
+                del v['flags']
+                main = click.option(*flags, **v)(main)
+if dovetail_config['cli']['arguments'] is not None:
+    for key, value in dovetail_config['cli']['arguments'].items():
+        if value is not None:
+            for k, v in value.items():
+                flags = v['flags']
+                del v['flags']
+                main = click.argument(flags, **v)(main)
+main = click.command(context_settings=CONTEXT_SETTINGS)(main)
+
+
 if __name__ == '__main__':
     main()