def test_tracked_kickstart_elements_filter(self):
        """Test filtering of elements."""

        expected_elements = [
            self._element1, self._element2, self._element3, self._element4,
            self._element5, self._element6, self._element7
        ]
        elements = TrackedKickstartElements()
        for element in expected_elements:
            elements.append(element)

        assert elements.all_elements == expected_elements

        # filtering
        network_commands = elements.get_elements(commands=["network"])
        assert network_commands == [self._element2, self._element3]

        pony_addon = elements.get_elements(addons=["pony"])
        assert pony_addon == [self._element4]

        pre_sections = elements.get_elements(sections=["pre"])
        assert pre_sections == [self._element1]
        # addon is not considered a section
        addon_sections = elements.get_elements(sections=["addon"])
        assert addon_sections == []

        mixed_elements = elements.get_elements(commands=["network"],
                                               sections=["pre", "post"],
                                               addons=["pony"])
        assert mixed_elements == \
            [self._element1, self._element2, self._element3, self._element4, self._element7]

        # nothing required - nothing got
        assert elements.get_elements() == []
Exemple #2
0
    def test_tracked_kickstart_elements_filter(self):
        """Test filtering of elements."""

        expected_elements = [self._element1, self._element2, self._element3,
                             self._element4, self._element5, self._element6,
                             self._element7]
        elements = TrackedKickstartElements()
        for element in expected_elements:
            elements.append(element)

        self.assertEqual(elements.all_elements, expected_elements)

        # filtering
        network_commands = elements.get_elements(commands=["network"])
        self.assertEqual(network_commands, [self._element2, self._element3])

        pony_addon = elements.get_elements(addons=["pony"])
        self.assertEqual(pony_addon, [self._element4])

        pre_sections = elements.get_elements(sections=["pre"])
        self.assertEqual(pre_sections, [self._element1])
        # addon is not considered a section
        addon_sections = elements.get_elements(sections=["addon"])
        self.assertEqual(addon_sections, [])

        mixed_elements = elements.get_elements(commands=["network"],
                                               sections=["pre", "post"],
                                               addons=["pony"])
        self.assertEqual(mixed_elements,
                         [self._element1, self._element2, self._element3, self._element4,
                          self._element7])

        # nothing required - nothing got
        self.assertEqual(elements.get_elements(), [])
Exemple #3
0
    def test_tracked_kickstart_elements_get_refs_kickstart(self):
        """Test getting of element references."""

        appended_elements = [self._element1, self._element2, self._element3,
                             self._element4, self._element5, self._element6,
                             self._element7]
        elements = TrackedKickstartElements()
        for element in appended_elements:
            elements.append(element)

        element_refs = elements.get_references_from_elements(elements.all_elements)
        self.assertEqual(element_refs, self._expected_element_refs)
Exemple #4
0
    def test_tracked_kickstart_elements_dump_kickstart(self):
        """Test dumping of elements into kickstart."""

        appended_elements = [self._element1, self._element2, self._element3,
                             self._element4, self._element5, self._element6,
                             self._element7]
        elements = TrackedKickstartElements()
        for element in appended_elements:
            elements.append(element)

        dumped_ks = elements.get_kickstart_from_elements(elements.all_elements)
        self.assertEqual(dumped_ks, self._expected_ks_content)
    def test_tracked_kickstart_elements_tracking(self):
        """Test tracking of elements."""

        appended_elements = [
            self._element1, self._element2, self._element3, self._element4,
            self._element5, self._element6, self._element7
        ]
        elements = TrackedKickstartElements()
        for element in appended_elements:
            elements.append(element)

        processed_elements = elements.get_and_process_elements(
            commands=["network"], sections=["pre"], addons=["pony"])
        unprocessed_elements = elements.unprocessed_elements
        # still keeping order of elements
        assert unprocessed_elements == [
            self._element5, self._element6, self._element7
        ]
        # nothing is missing
        assert set(elements.all_elements) == \
            set.union(set(processed_elements), set(unprocessed_elements))
        # elements once processed remain processed if you just get them
        elements.get_elements(commands=["network"],
                              sections=["pre"],
                              addons=["pony"])
        assert elements.unprocessed_elements == unprocessed_elements
        # processing some more elements - firewall command
        firewall_elements = elements.get_and_process_elements(
            commands=["firewall"])
        assert set(elements.unprocessed_elements) == \
                         set.difference(set(unprocessed_elements), set(firewall_elements))
        assert elements.unprocessed_elements == [
            self._element6, self._element7
        ]
