def setUp(self): """Use the same set of networks for all tests.""" super(TestValidateNetwork, self).setUp() self.virt_con = self.mox.CreateMock(libvirt.virConnect) self.mox.StubOutWithMock(networks, 'loadNetworkState') networks.loadNetworkState(self.virt_con).AndReturn(self.networks)
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 setUp(self): """Setup some mocks common to all tests of destroyNetwork""" super(TestDestroyNetwork, self).setUp() self.mox.StubOutWithMock(debmarshal.utils, 'acquireLock') debmarshal.utils.acquireLock('debmarshal-netlist', fcntl.LOCK_EX) self.mox.StubOutWithMock(libvirt, 'open') self.virt_con = self.mox.CreateMock(libvirt.virConnect) libvirt.open(mox.IgnoreArg()).AndReturn(self.virt_con) self.networks = {'debmarshal-0': 501, 'debmarshal-1': 500} self.mox.StubOutWithMock(networks, 'loadNetworkState') networks.loadNetworkState(self.virt_con).AndReturn(dict(self.networks))
def _validateNetwork(net, virt_con=None): """Validate a debmarshal network. This function checks that the network name passed in is a network known to (i.e. created by) debmarshal. It also verifies that the network was created by the user calling the function. Args: net: The name of the debmarshal network. virt_con: A read-only libvirt.virConnect instance. We'll open one of our own if one isn't passed in. Since libvirt network information is shared across all hypervisors, virt_con can be a connection to any libvirt driver. Raises: debmarshal.errors.NetworkNotFound if the named network doesn't exist. debmarshal.errors.AccessDenied if the network isn't owned by the caller. """ if not virt_con: virt_con = libvirt.open('qemu:///system') nets = networks.loadNetworkState(virt_con) if net not in nets: raise errors.NetworkNotFound("Network %s does not exist." % net) if nets[net] != utils.getCaller(): raise errors.AccessDenied("Network %s is not owned by UID %s." % (net, utils.getCaller()))
def destroyNetwork(self, name, _debmarshal_sender=None): """Destroy a debmarshal network. destroyNetwork uses the state recorded by createNetwork to verify that the user who created a network is the only one who can destroy it (except for root). Args: name: The name of the network returned from createNetwork Raises: debmarshal.errors.NetworkNotFound: The specified network name does not exist. debmarshal.errors.AccessDenied: The specified network is not owned by the user calling destroyNetwork. """ utils.caller = _debmarshal_sender virt_con = libvirt.open('qemu:///system') nets = networks.loadNetworkState(virt_con) if name not in nets: raise errors.NetworkNotFound("Network %s does not exist." % name) if utils.getCaller() not in (0, nets[name]): raise errors.AccessDenied("Network %s not owned by UID %d." % (name, utils.getCaller())) virt_con.networkLookupByName(name).destroy() virt_con.networkLookupByName(name).undefine() del nets[name] utils.storeState(nets, 'debmarshal-networks') utils.caller = None
def testNetworkExistenceTest(self): """Make sure that networks get dropped from the list in the state file if they don't still exist. And that they're kept if they do""" self.mox.StubOutWithMock(utils, 'loadState') utils.loadState('debmarshal-networks').AndReturn({ 'foo': 500, 'bar': 501 }) self.mox.StubOutWithMock(networks, '_listBridges') networks._listBridges().AndReturn(['virbr0', 'foo']) self.mox.ReplayAll() self.assertEqual(networks.loadNetworkState(), {'foo': 500})
def testNetworkExistenceTest(self): """Make sure that networks get dropped from the list in the state file if they don't still exist. And that they're kept if they do""" self.mox.StubOutWithMock(utils, 'loadState') utils.loadState('debmarshal-networks').AndReturn( {'foo': 500, 'bar': 501}) self.mox.StubOutWithMock(networks, '_listBridges') networks._listBridges().AndReturn(['virbr0', 'foo']) self.mox.ReplayAll() self.assertEqual(networks.loadNetworkState(), {'foo': 500})
def testTwoBadNetworks(self): """Test finding two nonexistent networks when loading state.""" nets = {'foo': 500, 'bar': 500, 'baz': 500, 'quux': 500, 'spam': 500, 'eggs': 500} self.mox.StubOutWithMock(utils, 'loadState') utils.loadState('debmarshal-networks').AndReturn(dict(nets)) self.mox.StubOutWithMock(networks, '_listBridges') networks._listBridges().AndReturn(['foo', 'bar', 'quux', 'eggs']) self.mox.ReplayAll() del nets['baz'] del nets['spam'] self.assertEqual(networks.loadNetworkState(), nets)
def testTwoBadNetworks(self): """Test finding two nonexistent networks when loading state.""" nets = { 'foo': 500, 'bar': 500, 'baz': 500, 'quux': 500, 'spam': 500, 'eggs': 500 } self.mox.StubOutWithMock(utils, 'loadState') utils.loadState('debmarshal-networks').AndReturn(dict(nets)) self.mox.StubOutWithMock(networks, '_listBridges') networks._listBridges().AndReturn(['foo', 'bar', 'quux', 'eggs']) self.mox.ReplayAll() del nets['baz'] del nets['spam'] self.assertEqual(networks.loadNetworkState(), nets)
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)