Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Excluded child subnets from overlapping validation #87 #100

Merged
merged 2 commits into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:

- name: Install python dependencies
run: |
pip install -U "pip==20.2.4" wheel setuptools
pip install -U pip wheel setuptools
pip install ${{ matrix.django-version }}
pip install -U -r requirements-test.txt

Expand Down
9 changes: 9 additions & 0 deletions openwisp_ipam/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ def _validate_overlapping_subnets(self):
while parent_subnet:
exclude.append(parent_subnet.pk)
parent_subnet = parent_subnet.master_subnet
# exclude child subnets
child_subnets = list(self.child_subnet_set.values_list('pk', flat=True))
while child_subnets:
exclude += child_subnets
child_subnets = list(
self._meta.model.objects.filter(
master_subnet__in=child_subnets
).values_list('pk', flat=True)
)
# exclude also identical subnets (handled by other checks)
qs = qs.exclude(pk__in=exclude).exclude(subnet=self.subnet)
for subnet in qs.iterator():
Expand Down
25 changes: 25 additions & 0 deletions openwisp_ipam/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,3 +397,28 @@ def test_nested_overlapping_subnets(self):
subnet='10.0.1.0/28', master_subnet=shared_level_1_subnet
)
self._create_subnet(subnet='10.0.1.0/31', master_subnet=org1_level_2_subnet)

def test_validation_nested_child_subnets(self):
# Tests child subnets are excluded from overlapping validation
master_subnet = self._create_subnet(subnet='10.0.0.0/16')

# Level 1 nesting
a_level_1 = self._create_subnet(
subnet='10.0.1.0/24', master_subnet=master_subnet
)
b_level_1 = self._create_subnet(
subnet='10.0.2.0/24', master_subnet=master_subnet
)

# Level 2 nesting
a_level_2 = self._create_subnet(subnet='10.0.1.8/29', master_subnet=a_level_1)
self._create_subnet(subnet='10.0.1.16/29', master_subnet=a_level_1)
b_level_2 = self._create_subnet(subnet='10.0.2.8/29', master_subnet=b_level_1)
self._create_subnet(subnet='10.0.2.16/29', master_subnet=b_level_1)

# Level 3 nesting
self._create_subnet(subnet='10.0.1.8/31', master_subnet=a_level_2)
self._create_subnet(subnet='10.0.2.8/31', master_subnet=b_level_2)

master_subnet.full_clean()
master_subnet.save()
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# TODO: change this when next version of openwisp_users is released
openwisp-users @ https://github.com/openwisp/openwisp-users/tarball/master
openwisp-utils[rest]~=0.7.2
# TODO: change this when next version of openwisp_utils is released
openwisp-utils[rest] @ https://github.com/openwisp/openwisp-utils/tarball/master
django>=3.0,<3.2
swapper~=1.1
xlrd~=1.1
Expand Down