[node-patches] Change in ovirt-node[master]: test: Add unit test for network config

fabiand at fedoraproject.org fabiand at fedoraproject.org
Mon Jun 10 12:29:13 UTC 2013


Fabian Deutsch has uploaded a new change for review.

Change subject: test: Add unit test for network config
......................................................................

test: Add unit test for network config

Change-Id: I7746e5d4f06637c70b2a7412ee277b57fdd2ec60
Signed-off-by: Fabian Deutsch <fabiand at fedoraproject.org>
---
M tests/Makefile.am
A tests/nose/network_config.py
2 files changed, 118 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-node refs/changes/12/15512/1

diff --git a/tests/Makefile.am b/tests/Makefile.am
index a98b225..03f18e3 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -15,7 +15,7 @@
 # MA  02110-1301, USA.  A copy of the GNU General Public License is
 # also available at http://www.gnu.org/copyleft/gpl.html.
 
-PYTHONUNITTESTS:=$(shell find unit_tests/ -name \*.py)
+PYTHONUNITTESTS:=$(shell find nose/ -name \*.py)
 
 check-local: unittests
 	@echo Passed $@
@@ -24,4 +24,4 @@
 	@echo Passed $@
 
 %.nose:
-	PYTHONPATH=$(top_srcdir)/src nosetests -v "$*"
\ No newline at end of file
+	PYTHONPATH=$(top_srcdir)/src nosetests -v "$*"
diff --git a/tests/nose/network_config.py b/tests/nose/network_config.py
new file mode 100644
index 0000000..9e4c5e8
--- /dev/null
+++ b/tests/nose/network_config.py
@@ -0,0 +1,116 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+#
+# network_config.py - Copyright (C) 2013 Red Hat, Inc.
+# Written by Fabian Deutsch <fabiand at redhat.com>
+#
+# 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; version 2 of the License.
+#
+# 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.  A copy of the GNU General Public License is
+# also available at http://www.gnu.org/copyleft/gpl.html.
+from mock import patch
+from ovirt.node.config import defaults
+from ovirt.node.utils import fs
+from ovirt.node.utils.fs import ShellVarFile, FakeFs
+from ovirt.node.utils.network import UdevNICInfo, SysfsNICInfo
+import logging
+
+# http://ivory.idyll.org/articles/nose-intro.html
+
+
+def test_fake():
+    """Ensure that FakeFs is working
+    """
+    with patch("ovirt.node.utils.fs.File", FakeFs.File):
+        f = fs.File("new-file")
+        f.touch()
+        assert "new-file" in FakeFs.filemap
+
+        f.delete()
+        assert FakeFs.filemap == {}
+
+
+ at patch("ovirt.node.utils.fs.File", FakeFs.File)
+ at patch.object(UdevNICInfo, "vendor")
+ at patch.object(UdevNICInfo, "devtype")
+ at patch.object(SysfsNICInfo, "hwaddr", "th:em:ac:ad:dr")
+class TestBridgedNIC():
+    """Test the bridged/legacy configuration
+    """
+    def setUp(self):
+        FakeFs.erase()
+        FakeFs.File("/etc/default/ovirt").touch()
+
+    def tearDown(self):
+        FakeFs.erase()
+
+    def test_dhcp(self, *args, **kwargs):
+        """Test BridgedNIC with DHCP configuration file creation
+        """
+        m = defaults.Network()
+
+        m.configure_dhcp("eth0")
+
+        run_tx_by_name(m.transaction(), "WriteConfiguration")
+
+        assert_ifcfg_has_items("eth0",
+                               [('BRIDGE', 'breth0'), ('DEVICE', 'eth0'),
+                                ('HWADDR', 'th:em:ac:ad:dr'),
+                                ('ONBOOT', 'yes')])
+        assert_ifcfg_has_items("breth0",
+                               [('BOOTPROTO', 'dhcp'), ('DELAY', '0'),
+                                ('DEVICE', 'breth0'), ('ONBOOT', 'yes'),
+                                ('PEERNTP', 'yes'), ('TYPE', 'Bridge')])
+
+    def test_static(self, *args, **kwargs):
+        """Test BridgedNIC with static IP configuration file creation
+        """
+        m = defaults.Network()
+
+        m.configure_static("ens1", "192.168.122.42", "255.255.255.0",
+                           "192.168.122.1", None)
+
+        run_tx_by_name(m.transaction(), "WriteConfiguration")
+
+        assert_ifcfg_has_items("ens1",
+                               [('BRIDGE', 'brens1'), ('DEVICE', 'ens1'),
+                                ('HWADDR', 'th:em:ac:ad:dr'),
+                                ('ONBOOT', 'yes')])
+        assert_ifcfg_has_items("brens1",
+                                [('BOOTPROTO', 'static'), ('DELAY', '0'),
+                                 ('DEVICE', 'brens1'),
+                                 ('GATEWAY', '192.168.122.1'),
+                                 ('IPADDR', '192.168.122.42'),
+                                 ('NETMASK', '255.255.255.0'),
+                                 ('ONBOOT', 'yes'),
+                                 ('PEERNTP', 'yes'),
+                                 ('TYPE', 'Bridge')])
+
+
+def run_tx_by_name(txs, name):
+    tx = None
+    for _tx in txs:
+        if _tx.__class__.__name__ == name:
+            tx = _tx
+            break
+    assert tx
+    tx()
+
+
+def assert_ifcfg_has_items(ifname, expected_items):
+    ifcfg = ShellVarFile("/etc/sysconfig/network-scripts/ifcfg-" + ifname)
+    ifcfg_items = sorted(ifcfg.get_dict().items())
+    logging.info("ifcfg : %s" % ifname)
+    logging.info("expect: %s" % expected_items)
+    logging.info("got   : %s" % ifcfg_items)
+    assert ifcfg_items == expected_items


-- 
To view, visit http://gerrit.ovirt.org/15512
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I7746e5d4f06637c70b2a7412ee277b57fdd2ec60
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-node
Gerrit-Branch: master
Gerrit-Owner: Fabian Deutsch <fabiand at fedoraproject.org>



More information about the node-patches mailing list