def build_deviceinfo_file(self, input_list):
        """
        Creates a file containing all devices info with format
        <property name= "<name>" value="<value>"/>

        :type input_list: dict
        :param input_list:List containing all getprop keys/values
        """
        # Check if input list is empty, if yes, write nothing.
        if not input_list or not isinstance(input_list, dict):
            return

        # Prepare XML file
        xml_doc = etree.Element("DeviceProperties")
        file_path = os.path.join(self.path, "device_info.xml")

        # Assign style sheet to test report
        processing_instruction = etree.ProcessingInstruction(
            "xml-stylesheet",
            "type=\"text/xsl\" href=\"../../Core/Report/html/deviceinfo.xsl\"")

        # Create the <DeviceProperties> base element
        for key, value in input_list.items():
            # Build <property name= "<name>" value="<value>"/>
            element = etree.SubElement(xml_doc, "property")
            element.set("name", clean_xml_text(key))
            element.set("value", clean_xml_text(value))

        # Write and close XML File (getprop.txt)
        with open(file_path, 'w') as f_test_report:
            f_test_report.write(
                etree.tostring(processing_instruction,
                               pretty_print=True,
                               xml_declaration=True))
            f_test_report.write(etree.tostring(xml_doc, pretty_print=True))
예제 #2
0
    def add_result(self,
                   test_id,
                   test_result,
                   test_comment,
                   acs_tc_id=None,
                   acs_tc_order=None):
        """
        Add result to the report
        """
        test_node = self.__get_test_node(acs_tc_id, acs_tc_order)
        test_node.append(self.__create_node_value("Test_Id", test_id))
        test_node.append(self.__create_node_value("Test_Result", test_result))

        node_test_comment = etree.SubElement(test_node, "Test_Comment")

        # Set test comment
        if isinstance(test_comment, basestring):
            node_test_comment.text = clean_xml_text(test_comment)
        elif isinstance(test_comment, list) and len(test_comment) == 1:
            node_test_comment.text = clean_xml_text(test_comment[0])
        else:
            for element in test_comment:
                sub_test_comment = etree.SubElement(node_test_comment,
                                                    "SubComment")
                sub_test_comment.text = clean_xml_text(element)
        self.__write_report()
    def _init_campaign_info_node(self, campaign_name, campaign_relative_path,
                                 campaign_type, user_email):
        """
        Initialize the CampaignInfo Node with theses values:

        DeviceInfo:
            - Campaign Name
            - Campaign Relative path
            - Campaign Type (MTBF, SANITY .....)
            - User email
            - MetaCampaign UUID
            - MetaCampaign result UUID

        Optional:
            - Development Campaign

        :param campaign_name: Name of the campaign
        :param campaign_name: Relative path of the campaign
        :param campaign_type: Type of the campaign
        :param user_email: Email address of the user who launch the campaign
        """
        # Create the <BenchInfo> element
        self.campaign_info = etree.SubElement(self.document, "CampaignInfo")

        # Create the <CampaignName> element
        campaign_name_el = etree.SubElement(self.campaign_info, "CampaignName")
        campaign_name_el.set("relative_path", campaign_relative_path)
        # Set CampaignName
        campaign_name_el.text = clean_xml_text(campaign_name)

        # Create the <CampaignType> element
        campaign_type_el = etree.SubElement(self.campaign_info, "CampaignType")

        # Set CampaignType value
        if campaign_type in (None, ""):
            campaign_type = "Default"
        campaign_type_el.text = clean_xml_text(campaign_type)

        # Create the <UserEmail> element
        user_email_el = etree.SubElement(self.campaign_info, "UserEmail")

        # Set UserEmail
        if user_email in (None, ""):
            user_email = Util.AcsConstants.NOT_AVAILABLE
        user_email_el.text = clean_xml_text(user_email)

        # Create the <MetaCampaignUUID> element
        metacampaign_uuid_el = etree.SubElement(self.campaign_info,
                                                "MetaCampaignUUID")
        # Set MetaCampaignUUID value
        metacampaign_uuid_el.text = self._metacampaign_uuid

        # Create the <MetaCampaignResultID> element
        metacampaign_result_id_el = etree.SubElement(self.campaign_info,
                                                     "MetaCampaignResultId")
        # Set MetaCampaignResultId value
        metacampaign_result_id_el.text = Util.AcsConstants.NOT_AVAILABLE
    def update_device_info_node(self, device_properties):
        """
        Update the DeviceInfo node in the campaign result file.

        :param device_properties: set of properties and their associated values
        :type device_properties: dict

        This dictionary contains following information :
            - SwRelease
            - DeviceId
            - Imei
            - ModelNumber
            - FwVersion
            - BasebandVersion
            - KernelVersion
            - AcsAgentVersion

        """

        if isinstance(device_properties, dict):
            for property_name, property_value in device_properties.items():
                if property_value in [None, ""]:
                    property_value = Util.AcsConstants.NOT_AVAILABLE
                property_node = self.device_info.find(property_name)
                if property_node is not None:
                    property_node.text = clean_xml_text(str(property_value))
            # Update test report file
            self.update_report_file()
    def update_flash_info_node(self, flash_properties):
        """
        Update the FlashInfo node in the campaign result file.

        :param properties: set of properties and their associated values
        :type properties: dict

        This dictionary contains following information :
            - SwRelease
            - FwVersion
            - BasebandVersion


        :rtype: None
        :return: None
        """

        if isinstance(flash_properties, dict):
            for property_name, property_value in flash_properties.items():
                if property_value in [None, ""]:
                    property_value = Util.AcsConstants.NOT_AVAILABLE
                property_node = self.flash_info.find(property_name)
                if property_node is not None:
                    property_node.text = clean_xml_text(str(property_value))
            # Update test report file
            self.update_report_file()
    def _init_bench_info_node(self):
        """
        Initialize the DeviceInfo Node with theses values:

        DeviceInfo:
            - Bench Name (hostname)
            - Bench user
            - Bench IP
            - Bench OS
            - ACS Version (host)

        :rtype: None
        """

        # Create the <BenchInfo> element
        self.bench_info = etree.SubElement(self.document, "BenchInfo")

        # Create the <BenchName> node for hostInfo
        bench_name = etree.SubElement(self.bench_info, "BenchName")
        # Set BenchName value
        bench_name.text = clean_xml_text(self.__get_bench_name())

        # Create the <BenchUser> node for hostInfo
        bench_user = etree.SubElement(self.bench_info, "BenchUser")
        # Set BenchUser value
        bench_user.text = clean_xml_text(self.__get_bench_user())

        # Create the <BenchIp> node for hostInfo
        bench_ip = etree.SubElement(self.bench_info, "BenchIp")
        # Set BenchIp value
        bench_ip.text = self.__get_bench_ip()

        # Create the <BenchOs> node for hostInfo
        bench_os = etree.SubElement(self.bench_info, "BenchOs")
        # Set BenchName value
        bench_os.text = self.__get_bench_os()

        # Create the <AcsVersion> node for hostInfo
        acs_version = etree.SubElement(self.bench_info, "AcsVersion")
        # Set AcsVersion value
        acs_version.text = Util.get_acs_release_version()
