Exemplo n.º 1
0
    def test_init_with_empty_instance(self):
        """
        Tests the class constructor from an empty instance
        """

        error_msg = 'either xml_element or idref are required'

        if sys.version_info[0] >= 3 and sys.version_info[1] >= 2:
            with self.assertRaisesRegex(ValueError,
                                        error_msg):
                Select()
        else:
            with self.assertRaisesRegexp(ValueError,
                                         error_msg):
                Select()
Exemplo n.º 2
0
    def test_init_no_xml_element(self):
        """
        Tests the class constructor from an empty instance
        """

        idref = 'usgcb-rhel5desktop-rule-2.1.1.1.1.a'
        xccdf_select = Select(idref=idref)

        self.assertEqual(xccdf_select.name, 'select',
                         'select tag name does not match')

        self.assertEqual(xccdf_select.idref, idref,
                         'select idref does not match')

        self.assertFalse(xccdf_select.is_selected())
Exemplo n.º 3
0
    def test_method_update_xml_element_empty_instance(self):
        """
        Tests the update_xml_element method with an empty instance
        """

        idref = 'usgcb-rhel5desktop-rule-2.1.1.1.1.a'
        xccdf_select = Select(idref=idref)

        self.assertFalse(hasattr(xccdf_select, 'xml_element'),
                         'XML element is defined')

        xccdf_select.update_xml_element()

        self.assertTrue(hasattr(xccdf_select, 'xml_element'),
                        'XML element is not defined')
Exemplo n.º 4
0
    def load_children(self):
        """
        Load the subelements from the xml_element in its correspondent classes.

        :returns: List of child objects.
        :rtype: list
        :raises CardinalityException: If there is more than one Version child.
        :raises CardinalityException: If there is no Title child element.
        """

        # Containers
        children = list()
        statuses = list()
        version = None
        titles = list()
        descriptions = list()
        platforms = list()
        selects = list()

        # Element load
        for element in self.xml_element:
            uri, tag = Element.get_namespace_and_tag(element.tag)
            if tag == 'version':
                if version is None:
                    version = Version(element)
                else:
                    error_msg = 'version element found more than once'
                    raise CardinalityException(error_msg)
            elif tag == 'status':
                statuses.append(Status(element))
            elif tag == 'title':
                titles.append(Title(element))
            elif tag == 'description':
                descriptions.append(Description(element))
            elif tag == 'platform':
                platforms.append(Platform(element))
            elif tag == 'select':
                selects.append(Select(element))

        # Element validation
        if len(titles) <= 0:
            error_msg = 'title element is required at least once'
            raise CardinalityException(error_msg)

        # List construction
        children.extend(statuses)
        if version is not None:
            children.append(version)
        children.extend(titles)
        children.extend(descriptions)
        children.extend(platforms)
        children.extend(selects)

        return children
Exemplo n.º 5
0
    def create_select_object(self, object_type='ok'):
        """
        Helper method to create the Select object

        :returns: Select object
        :rtype: xccdf.models.select.Select
        """

        xml_element = self.load_example_element(object_type)

        return Select(xml_element)
Exemplo n.º 6
0
    def test_print_object_empty_instance(self):
        """
        Tests the string representation of an Select object
        from an empty instance
        """

        idref = 'usgcb-rhel5desktop-rule-2.1.1.1.1.a'
        xccdf_select = Select(idref=idref)

        string_value = 'select {idref} {sel}'.format(idref=idref, sel=False)
        self.assertEqual(str(xccdf_select), string_value,
                         'String representation does not match')
Exemplo n.º 7
0
    def test_method_to_xml_string(self):
        """
        Tests the to_xml_string method
        """

        xccdf_select = self.create_select_object('ok')

        xml_content = xccdf_select.to_xml_string()

        new_xccdf_select = Select(
            etree.fromstring(xml_content.encode('utf-8')))

        self.assertEqual(xccdf_select.idref, new_xccdf_select.idref,
                         'Select idref does not match')
        self.assertEqual(xccdf_select.selected, new_xccdf_select.selected,
                         'Select selected does not match')