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

Add ordering after data normalization. #112

Merged
merged 2 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion jdiff/extract_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ def extract_data_from_json(data: Union[Mapping, List], path: str = "*", exclude:
else:
raise ValueError("Reference Key normalization failure. Please verify data type returned.")

return keys_values_zipper(list_of_reference_keys, paired_key_value)
normalized = keys_values_zipper(list_of_reference_keys, paired_key_value)
# Data between pre and post may come in different order, so it needs to be sorted.
return sorted(normalized, key=lambda arg: list(arg.keys()))

return values
20 changes: 10 additions & 10 deletions tests/mock/napalm_get_lldp_neighbors/pre.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
{
"Ethernet2": [
"Ethernet1": [
{
"hostname": "junos-unittest",
"port": "520"
"port": "519"
},
{
"hostname": "ios-xrv-unittest",
"port": "Gi0/0/0/0"
}
],
"Ethernet3": [
"Ethernet2": [
{
"hostname": "junos-unittest",
"port": "522"
"port": "520"
}
],
"Ethernet1": [
"Ethernet3": [
{
"hostname": "junos-unittest",
"port": "519"
},
{
"hostname": "ios-xrv-unittest",
"port": "Gi0/0/0/0"
"port": "522"
}
],
"Management1": [
Expand Down
26 changes: 13 additions & 13 deletions tests/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@
"result[0].vrfs.default.peerList[*].[$peerAddress$,peerGroup,vrf,state]",
(
[
{
"7.7.7.7": {
"peerGroup": "EVPN-OVERLAY-SPINE",
"state": "Idle",
"vrf": "default",
}
},
{
"10.1.0.0": {
"peerGroup": "IPv4-UNDERLAY-SPINE",
Expand All @@ -38,6 +31,13 @@
"vrf": "default",
}
},
{
"7.7.7.7": {
"peerGroup": "EVPN-OVERLAY-SPINE",
"state": "Idle",
"vrf": "default",
}
},
],
False,
),
Expand Down Expand Up @@ -124,10 +124,10 @@
"result[0].vrfs.default.peerList[*].[$peerAddress$,prefixesSent]",
(
[
{"7.7.7.7": {"prefixesSent": 50}},
{"10.1.0.0": {"prefixesSent": 50}},
{"10.2.0.0": {"prefixesSent": 50}},
{"10.64.207.255": {"prefixesSent": 50}},
{"7.7.7.7": {"prefixesSent": 50}},
],
False,
),
Expand All @@ -146,10 +146,10 @@
"result[0].vrfs.default.peerList[*].[$peerAddress$,prefixesSent]",
(
[
{"7.7.7.7": {"prefixesSent": 50}},
{"10.1.0.0": {"prefixesSent": 50}},
{"10.2.0.0": {"prefixesSent": 50}},
{"10.64.207.255": {"prefixesSent": 50}},
{"7.7.7.7": {"prefixesSent": 50}},
],
False,
),
Expand Down Expand Up @@ -191,22 +191,22 @@ def test_operator(filename, check_type_str, evaluate_args, path, expected_result
[
(
[
{"7.7.7.7": {"peerGroup": "EVPN-OVERLAY-SPINE", "vrf": "default", "state": "Connected"}},
{"10.1.0.0": {"peerGroup": "EVPN-OVERLAY-SPINE", "vrf": "default", "state": "Connected"}},
{"7.7.7.7": {"peerGroup": "EVPN-OVERLAY-SPINE", "vrf": "default", "state": "Connected"}},
],
True,
([], True),
),
(
[
{"7.7.7.7": {"peerGroup": "EVPN-OVERLAY-SPINE", "vrf": "default", "state": "Connected"}},
{"10.1.0.0": {"peerGroup": "IPv4-UNDERLAY-SPINE", "vrf": "default", "state": "Connected"}},
{"7.7.7.7": {"peerGroup": "EVPN-OVERLAY-SPINE", "vrf": "default", "state": "Connected"}},
],
True,
(
[
{"7.7.7.7": {"peerGroup": "EVPN-OVERLAY-SPINE", "vrf": "default", "state": "Connected"}},
{"10.1.0.0": {"peerGroup": "IPv4-UNDERLAY-SPINE", "vrf": "default", "state": "Connected"}},
{"7.7.7.7": {"peerGroup": "EVPN-OVERLAY-SPINE", "vrf": "default", "state": "Connected"}},
],
False,
),
Expand All @@ -227,8 +227,8 @@ def test_operator(filename, check_type_str, evaluate_args, path, expected_result
),
(
[
{"7.7.7.7": {"peerGroup": "EVPN-OVERLAY-SPINE", "vrf": "default", "state": "Connected"}},
{"10.1.0.0": {"peerGroup": "IPv4-UNDERLAY-SPINE", "vrf": "default", "state": "Connected"}},
{"7.7.7.7": {"peerGroup": "EVPN-OVERLAY-SPINE", "vrf": "default", "state": "Connected"}},
],
False,
([], True),
Expand Down
30 changes: 28 additions & 2 deletions tests/test_sw_upgrade_device_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,36 @@ def test_lldp_neighbor_state(platform, command, check_should_pass):
"""Test LLDP neighbors with exact match."""
command_pre = load_json_file("sw_upgrade", f"{platform}_{command}.json")
command_post = deepcopy(command_pre)
jpath = "[*].[neighbor,capabilities,$local_interface$,neighbor_interface]"
command_pre_normalized = extract_data_from_json(command_pre, jpath)
command_post_normalized = extract_data_from_json(command_post, jpath)

if check_should_pass is False:
command_post = command_post[:2]
command_post_normalized = command_post_normalized[:2]

check = CheckType.create("exact_match")
eval_results, passed = check.evaluate(command_post, command_pre)
eval_results, passed = check.evaluate(command_pre_normalized, command_post_normalized)
assert passed is check_should_pass, f"FAILED, eval_result: {eval_results}"


@pytest.mark.parametrize(
"platform, command",
[
("arista_eos", "show_lldp_neighbors"),
("cisco_ios", "show_lldp_neighbors"),
("cisco_nxos", "show_lldp_neighbors"),
],
)
def test_lldp_neighbor_state_changed_order(platform, command):
"""Test LLDP neighbors with exact match, when post-check is in reversed order."""
command_pre = load_json_file("sw_upgrade", f"{platform}_{command}.json")
command_post = deepcopy(command_pre)
command_post.reverse() # reverse data for the test

jpath = "[*].[neighbor,capabilities,$local_interface$,neighbor_interface]"
command_pre_normalized = extract_data_from_json(command_pre, jpath)
command_post_normalized = extract_data_from_json(command_post, jpath)

check = CheckType.create("exact_match")
eval_results, passed = check.evaluate(command_pre_normalized, command_post_normalized)
assert passed is True, f"FAILED, eval_result: {eval_results}"