Dan Kenigsberg has uploaded a new change for review.

Change subject: net: find toplevel iterface name based on net attributes
......................................................................

net: find toplevel iterface name based on net attributes

It is quite a common task to look for the name of the top-level iterface
of a network. This patch makes it a unitility function, and uses it in
one location. Note that the original code, introduced in commit 70608a5,
was buggy, as it forgot the dot in interfaces such as eth0.100.

Change-Id: Ieb6ec043091eed6b3b0d8b3b66924aed66af3e12
Signed-off-by: Dan Kenigsberg <[email protected]>
---
M lib/vdsm/netconfpersistence.py
M lib/vdsm/network/utils.py
A tests/network/unit_test.py
3 files changed, 81 insertions(+), 10 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/23/55323/1

diff --git a/lib/vdsm/netconfpersistence.py b/lib/vdsm/netconfpersistence.py
index 6397a7c..ad88640 100644
--- a/lib/vdsm/netconfpersistence.py
+++ b/lib/vdsm/netconfpersistence.py
@@ -1,5 +1,5 @@
 #
-# Copyright 2013 Red Hat, Inc.
+# Copyright 2013-2016 Red Hat, Inc.
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -31,6 +31,7 @@
 from . import constants
 from . import utils
 from vdsm.network.canonicalize import canonicalize_networks
+from vdsm.network.utils import toplevel_iface
 
 CONF_RUN_DIR = constants.P_VDSM_RUN + 'netconf/'
 # The persistent path is inside of an extra "persistence" dir in order to get
@@ -220,13 +221,8 @@
     if bridge not in nets:
         return []
 
-    network = nets[bridge]
-    nic = network.get('nic')
-    bond = network.get('bonding')
-    vlan = str(network.get('vlan', ''))
-    if bond:
-        return [bond + vlan]
-    elif nic:
-        return [nic + vlan]
-    else:  # isolated bridged network
+    iface = toplevel_iface(bridge, nets[bridge])
+    if iface is None:
         return []
+    else:
+        return [iface]
diff --git a/lib/vdsm/network/utils.py b/lib/vdsm/network/utils.py
index 4bb5e50..22c1b55 100644
--- a/lib/vdsm/network/utils.py
+++ b/lib/vdsm/network/utils.py
@@ -28,3 +28,23 @@
     """
     return ' '.join((option for option in options.split()
                      if not option.startswith('custom=')))
+
+
+def toplevel_iface(net, net_attr):
+    if net_attr['bridged']:
+        return net
+
+    nic = net_attr.get('nic')
+    bond = net_attr.get('bonding')
+
+    if 'vlan' in net_attr:
+        vlan = '.' + str(net_attr['vlan'])
+    else:
+        vlan = ''
+
+    if bond:
+        return bond + vlan
+    elif nic:
+        return nic + vlan
+    else:
+        return None
diff --git a/tests/network/unit_test.py b/tests/network/unit_test.py
new file mode 100644
index 0000000..ac4decc
--- /dev/null
+++ b/tests/network/unit_test.py
@@ -0,0 +1,55 @@
+#
+# Copyright 2016 Red Hat, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
+#
+# Refer to the README and COPYING files for full details of the license
+#
+
+from __future__ import absolute_import
+
+from nose.plugins.attrib import attr
+
+from testlib import VdsmTestCase as TestCaseBase
+
+from vdsm.network.utils import toplevel_iface
+
+
+@attr(type='unit')
+class TopLevelIfaceTests(TestCaseBase):
+    def test_bridged(self):
+        self.assertEqual(
+            'n',
+            toplevel_iface('n', {'bridged': True, 'nic': 'eth0'}))
+
+    def test_nicless(self):
+        self.assertEqual('n', toplevel_iface('n', {'bridged': True}))
+
+    def test_nic(self):
+        self.assertEqual(
+            'eth0',
+            toplevel_iface('n', {'bridged': False, 'nic': 'eth0'}))
+
+    def test_vlan(self):
+        self.assertEqual(
+            'eth0.7',
+            toplevel_iface(
+                'n', {'bridged': False, 'nic': 'eth0', 'vlan': 7}))
+
+    def test_bond(self):
+        self.assertEqual(
+            'bond7.7',
+            toplevel_iface(
+                'n', {'bridged': False, 'bonding': 'bond7', 'vlan': 7}))


-- 
To view, visit https://gerrit.ovirt.org/55323
To unsubscribe, visit https://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ieb6ec043091eed6b3b0d8b3b66924aed66af3e12
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Dan Kenigsberg <[email protected]>
Gerrit-Reviewer: Edward Haas <[email protected]>
Gerrit-Reviewer: gerrit-hooks <[email protected]>
_______________________________________________
vdsm-patches mailing list
[email protected]
https://lists.fedorahosted.org/mailman/listinfo/vdsm-patches

Reply via email to