diff --git a/selftests/unit/test_setup_networking.py b/selftests/unit/test_setup_networking.py index 81dbe99410..95175665ce 100644 --- a/selftests/unit/test_setup_networking.py +++ b/selftests/unit/test_setup_networking.py @@ -1,7 +1,8 @@ import unittest from unittest.mock import Mock, patch -from virttest.test_setup.networking import NetworkProxies +from virttest.utils_params import Params +from virttest.test_setup.networking import NetworkProxies, BridgeConfig class TestProxySetuper(unittest.TestCase): @@ -99,5 +100,94 @@ def test_config_half_valid_config(self): np.setup() +class TestBridgeSetuper(unittest.TestCase): + + def setUp(self): + self._test_mock = Mock() + self._env_mock = Mock() + + @patch('virttest.test_setup.networking.PrivateBridgeConfig') + @patch('virttest.test_setup.networking.PrivateOvsBridgeConfig') + def test_no_nics(self, ovs_mock, bridge_mock): + params = Params({ + }) + brcfg = BridgeConfig(self._test_mock, params, self._env_mock) + brcfg.setup() + ovs_mock.setup.assert_not_called() + bridge_mock.setup.assert_not_called() + brcfg.cleanup() + ovs_mock.cleanup.assert_not_called() + bridge_mock.cleanup.assert_not_called() + + @patch('virttest.test_setup.networking.PrivateBridgeConfig') + @patch('virttest.test_setup.networking.PrivateOvsBridgeConfig') + def test_nics_not_private(self, ovs_mock, bridge_mock): + params = Params({ + 'nics': 'bridge', + 'netdst': 'virbr0', + }) + brcfg = BridgeConfig(self._test_mock, params, self._env_mock) + brcfg.setup() + ovs_mock.setup.assert_not_called() + bridge_mock.setup.assert_not_called() + brcfg.cleanup() + ovs_mock.cleanup.assert_not_called() + bridge_mock.cleanup.assert_not_called() + + @patch('virttest.test_setup.networking.PrivateBridgeConfig') + def test_nics_prbr(self, pbc_mock): + mock_instance = Mock() + pbc_mock.return_value = mock_instance + params = Params({ + 'nics': 'bridge', + 'netdst': 'private', + 'priv_brname': 'foobr0', + }) + brcfg = BridgeConfig(self._test_mock, params, self._env_mock) + brcfg.setup() + pbc_mock.assert_called_once_with({ + 'nics': 'bridge', + 'netdst': 'private', + 'priv_brname': 'foobr0', + }) + mock_instance.setup.assert_called_once() + pbc_mock.reset_mock() + brcfg.cleanup() + pbc_mock.assert_called_once_with({ + 'nics': 'bridge', + 'netdst': 'private', + 'netdst_bridge': 'foobr0', + 'priv_brname': 'foobr0', + }) + mock_instance.cleanup.assert_called_once() + + @patch('virttest.test_setup.networking.PrivateOvsBridgeConfig') + def test_nics_ovs(self, povsbc_mock): + mock_instance = Mock() + povsbc_mock.return_value = mock_instance + params = Params({ + 'nics': 'ovs', + 'netdst': 'private', + 'priv_br_type': 'openvswitch', + }) + brcfg = BridgeConfig(self._test_mock, params, self._env_mock) + brcfg.setup() + povsbc_mock.assert_called_once_with({ + 'nics': 'ovs', + 'netdst': 'private', + 'priv_br_type': 'openvswitch', + }) + mock_instance.setup.assert_called_once() + povsbc_mock.reset_mock() + brcfg.cleanup() + povsbc_mock.assert_called_once_with({ + 'nics': 'ovs', + 'netdst': 'private', + 'priv_br_type': 'openvswitch', + 'netdst_ovs': 'atbr0', + }) + mock_instance.cleanup.assert_called_once() + + if __name__ == '__main__': unittest.main() diff --git a/virttest/env_process.py b/virttest/env_process.py index 698e65a399..31963743d7 100644 --- a/virttest/env_process.py +++ b/virttest/env_process.py @@ -50,7 +50,7 @@ from virttest.staging import service from virttest.test_setup.core import SetupManager from virttest.test_setup.os_posix import UlimitConfig -from virttest.test_setup.networking import NetworkProxies +from virttest.test_setup.networking import NetworkProxies, BridgeConfig # lazy imports for dependencies that are not needed in all modes of use @@ -1016,6 +1016,7 @@ def preprocess(test, params, env): _setup_manager.initialize(test, params, env) _setup_manager.register(UlimitConfig) _setup_manager.register(NetworkProxies) + _setup_manager.register(BridgeConfig) _setup_manager.do_setup() vm_type = params.get('vm_type') @@ -1034,24 +1035,6 @@ def preprocess(test, params, env): log_permission) libvirtd_debug_log.enable() - setup_pb = False - ovs_pb = False - for nic in params.get('nics', "").split(): - nic_params = params.object_params(nic) - if nic_params.get('netdst') == 'private': - setup_pb = True - params_pb = nic_params - params['netdst_%s' % nic] = nic_params.get("priv_brname", 'atbr0') - if nic_params.get("priv_br_type") == "openvswitch": - ovs_pb = True - - if setup_pb: - if ovs_pb: - brcfg = test_setup.PrivateOvsBridgeConfig(params_pb) - else: - brcfg = test_setup.PrivateBridgeConfig(params_pb) - brcfg.setup() - base_dir = data_dir.get_data_dir() if params.get("storage_type") == "iscsi": iscsidev = qemu_storage.Iscsidev(params, base_dir, "iscsi") @@ -1868,32 +1851,6 @@ def postprocess(test, params, env): if params.get("setup_ssh") == "yes" and params.get("ssh_conn_obj"): del params["ssh_conn_obj"] - setup_pb = False - ovs_pb = False - for nic in params.get('nics', "").split(): - nic_params = params.object_params(nic) - if params.get('netdst_%s' % nic) == 'private': - setup_pb = True - params_pb = nic_params - break - else: - setup_pb = params.get("netdst") == 'private' - params_pb = params - - if params_pb.get("priv_br_type") == "openvswitch": - ovs_pb = True - - if setup_pb: - try: - if ovs_pb: - brcfg = test_setup.PrivateOvsBridgeConfig(params_pb) - else: - brcfg = test_setup.PrivateBridgeConfig(params_pb) - brcfg.cleanup() - except Exception as details: - err += "\nPB cleanup: %s" % str(details).replace('\\n', '\n ') - LOG.error(details) - if params.get("verify_host_dmesg", "yes") == "yes": dmesg_log_file = params.get("host_dmesg_logfile", "host_dmesg.log") level = params.get("host_dmesg_level", 3) diff --git a/virttest/test_setup/networking.py b/virttest/test_setup/networking.py index 5655415ee0..45afe23df4 100644 --- a/virttest/test_setup/networking.py +++ b/virttest/test_setup/networking.py @@ -4,6 +4,7 @@ import urllib.request from virttest.test_setup.core import Setuper +from virttest.test_setup import PrivateBridgeConfig, PrivateOvsBridgeConfig class NetworkProxies(Setuper): @@ -26,3 +27,48 @@ def cleanup(self): process in which such installation has taken place. """ pass + + +class BridgeConfig(Setuper): + + def setup(self): + setup_pb = False + ovs_pb = False + for nic in self.params.get('nics', "").split(): + nic_params = self.params.object_params(nic) + if nic_params.get('netdst') == 'private': + setup_pb = True + params_pb = nic_params + self.params['netdst_%s' % nic] = nic_params.get("priv_brname", 'atbr0') + if nic_params.get("priv_br_type") == "openvswitch": + ovs_pb = True + + if setup_pb: + if ovs_pb: + brcfg = PrivateOvsBridgeConfig(params_pb) + else: + brcfg = PrivateBridgeConfig(params_pb) + brcfg.setup() + + def cleanup(self): + setup_pb = False + ovs_pb = False + for nic in self.params.get('nics', "").split(): + nic_params = self.params.object_params(nic) + if nic_params.get('netdst') == 'private': + setup_pb = True + params_pb = nic_params + break + else: + setup_pb = self.params.get("netdst") == 'private' + params_pb = self.params + + if params_pb.get("priv_br_type") == "openvswitch": + ovs_pb = True + + if setup_pb: + if ovs_pb: + brcfg = PrivateOvsBridgeConfig(params_pb) + else: + brcfg = PrivateBridgeConfig(params_pb) + brcfg.cleanup()