Exemple #6
0
    def __init__(self, handler, valid_sections=None, missing_include_is_fatal=True):
        """Initialize the parser.

        :param valid_sections: list of valid section names (including '%')
        :type valid_sections: list(str)
        :param missing_include_is_fatal: raise KickstartError if included file
                                         is not found
        :type missing_include_is_fatal: bool
        """

        self._valid_sections = valid_sections or []
        # calls setupSections
        super().__init__(handler, missingIncludeIsFatal=missing_include_is_fatal)
        self._current_ks_filename = self.unknown_filename
        self._result = TrackedKickstartElements()
Exemple #7
0
    def __init__(self, handler, valid_sections=None, missing_include_is_fatal=True):
        """Initialize the parser.

        :param valid_sections: list of valid section names (including '%')
        :type valid_sections: list(str)
        :param missing_include_is_fatal: raise KickstartError if included file
                                         is not found
        :type missing_include_is_fatal: bool
        """

        self._valid_sections = valid_sections or []
        # calls setupSections
        super().__init__(handler, missingIncludeIsFatal=missing_include_is_fatal)
        self._current_ks_filename = self.unknown_filename
        self._result = TrackedKickstartElements()
Exemple #8
0
class SplitKickstartParser(KickstartParser):
    """Kickstart parser for storing kickstart elements.

    Stores kickstart elements (commands, sections, addons) with their line
    number and file name references to kickstart file.
    Does not do any actual command or section parsing (ie command syntax
    checking).

    :raises KickstartParseError: on invalid section
    :raises KickstartError: on missing %include unless instantiated with
                            missing_include_is_fatal=False
    """

    # file name to be used in case of parsing string if not supplied
    unknown_filename = "<MAIN>"

    def __init__(self, handler, valid_sections=None, missing_include_is_fatal=True):
        """Initialize the parser.

        :param valid_sections: list of valid section names (including '%')
        :type valid_sections: list(str)
        :param missing_include_is_fatal: raise KickstartError if included file
                                         is not found
        :type missing_include_is_fatal: bool
        """

        self._valid_sections = valid_sections or []
        # calls setupSections
        super().__init__(handler, missingIncludeIsFatal=missing_include_is_fatal)
        self._current_ks_filename = self.unknown_filename
        self._result = TrackedKickstartElements()

    @property
    def valid_sections(self):
        """List of valid kickstart sections"""
        return list(self._valid_sections)

    @valid_sections.setter
    def valid_sections(self, value):
        self._valid_sections = value

    def split(self, filename):
        """Split the kickstart file into elements.

        :param filename: name of kickstart file
        :type filename: str

        :return: object containing kickstart elements with references to
                 kickstart files
        :rtype: KickstartElements
        """
        with open(filename, "r") as f:
            kickstart = f.read()
        return self.split_from_string(kickstart, filename=filename)

    def split_from_string(self, kickstart, filename=None):
        """Split the kickstart given as string into elements.

        :param kickstart: kickstart to be split
        :type kickstart: str
        :param filename: filename to be used as file reference in the result
        :type filename: str

        :return: object containing kickstart elements with references to
                 kickstart
        :rtype: KickstartElements
        """
        self._reset()
        self._current_ks_filename = filename or self.unknown_filename
        self.readKickstartFromString(kickstart)
        return self._result

    def add_section(self, section):
        """Adds a StoreSection to the result."""
        element = KickstartElement(section.args, section.lines,
                                   section.header_lineno, self._current_ks_filename)
        self._result.append(element)

    def _reset(self):
        self._result = TrackedKickstartElements()
        self.setupSections()

    def _handleInclude(self, f):
        """Overrides parent to keep track of include file names."""
        parent_file = self._current_ks_filename
        self._current_ks_filename = f
        super()._handleInclude(f)
        self._current_ks_filename = parent_file

    def handleCommand(self, lineno, args):
        """Overrides parent method to store the command."""
        element = KickstartElement(args, [self._line], lineno, self._current_ks_filename)
        self._result.append(element)

    def setupSections(self):
        """Overrides parent method to store sections."""
        self._sections = {}
        for section in self._valid_sections:
            self.registerSection(StoreSection(self.handler,
                                              sectionOpen=section,
                                              store=self))
