devices: interface: Add set_default function

Move all the Guest network default logic to it, and drop a now
redundant test
This commit is contained in:
Cole Robinson 2018-09-01 20:22:47 -04:00
parent 7b5f1a304d
commit 64dc334538
4 changed files with 69 additions and 131 deletions

View File

@ -219,22 +219,24 @@ class AddHardware(uiutils.UITestCase):
src.click()
self.pressKey("End")
tab.find_fuzzy("Specify shared device", "menu item").click()
tab.find("Bridge name:", "text").text = "zbr0"
finish.click()
# Check validation error
# Check MAC validation error
alert = self.app.root.find("vmm dialog", "alert")
alert.find_fuzzy("Error adding device", "label")
alert.find_fuzzy("00:11:22:33:44:55", "label")
alert.find("Close", "push button").click()
# Enter bridge name
tab.find("Bridge name:", "text").text = "zbr0"
# Fix MAC
tab.find("mac-address-enable", "check box").click()
tab.find("MAC Address Field", "text").text = "00:11:0A:11:00:11"
finish.click()
uiutils.check_in_loop(lambda: details.active)
# Network with portops
self._open_addhw_window(details)
tab = self._select_hw(addhw, "Network", "network-tab")
tab.find("mac-address-enable", "check box").click()
tab.find("MAC Address Field", "text").text = "00:11:0B:11:00:11"
src.click()
self.sleep(1)
self.pressKey("Home")
@ -249,7 +251,7 @@ class AddHardware(uiutils.UITestCase):
# Network with vport stuff
self._open_addhw_window(details)
tab = self._select_hw(addhw, "Network", "network-tab")
tab.find("mac-address-enable", "check box").click()
tab.find("MAC Address Field", "text").text = "00:11:0C:11:00:11"
src.click()
tab.find_fuzzy("OpenVSwitch", "menu item").click()
t = tab.find("Virtual port", "toggle button")

View File

@ -119,46 +119,6 @@ class TestXMLMisc(unittest.TestCase):
utils.diff_compare(actualXML, filename)
utils.test_create(guest.conn, actualXML)
def testDefaultBridge(self):
# Test our handling of the default bridge routines
from virtinst.devices import interface as deviceinterface
origfunc = getattr(deviceinterface, "_default_bridge")
try:
def newbridge(ignore_conn):
return "bzz0"
setattr(deviceinterface, "_default_bridge", newbridge)
dev1 = virtinst.DeviceInterface(self.conn)
dev1.macaddr = "22:22:33:44:55:66"
dev2 = virtinst.DeviceInterface(self.conn,
parsexml=dev1.get_xml())
dev2.source = None
dev2.source = "foobr0"
dev2.macaddr = "22:22:33:44:55:67"
dev3 = virtinst.DeviceInterface(self.conn,
parsexml=dev1.get_xml())
dev3.source = None
dev3.macaddr = "22:22:33:44:55:68"
utils.diff_compare(dev1.get_xml(), None,
"<interface type=\"bridge\">\n"
" <source bridge=\"bzz0\"/>\n"
" <mac address=\"22:22:33:44:55:66\"/>\n"
"</interface>\n")
utils.diff_compare(dev2.get_xml(), None,
"<interface type=\"bridge\">\n"
" <source bridge=\"foobr0\"/>\n"
" <mac address=\"22:22:33:44:55:67\"/>\n"
"</interface>\n")
utils.diff_compare(dev3.get_xml(), None,
"<interface type=\"bridge\">\n"
" <mac address=\"22:22:33:44:55:68\"/>\n"
"</interface>\n")
finally:
setattr(deviceinterface, "_default_bridge", origfunc)
def testDiskNumbers(self):
# Various testing our target generation
self.assertEqual("a", DeviceDisk.num_to_target(1))

View File

