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

Test bad parsing of duplicate line #540

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
9 changes: 8 additions & 1 deletion netutils/config/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,14 @@ def _update_same_line_children_configs(self) -> None:
new_config_lines: t.List[ConfigLine] = []
for line in self.config_lines:
if line in self.same_line_children:
previous_line = new_config_lines[-1]
try:
previous_line = new_config_lines[-1]
except IndexError as error:
raise IndexError(
f"This error is likely from a duplicate line detected at the line `{line.config_line}`, "
"see https://netutils.readthedocs.io/en/latest/dev/dev_config/#duplicate-line-detection "
f"for more details.\nOriginal Error: {error}"
)
previous_config_line = previous_line.config_line
current_parents = previous_line.parents + (previous_config_line,)
line = ConfigLine(line.config_line, current_parents)
Expand Down
49 changes: 49 additions & 0 deletions tests/unit/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,44 @@ def test_find_children_w_parents(
assert "\n".join(os_parser(device_cfg).find_children_w_parents(**kwargs)) == received_data


def test_bad_parsing_of_duplicate_line():
"""This is a test to show an issue, this should raise and Index error, but the issue is the
Parents calculation is off, here is an example of what it returns:
[

ConfigLine(config_line='logging host one.two.three.one', parents=()),
ConfigLine(config_line='logging host one.two.three.two', parents=('logging host one.two.three.one',)),
ConfigLine(config_line='logging host one.two.three.two', parents=('logging host one.two.three.one', 'logging host one.two.three.two')),
ConfigLine(config_line='logging host one.two.three.four', parents=())
]
We see that the parents of `parents=('logging host one.two.three.one',` shows up, which is wrong.
The key behaviour I have seen, is when there is 3 dots e.g. `.` in the config, so `logging host 10.1.1` would work
but not `logging host 10.1.1.1`.
When this tests pass, everything should be good, but the underlying code to be checked in is `_update_same_line_children_configs` method.
."""
logging = (
"!\n"
"logging host one.two.three.one\n"
"logging host one.two.three.two\n"
"logging host one.two.three.two\n"
"logging host one.two.three.four\n"
)
with pytest.raises(IndexError, match=r".*This error is from likely a duplicate line detected.*"):
compliance.parser_map["cisco_ios"](logging).config_lines # pylint: disable=expression-not-assigned
logging = (
"!\n"
"logging host 10.1.1.1\n"
"logging host 10.1.1.2\n"
"logging host 10.1.1.2\n"
"logging host 10.1.1.4\n"
"!\n"
"!\n"
"!\n"
)
with pytest.raises(IndexError, match=r".*This error is from likely a duplicate line detected.*"):
compliance.parser_map["cisco_ios"](logging).config_lines # pylint: disable=expression-not-assigned


def test_incorrect_banner_ios():
banner_cfg = (
"aaa new-model\n"
Expand All @@ -68,3 +106,14 @@ def test_incorrect_banner_ios():
)
with pytest.raises(ValueError):
compliance.parser_map["cisco_ios"](banner_cfg).config_lines # pylint: disable=expression-not-assigned


def test_duplicate_line():
logging = (
"!\n"
"snmp-server community <<REPLACED>> RO SNMP_ACL_RO\n"
"snmp-server community <<REPLACED>> RO SNMP_ACL_RO\n"
"snmp-server community <<REPLACED>> RW SNMP_ACL_RW\n"
)
with pytest.raises(IndexError, match=r".*This error is likely from a duplicate line detected.*"):
compliance.parser_map["cisco_ios"](logging).config_lines # pylint: disable=expression-not-assigned
Loading