Ejemplo n.º 1
0
    def store_in_repo(self, datavar, datavalue, type='str'):
        """For storing datavalue in datavar datarepository
        :Argument:
            datavar = var in data repository in which to store
                      this could be dot separated to store in nested fashion
                      i.e., if var is k1.k2.k3 then the data value would be
                      stored as a value in datarepository[k1][k2][k3]
            datavalue = the value to be stored
            type = type of datavalue (string/int/float)
        """
        def get_dict_to_update(var, val):
            dic = {}
            if '.' in var:
                [key, value] = var.split('.', 1)
                dic[key] = get_dict_to_update(value, val)
            else:
                dic[var] = val
            return dic

        if type == 'int':
            value = int(datavalue)
        elif type == 'float':
            value = float(datavalue)
        else:
            value = datavalue
        dict_to_update = get_dict_to_update(datavar, value)
        update_datarepository(dict_to_update)
        return True
    def write_to_file(self, key, system_name):
        """
            Here we are writing to the file which keyword ran and on which system

            Argument:
                Key: It is basically the keyword name followed by step num in the test case
                system_name: It is the system_name on which the step ran
            Returns:
                True
        """
        data_Utils.update_datarepository({"output_file": self.logfile})
        with open(self.logfile, "a+") as fo:
            fo.write("\n" + "****************************")
            fo.write("\n" + key + " ran")
            fo.write("\n" + "Ran on " + system_name)
            fo.write("\n" + "****************************" + "\n")
        return True
Ejemplo n.º 3
0
    def store_in_repo(self, datavar, datavalue, type='str'):
        """For storing datavalue in datavar datarepository
        :Argument:
            datavar = var in data repository in which to store
                      this could be dot separated to store in nested fashion
                      i.e., if var is k1.k2.k3 then the data value would be
                      stored as a value in datarepository[k1][k2][k3]
            datavalue = the value to be stored
            type = type of datavalue (string/int/float)
        """
        def get_dict_to_update(var, val):
            """

            The function creates a dictionary with Variable and value. If Variable has "." seperated
            keys then the value is updated at appropriate level of the nested dictionary.
            :param var: Dictionary Key or Key seperated with "." for nested dict keys.
            :param val: Value for the Key.

            :return: Dictionary

            """
            dic = {}
            if '.' in var:
                [key, value] = var.split('.', 1)
                dic[key] = get_dict_to_update(value, val)
            else:
                dic[var] = val
            return dic

        if type == 'int':
            value = int(datavalue)
        elif type == 'float':
            value = float(datavalue)
        else:
            value = datavalue
        dict_to_update = get_dict_to_update(datavar, value)
        update_datarepository(dict_to_update)
        print_info("Value: {0} is stored in a Key:{1} of Warrior "
                   "data_repository ".format(datavalue, datavar))
        return True
