Esempio n. 1
0
    def create(self, docker_image):
        dovetail_config = dt_cfg.dovetail_config
        project_cfg = dovetail_config[self.valid_type]

        kwargs = dt_utils.get_value_from_dict('opts', project_cfg)
        shell = dt_utils.get_value_from_dict('shell', project_cfg)
        if not shell:
            return None, "Lacking of key word 'shell' in config file."
        env_list = dt_utils.get_value_from_dict('envs', project_cfg)
        if env_list:
            kwargs['environment'] = \
                [env for env in env_list if env is not None]
        volume_list = dt_utils.get_value_from_dict('volumes', project_cfg)
        kwargs['volumes'] = [vol for vol in volume_list if vol is not None]

        kwargs['mounts'], msg = dt_utils.get_mount_list(project_cfg)
        if not kwargs['mounts']:
            return None, msg

        kwargs['extra_hosts'] = dt_utils.get_hosts_info(self.logger)

        try:
            self.container = self.client.containers.run(
                docker_image, shell, **kwargs)
        except (docker.errors.ContainerError, docker.errors.ImageNotFound,
                docker.errors.APIError) as e:
            return None, e

        return self.container.id, 'Successfully to create container.'
Esempio n. 2
0
def get_testcase_list(logger=None, **kwargs):
    dt_testcase.Testcase.load()
    testcase_list = kwargs['testcase']

    # If specify 'testcase' on the CLI, ignore 'testsuite' and 'testarea'. In
    # this case, all test cases are marked as mandatory=false in the result
    # file because there is no testsuite to relate to.
    # If 'testcase' is not specified on the CLI, check the combination of
    # 'testsuite' and 'testarea'
    if testcase_list:
        return check_testcase_list(testcase_list, logger)

    testsuite_validation = False
    testsuite = kwargs['testsuite']
    if testsuite in dt_cfg.dovetail_config['testsuite_supported']:
        testsuite_validation = True
    origin_testarea = kwargs['testarea']
    testarea_validation, testarea = dt_testcase.Testcase.check_testarea(
        origin_testarea)

    if testsuite_validation and testarea_validation:
        testsuite_yaml = load_testsuite(testsuite)
        dt_cfg.dovetail_config['version'] = dt_utils.get_value_from_dict(
            'version', testsuite_yaml)
        dt_cfg.dovetail_config['vnf_type'] = dt_utils.get_value_from_dict(
            'vnf_type', testsuite_yaml)
        testcase_list = dt_testcase.Testcase.get_testcases_for_testsuite(
            testsuite_yaml, testarea)
        return check_testcase_list(testcase_list, logger)
    elif not testsuite_validation:
        logger.error('Test suite {} is not defined.'.format(testsuite))
    else:
        logger.error('Test area {} is not defined.'.format(origin_testarea))
    return None
Esempio n. 3
0
 def _get_config(self, field, project_cfg, testcase_cfg):
     value = dt_utils.get_value_from_dict(field, testcase_cfg)
     if not value:
         value = dt_utils.get_value_from_dict(field, project_cfg)
         if not value:
             self.logger.error("Couldn't find key {}.".format(field))
             return None
     return value
Esempio n. 4
0
 def check_tc_result(self, testcase):
     result_path = dt_cfg.dovetail_config['result_dir']
     check_results_files = dt_utils.get_value_from_dict(
         'report.check_results_files', testcase.testcase)
     if not check_results_files:
         self.logger.error("Failed to get 'check_results_files' from config"
                           " file of test case {}".format(testcase.name()))
         self.check_result(testcase)
         return None
     result_files = []
     for check_results_file in check_results_files:
         result_file = os.path.join(result_path, check_results_file)
         if not os.path.isfile(result_file):
             self.logger.error(
                 'Failed to store results with file {}.'.format(
                     result_file))
             self.check_result(testcase)
             return None
         else:
             result_files.append(result_file)
     self.logger.info(
         'Results have been stored with files: {}.'.format(result_files))
     result = self.get_result(testcase, result_files)
     self.check_result(testcase, result)
     return result
Esempio n. 5
0
 def list_one_testsuite(testsuite):
     testsuite_stream = Testsuite.get(testsuite)
     if testsuite_stream:
         mandatory = dt_utils.get_value_from_dict(
             'testcases_list.mandatory', testsuite_stream)
         optional = dt_utils.get_value_from_dict(
             'testcases_list.optional', testsuite_stream)
         if mandatory:
             click.echo("- mandatory")
             for testcase in mandatory:
                 click.echo("    {}".format(testcase))
         if optional:
             click.echo("- optional")
             for testcase in optional:
                 click.echo("    {}".format(testcase))
         if not (mandatory or optional):
             click.echo("No testcase in testsuite {}".format(testsuite))
     else:
         click.echo("testsuite {} does not exist".format(testsuite))
