コード例 #1
0
ファイル: tests.py プロジェクト: petterreinholdtsen/mreg
    def setUp(self):
        """Define the test client and other test variables."""
        # Needs sample host and sample network to test properly
        self.host = Host.objects.create(name='host.example.org')

        self.ipaddress_sample = Ipaddress(host=self.host,
                                          ipaddress='192.168.202.123',
                                          macaddress='a4:34:d9:0e:88:b9')

        self.ipv6address_sample = Ipaddress(host=self.host,
                                            ipaddress='2001:db8::beef',
                                            macaddress='a4:34:d9:0e:88:b9')
コード例 #2
0
 def test_model_updated_by_added_ipv6(self):
     """Test to check that an PtrOverride is added when two hosts share the same ipv6.
        Also makes sure that the PtrOverride points to the first host which held the ipv6."""
     initial_count = PtrOverride.objects.count()
     ipv6_one = Ipaddress(host=self.host_ipv6_one, ipaddress='2001:db8::4')
     clean_and_save(ipv6_one)
     one_count = PtrOverride.objects.count()
     ipv6_two = Ipaddress(host=self.host_ipv6_two, ipaddress='2001:db8::4')
     clean_and_save(ipv6_two)
     ptr =  PtrOverride.objects.first()
     self.assertEqual(ptr.host, self.host_ipv6_one)
     self.assertEqual(ptr.ipaddress, '2001:db8::4')
     self.assertEqual(initial_count, 0)
     self.assertEqual(initial_count, one_count)
     self.assertEqual(PtrOverride.objects.count(), 1)
コード例 #3
0
ファイル: views.py プロジェクト: JarleB/mreg
    def post(self, request, *args, **kwargs):
        if "name" in request.data:
            if self.queryset.filter(name=request.data["name"]).exists():
                content = {'ERROR': 'name already in use'}
                return Response(content, status=status.HTTP_409_CONFLICT)

        hostdata = request.data.copy()

        if 'ipaddress' in hostdata:
            ipkey = hostdata['ipaddress']
            del hostdata['ipaddress']
            host = Host()
            hostserializer = HostSerializer(host, data=hostdata)

            if hostserializer.is_valid(raise_exception=True):
                with transaction.atomic():
                    hostserializer.save()
                    ipdata = {'host': host.pk, 'ipaddress': ipkey}
                    ip = Ipaddress()
                    ipserializer = IpaddressSerializer(ip, data=ipdata)
                    if ipserializer.is_valid(raise_exception=True):
                        self.perform_create(ipserializer)
                        location = request.path + host.name
                        return Response(status=status.HTTP_201_CREATED, headers={'Location': location})
        else:
            host = Host()
            hostserializer = HostSerializer(host, data=hostdata)
            if hostserializer.is_valid(raise_exception=True):
                self.perform_create(hostserializer)
                location = request.path + host.name
                return Response(status=status.HTTP_201_CREATED, headers={'Location': location})
コード例 #4
0
    def setUp(self):
        """Define the test client and other test variables."""
        self.zone_sample = ForwardZone(name='example.org',
                                       primary_ns='ns.example.org',
                                       email='*****@*****.**')
        clean_and_save(self.zone_sample)

        self.zone_1010 = ReverseZone(name='10.10.in-addr.arpa',
                                     primary_ns='ns.example.org',
                                     email='*****@*****.**')
        clean_and_save(self.zone_1010)

        self.network_sample = Network(range='10.0.0.0/24',
                                      description='some description')
        clean_and_save(self.network_sample)

        self.ns_hostsample = Host(name='ns.example.org',
                                  contact='*****@*****.**')
        clean_and_save(self.ns_hostsample)

        self.ns_hostip = Ipaddress(host=self.ns_hostsample,
                                   ipaddress='10.0.0.111')
        clean_and_save(self.ns_hostip)

        self.ns_sample = NameServer(name='ns.example.org', ttl=300)
        clean_and_save(self.ns_sample)
        self.zone_sample.nameservers.add(self.ns_sample)
        self.zone_sample.save()
コード例 #5
0
 def test_model_can_delete_ns_hostip(self):
     """Test that the model is able to delete an IP from a nameserver, if nameserver has multiple IPs."""
     self.ns_hostip2 = Ipaddress(host=self.ns_hostsample,
                                 ipaddress='10.0.0.112')
     clean_and_save(self.ns_hostip2)
     old_count = Ipaddress.objects.count()
     self.ns_hostip2.delete()
     new_count = Ipaddress.objects.count()
     self.assertNotEqual(old_count, new_count)
