def test_init_empty_instance(self): """ Tests the class constructor with 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): Platform() else: with self.assertRaisesRegexp(ValueError, error_msg): Platform()
def test_method_update_xml_element_empty_instance(self): """ Tests the update_xml_element method """ idref = 'cpe:/o:redhat:enterprise_linux:5' xccdf_platform = Platform(idref=idref) self.assertFalse(hasattr(xccdf_platform, 'xml_element'), 'XML element is defined') xccdf_platform.update_xml_element() self.assertTrue(hasattr(xccdf_platform, 'xml_element'), 'XML element is not defined')
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 Group and no Rule child. """ # Containers children = list() statuses = list() version = None titles = list() descriptions = list() platforms = list() groups = list() rules = 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 == 'Group': groups.append(Group(element)) elif tag == 'Rule': rules.append(Rule(element)) # Element validation if len(groups) <= 0 and len(rules) <= 0: error_msg = 'a group must contain at least a group or a rule' 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(groups) children.extend(rules) return children
def create_platform_object(self, object_type='ok'): """ Helper method to create the Platform object :returns: Platform object :rtype: xccdf.models.platform.Platform """ xml_element = self.load_example_element(object_type) return Platform(xml_element)
def test_init_no_xml_element(self): """ Tests the class constructor with an empty instance """ idref = 'cpe:/o:redhat:enterprise_linux:5' xccdf_platform = Platform(idref=idref) self.assertEqual(xccdf_platform.name, 'platform', 'platform tag name does not match') self.assertEqual(xccdf_platform.idref, idref, 'platform idref does not match')
def test_method_to_xml_string(self): """ Tests the to_xml_string method """ xccdf_platform = self.create_platform_object('ok') xml_content = xccdf_platform.to_xml_string() new_xccdf_platform = Platform( etree.fromstring(xml_content.encode('utf-8'))) self.assertEqual(xccdf_platform.idref, new_xccdf_platform.idref, 'Platform idref does not match')