Skip to content

Commit

Permalink
Fixed issue where compliance amongst other fields were not being upda…
Browse files Browse the repository at this point in the history
…ted when Django 4.2 was installed.
  • Loading branch information
itdependsnetworks committed Aug 31, 2024
1 parent 237ddd4 commit 8e6ed07
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/admin/release_notes/version_2.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
### Fixed

- [#792](https://github.com/nautobot/nautobot-app-golden-config/issues/792) - Fixed issue with dynamic groups not being called in 2.3.0.
- [#800](https://github.com/nautobot/nautobot-app-golden-config/issues/800) - Fixed issue where compliance amongst other fields were not being updated when Django 4.2 was installed.

### Changed

Expand Down
2 changes: 1 addition & 1 deletion nautobot_golden_config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class GoldenConfig(NautobotAppConfig):
"get_custom_compliance": None,
# This is an experimental and undocumented setting that will change in the future!!
# Use at your own risk!!!!!
"_manual_dynamic_group_mgmt": False,
"_manual_dynamic_group_mgmt": False,
"jinja_env": {
"undefined": "jinja2.StrictUndefined",
"trim_blocks": True,
Expand Down
7 changes: 7 additions & 0 deletions nautobot_golden_config/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,13 @@ def save(self, *args, **kwargs):
self.remediation_on_save()
self.full_clean()

# This accounts for django 4.2 `Setting update_fields in Model.save() may now be required` change
# in behavior
if kwargs.get("update_fields"):
kwargs["update_fields"].update(
{"compliance", "compliance_int", "ordered", "missing", "extra", "remediation"}
)

super().save(*args, **kwargs)


Expand Down
53 changes: 53 additions & 0 deletions nautobot_golden_config/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from .conftest import (
create_config_compliance,
create_device,
create_feature_rule_cli_with_remediation,
create_feature_rule_json,
create_feature_rule_xml,
create_job_result,
Expand All @@ -36,6 +37,7 @@ def setUp(self):
self.device = create_device()
self.compliance_rule_json = create_feature_rule_json(self.device)
self.compliance_rule_xml = create_feature_rule_xml(self.device)
self.compliance_rule_cli = create_feature_rule_cli_with_remediation(self.device)

def test_create_config_compliance_success_json(self):
"""Successful."""
Expand Down Expand Up @@ -119,6 +121,57 @@ def test_config_compliance_signal_change_platform(self):
)
self.assertEqual(ConfigCompliance.objects.filter(device=self.device).count(), 1)

def test_update_or_create(self):
"""We test this to ensure regression against
https://docs.djangoproject.com/en/5.1/releases/4.2/#setting-update-fields-in-model-save-may-now-be-required."""

RemediationSetting.objects.create(
platform=self.device.platform,
remediation_type=RemediationTypeChoice.TYPE_HIERCONFIG,
)

cc_obj, _ = ConfigCompliance.objects.update_or_create(
device=self.device,
rule=self.compliance_rule_cli,
defaults={
"actual": "ntp 1.1.1.1\nntp 2.2.2.2",
"intended": "ntp 1.1.1.1\nntp 3.3.3.3",
},
)

self.assertFalse(cc_obj.compliance)
self.assertFalse(cc_obj.compliance_int)
self.assertEqual(cc_obj.missing, "ntp 3.3.3.3")
self.assertEqual(cc_obj.extra, "ntp 2.2.2.2")
self.assertEqual(cc_obj.remediation, "no ntp 2.2.2.2\nntp 3.3.3.3")

# We run again to ensure this works, the issue actually only shows on
# when `update_fields` is set
cc_obj_2, _ = ConfigCompliance.objects.update_or_create(
device=self.device,
rule=self.compliance_rule_cli,
defaults={
"actual": "ntp 1.1.1.1\nntp 2.2.2.2",
"intended": "ntp 1.1.1.1\nntp 2.2.2.2",
},
)

self.assertTrue(cc_obj_2.compliance)
self.assertTrue(cc_obj_2.compliance_int)
self.assertEqual(cc_obj_2.missing, "")
self.assertEqual(cc_obj_2.extra, "")
self.assertEqual(cc_obj_2.remediation, "")

cc_obj_3 = ConfigCompliance.objects.get(device=self.device, rule=self.compliance_rule_cli)
cc_obj_3.intended = "ntp 1.1.1.1\nntp 3.3.3.3"
cc_obj_3.save()

self.assertFalse(cc_obj.compliance)
self.assertFalse(cc_obj.compliance_int)
self.assertEqual(cc_obj.missing, "ntp 3.3.3.3")
self.assertEqual(cc_obj.extra, "ntp 2.2.2.2")
self.assertEqual(cc_obj.remediation, "no ntp 2.2.2.2\nntp 3.3.3.3")


class GoldenConfigTestCase(TestCase):
"""Test GoldenConfig Model."""
Expand Down

0 comments on commit 8e6ed07

Please sign in to comment.