Change in vdsm[master]: test: Use mock module for testing

edwardh at redhat.com edwardh at redhat.com
Mon Mar 28 10:31:15 UTC 2016


Edward Haas has uploaded a new change for review.

Change subject: test: Use mock module for testing
......................................................................

test: Use mock module for testing

Mock is part of py3 std lib (under unittest package) with enhanced
functionality to support mocking in tests.
For py2, the python-mock package is used.

Used for covering netswitch module.

Change-Id: I1c0af7baab7c35a2617bd60a62a0b1534e5f8894
Signed-off-by: Edward Haas <edwardh at redhat.com>
---
M lib/vdsm/compat.py
A tests/network/netswitch_test.py
M vdsm.spec.in
3 files changed, 81 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/42/55342/1

diff --git a/lib/vdsm/compat.py b/lib/vdsm/compat.py
index 3e28d06..85ec73c 100644
--- a/lib/vdsm/compat.py
+++ b/lib/vdsm/compat.py
@@ -45,3 +45,10 @@
 except ImportError:
     from vdsm.utils import suppress
     suppress  # yep, this is needed twice.
+
+try:
+    from unittest import mock
+    mock
+except ImportError:  # py2
+    import mock
+    mock
diff --git a/tests/network/netswitch_test.py b/tests/network/netswitch_test.py
new file mode 100644
index 0000000..7358e1d
--- /dev/null
+++ b/tests/network/netswitch_test.py
@@ -0,0 +1,73 @@
+#
+# 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 vdsm.network import netswitch
+
+from vdsm.compat import mock
+from testlib import VdsmTestCase
+
+
+ at attr(type='unit')
+class TestNetSwitch(VdsmTestCase):
+
+    NET_TEST = {'netname': {'A': 'a'}}
+    BOND_TEST = {'bondname': {'B': 'b'}}
+    OPT_TEST = {'optionname': {'O': 'o'}}
+
+    @mock.patch('vdsm.network.netswitch.legacy_switch')
+    def test_validate(self, mock_switch):
+        netswitch.validate(self.NET_TEST, self.BOND_TEST)
+        mock_switch.validate_network_setup.assert_called_with(self.NET_TEST,
+                                                              self.BOND_TEST)
+
+    @mock.patch('vdsm.network.netswitch.connectivity')
+    @mock.patch('vdsm.network.netswitch.CachingNetInfo')
+    @mock.patch('vdsm.network.netswitch.netinfo_get')
+    @mock.patch('vdsm.network.netswitch.libvirtNets2vdsm')
+    @mock.patch('vdsm.network.netswitch.libvirt_nets')
+    @mock.patch('vdsm.network.netswitch.legacy_switch')
+    def test_setup(self, mock_switch, mock_libvirt_nets, mock_libvirtNets2vdsm,
+                   mock_netinfo_get, mock_CachingNetInfo, mock_connectivity):
+
+        mock_libvirt_nets.return_value = None
+        mock_libvirtNets2vdsm.return_value = None
+        mock_netinfo_get.return_value = None
+        mock_CachingNetInfo.return_value = None
+
+        netswitch.setup(self.NET_TEST, self.BOND_TEST, self.OPT_TEST, False)
+
+        mock_configurator = mock_switch.ConfiguratorClass().__enter__()
+        mock_switch.remove_networks.assert_called_with(self.NET_TEST,
+                                                       self.BOND_TEST,
+                                                       mock_configurator,
+                                                       None, None)
+        mock_switch.bonds_setup.assert_called_with(self.BOND_TEST,
+                                                   mock_configurator,
+                                                   None,
+                                                   False)
+        mock_switch.add_missing_networks.assert_called_with(mock_configurator,
+                                                            self.NET_TEST,
+                                                            self.BOND_TEST,
+                                                            None)
+        mock_connectivity.check.assert_called_with(self.OPT_TEST)
diff --git a/vdsm.spec.in b/vdsm.spec.in
index 87be9ca..5ba81ef 100644
--- a/vdsm.spec.in
+++ b/vdsm.spec.in
@@ -57,6 +57,7 @@
 BuildRequires: cyrus-sasl-lib
 BuildRequires: python
 BuildRequires: python-devel
+BuildRequires: python-mock
 BuildRequires: python-netaddr
 BuildRequires: python-nose
 BuildRequires: python-six >= 1.9.0


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I1c0af7baab7c35a2617bd60a62a0b1534e5f8894
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Edward Haas <edwardh at redhat.com>


More information about the vdsm-patches mailing list