Esempio n. 6
0
    def get_testcases_for_testsuite(cls, testsuite, testarea):
        testcase_list = []
        selected_tests = []
        testcases = dt_utils.get_value_from_dict('testcases_list', testsuite)
        mandatory = dt_utils.get_value_from_dict('mandatory', testcases)
        optional = dt_utils.get_value_from_dict('optional', testcases)
        if not testcases:
            return testcase_list
        if dt_cfg.dovetail_config['mandatory']:
            if not mandatory:
                cls.logger.error("There is no mandatory test case in "
                                 "test suite {}".format(testsuite['name']))
            else:
                selected_tests += mandatory
        if dt_cfg.dovetail_config['optional']:
            if not optional:
                cls.logger.error("There is no optional test case in "
                                 "test suite {}".format(testsuite['name']))
            else:
                selected_tests += optional
        if (not dt_cfg.dovetail_config['mandatory']
                and not dt_cfg.dovetail_config['optional']):
            if mandatory:
                selected_tests += mandatory
            if optional:
                selected_tests += optional

        if not selected_tests:
            return None
        for value in selected_tests:
            for area in testarea:
                if cls.check_testcase_area(value, area):
                    testcase_list.append(value)
                    if mandatory and value in mandatory:
                        Testcase.testcase_list[value].is_mandatory = True
                    else:
                        Testcase.testcase_list[value].is_mandatory = False
                    break
        return testcase_list
Esempio n. 7
0
    def create(self, docker_image):
        dovetail_config = dt_cfg.dovetail_config
        project_cfg = dovetail_config[self.valid_type]

        kwargs = dt_utils.get_value_from_dict('opts', project_cfg)
        shell = dt_utils.get_value_from_dict('shell', project_cfg)
        if not shell:
            return None
        env_list = dt_utils.get_value_from_dict('envs', project_cfg)
        if env_list:
            kwargs['environment'] = \
                [env for env in env_list if env is not None]
        volume_list = dt_utils.get_value_from_dict('volumes', project_cfg)
        kwargs['volumes'] = [vol for vol in volume_list if vol is not None]
        kwargs['extra_hosts'] = dt_utils.get_hosts_info(self.logger)

        try:
            self.container = self.client.containers.run(
                docker_image, shell, **kwargs)
        except (docker.errors.ContainerError, docker.errors.ImageNotFound,
                docker.errors.APIError):
            return None

        return self.container.id
Esempio n. 8
0
 def archive_logs(self):
     result_path = os.path.join(os.environ['DOVETAIL_HOME'], 'results')
     src_files = dt_utils.get_value_from_dict('report.source_archive_files',
                                              self.testcase.testcase)
     dest_files = dt_utils.get_value_from_dict('report.dest_archive_files',
                                               self.testcase.testcase)
     if not src_files and not dest_files:
         return True
     if not (src_files and dest_files) or len(src_files) != len(dest_files):
         self.logger.error(
             "Can't find corresponding 'result_dest_files' "
             "for 'result_source_files' with testcase {}".format(
                 self.testcase.name()))
         return False
     res = True
     for index in range(0, len(src_files)):
         src_file_path = os.path.join(result_path, src_files[index])
         dest_file_path = os.path.join(result_path, dest_files[index])
         if os.path.isfile(src_file_path):
             os.renames(src_file_path, dest_file_path)
         else:
             self.logger.error("Can't find file {}.".format(src_file_path))
             res = False
     return res
Esempio n. 9
0
    def exec_cmd(self, sub_cmd, exit_on_error=False):
        if not sub_cmd:
            return (1, 'sub_cmd is empty')
        shell = dt_utils.get_value_from_dict(
            'shell', dt_cfg.dovetail_config[self.valid_type])
        if not shell:
            return (1, 'shell is empty')
        cmd = '{} -c "{}"'.format(shell, sub_cmd)
        try:
            result = self.container.exec_run(cmd)
        except docker.errors.APIError as e:
            result = (e.response.status_code, str(e))
            self.logger.error(e)
            if exit_on_error:
                sys.exit(1)

        return result
Esempio n. 10
0
 def clean(self):
     try:
         self.container.remove(force=True)
         self.logger.debug(
             'container: {} was removed'.format(self.container.name))
     except docker.errors.APIError as e:
         self.logger.error(e)
     extra_containers = dt_utils.get_value_from_dict(
         'extra_container', dt_cfg.dovetail_config[self.valid_type])
     if extra_containers:
         for container_name in extra_containers:
             container = self.get_container(container_name)
             if container:
                 try:
                     container.remove(force=True)
                     self.logger.debug(
                         'container: {} was removed'.format(container_name))
                 except docker.errors.APIError as e:
                     self.logger.error(e)
Esempio n. 11
0
    def crawl_from_file(self, testcase, file_path):
        if not os.path.exists(file_path):
            self.logger.error('Result file not found: {}'.format(file_path))
            return None
        criteria = 'FAIL'
        with open(file_path, 'r') as f:
            for jsonfile in f:
                data = json.loads(jsonfile)
                try:
                    criteria = dt_utils.get_value_from_dict(
                        'result.criteria', data)
                    if criteria == 'PASS':
                        valid_tc = testcase.validate_testcase()
                        details = data['result']['testcases'][valid_tc]
                        sla_pass = details['tc_data'][0]['data']['sla_pass']
                        if not 1 == sla_pass:
                            criteria = 'FAIL'
                except KeyError as e:
                    self.logger.exception('Pass flag not found {}'.format(e))
        json_results = {'criteria': criteria}

        testcase.set_results(json_results)
        return json_results
Esempio n. 12
0
 def sub_testcase(self):
     return dt_utils.get_value_from_dict('report.sub_testcase_list',
                                         self.testcase)