def test_rent_from_datacenter(self): self._auth() moscow = RegionResource.objects.create(name='Moscow') dc1 = Datacenter.objects.create(name='Test DC 1', parent=moscow) dc2 = Datacenter.objects.create(name='Test DC 2', parent=moscow) net_pool1 = IPAddressPoolFactory.from_network(network="46.17.40.0/24", parent=dc1) net_pool2 = IPAddressPoolFactory.from_network(network="46.17.41.0/24", parent=dc1) net_pool3 = IPAddressPoolFactory.from_network(network="46.17.42.0/24", parent=dc2) response = self.client.get('/v1/ip/rent/dc?dc=%s&count=25' % dc1.id, format='json') self.assertEqual(200, response.status_code) self.assertEqual(25, response.data['count']) self.assertEqual(25, len(response.data['results'])) items = response.data['results'] self.assertEqual('46.17.40.1', items[0]['address']) self.assertEqual(Resource.STATUS_LOCKED, items[0]['status']) self.assertEqual('46.17.41.1', items[1]['address']) self.assertEqual(Resource.STATUS_LOCKED, items[1]['status']) self.assertEqual('46.17.40.2', items[2]['address']) self.assertEqual(Resource.STATUS_LOCKED, items[2]['status']) self.assertEqual(241, net_pool1.get_free_ips().count()) self.assertEqual(242, net_pool2.get_free_ips().count()) self.assertEqual(254, net_pool3.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_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_ip_address_find(self): """Поиск IP адресов""" self._auth() moscow = RegionResource.objects.create(name='Moscow') dc1 = Datacenter.objects.create(name='Test DC 1', parent=moscow) dc2 = Datacenter.objects.create(name='Test DC 2', parent=moscow) net_pool1 = IPAddressPoolFactory.from_network(network="46.17.40.0/24", parent=dc1) net_pool2 = IPAddressPoolFactory.from_network(network="46.17.41.0/24", parent=dc2) # filter by param1 response = self.client.get('/v1/ip/', data={'status': Resource.STATUS_INUSE}, format='json') self.assertEqual(200, response.status_code) self.assertEqual(0, len(response.data['results'])) response = self.client.get('/v1/ip/', data={'address': '46.17.40.10'}, format='json') self.assertEqual(200, response.status_code) self.assertEqual(1, len(response.data['results'])) response = self.client.get('/v1/ip/', data={'address__exact': '46.17.40.10'}, format='json') self.assertEqual(200, response.status_code) self.assertEqual(1, len(response.data['results'])) response = self.client.get('/v1/ip/', data={'address__endswith': '111'}, format='json') self.assertEqual(200, response.status_code) self.assertEqual(2, len(response.data['results'])) # разбивка по страницам response = self.client.get('/v1/ip/', data={'address__contains': '17.4'}, format='json') self.assertEqual(200, response.status_code) self.assertEqual(508, response.data['count']) self.assertEqual(50, len(response.data['results']))
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_datacenter_pool_manager(self): russia = RegionResource.objects.create(name='Russia') moscow = RegionResource.objects.create(name='Moscow', parent=russia) dc1 = Datacenter.objects.create(name='Test DC 1', parent=moscow) dc2 = Datacenter.objects.create(name='Test DC 2', parent=moscow) rack1 = Rack.objects.create(name='Test Rack 1', parent=dc1) srv1 = Server.objects.create(name='Test server 1', parent=rack1) pool1 = IPAddressPoolFactory.from_network('192.168.0.0/23') dc1 += pool1 pool2 = IPAddressPoolFactory.from_network('192.169.0.0/23') pool2.use() dc2 += pool2 # testing manager pools1 = GlobalIPManager.find_pools() self.assertEqual(2, len(pools1)) pools2 = GlobalIPManager.find_pools(datacenter=dc1) self.assertEqual(1, len(pools2)) ip_address = GlobalIPManager.get_ip('192.169.0.100') self.assertEqual('192.169.0.100', ip_address.address) self.assertEqual(pool2.id, ip_address.pool.id)
def _handle_pool_addrange(self, *args, **options): range_from = options['ip_start'].decode('utf-8') range_to = options['ip_end'].decode('utf-8') IPAddressPoolFactory.from_range(range_from=range_from, range_to=range_to) self._list_pools()
def test_ip_address_crud(self): """ API управления IP позволяет только запрашивать, обновлять и искать IP. Создать и удалить IP - нельзя. """ self._auth() moscow = RegionResource.objects.create(name='Moscow') dc1 = Datacenter.objects.create(name='Test DC 1', parent=moscow) dc2 = Datacenter.objects.create(name='Test DC 2', parent=moscow) net_pool1 = IPAddressPoolFactory.from_network(network="46.17.40.0/24", parent=dc1) net_pool2 = IPAddressPoolFactory.from_network(network="46.17.41.0/24", parent=dc1) net_pool3 = IPAddressPoolFactory.from_network(network="46.17.42.0/24", parent=dc2) # арендуем 5 IP response = self.client.get('/v1/ip/rent/dc?dc=%s&count=5' % dc1.id, format='json') self.assertEqual(200, response.status_code) # проверить данные items = response.data['results'] self.assertEqual('46.17.40.1', items[0]['address']) self.assertEqual(Resource.STATUS_LOCKED, items[0]['status']) self.assertEqual('46.17.41.1', items[1]['address']) self.assertEqual(Resource.STATUS_LOCKED, items[1]['status']) # объекты для работы ip1 = items[0] ip2 = items[1] ip1_obj = IPAddressGeneric.objects.get(pk=ip1['id']) ip2_obj = IPAddressGeneric.objects.get(pk=ip2['id']) self.assertEqual(Resource.STATUS_LOCKED, ip1_obj.status) self.assertEqual(Resource.STATUS_LOCKED, ip2_obj.status) # обновить статус response = self.client.patch('/v1/ip/%s/' % ip1_obj.id, { 'status': Resource.STATUS_INUSE }) self.assertEqual(200, response.status_code) ip1_obj.refresh_from_db() ip2_obj.refresh_from_db() self.assertEqual(Resource.STATUS_INUSE, ip1_obj.status) self.assertEqual(Resource.STATUS_LOCKED, ip2_obj.status)
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 setUp(self): super(ResourcesAPITests, self).setUp() user_name = 'admin' user, created = User.objects.get_or_create(username=user_name, password=user_name, email='*****@*****.**', is_staff=True) token, created = Token.objects.get_or_create(user=user) self.client.credentials(HTTP_AUTHORIZATION='Token ' + token.key) Resource.objects.all().delete() self.moscow = RegionResource.objects.create(name='Moscow') self.dc1 = Datacenter.objects.create(name='Test DC 1', parent=self.moscow) self.rack1 = Rack.objects.create(name='Test Rack 1', parent=self.dc1) self.srv1 = Server.objects.create(name='Test hypervisor 1', role='hypervisor', parent=self.rack1) self.pools_group1 = RegionResource.objects.create( name='Test DC 1 IP Pools', parent=self.dc1) self.pool1 = IPAddressPoolFactory.from_network( '192.168.0.0/23', parent=self.pools_group1, dns1='46.17.46.200', dns2='46.17.40.200') GlobalIPManager.get_ip('192.168.0.1').lock() self.pool11 = IPAddressPoolFactory.from_network( '192.169.0.0/23', parent=self.pools_group1, dns1='46.17.46.200', dns2='46.17.40.200') self.pool11.use() GlobalIPManager.get_ip('192.169.0.1').lock() self.srv1.set_option('agentd_taskqueue', 'test_task_queue') MockVpsControlTask.REMOTE_WORKER.sent_tasks = [] self.cloud = CmdbCloudConfig() self.backend = ProxMoxJBONServiceBackend(self.cloud) ProxMoxJBONServiceBackend.TASK_CREATE = MockVpsControlTask ProxMoxJBONServiceBackend.TASK_START = MockVpsControlTask ProxMoxJBONServiceBackend.TASK_STOP = MockVpsControlTask
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_create_from_network(self): pool1 = IPAddressPoolFactory.from_network('192.168.1.0/24') pool2 = IPAddressPoolFactory.from_network('192.168.2.0/23') self.assertEqual(254, pool1.get_ips().count()) self.assertEqual(254, pool1.get_free_ips().count()) self.assertEqual('192.168.1.0/24', pool1.name) self.assertEqual('192.168.1.1', pool1.get_option_value('gateway')) self.assertEqual('255.255.255.0', pool1.get_option_value('netmask')) self.assertEqual(510, pool2.get_ips().count()) self.assertEqual(510, pool2.get_free_ips().count()) self.assertEqual('192.168.2.0/23', pool2.name) self.assertEqual('192.168.2.1', pool2.get_option_value('gateway')) self.assertEqual('255.255.254.0', pool2.get_option_value('netmask'))
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 test_mptt_tree_navigation_single_tree(self): russia = RegionResource.objects.create(name='Russia') moscow = RegionResource.objects.create(name='Moscow', parent=russia) dc1 = Datacenter.objects.create(name='Test DC 1', parent=moscow) rack1 = Rack.objects.create(name='Test Rack 1', parent=dc1) srv1 = Server.objects.create(name='Test server 1', parent=rack1) pools_group1 = RegionResource.objects.create(name='Test DC 1 IP Pools', parent=dc1) pool1 = IPAddressPoolFactory.from_network(network='192.168.0.0/23', parent=pools_group1, status=Resource.STATUS_FREE) pool11 = IPAddressPoolFactory.from_network( network='192.169.0.0/23', parent=pools_group1, status=Resource.STATUS_INUSE) kazan = RegionResource.objects.create(name='Kazan', parent=russia) dc2 = Datacenter.objects.create(name='Test DC 2', parent=kazan) rack2 = Rack.objects.create(name='Test Rack 2', parent=dc2) srv2 = Server.objects.create(name='Test server 1', parent=rack2) pools_group2 = RegionResource.objects.create(name='Test DC 2 IP Pools', parent=dc2) pool2 = IPAddressPoolFactory.from_network(network='172.168.0.0/23', parent=pools_group2) # find Pool of the server my_dcs_raw = Datacenter.active.filter(lft__lt=srv1.lft, rght__gt=srv1.rght, tree_id=srv1.tree_id) self.assertEqual(1, len(my_dcs_raw)) self.assertEqual(dc1.id, my_dcs_raw[0].id) # Find Datacenter my_dcs = srv1.filter_parents(Datacenter) self.assertEqual(1, len(my_dcs)) self.assertEqual(dc1.id, my_dcs[0].id) # Find free IPNetworkPool (we have free and used IP pools in this DC) my_ippools = GlobalIPManager.find_pools(datacenter=my_dcs[0], status=Resource.STATUS_FREE) self.assertEqual(1, len(my_ippools)) self.assertEqual('192.168.0.0/23', "%s" % my_ippools[0])
def test_delete_ip(self): """ IP is completely removed from DB on delete() """ srv1 = Server.objects.create(name='Test server 1') pool1 = IPAddressPoolFactory.from_network('192.168.0.0/24') self.assertEqual(254, pool1.get_ips().count()) ip = pool1.get_ips()[10] ip.delete() self.assertEqual(253, 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 setUp(self): Resource.objects.all().delete() self.moscow = RegionResource.objects.create(name='Moscow') self.dc1 = Datacenter.objects.create(name='Test DC 1', parent=self.moscow) self.rack1 = Rack.objects.create(name='Test Rack 1', parent=self.dc1) self.srv1 = Server.objects.create(name='Test hypervisor 1', role='hypervisor', parent=self.rack1) self.pools_group1 = RegionResource.objects.create( name='Test DC 1 IP Pools', parent=self.dc1) self.pool1 = IPAddressPoolFactory.from_network( '192.168.0.0/23', parent=self.pools_group1, dns1='46.17.46.200', dns2='46.17.40.200') GlobalIPManager.get_ip('192.168.0.1').lock() self.pool11 = IPAddressPoolFactory.from_network( '192.169.0.0/23', parent=self.pools_group1, dns1='46.17.46.200', dns2='46.17.40.200') self.pool11.use() GlobalIPManager.get_ip('192.169.0.1').lock() self.srv1.set_option('agentd_taskqueue', 'test_task_queue') MockVpsControlTask.REMOTE_WORKER.sent_tasks = [] self.cloud = CmdbCloudConfig() self.backend = ProxMoxJBONServiceBackend(self.cloud) ProxMoxJBONServiceBackend.TASK_CREATE = MockVpsControlTask ProxMoxJBONServiceBackend.TASK_START = MockVpsControlTask ProxMoxJBONServiceBackend.TASK_STOP = MockVpsControlTask
def test_rent_ips_from_dc(self): moscow = RegionResource.objects.create(name='Moscow') dc1 = Datacenter.objects.create(name='Test DC 1', parent=moscow) dc2 = Datacenter.objects.create(name='Test DC 2', parent=moscow) net_pool1 = IPAddressPoolFactory.from_network(network="46.17.40.0/24", parent=dc1) net_pool2 = IPAddressPoolFactory.from_network(network="46.17.41.0/24", parent=dc1) net_pool3 = IPAddressPoolFactory.from_network(network="46.17.42.0/24", parent=dc2) self.assertEqual(254, net_pool1.get_free_ips().count()) self.assertEqual(254, net_pool2.get_free_ips().count()) self.assertEqual(254, net_pool3.get_free_ips().count()) renter = IPAddressRenter.from_datacenter(dc1, ip_version=4) ips = renter.rent(count=25) self.assertEqual(25, len(ips)) self.assertEqual(241, net_pool1.get_free_ips().count()) self.assertEqual(242, net_pool2.get_free_ips().count()) self.assertEqual(254, net_pool3.get_free_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_delete_virtual_hierarchy_childs_control(self): vm1 = VirtualServer.objects.create(label="VM", status=Resource.STATUS_INUSE) vmport1 = VirtualServerPort.objects.create( number=15, mac='234567267845', parent=vm1, status=Resource.STATUS_INUSE) ippool1 = IPAddressPoolFactory.from_network( network='192.168.1.1/24', status=Resource.STATUS_INUSE) address1 = ippool1.get_free_ips()[:1][0] self.assertEqual(ippool1.id, address1.pool.id) address1.use() address1.parent = vmport1 address1.save() self.assertEqual(vmport1.id, address1.parent.id) self.assertEqual(ippool1.id, address1.pool.id) # existing childs try: vm1.delete() self.fail("Waiting for the exception.") except ValidationError: pass self.assertEqual( 1, IPAddressGeneric.objects.filter(address__exact="%s" % address1).count()) address1.delete() # removed from DB vmport1.delete() vm1.delete() vm1.refresh_from_db() vmport1.refresh_from_db() ippool1.refresh_from_db() self.assertEqual( 0, IPAddressGeneric.objects.filter(address__exact="%s" % address1).count()) self.assertEqual(Resource.STATUS_DELETED, vm1.status) self.assertEqual(Resource.STATUS_DELETED, vmport1.status)
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 test_free_ip(self): """ IP.parent set to None on free() """ srv1 = Server.objects.create(name='Test server 1') pool1 = IPAddressPoolFactory.from_network('192.168.0.0/24') ip = pool1.get_ips()[10] self.assertEqual(None, ip.parent) self.assertEqual(Resource.STATUS_FREE, ip.status) self.assertEqual(pool1.id, ip.pool.id) ip.use(srv1) ip.refresh_from_db() self.assertEqual(srv1.id, ip.parent.id) self.assertEqual(Resource.STATUS_INUSE, ip.status) self.assertEqual(pool1.id, ip.pool.id) ip.free() self.assertEqual(srv1.id, ip.parent.id) # keep .parent field self.assertEqual(Resource.STATUS_FREE, ip.status) self.assertEqual(pool1.id, ip.pool.id)
def test_create_from_range(self): pool1 = IPAddressPoolFactory.from_range('46.17.40.50', '46.17.40.192') self.assertEqual(143, pool1.get_ips().count()) self.assertEqual(143, pool1.get_free_ips().count()) self.assertEqual('46.17.40.50-46.17.40.192', pool1.name)