Commit 5ea451a2 by Czémán Arnold

Merge branch 'issue_425' into 'master'

Fix Issue 425



See merge request !375
parents b69cd81e 010b6c8d
Pipeline #101 passed with stage
in 0 seconds
...@@ -499,7 +499,11 @@ class Vlan(AclBase, models.Model): ...@@ -499,7 +499,11 @@ class Vlan(AclBase, models.Model):
def get_new_address(self): def get_new_address(self):
hosts = self.host_set hosts = self.host_set
used_v4 = IPSet(hosts.values_list('ipv4', flat=True)) used_ext_addrs = Host.objects.filter(
external_ipv4__isnull=False).values_list(
'external_ipv4', flat=True)
used_v4 = IPSet(hosts.values_list('ipv4', flat=True)).union(
used_ext_addrs).union([self.network4.ip])
used_v6 = IPSet(hosts.exclude(ipv6__isnull=True) used_v6 = IPSet(hosts.exclude(ipv6__isnull=True)
.values_list('ipv6', flat=True)) .values_list('ipv6', flat=True))
......
...@@ -77,13 +77,13 @@ class GetNewAddressTestCase(MockCeleryMixin, TestCase): ...@@ -77,13 +77,13 @@ class GetNewAddressTestCase(MockCeleryMixin, TestCase):
d = Domain(name='example.org', owner=self.u1) d = Domain(name='example.org', owner=self.u1)
d.save() d.save()
# /29 = .1-.6 = 6 hosts/subnet + broadcast + network id # /29 = .1-.6 = 6 hosts/subnet + broadcast + network id
self.vlan = Vlan(vid=1, name='test', network4='10.0.0.0/29', self.vlan = Vlan(vid=1, name='test', network4='10.0.0.1/29',
network6='2001:738:2001:4031::/80', domain=d, network6='2001:738:2001:4031::/80', domain=d,
owner=self.u1) owner=self.u1)
self.vlan.clean() self.vlan.clean()
self.vlan.save() self.vlan.save()
self.vlan.host_set.all().delete() self.vlan.host_set.all().delete()
for i in [1] + range(3, 6): for i in range(3, 6):
Host(hostname='h-%d' % i, mac='01:02:03:04:05:%02d' % i, Host(hostname='h-%d' % i, mac='01:02:03:04:05:%02d' % i,
ipv4='10.0.0.%d' % i, vlan=self.vlan, ipv4='10.0.0.%d' % i, vlan=self.vlan,
owner=self.u1).save() owner=self.u1).save()
...@@ -102,6 +102,15 @@ class GetNewAddressTestCase(MockCeleryMixin, TestCase): ...@@ -102,6 +102,15 @@ class GetNewAddressTestCase(MockCeleryMixin, TestCase):
owner=self.u1).save() owner=self.u1).save()
self.assertRaises(ValidationError, self.vlan.get_new_address) self.assertRaises(ValidationError, self.vlan.get_new_address)
def test_all_addr_in_use2(self):
Host(hostname='h-xd', mac='01:02:03:04:05:06',
ipv4='10.0.0.6', vlan=self.vlan,
owner=self.u1).save()
Host(hostname='h-arni', mac='01:02:03:04:05:02',
ipv4='100.0.0.1', vlan=self.vlan, external_ipv4='10.0.0.2',
owner=self.u1).save()
self.assertRaises(ValidationError, self.vlan.get_new_address)
def test_new_addr(self): def test_new_addr(self):
used_v4 = IPSet(self.vlan.host_set.values_list('ipv4', flat=True)) used_v4 = IPSet(self.vlan.host_set.values_list('ipv4', flat=True))
assert self.vlan.get_new_address()['ipv4'] not in used_v4 assert self.vlan.get_new_address()['ipv4'] not in used_v4
...@@ -114,7 +123,7 @@ class HostGetHostnameTestCase(MockCeleryMixin, TestCase): ...@@ -114,7 +123,7 @@ class HostGetHostnameTestCase(MockCeleryMixin, TestCase):
self.d = Domain(name='example.org', owner=self.u1) self.d = Domain(name='example.org', owner=self.u1)
self.d.save() self.d.save()
Record.objects.all().delete() Record.objects.all().delete()
self.vlan = Vlan(vid=1, name='test', network4='10.0.0.0/24', self.vlan = Vlan(vid=1, name='test', network4='10.0.0.1/24',
network6='2001:738:2001:4031::/80', domain=self.d, network6='2001:738:2001:4031::/80', domain=self.d,
owner=self.u1, network_type='portforward', owner=self.u1, network_type='portforward',
snat_ip='10.1.1.1') snat_ip='10.1.1.1')
...@@ -194,13 +203,13 @@ class ReloadTestCase(MockCeleryMixin, TestCase): ...@@ -194,13 +203,13 @@ class ReloadTestCase(MockCeleryMixin, TestCase):
self.u1 = User.objects.create(username='user1') self.u1 = User.objects.create(username='user1')
self.u1.save() self.u1.save()
d = Domain.objects.create(name='example.org', owner=self.u1) d = Domain.objects.create(name='example.org', owner=self.u1)
self.vlan = Vlan(vid=1, name='test', network4='10.0.0.0/29', self.vlan = Vlan(vid=1, name='test', network4='10.0.0.1/29',
snat_ip='152.66.243.99', snat_ip='152.66.243.99',
network6='2001:738:2001:4031::/80', domain=d, network6='2001:738:2001:4031::/80', domain=d,
owner=self.u1, network_type='portforward', owner=self.u1, network_type='portforward',
dhcp_pool='manual') dhcp_pool='manual')
self.vlan.save() self.vlan.save()
self.vlan2 = Vlan(vid=2, name='pub', network4='10.1.0.0/29', self.vlan2 = Vlan(vid=2, name='pub', network4='10.1.0.1/29',
network6='2001:738:2001:4032::/80', domain=d, network6='2001:738:2001:4032::/80', domain=d,
owner=self.u1, network_type='public') owner=self.u1, network_type='public')
self.vlan2.save() self.vlan2.save()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment