Beispiel #1
0
    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)
Beispiel #2
0
    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']))
Beispiel #3
0
    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())
Beispiel #4
0
    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)
Beispiel #5
0
    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
Beispiel #6
0
    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())
Beispiel #7
0
    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'))
Beispiel #8
0
    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])
Beispiel #9
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())
Beispiel #10
0
    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
Beispiel #11
0
    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())
Beispiel #12
0
    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)
Beispiel #13
0
    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)
Beispiel #14
0
 def test_peer_network(self):
     pool = IPAddressPoolFactory.from_network(network='87.251.133.9/31')
     self.assertEqual(2, pool.get_ips().count())
Beispiel #15
0
    def _handle_pool_addcidr(self, *args, **options):
        net = options['net'].decode('utf-8')
        IPAddressPoolFactory.from_network(net)

        self._list_pools()
Beispiel #16
0
    def test_import_data(self):
        arp_file_path = os.path.join(self.DATA_DIR, 'arp-table.txt')
        mac_file_path = os.path.join(self.DATA_DIR, 'mac-table.txt')

        cmdb_importer = GenericCmdbImporter()

        # create IP pools and basic structure
        dc_anders = RegionResource.objects.create(name="Anders")
        dc_rtcom = RegionResource.objects.create(name="Rostelecom")

        # create source switch
        anders_gw = GatewaySwitch.objects.create(name="baxet-gw-q",
                                                 parent=dc_anders)

        # arp table provider
        qtech_switch = QtechL3Switch()
        qtech_switch.from_arp_dump(arp_file_path)

        switch_ports = list(qtech_switch.ports)

        self.assertEqual(12, len(switch_ports))

        # Add our IP pools
        pool_list = [
            IPAddressPoolFactory.from_network(network='46.17.40.0/23',
                                              parent=dc_anders),
            IPAddressPoolFactory.from_network(network='46.17.44.0/23',
                                              parent=dc_anders),
            IPAddressPoolFactory.from_network(network='176.32.34.0/23',
                                              parent=dc_anders),
            IPAddressPoolFactory.from_network(network='176.32.36.0/24',
                                              parent=dc_anders),
            IPAddressPoolFactory.from_network(network='176.32.37.0/24',
                                              parent=dc_anders),
            IPAddressPoolFactory.from_network(network='176.32.38.0/24',
                                              parent=dc_anders),
            IPAddressPoolFactory.from_network(network='176.32.39.0/24',
                                              parent=dc_anders),
            IPAddressPoolFactory.from_network(network='2a00:b700::/120',
                                              parent=dc_anders),
            IPAddressPoolFactory.from_network(network='46.17.46.0/23',
                                              parent=dc_rtcom),
            IPAddressPoolFactory.from_network(network='46.29.160.0/23',
                                              parent=dc_rtcom),
            IPAddressPoolFactory.from_network(network='176.32.32.0/23',
                                              parent=dc_rtcom),
            IPAddressPoolFactory.from_network(network='46.29.162.0/23',
                                              parent=dc_rtcom),
            IPAddressPoolFactory.from_network(network='46.29.164.0/22',
                                              parent=dc_rtcom),
            IPAddressPoolFactory.from_network(network='185.22.152.0/22',
                                              parent=dc_rtcom),
            IPAddressPoolFactory.from_network(network='2a00:b700:1::/120',
                                              parent=dc_rtcom),
            IPAddressPoolFactory.from_network(network='87.251.133.9/31',
                                              parent=dc_rtcom)
        ]

        # Double the proccess, to test data update process
        cmdb_importer.import_switch(anders_gw.id, qtech_switch)
        cmdb_importer.import_switch(anders_gw.id, qtech_switch)

        # +1 IP: 87.251.133.9, /31 peering network address
        self.assertEqual(
            1329, len(GlobalIPManager.find_ips(status=Resource.STATUS_INUSE)))

        for pool in pool_list:
            print("%s - %d" % (pool, pool.usage))

        # count servers
        self.assertEqual(71, len(Server.active.filter()))
        self.assertEqual(39, len(VirtualServer.active.filter()))
        self.assertEqual(71, len(ServerPort.active.filter()))
        self.assertEqual(0, len(ServerPort.active.filter(parent=None)))
        self.assertEqual(39, len(VirtualServerPort.active.filter()))

        self.assertEqual(10, len(PortConnection.active.filter()))

        # Servers and ports equality check
        self.assertEqual(
            (len(ServerPort.active.filter()) +
             len(VirtualServerPort.active.filter())),
            (len(Server.active.filter()) + len(VirtualServer.active.filter())))

        # import MAC data from mac table
        anders_sw1 = Switch.objects.create(name="baxet-sw-1", parent=dc_anders)
        qtech3400_switch = Qtech3400Switch()
        qtech3400_switch.from_mac_dump(mac_file_path)

        # double call, to check update
        cmdb_importer.import_switch(anders_sw1.id, qtech3400_switch)
        cmdb_importer.import_switch(anders_sw1.id, qtech3400_switch)

        # count servers
        self.assertEqual(76, len(Server.active.filter()))
        self.assertEqual(41, len(VirtualServer.active.filter()))
        self.assertEqual(76, len(ServerPort.active.filter()))
        self.assertEqual(0, len(ServerPort.active.filter(parent=None)))
        self.assertEqual(41, len(VirtualServerPort.active.filter()))
        self.assertEqual(54, len(PortConnection.active.filter()))
        self.assertEqual(
            1329, len(GlobalIPManager.find_ips(status=Resource.STATUS_INUSE)))

        # update VPS links to hypervisors
        for switch in Switch.active.all():
            for switch_port in SwitchPort.active.filter(parent=switch):
                cmdb_importer.process_hypervisors(switch_port)

        for switch in GatewaySwitch.active.all():
            for switch_port in SwitchPort.active.filter(parent=switch):
                cmdb_importer.process_hypervisors(switch_port)

        hypervisors = Server.active.filter(role='hypervisor')
        self.assertEqual(3, len(hypervisors))
        for server in hypervisors:
            print(server.id)

        # There are linked VPS, hypervisor detection logic test.
        self.assertEqual(
            4, len(VirtualServer.active.filter(parent=hypervisors[0].id)))
        self.assertEqual(
            2, len(VirtualServer.active.filter(parent=hypervisors[1].id)))
        self.assertEqual(
            3, len(VirtualServer.active.filter(parent=hypervisors[2].id)))

        events = HistoryEvent.objects.filter(event_type=HistoryEvent.CREATE)
        self.assertEqual(7492, len(events))  # all IPs are beign created