Ejemplo n.º 4
0
    def store_in_repo(self, datavar=None, datavalue=None, type='str',
                      filepath=None, jsonkey="repo_variables", bool_store_all=False):
        """Stores datavalue in datavar of datarepository
        :Argument:
            1. datavar = Key to be used to store datavalue in data_repository,
                         this could be dot separated to store in nested fashion
                            i.e., if var is k1.k2.k3 then the data value would be
                            stored as a value in datarepository[k1][k2][k3]
            2. datavalue = Value to be stored
            3. type = Type of datavalue(string/int/float)
            4. filepath = Json file where datarepository variables are defined.
                          It is to store multiple key,value pairs in datarepository.
            5. jsonkey = The key where all the REPO variables & values are
                         defined in the filepath
            6. bool_store_all = Set to True to store whole json file content to data repository.
                       keys from the json file will be used as it is to store in repo if this
                       value is set to True.
                       default value is set to False.

            Sample JSON file:
                 {
                     "repo_variables": {
                         "var1": {"type": "int", "value": "10"},
                         "var2.var3": {"value": "10"},
                         "var4.var5": "1"
                         },
                     "user_defined_tag":{
                         "var6" : {"type": "int", "value": "40"}
                         }
                 }
            All three formats in the above sample block are allowed. If 'type'
            is not provided, value will be converted as string by default.
        """
        status = False
        pass_msg = "Value: {0} is stored in a Key: {1} of Warrior data_repository"

        if datavar is not None and datavalue is not None:
            if type == 'int':
                datavalue = int(datavalue)
            elif type == 'float':
                datavalue = float(datavalue)
            dict_to_update = Utils.dict_Utils.get_dict_to_update(datavar, datavalue)
            update_datarepository(dict_to_update)
            print_info(pass_msg.format(datavalue, datavar))
            status = True

        if filepath is not None:
            testcasefile_path = get_object_from_datarepository('wt_testcase_filepath')
            try:
                filepath = getAbsPath(filepath, os.path.dirname(testcasefile_path))
                with open(filepath, "r") as json_handle:
                    json_doc = json.load(json_handle)
                #if bool_store_all is set to True, all content of given json file will be
                #stored in data repository
                if isinstance(bool_store_all, bool) and bool_store_all is True:
                    print_info("bool_store_all is set to True, all content of given"
                               " json file will be stored in data repository")
                    update_datarepository(json_doc)
                    print_info("{0} dictionary stored in Warrior data_repository".
                               format(json_doc))
                    status = True
                elif not isinstance(bool_store_all, bool):
                    print_error("invalid value : {0} given for bool_store_all,"
                                "valid value: boolean True or False".format(bool_store_all))
                    status = False
                elif jsonkey in json_doc:
                    dict_to_update = {}
                    repo_dict = json_doc[jsonkey]
                    for var_key, var_value in repo_dict.items():
                        if isinstance(var_value, dict):
                            if var_value.get('type') == 'int':
                                value = int(var_value['value'])
                            elif var_value.get('type') == 'float':
                                value = float(var_value['value'])
                            else:
                                value = str(var_value['value'])
                        else:
                            value = str(var_value)
                        build_dict = Utils.dict_Utils.get_dict_to_update(var_key, value)
                        Utils.dict_Utils.verify_key_already_exists_and_update\
                            (orig_dict=dict_to_update, new_dict=build_dict)
                    update_datarepository(dict_to_update)
                    print_info("{0} dictionary stored in Warrior data_repository".\
                        format(dict_to_update))
                    status = True
                else:
                    print_error('The {0} file is missing the key '
                                '\"repo_variables\", please refer to '
                                'the Samples in Config_files'.format(filepath))
                    status = True
            except ValueError:
                print_error('The file {0} is not a valid json '
                            'file'.format(filepath))
            except IOError:
                print_error('The file {0} does not exist'.format(filepath))
            except Exception as error:
                print_error('Encountered {0} error'.format(error))

        if (type is None or datavalue is None) and filepath is None:
            print_error('Either Provide values to arguments \"datavar\" & '
                        '\"datavalue\" or to argument \"filepath\"')

        return status
    def store_in_repo(self,
                      datavar=None,
                      datavalue=None,
                      type='str',
                      filepath=None,
                      jsonkey="repo_variables"):
        """Stores datavalue in datavar of datarepository
        :Argument:
            1. datavar = Key to be used to store datavalue in data_repository,
                         this could be dot separated to store in nested fashion
                            i.e., if var is k1.k2.k3 then the data value would be
                            stored as a value in datarepository[k1][k2][k3]
            2. datavalue = Value to be stored
            3. type = Type of datavalue(string/int/float)
            4. filepath = Json file where datarepository variables are defined.
                          It is to store multiple key,value pairs in datarepository.
            5. jsonkey = The key where all the REPO variables & values are
                         defined in the filepath

            Sample JSON file:
                 {
                     "repo_variables": {
                         "var1": {"type": "int", "value": "10"},
                         "var2.var3": {"value": "10"},
                         "var4.var5": "1"
                         },
                     "user_defined_tag":{
                         "var6" : {"type": "int", "value": "40"}
                         }
                 }
            All three formats in the above sample block are allowed. If 'type'
            is not provided, value will be converted as string by default.
        """
        def get_dict_to_update(var, val):
            """
            The function creates a dictionary with Variable and value.
            If Variable has "." separated keys then the value is updated at
            appropriate level of the nested dictionary.
            :param var: Dictionary Key or Key separated with "." for nested dict keys.
            :param val: Value for the Key.

            :return: Dictionary
            """
            dic = {}
            if '.' in var:
                [key, value] = var.split('.', 1)
                dic[key] = get_dict_to_update(value, val)
            else:
                dic[var] = val
            return dic

        status = False
        pass_msg = "Value: {0} is stored in a Key: {1} of Warrior data_repository"

        if datavar is not None and datavalue is not None:
            if type == 'int':
                datavalue = int(datavalue)
            elif type == 'float':
                datavalue = float(datavalue)
            dict_to_update = get_dict_to_update(datavar, datavalue)
            update_datarepository(dict_to_update)
            print_info(pass_msg.format(datavalue, datavar))
            status = True

        if filepath is not None:
            testcasefile_path = get_object_from_datarepository(
                'wt_testcase_filepath')
            try:
                filepath = getAbsPath(filepath,
                                      os.path.dirname(testcasefile_path))
                with open(filepath, "r") as json_handle:
                    json_doc = json.load(json_handle)
                    if jsonkey in json_doc:
                        repo_dict = json_doc[jsonkey]
                        for var_key, var_value in list(repo_dict.items()):
                            if isinstance(var_value, dict):
                                if var_value.get('type') == 'int':
                                    value = int(var_value['value'])
                                elif var_value.get('type') == 'float':
                                    value = float(var_value['value'])
                                else:
                                    value = str(var_value['value'])
                            else:
                                value = str(var_value)
                            dict_to_update = get_dict_to_update(var_key, value)
                            update_datarepository(dict_to_update)
                            print_info(pass_msg.format(value, var_key))
                    else:
                        print_error(
                            'The {0} file is missing the key '
                            '\"repo_variables\", please refer to '
                            'the Samples in Config_files'.format(filepath))
                status = True
            except ValueError:
                print_error('The file {0} is not a valid json '
                            'file'.format(filepath))
            except IOError:
                print_error('The file {0} does not exist'.format(filepath))
            except Exception as error:
                print_error('Encountered {0} error'.format(error))

        if (type is None or datavalue is None) and filepath is None:
            print_error('Either Provide values to arguments \"datavar\" & '
                        '\"datavalue\" or to argument \"filepath\"')

        return status
