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

Mark Wu wudxw at linux.vnet.ibm.com
Fri Jan 10 03:28:17 UTC 2014


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 | 19 +++++++++++++++++++
 tests/test_networkxml.py | 22 ++++++++++++++++++++++
 tests/utils.py           |  6 ++++++
 3 files changed, 47 insertions(+)

diff --git a/src/kimchi/networkxml.py b/src/kimchi/networkxml.py
index 786cb69..63cb210 100644
--- a/src/kimchi/networkxml.py
+++ b/src/kimchi/networkxml.py
@@ -21,6 +21,10 @@
 # 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 +113,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..42b3ea9 100644
--- a/tests/test_networkxml.py
+++ b/tests/test_networkxml.py
@@ -25,6 +25,9 @@ import unittest
 
 
 import kimchi.networkxml as nxml
+import utils
+
+
 from kimchi.xmlutils import xpath_get_text
 
 
@@ -151,3 +154,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)))
-- 
1.8.4.2




More information about the Kimchi-devel mailing list