예제 #7
0
    def __replace_special_characters(self, logs):
        """
        Replace all special characters to be readable in xml format
        In xml text content we have to perform following replacement:
            & by &amp;
            ' by &apos;
            " by &quot;
            < by &lt;
            > by &gt;

        :type logs : string
        :param iteration: Logs to parse

        :rtype: string
        :return: logs reworked to be readable in xml format
        """
        reworked_logs = str(logs)
        reworked_logs = reworked_logs.replace("&", "&amp;").replace("\"", "&quot;").replace("'", "&apos;")
        reworked_logs = reworked_logs.replace("<", "&lt;").replace(">", "&gt;")
        return clean_xml_text(reworked_logs)
    def add_comment(self, tc_order, comment):
        """
        Add a comment to a testcase into test report file.
        If no testcase exists for given order, nothing will be done.

        :type tc_order: int
        :param tc_order: order of the testcase in which comment will be added

        :type comment: str
        :param comment: the comment to add to given testcase
        """

        tc_order = str(tc_order)
        # get all TestCase nodes and loop on them
        node_list = self.document.xpath("//TestCase[@order='%s']" % tc_order)
        if node_list:
            node = node_list[0]
            # wanted node is found
            # get node comment, or create one if no comment node exists
            node_comment = node.find("Comment")
            if node_comment is None:
                # Comment node doesnt exist, so we only update the text of this node
                node_comment = etree.SubElement(node, "Comment")
            elif node_comment.find("SubComment") is None:
                # previous text exist but there is not subcomment node
                old_text_content = node_comment.text
                # add 2 sub comment
                # one dedicated to new comment
                etree.SubElement(node_comment, "SubComment").text = comment
                # one dedicated to previous comment
                etree.SubElement(node_comment,
                                 "SubComment").text = old_text_content
                node_comment.text = ""
            else:
                # previous subcomments exist, add a new one with new comment
                etree.SubElement(node_comment,
                                 "SubComment").text = clean_xml_text(comment)

        # Update test report file
        self.update_report_file()
    def _create_testcase_node(self,
                              test_case_name,
                              test_case_relative_path="",
                              test_case_order=-1,
                              test_case_description="unknown",
                              use_case_name='unknown',
                              parameters="unknown",
                              verdict=None,
                              comment="not executed",
                              start_time=None,
                              end_time=None,
                              exec_nb=1,
                              retry_nb=1,
                              acceptance_nb=1,
                              exec_results=[]):
        """
        Create a node containing test results.

        :type test_case_name: str
        :param test_case_name: Name of the executed test case

        :type test_case_relative_path: str
        :param test_case_relative_path: Relative path of the executed test case

        :type test_case_order: int
        :param test_case_order: Test case order

        :type test_case_description: str
        :param test_case_description: Description of the usecase's purpose

        :type use_case_name: str
        :param use_case_name: Name of the use case which the test case refers to

        :type parameters: str
        :param parameters: String describing a set of usecase's parameters

        :type verdict: tuple
        :param verdict: Test case verdict containing (expected verdict, obtained verdict, reported verdict)

        :type comment: str
        :param comment: Verdict's comment

        :type start_time: datetime
        :param start_time: Time of testcase begin

        :type end_time: datetime
        :param end_time: Time of testcase ends

        :type exec_nb: int
        :param exec_nb: Execution time/occurence

        :type retry_nb: int
        :param retry_nb: Max retry nb

        :type acceptance_nb: int
        :param acceptance_nb: Acceptance number

        :type exec_results: list
        :param exec_results: Results for each execution, as strings

        :rtype: Element
        :return: Node containing all test result's values
        """
        # Set default value for start_time and end_time
        # if necessary
        if start_time is None or end_time is None:
            start_time = datetime.now()
            end_time = start_time

        # compute execution time
        delta_time = end_time - start_time
        # Round seconds to upper integer to avoid 0s execution time when
        # execution is 0s < t < 1s
        if delta_time.microseconds > 0:
            round_up = 1
        else:
            round_up = 0
        delta_time_seconds = (delta_time.days * 3600 *
                              24) + delta_time.seconds + round_up
        days, remainder = divmod(delta_time_seconds, 3600 * 24)
        hours, remainder = divmod(remainder, 3600)
        minutes, seconds = divmod(remainder, 60)
        delta_time_str = '%0.2d:%0.2d:%0.2d' \
            % (hours + days * 24, minutes, seconds)

        # Create the main <TestCase> element
        test_case = etree.Element("TestCase")
        test_case.set("id", test_case_name)
        test_case.set("order", str(test_case_order))
        test_case.set("relative_path", test_case_relative_path)

        # Create a <UseCase> element
        use_case = etree.SubElement(test_case, "UseCase")
        # Set UseCase value
        use_case.text = use_case_name

        # create a <ExecutionTime> element
        test_execution_time = etree.SubElement(test_case, "ExecutionTime")
        test_execution_time.text = delta_time_str

        # create a <StartExecutionDate> element
        test_execution_date = etree.SubElement(test_case, "StartExecutionDate")
        test_execution_date.text = datetime.strftime(start_time, "%d/%m/%Y")

        # create a <StartExecutionTime> element
        test_start_exec_time = etree.SubElement(test_case,
                                                "StartExecutionTime")
        test_start_exec_time.text = datetime.strftime(start_time, "%H:%M:%S")

        # Create a <Description> element
        test_description = etree.SubElement(test_case, "Description")
        # Set Test Description
        test_description.text = test_case_description

        # Create a <Parameters> element
        test_parameters = etree.SubElement(test_case, "Parameters")
        # Set Test parameters
        test_parameters.text = parameters

        # Create verdict nodes
        self._create_verdict_nodes(test_case, verdict)

        # Create a Exec element
        exec_nb_node_deprecated = etree.SubElement(test_case, "Exec")
        exec_nb_node_deprecated.text = str(exec_nb)
        exec_nb_node = etree.SubElement(test_case, "Tries")
        exec_nb_node.attrib['nb'] = str(len(exec_results))
        for i, res in enumerate(exec_results):
            try_node = etree.SubElement(exec_nb_node, "Try")
            try_node.attrib['id'] = str(i + 1)
            try_node.attrib['result'] = str(res)

        # Create a Max Attempt element
        max_attempt_node = etree.SubElement(test_case, "MaxAttempt")
        max_attempt_node.text = str(retry_nb)

        # Create a acceptance element
        acceptance_node = etree.SubElement(test_case, "Acceptance")
        acceptance_node.text = str(acceptance_nb)

        # Create a <Comment> element
        test_comment = etree.SubElement(test_case, "Comment")

        # Set test comment
        if isinstance(comment, basestring):
            test_comment.text = clean_xml_text(comment)
        elif isinstance(comment, list) and len(comment) == 1:
            test_comment.text = clean_xml_text(comment[0])
        else:
            for element in comment:
                if element is not None:
                    sub_test_comment = etree.SubElement(
                        test_comment, "SubComment")
                    sub_test_comment.text = clean_xml_text(element)
        return test_case