Skip to content

Commit

Permalink
support removal of compliance features, add a test for this table
Browse files Browse the repository at this point in the history
  • Loading branch information
gsnider2195 committed Jan 10, 2025
1 parent de1838f commit a31f588
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
11 changes: 10 additions & 1 deletion nautobot_golden_config/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class ConfigComplianceTable(BaseTable):

pk = ToggleColumn(accessor=A("device"))
device = TemplateColumn(
template_code="""<a href="{% url 'plugins:nautobot_golden_config:configcompliance_devicetab' pk=record.device %}?tab=nautobot_golden_config:1" <strong>{{ record.device__name }}</strong></a> """
template_code="""<a href="{% url 'plugins:nautobot_golden_config:configcompliance_devicetab' pk=record.device %}?tab=nautobot_golden_config:1"><strong>{{ record.device__name }}</strong></a> """
)

def __init__(self, *args, **kwargs):
Expand All @@ -170,8 +170,17 @@ def __init__(self, *args, **kwargs):
)
# Nautobot's BaseTable.configurable_columns() only recognizes columns in self.base_columns,
# so override the class's base_columns to include our additional columns as configurable.
# Note: The correct way to modify django_tables2 columns at init is to use the extra_columns kwarg but Nautobot doesn't support that.
for feature in features:
self.base_columns[feature] = ComplianceColumn(verbose_name=feature) # pylint: disable=no-member
compliance_columns = [
column_name
for column_name, column in self.base_columns.items() # pylint: disable=no-member
if isinstance(column, ComplianceColumn)
]
removed_features = set(compliance_columns) - set(features)
for column_name in removed_features:
del self.base_columns[column_name] # pylint: disable=no-member
super().__init__(*args, **kwargs)

class Meta(BaseTable.Meta):
Expand Down
40 changes: 40 additions & 0 deletions nautobot_golden_config/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Unit tests for nautobot_golden_config views."""

import datetime
import re
from unittest import mock, skip

import nautobot
Expand Down Expand Up @@ -393,3 +394,42 @@ def test_alter_queryset(self):
self.assertSequenceEqual(list(device.keys()), ["device", "device__name", *features])
for feature in features:
self.assertIn(device[feature], [0, 1])

def test_table_columns(self):
"""Test the columns of the ConfigCompliance table return the expected pivoted data."""
response = self.client.get(reverse("plugins:nautobot_golden_config:configcompliance_list"))
expected_table_headers = ["Device", "TestFeature0", "TestFeature1", "TestFeature2", "TestFeature3"]
table_headers = re.findall(r'<th class="orderable"><a href=.*>(.+)</a></th>', response.content.decode())
self.assertEqual(table_headers, expected_table_headers)

# Add a new compliance feature and ensure the table headers update correctly
device2 = Device.objects.get(name="Device 2")
new_compliance_feature = create_feature_rule_json(device2, feature="NewTestFeature")
models.ConfigCompliance.objects.create(
device=device2,
rule=new_compliance_feature,
actual={"foo": {"bar-1": "baz"}},
intended={"foo": {"bar-1": "baz"}},
compliance=True,
compliance_int=1,
)

response = self.client.get(reverse("plugins:nautobot_golden_config:configcompliance_list"))
expected_table_headers = [
"Device",
"TestFeature0",
"TestFeature1",
"TestFeature2",
"TestFeature3",
"NewTestFeature",
]
table_headers = re.findall(r'<th class="orderable"><a href=.*>(.+)</a></th>', response.content.decode())
self.assertEqual(table_headers, expected_table_headers)

# Remove compliance features and ensure the table headers update correctly
models.ConfigCompliance.objects.filter(rule__feature__name__in=["TestFeature0", "TestFeature1"]).delete()

response = self.client.get(reverse("plugins:nautobot_golden_config:configcompliance_list"))
expected_table_headers = ["Device", "TestFeature2", "TestFeature3", "NewTestFeature"]
table_headers = re.findall(r'<th class="orderable"><a href=.*>(.+)</a></th>', response.content.decode())
self.assertEqual(table_headers, expected_table_headers)

0 comments on commit a31f588

Please sign in to comment.