Skip to content

Commit

Permalink
env_process: Refactor BridgeConfig into a Setuper
Browse files Browse the repository at this point in the history
Move the functionality that lived in env_process::{pre,post}process
hooks into an appropriate Setuper's child class. That will be used
inside of the env_process setup_manager instance.

Signed-off-by: bgartzi <[email protected]>
  • Loading branch information
bgartzi committed Oct 11, 2023
1 parent 9d83999 commit 96c2fbe
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 46 deletions.
92 changes: 91 additions & 1 deletion selftests/unit/test_setup_networking.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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()
47 changes: 2 additions & 45 deletions virttest/env_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand All @@ -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")
Expand Down Expand Up @@ -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)
Expand Down
46 changes: 46 additions & 0 deletions virttest/test_setup/networking.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import urllib.request

from virttest.test_setup.core import Setuper
from virttest.test_setup import PrivateBridgeConfig, PrivateOvsBridgeConfig


class NetworkProxies(Setuper):
Expand All @@ -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()

0 comments on commit 96c2fbe

Please sign in to comment.