Exemple #9
0
 def _reset(self):
     self._result = TrackedKickstartElements()
     self.setupSections()
Exemple #10
0
class SplitKickstartParser(KickstartParser):
    """Kickstart parser for storing kickstart elements.

    Stores kickstart elements (commands, sections, addons) with their line
    number and file name references to kickstart file.
    Does not do any actual command or section parsing (ie command syntax
    checking).

    :raises KickstartParseError: on invalid section
    :raises KickstartError: on missing %include unless instantiated with
                            missing_include_is_fatal=False
    """

    # file name to be used in case of parsing string if not supplied
    unknown_filename = "<MAIN>"

    def __init__(self, handler, valid_sections=None, missing_include_is_fatal=True):
        """Initialize the parser.

        :param valid_sections: list of valid section names (including '%')
        :type valid_sections: list(str)
        :param missing_include_is_fatal: raise KickstartError if included file
                                         is not found
        :type missing_include_is_fatal: bool
        """

        self._valid_sections = valid_sections or []
        # calls setupSections
        super().__init__(handler, missingIncludeIsFatal=missing_include_is_fatal)
        self._current_ks_filename = self.unknown_filename
        self._result = TrackedKickstartElements()

    @property
    def valid_sections(self):
        """List of valid kickstart sections"""
        return list(self._valid_sections)

    @valid_sections.setter
    def valid_sections(self, value):
        self._valid_sections = value

    def split(self, filename):
        """Split the kickstart file into elements.

        :param filename: name of kickstart file
        :type filename: str

        :return: object containing kickstart elements with references to
                 kickstart files
        :rtype: KickstartElements
        """
        with open(filename, "r") as f:
            kickstart = f.read()
        return self.split_from_string(kickstart, filename=filename)

    def split_from_string(self, kickstart, filename=None):
        """Split the kickstart given as string into elements.

        :param kickstart: kickstart to be split
        :type kickstart: str
        :param filename: filename to be used as file reference in the result
        :type filename: str

        :return: object containing kickstart elements with references to
                 kickstart
        :rtype: KickstartElements
        """
        self._reset()
        self._current_ks_filename = filename or self.unknown_filename
        self.readKickstartFromString(kickstart)
        return self._result

    def add_section(self, section):
        """Adds a StoreSection to the result."""
        element = KickstartElement(section.args, section.lines,
                                   section.header_lineno, self._current_ks_filename)
        self._result.append(element)

    def _reset(self):
        self._result = TrackedKickstartElements()
        self.setupSections()

    def _handleInclude(self, f):
        """Overrides parent to keep track of include file names."""
        parent_file = self._current_ks_filename
        self._current_ks_filename = f
        super()._handleInclude(f)
        self._current_ks_filename = parent_file

    def handleCommand(self, lineno, args):
        """Overrides parent method to store the command."""
        element = KickstartElement(args, [self._line], lineno, self._current_ks_filename)
        self._result.append(element)

    def setupSections(self):
        """Overrides parent method to store sections."""
        self._sections = {}
        for section in self._valid_sections:
            self.registerSection(StoreSection(self.handler,
                                              sectionOpen=section,
                                              store=self))
Exemple #11
0
 def _reset(self):
     self._result = TrackedKickstartElements()
     self.setupSections()