Skip to content

Commit

Permalink
Bugfix: Used&reserved addresses are counted twice
Browse files Browse the repository at this point in the history
When reserved addresses are in use, unused_count would subtract them
twice.
Fixes #440.
  • Loading branch information
oyvindhagberg authored Jun 3, 2021
1 parent cfd319c commit 498f510
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
3 changes: 3 additions & 0 deletions mreg/api/v1/tests/test_networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,9 @@ def test_reject_ip_usage(self):
def test_count_unused_ips(self):
"""Test that excluded ipaddresses are exempted from the unused count"""
self.test_create()
# Create a host that uses one of the reserved addresses. (Should not be counted twice)
host1 = Host.objects.create(name='host1.example.org')
Ipaddress.objects.create(host=host1, ipaddress='10.0.0.2')
ret = self.assert_get(f'/api/v1/networks/{self.network4}/unused_count').json()
# /24 - net/broadcast - reserved - [10, 200] - 201
# 256 - 2 - 3 - 191 - 1 = 59
Expand Down
7 changes: 3 additions & 4 deletions mreg/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,10 +609,9 @@ def unused_count(self):
# subtract excluded ranges
for i in self.excluded_ranges.all():
result -= i.num_addresses()
# subtract reserved addresses
result -= len(self.get_reserved_ipaddresses())
# subtract used addresses
result -= len(self.used_addresses)
# subtract used and reserved addresses
used_or_reserved = self.used_addresses | self.get_reserved_ipaddresses()
result -= len(used_or_reserved)
return result

def get_first_unused(self):
Expand Down

0 comments on commit 498f510

Please sign in to comment.