Beispiel #1
0
    def _collect_from_instrcutions(self,
                                   current_instruction: InstructionWrapper,
                                   callpath: str = '') -> None:
        if callpath == '':
            callpath = str(current_instruction)
        else:
            callpath = f"{callpath}->{current_instruction}"

        # travel down to the leaf elements of the instruction tree
        # which are artefacts
        for child in current_instruction.instructionchildren:
            self._collect_from_instrcutions(child, callpath)

        # Artefacts do implement the method '_collect'.
        if '_collect' in dir(current_instruction.instruction):
            self._collect_and_document(current_instruction.instruction,
                                       callpath=callpath)

            # if the current instruction contains a "placeholdername" attribute this means
            # there is a placeholder for the global placeholder dictionary which needs to be
            # filled with the result of the current collector, so that following collectors
            # can use this information for their own collection process, like for example the
            # machine name.
            if current_instruction.placeholdername != '':
                Placeholder.update_placeholder(
                    current_instruction.placeholdername,
                    current_instruction.instruction.data[0].collecteddata)
                Collector._logger.info(
                    f"Stored artefact data '{current_instruction.instruction.data}' as placeholder "
                    f"'{current_instruction.placeholdername}'.")
        else:
            Collector._logger.debug(callpath)
Beispiel #2
0
    def test_update_placeolder_with_value_which_contains_placeholder(self):
        """
        Should accept the placeholder.
        """
        expected_placeholer = f"{Placeholder.PLACEHOLDER_START}test_placeholder{Placeholder.PLACEHOLDER_END}"
        expected_placeholder_value = "test_value"

        Placeholder.update_placeholder(placeholdername=expected_placeholer, placeholdervalue=expected_placeholder_value)
        actual_value = Placeholder._instruction_placeholders[expected_placeholer]

        self.assertEqual(expected_placeholder_value, actual_value)
Beispiel #3
0
    def test_update_placeholder_with_new_placeholder(self):
        """
        Should add the new placeholder to the global placeholders dicitionary.
        """
        expected_placeholer = "test_placeholder"
        expected_placeholder_value = "test_value"

        Placeholder.update_placeholder(placeholdername=expected_placeholer, placeholdervalue=expected_placeholder_value)
        actual_value = Placeholder._instruction_placeholders[expected_placeholer]

        self.assertEqual(expected_placeholder_value, actual_value)
Beispiel #4
0
    def _init_instructions(self,
                           current: Element = None,
                           parentinstruction=None,
                           instructionid: int = 0):
        """
        Parses the instructions xml file and initializes the artefact collectors in memory.
        Artefact xml elements need to have the attribute "module". If not, they are been ignored.
        :param current: Current xml element. Needed for recursive traversal.
        :param parentinstruction: Parent to be stored for current so later the collector can call back
        parent for context information.
        :param instructionid: Id of the current instruction which is an iterating integer starting with 0
        for the first found collector.
        :return: object of type InstructionWrapper
        """
        if current is None:
            current = self._get_first_instruction_element()

        currentinstruction = getattr(
            importlib.import_module(
                current.attributes[XmlParser.MODULE_ATTRIBUTE].nodeValue),
            current.tagName)(parent=parentinstruction,
                             parameters=XmlParser._get_parameter_attributes(
                                 current.attributes))

        currentinstruction.protocol = self._protocol

        placeholder_name = XmlParser._get_placeholder_name(current)
        # If there is a placeholder in the element store it for later use in the global placeholders
        if placeholder_name is not None and placeholder_name != "":
            Placeholder.update_placeholder(
                placeholder_name, f"{Placeholder.PLACEHOLDER_START}"
                f"{placeholder_name}"
                f"{Placeholder.PLACEHOLDER_END}")
        instructionwrapper = InstructionWrapper(
            instruction=currentinstruction,
            parentinstrutction=parentinstruction,
            instructionid=instructionid,
            placeholdername=placeholder_name)

        for child in current.childNodes:
            if type(child) is Element:
                instructionwrapper_child = self._init_instructions(
                    current=child,
                    parentinstruction=currentinstruction,
                    instructionid=instructionid + 1)
                XmlParser._logger.debug(
                    f"Adding '{instructionwrapper_child.instructionname}' with "
                    f"id {instructionwrapper_child.instructionid} as child of "
                    f"'{instructionwrapper.instructionname}' with id {instructionwrapper.instructionid}."
                )
                instructionwrapper.addchild(instructionwrapper_child)

        return instructionwrapper
Beispiel #5
0
    def test_update_placeholder_with_defined_placeholder(self):
        """
        Should overwrite the placeholder already defined with the new value.
        """
        expected_placeholer = "test_placeholder"
        expected_placeholder_value = "test_value"
        expected_placeholder_new_value = "test_value_new"

        Placeholder.update_placeholder(placeholdername=expected_placeholer,
                                       placeholdervalue=expected_placeholder_value)
        Placeholder.update_placeholder(placeholdername=expected_placeholer,
                                       placeholdervalue=expected_placeholder_new_value)
        actual_value = Placeholder._instruction_placeholders[expected_placeholer]

        self.assertEqual(expected_placeholder_new_value, actual_value)
Beispiel #6
0
    def test_get_metadata_with_metadata_with_placeholder(self):
        """
        Should return the metadata value, but the placeholder is replaced with the correct placeholder
        value.
        :return:
        """
        from businesslogic.placeholders import Placeholder

        expected_metadata = {"Metadata1": "!@placeholder@!"}
        expected_metadata_key = "Metadata1"
        expected_metadata_value = "placeholdervalue"

        Placeholder.update_placeholder(
            placeholdername="placeholder",
            placeholdervalue=expected_metadata_value)

        actual_metadata = CollectionMetaData(metadata=expected_metadata)
        actual_metadata_value = actual_metadata.get_metadata(
            expected_metadata_key)

        self.assertEqual(expected_metadata_value, actual_metadata_value)
Beispiel #7
0
 def cache_parameters(cls, attributes: UserDict = None) -> None:
     for attributename in attributes.keys():
         attributevalue = Placeholder.replace_placeholders(attributes[attributename])
         Placeholder.update_placeholder(attributename, attributevalue)
         SourceBase._logger.debug(
             f"Source: Cached source parameter '{attributename}'. Parameter value: '{attributevalue}'")
Beispiel #8
0
"""