Ejemplo n.º 1
0
    def testGetBootProtocolIfcfg(self):
        deviceName = "___This_could_never_be_a_device_name___"
        ifcfg = ('DEVICE=%s' % deviceName + '\n' + 'ONBOOT=yes' + '\n' +
                 'MTU=1500' + '\n' + 'HWADDR=5e:64:6d:12:16:84' + '\n')
        with namedTemporaryDir() as tempDir:
            ifcfgPrefix = os.path.join(tempDir, 'ifcfg-')
            filePath = ifcfgPrefix + deviceName

            with MonkeyPatchScope([(netinfo, 'NET_CONF_PREF', ifcfgPrefix)]):
                with open(filePath, 'w') as ifcfgFile:
                    ifcfgFile.write(ifcfg + 'BOOTPROTO=dhcp\n')
                self.assertEqual(getBootProtocol(deviceName, 'ifcfg'), 'dhcp')

                with open(filePath, 'w') as ifcfgFile:
                    ifcfgFile.write(ifcfg + 'BOOTPROTO=none\n')
                self.assertEqual(getBootProtocol(deviceName, 'ifcfg'), 'none')

                with open(filePath, 'w') as ifcfgFile:
                    ifcfgFile.write(ifcfg)
                self.assertEqual(getBootProtocol(deviceName, 'ifcfg'), None)
Ejemplo n.º 2
0
    def testGetBootProtocolIfcfg(self):
        deviceName = "___This_could_never_be_a_device_name___"
        ifcfg = ('DEVICE=%s' % deviceName + '\n' + 'ONBOOT=yes' + '\n' +
                 'MTU=1500' + '\n' + 'HWADDR=5e:64:6d:12:16:84' + '\n')
        with namedTemporaryDir() as tempDir:
            ifcfgPrefix = os.path.join(tempDir, 'ifcfg-')
            filePath = ifcfgPrefix + deviceName

            with MonkeyPatchScope([(netinfo, 'NET_CONF_PREF', ifcfgPrefix)]):
                with open(filePath, 'w') as ifcfgFile:
                    ifcfgFile.write(ifcfg + 'BOOTPROTO=dhcp\n')
                self.assertEqual(getBootProtocol(deviceName, 'ifcfg'), 'dhcp')

                with open(filePath, 'w') as ifcfgFile:
                    ifcfgFile.write(ifcfg + 'BOOTPROTO=none\n')
                self.assertEqual(getBootProtocol(deviceName, 'ifcfg'), 'none')

                with open(filePath, 'w') as ifcfgFile:
                    ifcfgFile.write(ifcfg)
                self.assertEqual(getBootProtocol(deviceName, 'ifcfg'), None)
Ejemplo n.º 3
0
def delNetwork(network,
               vlan=None,
               bonding=None,
               nics=None,
               force=False,
               configurator=None,
               implicitBonding=True,
               _netinfo=None,
               **options):
    if _netinfo is None:
        _netinfo = netinfo.NetInfo()

    if configurator is None:
        configurator = Ifcfg()

    if network not in _netinfo.networks:
        logging.info("Network %r: doesn't exist in libvirt database", network)
        _delNonVdsmNetwork(network, vlan, bonding, nics, _netinfo,
                           configurator)
        return

    nics, vlan, bonding = _netinfo.getNicsVlanAndBondingForNetwork(network)
    bridged = _netinfo.networks[network]['bridged']

    logging.info("Removing network %s with vlan=%s, bonding=%s, nics=%s,"
                 "options=%s" % (network, vlan, bonding, nics, options))

    if not utils.tobool(force):
        _validateDelNetwork(network, vlan, bonding, nics, bridged, _netinfo)

    netEnt = objectivizeNetwork(bridge=network if bridged else None,
                                vlan=vlan,
                                bonding=bonding,
                                nics=nics,
                                _netinfo=_netinfo,
                                configurator=configurator,
                                implicitBonding=implicitBonding)
    netEnt.ip.bootproto = netinfo.getBootProtocol(netEnt.name)

    # We must first remove the libvirt network and then the network entity.
    # Otherwise if we first remove the network entity while the libvirt
    # network is still up, the network entity (In some flows) thinks that
    # it still has users and thus does not allow its removal
    configurator.removeLibvirtNetwork(network)
    netEnt.remove()

    # We need to gather NetInfo again to refresh networks info from libvirt.
    # The deleted bridge should never be up at this stage.
    _netinfo = netinfo.NetInfo()
    if network in _netinfo.networks:
        raise ConfigNetworkError(
            ne.ERR_USED_BRIDGE, 'delNetwork: bridge %s '
            'still exists' % network)
