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

Aline Manera alinefm at linux.vnet.ibm.com
Mon Jan 6 17:36:06 UTC 2014


On 01/03/2014 05:15 AM, Mark Wu wrote:
> To support vlan tagged 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 | 21 ++++++++++++++++++++-
>   tests/utils.py           |  6 ++++++
>   3 files changed, 43 insertions(+), 1 deletion(-)
>
> diff --git a/src/kimchi/networkxml.py b/src/kimchi/networkxml.py
> index 786cb69..61a9ba0 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

2 lines

> +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_vlan_tagged_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..ecc15b0 100644
> --- a/tests/test_networkxml.py
> +++ b/tests/test_networkxml.py
> @@ -23,8 +23,8 @@
>   import ipaddr
>   import unittest
>
> -
>   import kimchi.networkxml as nxml
> +import utils

  2 lines

>   from kimchi.xmlutils import xpath_get_text
>
>
> @@ -151,3 +151,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_vlan_tagged_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_vlan_tagged_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)))




More information about the Kimchi-devel mailing list