def test_rent_ips(self): net_pool1 = IPAddressPoolFactory.from_name(name='Generic pool 1') utils.fill_ip_pool(net_pool1, c_size=3, d_size=25, prefix='46.17.') net_pool2 = IPAddressPoolFactory.from_name(name='Generic pool 2') utils.fill_ip_pool(net_pool2, c_size=5, d_size=35, prefix='46.18.') self.assertEqual(48, net_pool1.get_free_ips().count()) self.assertEqual(136, net_pool2.get_free_ips().count()) renter = IPAddressRenter() renter.add_source(net_pool1) renter.add_source(net_pool2) # 5 and 5 addresses reserved ips = renter.rent(count=10) self.assertEqual(10, len(ips)) self.assertEqual(48 - 5, net_pool1.get_free_ips().count()) self.assertEqual(136 - 5, net_pool2.get_free_ips().count()) # 3 and 2 addresses reserved ips = renter.rent(count=5) self.assertEqual(5, len(ips)) self.assertEqual(48 - 5 - 3, net_pool1.get_free_ips().count()) self.assertEqual(136 - 5 - 2, net_pool2.get_free_ips().count()) # 1 addresses reserved ips = renter.rent(count=1) self.assertEqual(1, len(ips)) self.assertEqual(48 - 5 - 3 - 1, net_pool1.get_free_ips().count()) self.assertEqual(136 - 5 - 2, net_pool2.get_free_ips().count())
def test_directadmin_delete_assigned_license(self): da_pool = IPAddressPoolFactory.from_name(name='DirectAdmin pool') ipv4_net_pool = IPAddressPoolFactory.from_name(name='Generic pool') ip_addr = IPAddressGeneric.objects.create(pool=ipv4_net_pool, address='46.17.40.200') ip_addr1 = IPAddressGeneric.objects.create(pool=ipv4_net_pool, address='46.17.40.201') self.assertEqual(ipv4_net_pool.id, ip_addr.pool.id) da_license = DirectAdminLicense.register_license(cid=6768, lid=362163, pool=da_pool, ip_address=ip_addr) da_license.use() da_license.refresh_from_db() ip_addr.refresh_from_db() self.assertEqual(Resource.STATUS_INUSE, da_license.status) self.assertEqual(Resource.STATUS_INUSE, ip_addr.status) self.assertEqual(da_license.assigned_ip.id, ip_addr.id) da_license.delete() self.assertEqual(0, DirectAdminLicense.objects.all().count()) self.assertEqual(2, IPAddressGeneric.objects.all().count()) ip_addr.refresh_from_db() self.assertEqual(Resource.STATUS_FREE, ip_addr.status) self.assertEqual(ipv4_net_pool.id, ip_addr.pool.id)
def test_directadmin_ip_free(self): """ Test releasing of the DA license when assigned IP is freed """ da_pool = IPAddressPoolFactory.from_name(name='DirectAdmin pool') ipv4_net_pool = IPAddressPoolFactory.from_name(name='Generic pool') ip_addr = IPAddressGeneric.objects.create(pool=ipv4_net_pool, address='46.17.40.200') da_license = DirectAdminLicense.register_license(cid=6768, lid=362163, pool=da_pool, ip_address=ip_addr) da_license.refresh_from_db() ip_addr.refresh_from_db() self.assertEqual(Resource.STATUS_FREE, da_license.status) self.assertEqual(Resource.STATUS_FREE, ip_addr.status) da_license.use() da_license.refresh_from_db() ip_addr.refresh_from_db() self.assertEqual(Resource.STATUS_INUSE, da_license.status) self.assertEqual(Resource.STATUS_INUSE, ip_addr.status) ip_addr.free() da_license.refresh_from_db() ip_addr.refresh_from_db() self.assertEqual(Resource.STATUS_FREE, da_license.status) self.assertEqual(Resource.STATUS_FREE, ip_addr.status)
def test_rent_from_pools(self): self._auth() ipnet1 = IPAddressPoolFactory.from_name(name='Generic pool 1') utils.fill_ip_pool(ipnet1, c_size=3, d_size=25, prefix='46.17.') ipnet2 = IPAddressPoolFactory.from_name(name='Generic pool 2') utils.fill_ip_pool(ipnet2, c_size=5, d_size=35, prefix='46.18.') response = self.client.get('/v1/ip/rent/pool?pool=%s&pool=%s&count=3' % (ipnet1.id, ipnet2.id), format='json') self.assertEqual(200, response.status_code) self.assertEqual(3, response.data['count']) self.assertEqual(3, len(response.data['results'])) items = response.data['results'] self.assertEqual('46.17.1.1', items[0]['address']) self.assertEqual(Resource.STATUS_LOCKED, items[0]['status']) self.assertEqual(False, items[0]['main']) self.assertEqual('46.18.1.1', items[1]['address']) self.assertEqual(Resource.STATUS_LOCKED, items[1]['status']) self.assertEqual(False, items[0]['main']) self.assertEqual('46.17.1.2', items[2]['address']) self.assertEqual(Resource.STATUS_LOCKED, items[2]['status']) self.assertEqual(False, items[0]['main'])
def test_mass_manage_ip(self): net_pool1 = IPAddressPoolFactory.from_name(name='Generic pool 1') net_pool2 = IPAddressPoolFactory.from_name(name='Generic pool 2') utils.fill_ip_pool(net_pool1, c_size=25, d_size=105) self.assertEqual(2496, net_pool1.total_addresses) self.assertEqual(0, net_pool2.total_addresses) IPAddressGeneric.objects.filter( address__startswith='46.17.10.').update(pool=net_pool2) self.assertEqual(2392, net_pool1.total_addresses) self.assertEqual(104, net_pool2.total_addresses)
def test_add_ip_duplicate(self): pool1 = IPAddressPoolFactory.from_name('Test pool 1') pool2 = IPAddressPoolFactory.from_name('Test pool 1') ip1 = pool1.add_ip("46.17.40.111") ip2 = pool2.add_ip("46.17.40.111") ip1.refresh_from_db() ip2.refresh_from_db() self.assertEqual(ip1.id, ip2.id) self.assertEqual(ip1.address, ip2.address) self.assertEqual(ip1.pool.id, pool2.id)
def test_add_ip(self): ipv4 = '46.17.40.27' ipv6 = '2001:0::ab:cd' ipv4_net_pool = IPAddressPoolFactory.from_name(name='ipv4_net_pool') ipv6_net_pool = IPAddressPoolFactory.from_name(name='ipv6_net_pool') addrv4, created = IPAddressGeneric.objects.update_or_create( address=ipv4, pool=ipv4_net_pool) self.assertEqual('46.17.40.27', "%s" % addrv4) addrv6, created = IPAddressGeneric.objects.update_or_create( address=ipv6, pool=ipv6_net_pool) self.assertEqual('2001:0::ab:cd', "%s" % addrv6)
def test_directadmin_model(self): da_pool = IPAddressPoolFactory.from_name(name='DirectAdmin pool') ipv4_net_pool = IPAddressPoolFactory.from_name(name='Generic pool') ip_addr = IPAddressGeneric.objects.create(pool=ipv4_net_pool, address='46.17.40.200') da_license = DirectAdminLicense.register_license(cid=6768, lid=362163, pool=da_pool, ip_address=ip_addr) self.assertTrue(isinstance(da_license, DirectAdminLicense)) self.assertEqual(Resource.STATUS_FREE, da_license.status) self.assertEqual(6768, da_license.cid) self.assertEqual(362163, da_license.lid) self.assertEqual('46.17.40.200', "%s" % da_license.assigned_ip)
def _handle_daimport(self, *args, **options): cid = options['cid'] da_pool = IPAddressPoolFactory.from_name(name="Imported DirectAdmin") with open(options['tsv-file']) as tsv_file: for line in tsv_file: line = line.strip() if line == '': continue (lid_id, ipaddr, lic_status) = line.decode('utf-8').split(None, 3) logger.info("> Processing: %s %s %s" % (lid_id, ipaddr, lic_status)) try: ip_obj = GlobalIPManager.get_ip(ipaddr) except: # TODO: refactor IP creation ip_obj, created = IPAddressGeneric.objects.update_or_create( address=ipaddr, pool=da_pool) if lic_status == Resource.STATUS_FREE: if ip_obj.status == Resource.STATUS_FREE: license = DirectAdminLicense.register_license( pool=da_pool, cid=cid, lid=int(lid_id), ip_address=ip_obj) license.free() logger.info("LIC %s (%s). Added as FREE" % (lid_id, ipaddr)) else: logger.warning( "(!!) LIC %s (%s). You must change IP." % (lid_id, ipaddr)) else: if ip_obj.status == Resource.STATUS_FREE: license = DirectAdminLicense.register_license( pool=da_pool, cid=cid, lid=int(lid_id), ip_address=ip_obj) license.free() logger.warning( "(!) LIC %s (%s). Added as FREE (changed License status to FREE)" % (lid_id, ipaddr)) else: license = DirectAdminLicense.register_license( pool=da_pool, cid=cid, lid=int(lid_id), ip_address=ip_obj) license.use() logger.info("LIC %s (%s). Added as USED." % (lid_id, ipaddr))
def test_add_ip_to_the_pool(self): pool1 = IPAddressPoolFactory.from_name('Test pool') pool1.add_ip("46.17.40.111") pool1.add_ip( IPAddressGeneric.objects.create(address="46.17.40.112", pool=pool1)) self.assertEqual(2, pool1.get_ips().count())
def migrate_ips(apps, schema_editor): IPAddress = apps.get_model("ipman", "IPAddress") if not IPAddress.objects.filter().exists(): return pool = IPAddressPoolFactory.from_name("Global pool") for old_ip in IPAddress.objects.all(): new_ip = pool.add_ip(old_ip.address) if old_ip.is_used: new_ip.use(old_ip.parent)
def test_move_ips(self): pool1 = IPAddressPoolFactory.from_network('192.168.1.0/24') pool2 = IPAddressPoolFactory.from_network('192.168.2.0/23') pool3 = IPAddressPoolFactory.from_name('Target pool') self.assertEqual(254, pool1.get_ips().count()) self.assertEqual(510, pool2.get_ips().count()) self.assertEqual(0, pool3.get_ips().count()) GlobalIPManager.move_ips(pool3, '192.168.1.100', 50) GlobalIPManager.move_ips(pool3, '192.168.2.100', 1) self.assertEqual(204, pool1.get_ips().count()) self.assertEqual(509, pool2.get_ips().count()) self.assertEqual(51, pool3.get_ips().count())
def test_directadmin_use_license(self): da_pool = IPAddressPoolFactory.from_name(name='DirectAdmin pool') ipv4_net_pool = IPAddressPoolFactory.from_name(name='Generic pool') ip_addr = IPAddressGeneric.objects.create(pool=ipv4_net_pool, address='46.17.40.200') server = Server.objects.create(name='Test server') da_license = DirectAdminLicense.register_license(cid=6768, lid=362163, pool=da_pool, ip_address=ip_addr) da_license.refresh_from_db() ip_addr.refresh_from_db() self.assertEqual(Resource.STATUS_FREE, da_license.status) self.assertEqual(Resource.STATUS_FREE, ip_addr.status) ip_addr.use(server) da_license.refresh_from_db() ip_addr.refresh_from_db() self.assertEqual(Resource.STATUS_INUSE, da_license.status) self.assertEqual(Resource.STATUS_INUSE, ip_addr.status) self.assertEqual(da_license.assigned_ip.id, ip_addr.id)
def test_pool_usage(self): ipv4_net_pool = IPAddressPoolFactory.from_name(name='Generic pool') ipv4_net_pool.use() utils.fill_ip_pool(ipv4_net_pool, c_size=25, d_size=105) all_count = IPAddressGeneric.objects.all().count() / 3 vm1 = VirtualServer.objects.create(label="VM", status=Resource.STATUS_INUSE) for address in IPAddressGeneric.objects.filter(): if all_count <= 0: break all_count -= 1 address.use(parent=vm1) self.assertEqual(2496, ipv4_net_pool.total_addresses) self.assertEqual(832, ipv4_net_pool.used_addresses)
def test_ipaddress_history(self): pool = IPAddressPoolFactory.from_name('Test pool') ip1 = IPAddressGeneric.objects.create(address='192.168.1.2', pool=pool) ip2 = IPAddressGeneric.objects.create(address='192.168.2.2', pool=pool) ip3 = IPAddressGeneric.objects.create(address='192.168.3.2', pool=pool) self.assertEqual( 4, len(HistoryEvent.objects.filter(event_type=HistoryEvent.CREATE))) self.assertEqual( 11, len(HistoryEvent.objects.filter(event_type=HistoryEvent.UPDATE))) # skip record with no changes ip1.address = '192.168.1.2' ip1.save() self.assertEqual( 11, len(HistoryEvent.objects.filter(event_type=HistoryEvent.UPDATE)))
def test_relink_ip(self): new_server1 = Server.objects.create(label="test server") new_server2 = Server.objects.create(label="test server") pool = IPAddressPoolFactory.from_name('test') cmdb_importer = GenericCmdbImporter() ip1 = IPAddressGeneric.objects.create(address='46.17.40.2', status=Resource.STATUS_LOCKED, pool=pool, parent=new_server1) self.assertEqual(Resource.STATUS_LOCKED, ip1.status) self.assertEqual(new_server1.id, ip1.parent.id) # used IP relink between parents cmdb_importer._add_ip(ip1.address, parent=new_server2) ip1.refresh_from_db() self.assertEqual(Resource.STATUS_INUSE, ip1.status) self.assertEqual(new_server2.id, ip1.parent.id)
def _handle_pool_addnamed(self, *args, **options): pool_name = options['pool-name'].decode('utf-8') IPAddressPoolFactory.from_name(pool_name) self._list_pools()