def create_tmp_dir(self):
     """
         Create a temp directory for parallel execution test
     """
     path = file_Utils.createDir(file_Utils.getDirName(self.logsdir), "tmp")
     return True, {
         "parallel_exec_tmp_dir":
         os.path.join(file_Utils.getDirName(self.logsdir), "tmp")
     } if path else False
Esempio n. 2
0
 def verify_json(self, incoming_json, respond_obj, file=False):
     """
         Verify the incoming_json data with either
         a. whole json file
         b. key value pairs
         :param:
             incoming_json: a json string/json obj
             respond_obj: contains the verification detail from datafile
             file: indicate if comparing whole file or just pairs
         :return:
             True if whole file match/all pairs match
             False if not match
     """
     if isinstance(incoming_json, str):
         incoming_json = json.loads(incoming_json)
     if file:
         for expect_json_file in respond_obj["request_verify_data"]:
             expect_json_file = getAbsPath(expect_json_file,
                                           getDirName(self.datafile))
             expect_json = json.load(open(expect_json_file))
             if sorted(incoming_json.items()) == sorted(
                     expect_json.items()):
                 return True
         return False
     else:
         for json_pair in respond_obj["request_verify"]:
             json_keys = json_pair.split(",")[0][4:].split("[")
             # Since datafile is xml and it only have string
             # must have a way to process different object type in json
             json_value = literal_eval(json_pair.split(",")[1][6:])
             # travesing to get the child element value
             incoming_json_index = incoming_json
             for json_key in json_keys:
                 json_key = json_key.replace("]", "")
                 if json_key not in incoming_json_index:
                     return False
                 else:
                     incoming_json_index = incoming_json_index[json_key]
             if incoming_json_index != json_value:
                 return False
     return True
Esempio n. 3
0
    def verify_xml(self, incoming_xml, respond_obj, file=False):
        """
            Verify the incoming_xml data with either
            a. whole xml file
            b. tag text pairs
            :param:
                incoming_xml: an xml string
                respond_obj: contains the verification detail from datafile
                file: indicate if comparing whole file or just pairs
            :return:
                True if whole file match/all pairs match
                False if not match
        """
        if file:
            status = False
            for expect_xml_file in respond_obj["request_verify_data"]:
                expect_xml_file = getAbsPath(expect_xml_file,
                                             getDirName(self.datafile))
                status, _, _, _ = compare_xml(incoming_xml,
                                              expect_xml_file,
                                              output_file=False,
                                              sorted_json=False,
                                              remove_namespaces=True)
            return status
        else:
            incoming_xml = ET.fromstring(incoming_xml,
                                         parser=ET.XMLParser(encoding="utf-8"))
            for element_pair in respond_obj["request_verify"]:
                xpath = element_pair.split(",")[0][4:]
                value = element_pair.split(",")[1][6:]
                incoming_value = getChildElementWithSpecificXpath(
                    incoming_xml, xpath)
                if incoming_value is None or value != incoming_value.text:
                    return False

        return True
Esempio n. 4
0
    def build_server(self, datafile, system_name):
        """
            Take in a system and read all its routes
            Load the routes into Bottle server object
            Start a thread with the bottle server

            return the bottle server adapter and server thread
        """
        app = Bottle()
        # Get system and routes
        system_data = data_Utils.get_credentials(datafile, system_name)
        self.datafile = datafile

        route_file = system_data['mapping_file']
        if route_file:
            route_file = getAbsPath(route_file, getDirName(datafile))
        # Loop through each route
        for route in data_Utils.get_all_system_or_subsystem(route_file):
            route_name = route.get('name')
            if route_name[0] != '/':
                route_name = '/' + route_name

            # Group request condition with the same method together
            route_methods = {}
            for request in route:
                request_method = request.find('request_method').text.upper()
                if request_method not in route_methods:
                    route_methods[request_method] = [request]
                else:
                    route_methods[request_method].append(request)

            # Build route with the grouped conditions
            for method_type, same_type_methods in route_methods.items():
                # A route can have general response and conditional response
                specific_res = []
                general_res = {}

                for method in same_type_methods:
                    dict_of_info = {}
                    method_req = {}
                    method_res = {}

                    # Get all info from the condition
                    for info in iter(method):
                        if info.tag in dict_of_info:
                            dict_of_info[info.tag].append(info.text)
                        else:
                            dict_of_info[info.tag] = [info.text]

                    # Extract request/response related info
                    for key, value in dict_of_info.items():
                        if key in request_verify_list:
                            method_req = {key: value}
                        elif key in response_list:
                            method_res[key] = value

                    if any([
                            key in request_verify_list
                            for key in dict_of_info.keys()
                    ]):
                        # this condition has request/response pair
                        method_combine = method_req
                        method_combine.update(method_res)
                        specific_res.append(method_req)
                        # this ensure when all verification fail and no general response given
                        # there will be some responses
                        if any([
                                key in on_fail_response_list
                                for key in dict_of_info.keys()
                        ]):
                            general_res.update(method_res)
                    else:
                        # this condition only has general response
                        general_res.update(method_res)

                app.route(
                    route_name, method_type,
                    self.build_route(route_name, method_type, specific_res,
                                     general_res))

        # Build a class to hold the server so it can be closed easily
        port = 5000 if "port" not in system_data else int(system_data["port"])
        server = ServerHandler(host="0.0.0.0", port=port)
        server_thread = threading.Thread(target=run,
                                         kwargs={
                                             "app": app,
                                             "server": server,
                                             "debug": True
                                         })
        server_thread.daemon = True
        server_thread.start()
        sleep(2)

        if server_thread.is_alive():
            return True, {"server": server, "server_thread": server_thread}
        else:
            return False, {}