Ejemplo n.º 4
0
def delNetwork(network,
               vlan=None,
               bonding=None,
               nics=None,
               force=False,
               configurator=None,
               implicitBonding=True,
               _netinfo=None,
               **options):
    if _netinfo is None:
        _netinfo = netinfo.NetInfo()

    if configurator is None:
        configurator = ConfiguratorClass()

    if network not in _netinfo.networks:
        logging.info("Network %r: doesn't exist in libvirt database", network)
        vlan = _vlanToInternalRepresentation(vlan)
        _delNonVdsmNetwork(network, vlan, bonding, nics, _netinfo,
                           configurator)
        return

    nics, vlan, bonding = _netinfo.getNicsVlanAndBondingForNetwork(network)
    bridged = _netinfo.networks[network]['bridged']

    logging.info("Removing network %s with vlan=%s, bonding=%s, nics=%s,"
                 "options=%s" % (network, vlan, bonding, nics, options))

    if not utils.tobool(force):
        _validateDelNetwork(network, vlan, bonding, nics, bridged, _netinfo)

    netEnt = objectivizeNetwork(bridge=network if bridged else None,
                                vlan=vlan,
                                bonding=bonding,
                                nics=nics,
                                _netinfo=_netinfo,
                                configurator=configurator,
                                implicitBonding=implicitBonding)
    netEnt.ip.bootproto = netinfo.getBootProtocol(netEnt.name)

    # We must first remove the libvirt network and then the network entity.
    # Otherwise if we first remove the network entity while the libvirt
    # network is still up, the network entity (In some flows) thinks that
    # it still has users and thus does not allow its removal
    configurator.removeLibvirtNetwork(network)
    netEnt.remove()
Ejemplo n.º 5
0
    def testGetBootProtocolUnified(self):
        tempDir = tempfile.mkdtemp()
        netsDir = os.path.join(tempDir, 'nets')
        os.mkdir(netsDir)
        networks = {
            'nonVMOverNic':
            {"nic": "eth0", "bridged": False, "bootproto": "dhcp"},
            'bridgeOverNic':
            {"nic": "eth1", "bridged": True},
            'nonVMOverBond':
            {"bonding": "bond0", "bridged": False, "bootproto": "dhcp"},
            'bridgeOverBond':
            {"bonding": "bond1", "bridged": True},
            'vlanOverNic':
            {"nic": "eth2", "bridged": False, "vlan": 1,
             "bootproto": "dhcp"},
            'bridgeOverVlan':
            {"nic": "eth3", "bridged": True, "vlan": 1},
            'vlanOverBond':
            {"bonding": "bond2", "bridged": False, "bootproto": "dhcp",
             "vlan": 1},
            'bridgeOverVlanOverBond':
            {"bonding": "bond3", "bridged": True, "vlan": 1}}

        with MonkeyPatchScope([(netconfpersistence, 'CONF_RUN_DIR', tempDir)]):
            try:
                runningConfig = netconfpersistence.RunningConfig()
                for network, attributes in networks.iteritems():
                    runningConfig.setNetwork(network, attributes)
                runningConfig.save()

                for network, attributes in networks.iteritems():
                    if attributes.get('bridged') == 'true':
                        topLevelDevice = network
                    else:
                        topLevelDevice = attributes.get('nic') or \
                            attributes.get('bonding')
                        if attributes.get('vlan'):
                            topLevelDevice += '.%s' % attributes.get('vlan')
                    self.assertEqual(
                        getBootProtocol(topLevelDevice, 'unified'),
                        attributes.get('bootproto'))
            finally:
                rmtree(tempDir)
Ejemplo n.º 6
0
def delNetwork(network, vlan=None, bonding=None, nics=None, force=False,
               configurator=None, implicitBonding=True, _netinfo=None,
               **options):
    if _netinfo is None:
        _netinfo = netinfo.NetInfo()

    if configurator is None:
        configurator = Ifcfg()

    if network not in _netinfo.networks:
        logging.info("Network %r: doesn't exist in libvirt database", network)
        vlan = _vlanToInternalRepresentation(vlan)
        _delNonVdsmNetwork(network, vlan, bonding, nics, _netinfo,
                           configurator)
        return

    nics, vlan, bonding = _netinfo.getNicsVlanAndBondingForNetwork(network)
    bridged = _netinfo.networks[network]['bridged']

    logging.info("Removing network %s with vlan=%s, bonding=%s, nics=%s,"
                 "options=%s" % (network, vlan, bonding, nics, options))

    if not utils.tobool(force):
        _validateDelNetwork(network, vlan, bonding, nics, bridged, _netinfo)

    netEnt = objectivizeNetwork(bridge=network if bridged else None, vlan=vlan,
                                bonding=bonding, nics=nics, _netinfo=_netinfo,
                                configurator=configurator,
                                implicitBonding=implicitBonding)
    netEnt.ip.bootproto = netinfo.getBootProtocol(netEnt.name)

    # We must first remove the libvirt network and then the network entity.
    # Otherwise if we first remove the network entity while the libvirt
    # network is still up, the network entity (In some flows) thinks that
    # it still has users and thus does not allow its removal
    configurator.removeLibvirtNetwork(network)
    netEnt.remove()

    # We need to gather NetInfo again to refresh networks info from libvirt.
    # The deleted bridge should never be up at this stage.
    _netinfo = netinfo.NetInfo()
    if network in _netinfo.networks:
        raise ConfigNetworkError(ne.ERR_USED_BRIDGE, 'delNetwork: bridge %s '
                                 'still exists' % network)
