Skip to content

Commit

Permalink
Fix(eos_validate_state): ANTA Decrease default logging level for tests (
Browse files Browse the repository at this point in the history
  • Loading branch information
carl-baillargeon authored Jan 10, 2024
1 parent c7a9105 commit 49f2938
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,14 @@ def get_interface_ip(self, interface_model: str, interface_name: str, host: str
self.log_skip_message(message=f"Host '{host or self.device_name}' interface '{interface_name}' IP address is unavailable.", logging_level="WARNING")
return None

def logged_get(self, key: str, host: str | None = None, logging_level: str = "WARNING"):
def logged_get(self, key: str, host: str | None = None, logging_level: str = "INFO"):
"""
Attempts to retrieve a value associated with a given key from structured_config and logs if it's missing.
Args:
key (str): The key to retrieve.
host (str | None): The host from which to retrieve the key. Defaults to the device running the test.
logging_level (str): The logging level to use for the log message.
logging_level (str): The logging level to use for the log message. Defaults to "INFO".
"""
host_struct_cfg = self.get_host_structured_config(host=host) if host else self.structured_config
try:
Expand All @@ -185,7 +185,7 @@ def validate_data(
data_path: str | None = None,
host: str | None = None,
required_keys: str | list[str] | None = None,
logging_level: str | None = None,
logging_level: str = "INFO",
**kwargs,
) -> bool:
"""
Expand All @@ -196,8 +196,7 @@ def validate_data(
data_path (str | None): The data path in dot notation. Used for logging purposes. Index or primary key can be used for lists.
host (str | None): The host from which data should be retrieved. Defaults to the device running the test.
required_keys (str | list[str] | None): The keys that are expected to be in the data.
logging_level (str): Overwrites all default logging levels within this function.
If not provided, the default logging level is 'WARNING' when a key is missing and 'INFO' when his value is not matching.
logging_level (str): The logging level to use for the log message. Defaults to "INFO".
**kwargs: Expected key-value pairs in the data.
Returns:
Expand All @@ -216,10 +215,10 @@ def validate_data(
for key, value in kwargs.items():
actual_value = get(data, key)
if actual_value is None:
self.log_skip_message(key=key, value=value, key_path=data_path, is_missing=True, logging_level=logging_level or "WARNING")
self.log_skip_message(key=key, value=value, key_path=data_path, is_missing=True, logging_level=logging_level)
valid = False
elif actual_value != value:
self.log_skip_message(key=key, value=value, key_path=data_path, is_missing=False, logging_level=logging_level or "INFO")
self.log_skip_message(key=key, value=value, key_path=data_path, is_missing=False, logging_level=logging_level)
valid = False

# Return False if any of the expected values are missing or not matching
Expand All @@ -231,7 +230,7 @@ def validate_data(
required_keys = [required_keys] if isinstance(required_keys, str) else required_keys
for key in required_keys:
if get(data, key) is None:
self.log_skip_message(key=key, key_path=data_path, is_missing=True, logging_level=logging_level or "WARNING")
self.log_skip_message(key=key, key_path=data_path, is_missing=True, logging_level=logging_level)
valid = False
return valid

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_definition(self) -> dict | None:
"""
anta_tests = []

if (ethernet_interfaces := self.logged_get(key="ethernet_interfaces")) is None:
if (ethernet_interfaces := self.logged_get(key="ethernet_interfaces", logging_level="WARNING")) is None:
return None

required_keys = ["name", "peer", "peer_interface", "ip_address"]
Expand Down Expand Up @@ -81,7 +81,7 @@ def test_definition(self) -> dict | None:
"""
anta_tests = []

if (management_interfaces := self.logged_get(key="management_interfaces")) is None:
if (management_interfaces := self.logged_get(key="management_interfaces", logging_level="WARNING")) is None:
return None

for idx, interface in enumerate(management_interfaces):
Expand Down Expand Up @@ -169,7 +169,7 @@ def test_definition(self) -> dict | None:
"""
anta_tests = []

if (ethernet_interfaces := self.logged_get(key="ethernet_interfaces")) is None:
if (ethernet_interfaces := self.logged_get(key="ethernet_interfaces", logging_level="WARNING")) is None:
return None

required_keys = ["name", "peer", "peer_interface"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def generate_test_details(interface: dict, description_template: str) -> tuple(s
return "adminDown", "down", description_template.format(state="adminDown")
return "up", "up", description_template.format(state="up")

required_keys = ["name", "shutdown", "description"]
required_keys = ["name", "shutdown"]

for interface_key, description_template in self.interface_types:
interfaces = get(self.structured_config, interface_key, [])
Expand All @@ -78,7 +78,8 @@ def generate_test_details(interface: dict, description_template: str) -> tuple(s
if not self.validate_data(data=interface, data_path=f"{interface_key}.[{idx}]", required_keys=required_keys):
continue
state, proto, description = generate_test_details(interface, description_template)
custom_field = f"{interface['name']} - {interface['description']}"
intf_description = interface.get("description")
custom_field = interface["name"] if not intf_description else f"{interface['name']} - {intf_description}"

add_test(str(interface["name"]), state, proto, description, custom_field)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_definition(self) -> dict:
Returns:
test_definition (dict): ANTA test definition.
"""
if self.logged_get(key="mlag_configuration", logging_level="INFO") is None:
if self.logged_get(key="mlag_configuration") is None:
return None

anta_tests = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ def add_test(description: str, afi: str, bgp_neighbor_ip: str, safi: str = None)
}
)

if self.logged_get(key="router_bgp", logging_level="INFO") is None or not self.validate_data(
service_routing_protocols_model="multi-agent", logging_level="WARNING"
):
if self.logged_get(key="router_bgp") is None or not self.validate_data(service_routing_protocols_model="multi-agent", logging_level="WARNING"):
return None

anta_tests.setdefault("generic", []).append(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_definition(self) -> dict | None:
test_definition (dict): ANTA test definition.
"""
anta_tests = []
if (profile := self.logged_get(key="management_api_http..https_ssl_profile", logging_level="INFO")) is None:
if (profile := self.logged_get(key="management_api_http..https_ssl_profile")) is None:
return None
anta_tests.append({"VerifyAPIHttpsSSL": {"profile": profile}})

Expand Down

0 comments on commit 49f2938

Please sign in to comment.