def test_subnet_edge(self): secondaddress = network.allocate_ip("netuser", "project0", utils.generate_mac()) hostname = "toomany-hosts" for project in range(1,5): project_id = "project%s" % (project) mac = utils.generate_mac() mac2 = utils.generate_mac() mac3 = utils.generate_mac() address = network.allocate_ip( "netuser", project_id, mac) address2 = network.allocate_ip( "netuser", project_id, mac2) address3 = network.allocate_ip( "netuser", project_id, mac3) self.assertEqual(False, is_in_project(address, "project0")) self.assertEqual(False, is_in_project(address2, "project0")) self.assertEqual(False, is_in_project(address3, "project0")) rv = network.deallocate_ip(address) rv = network.deallocate_ip(address2) rv = network.deallocate_ip(address3) net = network.get_project_network(project_id, "default") self.dnsmasq.release_ip(mac, address, hostname, net.bridge_name) self.dnsmasq.release_ip(mac2, address2, hostname, net.bridge_name) self.dnsmasq.release_ip(mac3, address3, hostname, net.bridge_name) net = network.get_project_network("project0", "default") rv = network.deallocate_ip(secondaddress) self.dnsmasq.release_ip(mac, address, hostname, net.bridge_name)
def test_range_allocation(self): mac = utils.generate_mac() secondmac = utils.generate_mac() hostname = "test-host" address = network.allocate_ip( "netuser", "project0", mac) secondaddress = network.allocate_ip( "netuser", "project1", secondmac) net = network.get_project_network("project0", "default") secondnet = network.get_project_network("project1", "default") self.assertEqual(True, is_in_project(address, "project0")) self.assertEqual(True, is_in_project(secondaddress, "project1")) self.assertEqual(False, is_in_project(address, "project1")) # Addresses are allocated before they're issued self.dnsmasq.issue_ip(mac, address, hostname, net.bridge_name) self.dnsmasq.issue_ip(secondmac, secondaddress, hostname, secondnet.bridge_name) rv = network.deallocate_ip(address) self.dnsmasq.release_ip(mac, address, hostname, net.bridge_name) self.assertEqual(False, is_in_project(address, "project0")) # First address release shouldn't affect the second self.assertEqual(True, is_in_project(secondaddress, "project1")) rv = network.deallocate_ip(secondaddress) self.dnsmasq.release_ip(secondmac, secondaddress, hostname, secondnet.bridge_name) self.assertEqual(False, is_in_project(secondaddress, "project1"))
def test_allocate_deallocate_ip(self): address = network.allocate_ip( "netuser", "project0", utils.generate_mac()) logging.debug("Was allocated %s" % (address)) net = network.get_project_network("project0", "default") self.assertEqual(True, is_in_project(address, "project0")) mac = utils.generate_mac() hostname = "test-host" self.dnsmasq.issue_ip(mac, address, hostname, net.bridge_name) rv = network.deallocate_ip(address) # Doesn't go away until it's dhcp released self.assertEqual(True, is_in_project(address, "project0")) self.dnsmasq.release_ip(mac, address, hostname, net.bridge_name) self.assertEqual(False, is_in_project(address, "project0"))
def test_too_many_addresses(self): """ Network size is 32, there are 5 addresses reserved for VPN. So we should get 23 usable addresses """ net = network.get_project_network("project0", "default") hostname = "toomany-hosts" macs = {} addresses = {} for i in range(0, 22): macs[i] = utils.generate_mac() addresses[i] = network.allocate_ip("netuser", "project0", macs[i]) self.dnsmasq.issue_ip(macs[i], addresses[i], hostname, net.bridge_name) self.assertRaises(NoMoreAddresses, network.allocate_ip, "netuser", "project0", utils.generate_mac()) for i in range(0, 22): rv = network.deallocate_ip(addresses[i]) self.dnsmasq.release_ip(macs[i], addresses[i], hostname, net.bridge_name)
def _get_project_addresses(project_id): project_addresses = [] for addr in network.get_project_network(project_id).list_addresses(): project_addresses.append(addr) return project_addresses
def is_in_project(address, project_id): return address in network.get_project_network(project_id).list_addresses()
def _get_project_addresses(self, project_id): project_addresses = [] for addr in network.get_project_network(project_id).list_addresses(): project_addresses.append(addr) return project_addresses