Skip to content
Closed
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
6 changes: 3 additions & 3 deletions openwisp_controller/subnet_division/rule_types/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def create_subnets(config, division_rule, max_subnet, generated_indexes):
generated_indexes.append(
SubnetDivisionIndex(
keyword=f"{division_rule.label}_subnet{subnet_id}",
subnet_id=subnet_obj.id,
subnet=subnet_obj,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this different?

rule_id=division_rule.id,
config=config,
)
Expand Down Expand Up @@ -294,8 +294,8 @@ def create_ips(config, division_rule, generated_subnets, generated_indexes):
generated_indexes.append(
SubnetDivisionIndex(
keyword=f"{subnet_obj.name}_ip{keyword_index}",
subnet_id=subnet_obj.id,
ip_id=ip_obj.id,
subnet=subnet_obj,
ip=ip_obj,
Comment on lines +297 to +298

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, how is this different?

rule_id=division_rule.id,
config=config,
)
Expand Down
2 changes: 1 addition & 1 deletion openwisp_controller/subnet_division/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def _create_ipaddress_and_subnetdivision_index_objects(ips, indexes):
SubnetDivisionIndex(
keyword=f"{division_rule.label}_subnet{subnet.id}_ip{ip_id}",
subnet_id=subnet.id,
ip_id=ip.id,
ip=ip,
rule_id=division_rule.id,
config_id=index.config_id,
)
Expand Down
42 changes: 42 additions & 0 deletions openwisp_controller/subnet_division/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,48 @@ def test_invalid_master_subnet(self):
e.message_dict.get("master_subnet", []),
)

def test_subnet_division_index_integrity(self):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you check that this test fails on the current master?

If yes, can you point out in the [issue comments[(https://github.com//issues/1435#issuecomment-4957428251) why the existing test cases are unable to detect the bug you reported?

rule = self._get_vpn_subdivision_rule()
self.config.templates.add(self.template)

# Verify initial indexes have non-NULL subnet_id and ip_id
# (if they are IP indexes)
indexes = rule.subnetdivisionindex_set.all()
self.assertTrue(indexes.exists())
initial_count = indexes.count()
for index in indexes:
self.assertIsNotNone(
index.subnet_id,
f"SubnetDivisionIndex {index.keyword} has NULL subnet_id",
)
if "_ip" in index.keyword:
self.assertIsNotNone(
index.ip_id,
f"SubnetDivisionIndex {index.keyword} has NULL ip_id",
)

# Update number of IPs to trigger tasks.provision_extra_ips
rule.number_of_ips = rule.number_of_ips + 2
rule.save()
rule.refresh_from_db()

# Verify count increased and all indexes, including new ones,
# have non-NULL subnet_id and ip_id
new_indexes = rule.subnetdivisionindex_set.all()
self.assertEqual(
new_indexes.count(), initial_count + (rule.number_of_subnets * 2)
)
for index in new_indexes:
self.assertIsNotNone(
index.subnet_id,
f"SubnetDivisionIndex {index.keyword} has NULL subnet_id after update",
)
if "_ip" in index.keyword:
self.assertIsNotNone(
index.ip_id,
f"SubnetDivisionIndex {index.keyword} has NULL ip_id after update",
)

Comment thread
coderabbitai[bot] marked this conversation as resolved.
Comment thread
coderabbitai[bot] marked this conversation as resolved.

class TestOpenVPNSubnetDivisionRule(
SubnetDivisionTestMixin,
Expand Down
Loading