def setUp(self): """The only two interesting conditions to test here are whether storeState raises an exception or not, so let's commonize everything else""" super(TestCreateNetwork, self).setUp() self.networks = { 'debmarshal-0': 500, 'debmarshal-3': 500, 'debmarshal-4': 500, 'debmarshal-4': 500 } self.name = 'debmarshal-1' self.gateway = '169.254.3.1' self.hosts = ['wiki.company.com', 'login.company.com'] self.host_dict = { 'wiki.company.com': ('169.254.3.2', '00:00:00:00:00:00'), 'login.company.com': ('169.254.3.3', '00:00:00:00:00:00') } self.mox.StubOutWithMock(utils, 'getCaller') utils.getCaller().AndReturn(1000) self.mox.StubOutWithMock(debmarshal.utils, 'acquireLock') debmarshal.utils.acquireLock('debmarshal-netlist', fcntl.LOCK_EX) self.mox.StubOutWithMock(networks, '_validateHostname') networks._validateHostname(mox.IgnoreArg()).MultipleTimes() self.mox.StubOutWithMock(libvirt, 'open') self.virt_con = self.mox.CreateMock(libvirt.virConnect) libvirt.open(mox.IgnoreArg()).AndReturn(self.virt_con) self.mox.StubOutWithMock(networks, '_findUnusedName') networks._findUnusedName().AndReturn(self.name) self.mox.StubOutWithMock(networks, '_findUnusedNetwork') networks._findUnusedNetwork(len(self.hosts)).\ AndReturn((self.gateway, '255.255.255.0')) self.mox.StubOutWithMock(networks, 'loadNetworkState') networks.loadNetworkState(self.virt_con).AndReturn(dict(self.networks)) self.mox.StubOutWithMock(virtinst.util, 'randomMAC') virtinst.util.randomMAC().MultipleTimes().AndReturn( '00:00:00:00:00:00') self.mox.StubOutWithMock(networks, '_genNetworkXML') networks._genNetworkXML(self.name, self.gateway, '255.255.255.0', self.host_dict).AndReturn('<fake_xml />') self.virt_net = self.mox.CreateMock(libvirt.virNetwork) self.virt_con.networkDefineXML('<fake_xml />').AndReturn(self.virt_net) self.virt_net.create()
def setUp(self): """The only two interesting conditions to test here are whether storeState raises an exception or not, so let's commonize everything else""" super(TestCreateNetwork, self).setUp() self.networks = {'debmarshal-0': 500, 'debmarshal-3': 500, 'debmarshal-4': 500, 'debmarshal-4': 500} self.name = 'debmarshal-1' self.gateway = '169.254.3.1' self.hosts = ['wiki.company.com', 'login.company.com'] self.host_dict = {'wiki.company.com': ('169.254.3.2', '00:00:00:00:00:00'), 'login.company.com': ('169.254.3.3', '00:00:00:00:00:00')} self.mox.StubOutWithMock(utils, 'getCaller') utils.getCaller().AndReturn(1000) self.mox.StubOutWithMock(debmarshal.utils, 'acquireLock') debmarshal.utils.acquireLock('debmarshal-netlist', fcntl.LOCK_EX) self.mox.StubOutWithMock(networks, '_validateHostname') networks._validateHostname(mox.IgnoreArg()).MultipleTimes() self.mox.StubOutWithMock(libvirt, 'open') self.virt_con = self.mox.CreateMock(libvirt.virConnect) libvirt.open(mox.IgnoreArg()).AndReturn(self.virt_con) self.mox.StubOutWithMock(networks, '_findUnusedName') networks._findUnusedName().AndReturn(self.name) self.mox.StubOutWithMock(networks, '_findUnusedNetwork') networks._findUnusedNetwork(len(self.hosts)).\ AndReturn((self.gateway, '255.255.255.0')) self.mox.StubOutWithMock(networks, 'loadNetworkState') networks.loadNetworkState(self.virt_con).AndReturn(dict(self.networks)) self.mox.StubOutWithMock(virtinst.util, 'randomMAC') virtinst.util.randomMAC().MultipleTimes().AndReturn('00:00:00:00:00:00') self.mox.StubOutWithMock(networks, '_genNetworkXML') networks._genNetworkXML(self.name, self.gateway, '255.255.255.0', self.host_dict).AndReturn('<fake_xml />') self.virt_net = self.mox.CreateMock(libvirt.virNetwork) self.virt_con.networkDefineXML('<fake_xml />').AndReturn(self.virt_net) self.virt_net.create()
def test(self): self.mox.StubOutWithMock(networks, '_listBridges') networks._listBridges().AndReturn(['virbr0', 'debmarshal-0']) self.mox.ReplayAll() self.assertEqual(networks._findUnusedName(), 'debmarshal-1')
def createNetwork(self, hosts, _debmarshal_sender=None): """All of the networking config you need for a debmarshal test rig. createNetwork creates an isolated virtual network within libvirt. It picks an IP address space that is as-yet unused (within debmarshal), and assigns that to the network. It then allocates IP addresses and MAC addresses for each of the hostnames listed in hosts. createNetwork tracks which users created which networks, and debmarshal will only allow the user that created a network to attach VMs to it or destroy it. Args: hosts: A list of hostnames that will eventually be attached to this network Returns: A 4-tuple containing: Network name: This is used to reference the newly created network in the future. It is unique across the local workstation Gateway: The network address. Also the DNS server, if that information isn't being grabbed over DHCP Netmask: The netmask for the network VMs: A dict mapping hostnames in hosts to (IP address, MAC address), as assigned by createNetwork """ utils.caller = _debmarshal_sender # First, input validation. Everything in hosts should be a valid # hostname for h in hosts: networks._validateHostname(h) # We don't really care which particular libvirt driver we connect # to, because they all share the same networking # config. libvirt.open() is supposed to take None to indicate a # default, but it doesn't seem to work, so we pass in what's # supposed to be the default for root. virt_con = libvirt.open('qemu:///system') net_name = networks._findUnusedName() net_gateway, net_mask = networks._findUnusedNetwork(len(hosts)) net_hosts = {} host_addr = ip.IP(net_gateway) + 1 for host in hosts: # Use the virtinst package's MAC address generator because it's # easier than writing another one for ourselves. # # This does mean that the MAC addresses are allocated from # Xensource's OUI, but whatever mac = virtinst.util.randomMAC() net_hosts[host] = (host_addr.ip_ext, mac) host_addr += 1 xml = networks._genNetworkXML(net_name, net_gateway, net_mask, net_hosts) virt_net = virt_con.networkDefineXML(xml) virt_net.create() try: nets = networks.loadNetworkState(virt_con) nets[net_name] = utils.getCaller() utils.storeState(nets, 'debmarshal-networks') except: virt_con.networkLookupByName(net_name).destroy() virt_con.networkLookupByName(net_name).undefine() raise utils.caller = None return (net_name, net_gateway, net_mask, net_hosts)