@ -121,8 +121,6 @@ class DeviceInterface(Device):
TYPE_VHOSTUSER = "vhostuser"
TYPE_ETHERNET = "ethernet"
TYPE_DIRECT = "direct"
network_types = [TYPE_BRIDGE, TYPE_VIRTUAL, TYPE_USER, TYPE_ETHERNET,
TYPE_DIRECT]
@staticmethod
def get_network_type_desc(net_type):
@ -182,42 +180,10 @@ class DeviceInterface(Device):
return (False, None)
def __init__(self, *args, **kwargs):
Device.__init__(self, *args, **kwargs)
self._random_mac = None
self._default_bridge = None
###############
# XML helpers #
###############
def _generate_default_bridge(self):
ret = self._default_bridge
if ret is None:
ret = False
default = _default_bridge(self.conn)
if default:
ret = default
self._default_bridge = ret
return ret or None
def _get_default_bridge(self):
if self.type == self.TYPE_BRIDGE:
return self._generate_default_bridge()
return None
def _default_source_mode(self):
if self.type == self.TYPE_DIRECT:
return "vepa"
return None
def _get_default_mac(self):
if not self._random_mac:
self._random_mac = self.generate_mac(self.conn)
return self._random_mac
def _validate_mac(self, val):
util.validate_macaddr(val)
return val
@ -263,23 +229,19 @@ class DeviceInterface(Device):
"source_mode", "portgroup", "macaddr", "target_dev", "model",
"virtualport", "filterref", "rom_bar", "rom_file", "mtu_size"]
_bridge = XMLProperty("./source/@bridge", default_cb=_get_default_bridge)
_bridge = XMLProperty("./source/@bridge")
_network = XMLProperty("./source/@network")
_source_dev = XMLProperty("./source/@dev")
virtualport = XMLChildProperty(_VirtualPort, is_single=True)
type = XMLProperty("./@type",
default_cb=lambda s: s.TYPE_BRIDGE)
type = XMLProperty("./@type")
trustGuestRxFilters = XMLProperty("./@trustGuestRxFilters", is_yesno=True)
macaddr = XMLProperty("./mac/@address",
set_converter=_validate_mac,
default_cb=_get_default_mac)
macaddr = XMLProperty("./mac/@address", set_converter=_validate_mac)
source_type = XMLProperty("./source/@type")
source_path = XMLProperty("./source/@path")
source_mode = XMLProperty("./source/@mode",
default_cb=_default_source_mode)
source_mode = XMLProperty("./source/@mode")
portgroup = XMLProperty("./source/@portgroup")
model = XMLProperty("./model/@type")
target_dev = XMLProperty("./target/@dev")
@ -317,3 +279,36 @@ class DeviceInterface(Device):
self.type = self.TYPE_USER
else:
self.type, self.source = _default_network(self.conn)
##################
# Default config #
##################
@staticmethod
def default_netmodel(guest):
if not guest.os.is_hvm():
return None
if guest.supports_virtionet():
return "virtio"
if guest.os.is_q35():
return "e1000e"
prefs = ["e1000", "rtl8139", "ne2k_pci", "pcnet"]
supported_models = guest.osinfo.supported_netmodels()
for pref in prefs:
if pref in supported_models:
return pref
return None
def set_defaults(self, guest):
if not self.type:
self.type = self.TYPE_BRIDGE
if not self.macaddr:
self.macaddr = self.generate_mac(self.conn)
if self.type == self.TYPE_BRIDGE and not self._bridge:
self._bridge = _default_bridge(self.conn)
if self.type == self.TYPE_DIRECT and not self.source_mode:
self.source_mode = "vepa"
if not self.model:
self.model = self.default_netmodel(guest)

View File

@ -227,9 +227,9 @@ class Guest(XMLBuilder):
xmlns_qemu = XMLChildProperty(DomainXMLNSQemu, is_single=True)
###############################
# Distro detection properties #
###############################
##############################
# osinfo related definitions #
##############################
def _set_osinfo(self, variant):
obj = OSDB.lookup_os(variant)
@ -255,6 +255,27 @@ class Guest(XMLBuilder):
self._set_osinfo(val)
os_variant = property(_get_os_variant, _set_os_variant)
def _supports_virtio(self, os_support):
if not self.conn.is_qemu():
return False
# These _only_ support virtio so don't check the OS
if (self.os.is_arm_machvirt() or
self.os.is_s390x() or
self.os.is_pseries()):
return True
if not os_support:
return False
if self.os.is_x86():
return True
return False
def supports_virtionet(self):
return self._supports_virtio(self.osinfo.supports_virtionet())
########################################
# Device Add/Remove Public API methods #
@ -742,7 +763,6 @@ class Guest(XMLBuilder):
self._set_disk_defaults()
self._add_implied_controllers()
self._add_spice_devices()
self._set_net_defaults()
def _is_full_os_container(self):
if not self.os.is_container():
@ -981,24 +1001,6 @@ class Guest(XMLBuilder):
self.add_device(ctrl)
break
def _supports_virtio(self, os_support):
if not self.conn.is_qemu():
return False
# These _only_ support virtio so don't check the OS
if (self.os.is_arm_machvirt() or
self.os.is_s390x() or
self.os.is_pseries()):
return True
if not os_support:
return False
if self.os.is_x86():
return True
return False
def _set_disk_defaults(self):
disks = self.devices.disk
@ -1044,27 +1046,6 @@ class Guest(XMLBuilder):
disk.cli_generated_target = False
used_targets.append(disk.generate_target(used_targets))
def _default_netmodel(self):
if not self.os.is_hvm():
return None
if self._supports_virtio(self.osinfo.supports_virtionet()):
return "virtio"
if self.os.is_q35():
return "e1000e"
prefs = ["e1000", "rtl8139", "ne2k_pci", "pcnet"]
supported_models = self.osinfo.supported_netmodels()
for pref in prefs:
if pref in supported_models:
return pref
return None
def _set_net_defaults(self):
default_model = self._default_netmodel()
for net in self.devices.interface:
if not net.model:
net.model = default_model
def _add_spice_channels(self):
if self.skip_default_channel:
return