コード例 #6
0
 def test_model_add_and_remove_ip(self):
     """Test to check that an PtrOverride is added when two hosts share the same ip.
        Also makes sure that the PtrOverride points to the first host which held the ip.
        Also makes sure that the PtrOverride is deleted when the host is deleted."""
     initial_count = PtrOverride.objects.count()
     ip_one = Ipaddress(host=self.host_one, ipaddress='10.0.0.1')
     clean_and_save(ip_one)
     one_count = PtrOverride.objects.count()
     ip_two = Ipaddress(host=self.host_two, ipaddress='10.0.0.1')
     clean_and_save(ip_two)
     two_count = PtrOverride.objects.count()
     ptr = PtrOverride.objects.first()
     self.assertEqual(ptr.host, self.host_one)
     self.assertEqual(ptr.ipaddress, '10.0.0.1')
     self.assertEqual(initial_count, 0)
     self.assertEqual(initial_count, one_count)
     self.assertEqual(two_count, 1)
     self.host_one.delete()
     self.assertEqual(PtrOverride.objects.count(), 0)
コード例 #7
0
    def setUp(self):
        """Define the test client and other test variables."""
        # Needs sample host and sample network to test properly
        self.host_one = Host(name='some-host.example.org',
                             contact='*****@*****.**',
                             ttl=300,
                             loc='23 58 23 N 10 43 50 E 80m',
                             comment='some comment')

        self.network_sample = Network(range='129.240.202.0/20',
                                    description='some description',
                                    vlan=123,
                                    dns_delegated=False)

        clean_and_save(self.host_one)
        # clean_and_save(self.network_sample) # Needed when network ForeignKey is implemented.

        self.ipaddress_sample = Ipaddress(host=Host.objects.get(name='some-host.example.org'),
                                          ipaddress='129.240.202.123',
                                          macaddress='a4:34:d9:0e:88:b9')
コード例 #8
0
    def post(self, request, *args, **kwargs):
        if "name" in request.data:
            if self.queryset.filter(name=request.data["name"]).exists():
                content = {'ERROR': 'name already in use'}
                return Response(content, status=status.HTTP_409_CONFLICT)

        if "ipaddress" in request.data and "network" in request.data:
            content = {
                'ERROR': '\'ipaddress\' and \'network\' is mutually exclusive'
            }
            return Response(content, status=status.HTTP_400_BAD_REQUEST)

        # request.data is immutable
        hostdata = request.data.copy()

        if 'network' in hostdata:
            try:
                ipaddress.ip_network(hostdata['network'])
            except ValueError as error:
                content = {'ERROR': str(error)}
                return Response(content, status=status.HTTP_400_BAD_REQUEST)

            network = Network.objects.filter(
                network=hostdata['network']).first()
            if not network:
                content = {'ERROR': 'no such network'}
                return Response(content, status=status.HTTP_404_NOT_FOUND)

            ip = network.get_random_unused()
            if not ip:
                content = {'ERROR': 'no available IP in network'}
                return Response(content, status=status.HTTP_404_NOT_FOUND)

            hostdata['ipaddress'] = ip
            del hostdata['network']

        if 'ipaddress' in hostdata:
            ipkey = hostdata['ipaddress']
            del hostdata['ipaddress']
            host = Host()
            hostserializer = HostSerializer(host, data=hostdata)

            if hostserializer.is_valid(raise_exception=True):
                with transaction.atomic():
                    # XXX: must fix. perform_creates failes as it has no ipaddress and the
                    # the permissions fails for most users. Maybe a nested serializer should fix it?
                    #self.perform_create(hostserializer)
                    hostserializer.save()
                    self.save_log_create(hostserializer)
                    ipdata = {'host': host.pk, 'ipaddress': ipkey}
                    ip = Ipaddress()
                    ipserializer = IpaddressSerializer(ip, data=ipdata)
                    if ipserializer.is_valid(raise_exception=True):
                        self.perform_create(ipserializer)
                        location = request.path + host.name
                        return Response(status=status.HTTP_201_CREATED,
                                        headers={'Location': location})
        else:
            host = Host()
            hostserializer = HostSerializer(host, data=hostdata)
            if hostserializer.is_valid(raise_exception=True):
                self.perform_create(hostserializer)
                location = request.path + host.name
                return Response(status=status.HTTP_201_CREATED,
                                headers={'Location': location})
コード例 #9
0
 def _add_ip(host, ipaddress):
     ip = Ipaddress(host=host, ipaddress=ipaddress)
     clean_and_save(ip)