Esempio n. 5
0
    def sftp_from_remotehost(cls, session_object, ip_type, sftp_operation, port,
                             filepath, prompt, dest_address, username,
                             password, filepath_dest):
        """sftp(test both put and get).This keyword can be used to transfer
        file from source _system to destination system or vice versa.
        It checks the size after transfer in both get and put.

        :Arguments:
            1. session_object(string)  = name of the Linux machine on which\
                                      to execute
            2. ip_type(string) = iptype of the dest system through \
                                 which it needs to be connected.
                                 needs to be one of \
                                 (ip/ipv4/dns/lmp_ip/lmp_ipv6).It has to be \
                                 present in the input data file.
            3. sftp_operation(string) = get/put/both
            4. port(string) = source port
            5. filepath(string) = file with filepath in source\
                                  system(used for put)
            6. prompt(string)  = prompt of the source system
            7. dest_address(string) = ipv4 address or defaulted to lcn ip
            8. username(string) = username of the dest system
            9. password(string) = password of the dest system
            9. filepath_dest(string) = file with filepath in destination\
                               system(used for get)
        :Returns:
            1. bool (True/False)

        """
        put = True if ("put" in sftp_operation or "both" in sftp_operation) \
                   else False
        get = True if ("get" in sftp_operation or "both" in sftp_operation) \
                   else False

        status_get = False if get else True
        status_put = False if put else True

        filedir, filename = (file_Utils.getDirName(filepath),
                             file_Utils.getFileName(filepath))
        filedir_dest, filename_dest = (file_Utils.getDirName(filepath_dest),
                                       file_Utils.getFileName(filepath_dest))

        sftp_cmd = "sftp"
        if ip_type == "ipv6":
            sftp_cmd = "sftp6"

        # move into the path
        cli_Utils.send_command(session_object, ".*", prompt, "cd {}".
                               format(filedir))
        command = sftp_cmd +"  -oPort={0} {1}@{2}".format(port, username,
                                                          dest_address)

        # check whether file is available
        putfilepresent = cls.file_exists_on_remote(session_object, prompt,
                                                   filename)
        if putfilepresent:
            # check the put file size
            put_file_size = cls.get_file_size(session_object, prompt, filename)

        else:
            pNote("Specified put file:{} not found".format(filename), "error")

        # Starting the sftp connection
        status = cls.start_sftp_on_remote(session_object, command,
                                          password, prompt)
        if status:
            cli_Utils.send_command(session_object, ".*", ">", "cd {}".
                                   format(filedir_dest))

            if putfilepresent and put:
                status_put = cls.sftp_put_from_remote(session_object,
                                                      filename, filedir)
                put_file_size_transf = cls.get_file_size(session_object, ">",
                                                         filename,
                                                         session="sftp")
                if status_put:
                    pNote("Actual put file size:%s Transferred put file size"
                          ":%s"%(str(put_file_size), str(put_file_size_transf)))
                    if str(put_file_size) == str(put_file_size_transf):
                        status_put = True
                        pNote("Transferred put file size matches the origin")
                    else:
                        status_put = False
                        pNote("Transferred put file size does not"
                              "match the origin")

            if get:
                # check whether file is available
                getfilepresent = cls.file_exists_on_remote(session_object, ">",
                                                           filename_dest)

                if getfilepresent:
                    gfile_size = cls.get_file_size(session_object, ">",
                                                   filename_dest,
                                                   session="sftp")
                    status_get = cls.sftp_get_from_remote(session_object,
                                                          filename_dest,
                                                          filedir_dest)

                    # exiting the sftp terminal
                    cli_Utils.send_command(session_object, ".*", prompt, "exit")

                    if status_get:
                        # check the get file size after transfer
                        gfile_size_transf = cls.get_file_size(session_object,
                                                              prompt,
                                                              filename_dest)

                        pNote("Actual get file size:%s Transferred get file"
                              "size:%s"%(str(gfile_size),
                                         str(gfile_size_transf)))
                        if str(gfile_size) == str(gfile_size_transf):
                            status_get = True
                            pNote("Transferred get file size matches the origin")
                        else:
                            status_get = False
                            pNote("Transferred get file size matches the origin")
                else:
                    pNote("Specified get file:{} not found".format(filename_dest),
                          "error")

            else:
                cli_Utils.send_command(session_object, ".*", prompt, "exit")

            status = status_get and status_put
        else:
            status = False
        return  status