Ejemplo n.º 7
0
def delNetwork(network, vlan=None, bonding=None, nics=None, force=False,
               configurator=None, implicitBonding=True, _netinfo=None,
               **options):
    if _netinfo is None:
        _netinfo = netinfo.NetInfo()

    if configurator is None:
        configurator = Ifcfg()

    if network not in _netinfo.networks:
        logging.info("Network %r: doesn't exist in libvirt database", network)
        _delNonVdsmNetwork(network, vlan, bonding, nics, _netinfo,
                           configurator)
        return

    nics, vlan, bonding = _netinfo.getNicsVlanAndBondingForNetwork(network)
    bridged = _netinfo.networks[network]['bridged']

    logging.info("Removing network %s with vlan=%s, bonding=%s, nics=%s,"
                 "options=%s" % (network, vlan, bonding, nics, options))

    if not utils.tobool(force):
        _validateDelNetwork(network, vlan, bonding, nics, bridged, _netinfo)

    netEnt = objectivizeNetwork(bridge=network if bridged else None, vlan=vlan,
                                bonding=bonding, nics=nics, _netinfo=_netinfo,
                                configurator=configurator,
                                implicitBonding=implicitBonding)
    netEnt.ip.bootproto = netinfo.getBootProtocol(netEnt.name)
    netEnt.remove()
    # libvirt net removal must be done after removal so that on dhcp ifdown
    # the dhcp hook still sees the network as belonging to vdsm.
    configurator.removeLibvirtNetwork(network)

    # We need to gather NetInfo again to refresh networks info from libvirt.
    # The deleted bridge should never be up at this stage.
    _netinfo = netinfo.NetInfo()
    if network in _netinfo.networks:
        raise ConfigNetworkError(ne.ERR_USED_BRIDGE, 'delNetwork: bridge %s '
                                 'still exists' % network)
Ejemplo n.º 8
0
    def testGetBootProtocolUnified(self):
        with namedTemporaryDir() as tempDir:
            netsDir = os.path.join(tempDir, 'nets')
            os.mkdir(netsDir)
            networks = {
                'nonVMOverNic': {
                    "nic": "eth0",
                    "bridged": False,
                    "bootproto": "dhcp"
                },
                'bridgeOverNic': {
                    "nic": "eth1",
                    "bridged": True
                },
                'nonVMOverBond': {
                    "bonding": "bond0",
                    "bridged": False,
                    "bootproto": "dhcp"
                },
                'bridgeOverBond': {
                    "bonding": "bond1",
                    "bridged": True
                },
                'vlanOverNic': {
                    "nic": "eth2",
                    "bridged": False,
                    "vlan": 1,
                    "bootproto": "dhcp"
                },
                'bridgeOverVlan': {
                    "nic": "eth3",
                    "bridged": True,
                    "vlan": 1
                },
                'vlanOverBond': {
                    "bonding": "bond2",
                    "bridged": False,
                    "bootproto": "dhcp",
                    "vlan": 1
                },
                'bridgeOverVlanOverBond': {
                    "bonding": "bond3",
                    "bridged": True,
                    "vlan": 1
                }
            }

            with MonkeyPatchScope([(netconfpersistence, 'CONF_RUN_DIR',
                                    tempDir)]):
                runningConfig = netconfpersistence.RunningConfig()
                for network, attributes in networks.iteritems():
                    runningConfig.setNetwork(network, attributes)
                runningConfig.save()

                for network, attributes in networks.iteritems():
                    if attributes.get('bridged') == 'true':
                        topLevelDevice = network
                    else:
                        topLevelDevice = attributes.get('nic') or \
                            attributes.get('bonding')
                        if attributes.get('vlan'):
                            topLevelDevice += '.%s' % attributes.get('vlan')
                    self.assertEqual(
                        getBootProtocol(topLevelDevice, 'unified'),
                        attributes.get('bootproto'))