def _create_ip_range_from_xml(self, subnet_id, network_name, ip_address, start_ip, end_ip): lib_utils = XMLUtils() net_xml = '<network><name>' + \ network_name + '</name><forward mode=\'nat\'/><ip address=\' ' + \ ip_address + \ '\' netmask=\'255.255.255.0\'><dhcp><range start=\'' + \ start_ip + '\' end=\'' + end_ip + '\'/>''</dhcp></ip></network>' ipRange = IpAddressRange() if lib_utils.parseXML(net_xml, '//network/ip/dhcp') is not None: startIpAddress = IpAddress() startIpAddress.set_address(lib_utils.parseXMLAttributes( net_xml, '//network/ip/dhcp/range', 'start')) ipRange.set_id(startIpAddress.get_address()) startIpAddress.set_id(startIpAddress.get_address()) startIpAddress.set_allocationType('DHCP') ipRange.set_startAddress(startIpAddress) endIpAddress = IpAddress() endIpAddress.set_address(lib_utils.parseXMLAttributes( net_xml, '//network/ip/dhcp/range', 'end')) endIpAddress.set_id(endIpAddress.get_address()) endIpAddress.set_allocationType('DHCP') ipRange.set_endAddress(endIpAddress) ipRange.set_allocationType('DHCP') else: ipRange.set_id(subnet_id + '_AUTO_STATIC') ipRange.set_allocationType('AUTO_STATIC') return ipRange
class XMLUtilsTest(unittest.TestCase): def setUp(self): self.utils = XMLUtils() def test_parseXML(self): self.assertEqual(self.utils.parseXML( """<outer><inner a='90'>mytext1</inner> <inner>mytext2</inner> </outer>""", "//outer/inner"), ['mytext1', 'mytext2']) self.assertEqual(self.utils.parseXML( "<outer><inner>mytext1</inner></outer>", "//outer/inner"), 'mytext1') def test_parseXMLAttributes(self): xml = """<outer><inner>mytext1</inner> <inner b='23'>mytext2</inner> </outer>""" self.assertEqual( self.utils.parseXMLAttributes(xml, '//outer/inner', 'b'), None) self.assertEqual(self.utils.parseXMLAttributes( xml, '//outer/inner', 'b', True), ['23']) def test_getNodeXML(self): xml = """<outer><inner>mytext1</inner> <inner b='23'>mytext2</inner> <a><b>88</b><b>87</b></a><b>89</b></outer>""" self.assertEquals( self.utils.getNodeXML(xml, "//outer/b"), ['<b>89</b>']) self.assertEquals(self.utils.getNodeXML( xml, "//outer/a/b"), ['<b>88</b>', '<b>87</b>']) xml = """<outer xmlns=\"http://namespace\"> <inner>mytext1</inner> <inner b='23'>mytext2</inner> <a><b>88</b><b>87</b></a><b>89</b> </outer>""" namespaces = {'a': 'http://namespace'} self.assertEquals(self.utils.getNodeXML(xml, "//a:outer/a:b", namespaces), ['<b xmlns="http://namespace">89</b>']) self.assertEquals( self.utils.getNodeXML(xml, "//a:outer/a:a/a:b", namespaces), ['<b xmlns="http://namespace">88</b>', '<b xmlns="http://namespace">87</b>'])
class XMLUtilsTest(unittest.TestCase): def setUp(self): self.utils = XMLUtils() def test_parseXML(self): self.assertEqual( self.utils.parseXML( "<outer><inner a='90'>mytext1</inner><inner>mytext2</inner></outer>", "//outer/inner"), ['mytext1', 'mytext2']) self.assertEqual( self.utils.parseXML("<outer><inner>mytext1</inner></outer>", "//outer/inner"), 'mytext1') def test_parseXMLAttributes(self): xml = "<outer><inner>mytext1</inner><inner b='23'>mytext2</inner></outer>" self.assertEqual( self.utils.parseXMLAttributes(xml, '//outer/inner', 'b'), None) self.assertEqual( self.utils.parseXMLAttributes(xml, '//outer/inner', 'b', True), ['23']) def test_getNodeXML(self): xml = "<outer><inner>mytext1</inner><inner b='23'>mytext2</inner><a><b>88</b><b>87</b></a><b>89</b></outer>" self.assertEquals(self.utils.getNodeXML(xml, "//outer/b"), ['<b>89</b>']) self.assertEquals(self.utils.getNodeXML(xml, "//outer/a/b"), ['<b>88</b>', '<b>87</b>']) xml = "<outer xmlns=\"http://namespace\"><inner>mytext1</inner><inner b='23'>mytext2</inner><a><b>88</b><b>87</b></a><b>89</b></outer>" namespaces = {'a': 'http://namespace'} self.assertEquals( self.utils.getNodeXML(xml, "//a:outer/a:b", namespaces), ['<b xmlns="http://namespace">89</b>']) self.assertEquals( self.utils.getNodeXML(xml, "//a:outer/a:a/a:b", namespaces), [ '<b xmlns="http://namespace">88</b>', '<b xmlns="http://namespace">87</b>' ])