def get_step_list(filepath, step_tag, sub_step_tag, loop_tag="Loop"):
    """
    Takes the location of Testcase/Suite/Project file as input
    Returns a list of all the step/testcase/testsuite elements
    present in the file.

    :Arguments:
        1. filepath     = full path of the Testcase/suite/project xml file
        2. step_tag     = xml tag for group of step in the file
        3. sub_step_tag = xml tag for each step in the file
        4. loop_tag     = xml tag for loop. Loop by default
    """
    step_list_with_rmt_retry = []
    root = Utils.xml_Utils.getRoot(filepath)
    steps = root.find(step_tag)
    if steps is None:
        print_warning("The file: '{0}' has no {1} to be executed".format(
            filepath, step_tag))
    step_list = []
    for child_node in steps:
        if child_node.tag == sub_step_tag:
            step_list.append(child_node)
        elif child_node.tag == loop_tag:
            loop_count = child_node.get("id")
            if loop_count is None:
                print_error('`id` attribute is mandatory in Loop tag.'
                            ' example : <Loop id="1" file="filename">')
                return False
            json_file = child_node.get("file")
            if json_file is None:
                print_error('`file` attribute is mandatory in Loop tag.'
                            ' example : <Loop id="1" file="filename">')
                return False
            loop_count = loop_count.strip()
            json_file = json_file.strip()
            json_file = Utils.data_Utils.sub_from_env_var(json_file)
            print_info("file is {}".format(json_file))
            loop_steps = child_node.findall(sub_step_tag)
            testcasefile_path = get_object_from_datarepository(
                'wt_testcase_filepath')
            valid_json = True
            try:
                filepath = getAbsPath(json_file,
                                      os.path.dirname(testcasefile_path))
                with open(filepath, "r") as json_handle:
                    json_doc = json.load(json_handle)
                    loop_json = {"loop_json": json_doc}
                    update_datarepository(loop_json)
                    if not isinstance(json_doc, list):
                        valid_json = False
                        print_error(
                            'invalid json format specified,'
                            'valid format : [{"arg1":"value"}, {"arg2":"value"}]'
                        )
                    else:
                        for blob in json_doc:
                            if not isinstance(blob, dict):
                                valid_json = False
                                print_error(
                                    "element is {}. should be dict".format(
                                        type(blob)))
                                print_error(
                                    'invalid json format specified,'
                                    'blob should be dict, valid format : '
                                    '[{"arg1":"value"}, {"arg2":"value"}]')
            except ValueError:
                valid_json = False
                print_error('The file {0} is not a valid json '
                            'file'.format(filepath))
            except IOError:
                valid_json = False
                print_error('The file {0} does not exist'.format(filepath))
            except Exception as error:
                valid_json = False
                print_error('Encountered {0} error'.format(error))

            if not valid_json:
                return False

            for iter_number, _ in enumerate(json_doc):
                for step_number, loop_step in enumerate(loop_steps):
                    copy_step = copy.deepcopy(loop_step)
                    copy_step.set("loop_id", "Loop:{}-Step:{}-Iter:{}".\
                            format(loop_count, step_number+1, iter_number+1))
                    copy_step.set("loop_iter_number", iter_number)
                    arguments = copy_step.find('Arguments')
                    if arguments is not None and arguments is not False:
                        for argument in arguments.findall('argument'):
                            arg_value = argument.get('value')
                            arg_value = Utils.data_Utils.sub_from_loop_json(
                                arg_value, iter_number)
                            argument.set("value", arg_value)
                    step_list.append(copy_step)

    if root.tag == 'Project' or root.tag == 'TestSuite':
        step_list = []
        orig_step_list = steps.findall(sub_step_tag)
        for orig_step in orig_step_list:
            orig_step_path = orig_step.find('path').text
            if '*' not in orig_step_path:
                step_list.append(orig_step)
            # When the file path has asterisk(*), get the Warrior XML testcase/testsuite
            # files matching the given pattern
            else:
                orig_step_abspath = Utils.file_Utils.getAbsPath(
                    orig_step_path, os.path.dirname(filepath))
                print_info("Provided {0} path: '{1}' has asterisk(*) in "
                           "it. All the Warrior XML files matching "
                           "the given pattern will be executed.".format(
                               sub_step_tag, orig_step_abspath))
                # Get all the files matching the pattern and sort them by name
                all_files = sorted(glob.glob(orig_step_abspath))
                # Get XML files
                xml_files = [fl for fl in all_files if fl.endswith('.xml')]
                step_files = []
                # Get Warrior testcase/testsuite XML files
                for xml_file in xml_files:
                    root = Utils.xml_Utils.getRoot(xml_file)
                    if root.tag.upper() == sub_step_tag.upper():
                        step_files.append(xml_file)
                # Copy the XML object and set the filepath as path value for
                # all the files matching the pattern
                if step_files:
                    for step_file in step_files:
                        new_step = copy.deepcopy(orig_step)
                        new_step.find('path').text = step_file
                        step_list.append(new_step)
                        print_info("{0}: '{1}' added to the execution "
                                   "list ".format(sub_step_tag, step_file))
                else:
                    print_warning(
                        "Asterisk(*) pattern match failed for '{}' due "
                        "to at least one of the following reasons:\n"
                        "1. No files matched the given pattern\n"
                        "2. Invalid testcase path is given\n"
                        "3. No testcase XMLs are available\n"
                        "Given path will be used for the Warrior "
                        "execution.".format(orig_step_abspath))
                    step_list.append(orig_step)

    # iterate all steps to get the runmode and retry details
    for _, step in enumerate(step_list):
        runmode, value, _ = get_runmode_from_xmlfile(step)
        retry_type, _, _, retry_value, _ = get_retry_from_xmlfile(step)
        if runmode is not None and value > 0:
            go_next = len(step_list_with_rmt_retry) + value + 1
            step_list_with_rmt_retry = append_step_list(
                step_list_with_rmt_retry,
                step,
                value,
                go_next,
                mode="runmode",
                tag="value")
        if retry_type is not None and retry_value > 0:
            go_next = len(step_list_with_rmt_retry) + retry_value + 1
            if runmode is not None:
                get_runmode = step.find('runmode')
                step.remove(get_runmode)
            step_list_with_rmt_retry = append_step_list(
                step_list_with_rmt_retry,
                step,
                retry_value,
                go_next,
                mode="retry",
                tag="count")
        if retry_type is None and runmode is None:
            step_list_with_rmt_retry.append(step)
    return step_list_with_rmt_retry