From 2ff81c4b52089c5f7bbb23b2430da21fed6f4258 Mon Sep 17 00:00:00 2001 From: Jeff Kala Date: Mon, 22 Jan 2024 23:48:16 -0600 Subject: [PATCH] create failing test for #704 --- .../test_config_compliance.py | 43 +++++++++++++++++-- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/nautobot_golden_config/tests/test_nornir_plays/test_config_compliance.py b/nautobot_golden_config/tests/test_nornir_plays/test_config_compliance.py index d6125395..4182f17f 100644 --- a/nautobot_golden_config/tests/test_nornir_plays/test_config_compliance.py +++ b/nautobot_golden_config/tests/test_nornir_plays/test_config_compliance.py @@ -1,13 +1,15 @@ """Unit tests for nautobot_golden_config nornir compliance.""" -import unittest import json -from unittest.mock import patch, Mock, MagicMock +import unittest +from unittest.mock import MagicMock, Mock, patch + from nautobot_golden_config.choices import ComplianceRuleConfigTypeChoice -from nautobot_golden_config.nornir_plays.config_compliance import get_rules, get_config_element +from nautobot_golden_config.nornir_plays.config_compliance import get_config_element, get_rules +from nautobot_golden_config.tests.conftest import create_device, create_feature_rule_cli -class ConfigComplianceTest(unittest.TestCase): +class ConfigComplianceJsonTest(unittest.TestCase): """Test Nornir Compliance Task.""" @patch("nautobot_golden_config.nornir_plays.config_compliance.ComplianceRule", autospec=True) @@ -47,3 +49,36 @@ def test_get_config_element_match_config_absent(self): mock_rule["obj"].config_type = ComplianceRuleConfigTypeChoice.TYPE_JSON return_config = json.dumps(get_config_element(mock_rule, mock_config, mock_obj, None)) self.assertEqual(return_config, mock_config) + + +class ConfigComplianceCliTest(unittest.TestCase): + """Test Nornir Compliance Task.""" + + def setUp(self): + """Setup test.""" + + self.device = create_device(name="config_plan_utility_test") + self.feature_rule = create_feature_rule_cli(self.device) + self.feature_rule.match_config = "aaa\nsnmp\n" + self.feature_rule.save() + + def test_get_config_element_match_config_present(self): + """Test proper return when Config CLI is returned with match_config""" + mock_config = "router bgp 123\naaa 123\n with a child line" + features = {'ordered': False, 'obj': self.feature_rule, 'section': ['aaa', 'snmp']} + return_config = get_config_element(features, mock_config, self.device, None) + self.assertEqual(return_config, "aaa 123\n with a child line") + + def test_get_config_element_match_nonduplicate_line_broken(self): + """Test proper return when Config CLI is returned with match_config""" + mock_config = "aaa 123\n with a child line\naccess-list 93 remark abcd\naccess-list 93 deny any any\n" + features = {'ordered': False, 'obj': self.feature_rule, 'section': ['aaa', 'snmp']} + return_config = get_config_element(features, mock_config, self.device, None) + self.assertEqual(return_config, "aaa 123\n with a child line") + + def test_get_config_element_match_duplicate_line_broken(self): + """Test proper return when Config CLI is returned with match_config""" + mock_config = "aaa 123\n with a child line\naccess-list 93 remark abcd\naccess-list 93 deny any any\naccess-list 93 remark abcd\n" + features = {'ordered': False, 'obj': self.feature_rule, 'section': ['aaa', 'snmp']} + return_config = get_config_element(features, mock_config, self.device, None) + self.assertEqual(return_config, "aaa 123\n with a child line")