[Kimchi-devel] [PATCH 1/3] Generate libvirt's interface XML definition for vlanned bridge

Sheldon shaohef at linux.vnet.ibm.com
Fri Jan 3 06:38:09 UTC 2014



lxml is a good xml tool.
Maybe we can use it to refactor other xml code.

On 01/02/2014 05:50 PM, Mark Wu wrote:
> To support vlanned virtual network, kimchi needs create the underlying
> vlan and bridge interface. Libvirt's interface driver can do it with
> a given XML definition. This patch targets to generate the XML according
> to the interface and vlan id.
>
> Signed-off-by: Mark Wu <wudxw at linux.vnet.ibm.com>
> ---
>   src/kimchi/networkxml.py | 17 +++++++++++++++++
>   tests/test_networkxml.py | 20 ++++++++++++++++++++
>   tests/utils.py           |  6 ++++++
>   3 files changed, 43 insertions(+)
>
> diff --git a/src/kimchi/networkxml.py b/src/kimchi/networkxml.py
> index 786cb69..25157fd 100644
> --- a/src/kimchi/networkxml.py
> +++ b/src/kimchi/networkxml.py
> @@ -21,6 +21,8 @@
>   # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
>
>   import ipaddr
> +import lxml.etree as ET
> +from lxml.builder import E
>
>
>   # FIXME, do not support ipv6
> @@ -109,3 +111,18 @@ def to_network_xml(**kwargs):
>       </network>
>       """ % params
>       return xml
> +
> +
> +def create_vlanned_bridge_xml(bridge, interface, vlan_id):
> +    vlan = E.vlan(E.interface(name=interface))
> +    vlan.set('tag', vlan_id)
> +    m = E.interface(
> +        E.start(mode='onboot'),
> +        E.bridge(
> +            E.interface(
> +                vlan,
> +                type='vlan',
> +                name='.'.join([interface, vlan_id]))),
> +        type='bridge',
> +        name=bridge)
> +    return ET.tostring(m)
> diff --git a/tests/test_networkxml.py b/tests/test_networkxml.py
> index 3073bce..1b2e77f 100644
> --- a/tests/test_networkxml.py
> +++ b/tests/test_networkxml.py
> @@ -26,6 +26,7 @@ import unittest
>
>   import kimchi.networkxml as nxml
>   from kimchi.xmlutils import xpath_get_text
> +import utils
>
>
>   class NetworkXmlTests(unittest.TestCase):
> @@ -151,3 +152,22 @@ class NetworkXmlTests(unittest.TestCase):
>           netmask = xpath_get_text(xml, "/network/ip/@netmask")[0]
>           self.assertEquals(netmask,
>                             str(ipaddr.IPNetwork(params["net"]).netmask))
> +
> +
> +class InterfaceXmlTests(unittest.TestCase):
> +
> +    def test_vlanned_bridge_no_ip(self):
> +        expected_xml = """
> +            <interface type='bridge' name='br10'>
> +                <start mode='onboot'/>
> +                <bridge>
> +                    <interface type='vlan' name='em1.10'>
> +                      <vlan tag='10'>
> +                        <interface name='em1'/>
> +                      </vlan>
> +                    </interface>
> +              </bridge>
> +            </interface>
> +            """
> +        actual_xml = nxml.create_vlanned_bridge_xml('br10', 'em1', '10')
> +        self.assertEquals(actual_xml, utils.normalize_xml(expected_xml))
> diff --git a/tests/utils.py b/tests/utils.py
> index 008f668..79fc2e2 100644
> --- a/tests/utils.py
> +++ b/tests/utils.py
> @@ -32,6 +32,7 @@ import unittest
>
>
>   from contextlib import closing
> +from lxml import etree
>
>
>   import kimchi.server
> @@ -159,3 +160,8 @@ def patch_auth():
>
>       import kimchi.auth
>       kimchi.auth.authenticate = _authenticate
> +
> +
> +def normalize_xml(xml_str):
> +    return etree.tostring(etree.fromstring(xml_str,
> +                          etree.XMLParser(remove_blank_text=True)))


-- 
Thanks and best regards!

Sheldon Feng(冯少合)<shaohef at linux.vnet.ibm.com>
IBM Linux Technology Center




More information about the Kimchi-devel mailing list