Assertion helpers

Utility assert functions for writing network tests (or policies).

All assert_* methods will raise an BatfishAssertException if the assertion fails.

pybatfish.client.asserts.assert_filter_denies(filters: str, headers: HeaderConstraints, startLocation: str | None = None, soft: bool = False, snapshot: str | None = None, session: Session | None = None, df_format: str = 'table') bool[source]

Check if a filter (e.g., ACL) denies a specified set of flows.

Parameters:
  • filters – the specification for the filter (filterSpec) to check

  • headersHeaderConstraints

  • startLocation – LocationSpec indicating where a flow starts

  • soft – whether this assertion is soft (i.e., generates a warning but not a failure)

  • snapshot – the snapshot on which to check the assertion

  • session – Batfish session to use for the assertion

  • df_format – How to format the Dataframe content in the output message. Valid options are ‘table’ and ‘records’ (each row is a key-value pairs).

Returns:

True if the assertion passes

pybatfish.client.asserts.assert_filter_has_no_unreachable_lines(filters: str, soft: bool = False, snapshot: str | None = None, session: Session | None = None, df_format: str = 'table') bool[source]

Check that a filter (e.g. an ACL) has no unreachable lines.

A filter line is considered unreachable if it will never match a packet, e.g., because its match condition is empty or covered completely by those of prior lines.”

Parameters:
  • filters – the specification for the filter (filterSpec) to check

  • soft – whether this assertion is soft (i.e., generates a warning but not a failure)

  • snapshot – the snapshot on which to check the assertion

  • session – Batfish session to use for the assertion

  • df_format – How to format the Dataframe content in the output message. Valid options are ‘table’ and ‘records’ (each row is a key-value pairs).

Returns:

True if the assertion passes

pybatfish.client.asserts.assert_filter_permits(filters: str, headers: HeaderConstraints, startLocation: str | None = None, soft: bool = False, snapshot: str | None = None, session: Session | None = None, df_format: str = 'table') bool[source]

Check if a filter (e.g., ACL) permits a specified set of flows.

Parameters:
  • filters – the specification for the filter (filterSpec) to check

  • headersHeaderConstraints

  • startLocation – LocationSpec indicating where a flow starts

  • soft – whether this assertion is soft (i.e., generates a warning but not a failure)

  • snapshot – the snapshot on which to check the assertion

  • session – Batfish session to use for the assertion

  • df_format – How to format the Dataframe content in the output message. Valid options are ‘table’ and ‘records’ (each row is a key-value pairs).

Returns:

True if the assertion passes

pybatfish.client.asserts.assert_flows_fail(startLocation: str, headers: HeaderConstraints, soft: bool = False, snapshot: str | None = None, session: Session | None = None, df_format: str = 'table') bool[source]

Check if the specified set of flows, denoted by starting locations and headers, fail.

Parameters:
  • startLocation – LocationSpec indicating where the flow starts

  • headersHeaderConstraints

  • soft – whether this assertion is soft (i.e., generates a warning but not a failure)

  • snapshot – the snapshot on which to check the assertion

  • session – Batfish session to use for the assertion

  • df_format – How to format the Dataframe content in the output message. Valid options are ‘table’ and ‘records’ (each row is a key-value pairs).

Returns:

True if the assertion passes

pybatfish.client.asserts.assert_flows_succeed(startLocation: str, headers: HeaderConstraints, soft: bool = False, snapshot: str | None = None, session: Session | None = None, df_format: str = 'table') bool[source]

Check if the specified set of flows, denoted by starting locations and headers, succeed.

Parameters:
  • startLocation – LocationSpec indicating where the flow starts

  • headersHeaderConstraints

  • soft – whether this assertion is soft (i.e., generates a warning but not a failure)

  • snapshot – the snapshot on which to check the assertion

  • session – Batfish session to use for the assertion

  • df_format – How to format the Dataframe content in the output message. Valid options are ‘table’ and ‘records’ (each row is a key-value pairs).

Returns:

True if the assertion passes

pybatfish.client.asserts.assert_has_no_route(routes: DataFrame | Dict[str, Dict[str, List[Dict[str, Any]]]], expected_route: Dict[str, Any], node: str, vrf: str = 'default', soft: bool = False) bool[source]

Assert that a particular route is NOT present.

Note

If a node or VRF is missing in the route answer the assertion will NOT fail, but a warning will be generated.

Parameters:
  • routes (Union[DataFrame, Dict[str, Dict[str, List[Dict[str, any]]]]]) – Routes returned by the Batfish routes or ribs questions, either as a Pandas DataFrame or a multilevel dictionary from hostname to VRF name to list of routes, where each route is a dictionary.

  • expected_route (Dict[str, any]) – A dictionary describing route to match.

  • node (str) – node hostname on which to look for a route.

  • vrf – VRF name where the route should be present. Default is default.

  • soft (bool) – whether this assertion is soft (i.e., generates a warning but not a failure)

pybatfish.client.asserts.assert_has_route(routes: DataFrame | Dict[str, Dict[str, List[Dict[str, Any]]]], expected_route: Dict[str, Any], node: str, vrf: str = 'default', soft: bool = False) bool[source]

Assert that a particular route is present.

Parameters:
  • routes (Union[DataFrame, Dict[str, Dict[str, List[Dict[str, any]]]]]) – Routes returned by the Batfish routes or ribs questions, either as a Pandas DataFrame or a multilevel dictionary from hostname to VRF name to list of routes, where each route is a dictionary.

  • expected_route (Dict[str, any]) – A dictionary describing route to match.

  • node (str) – node hostname on which to look for a route.

  • vrf – VRF name where the route should be present. Default is default.

  • soft (bool) – whether this assertion is soft (i.e., generates a warning but not a failure)

pybatfish.client.asserts.assert_no_duplicate_router_ids(snapshot: str | None = None, nodes: str | None = None, protocols: List[str] | None = None, soft: bool = False, session: Session | None = None, df_format: str = 'table', ignore_same_node: bool = False) bool[source]

Assert that there are no duplicate router IDs present in the snapshot.

Parameters:
  • snapshot – the snapshot on which to check the assertion

  • protocols – the protocols on which to run the assertion, currently BGP and OSPF are supported

  • soft – whether this assertion is soft (i.e., generates a warning but not a failure)

  • session – Batfish session to use for the assertion

  • df_format – How to format the Dataframe content in the output message. Valid options are ‘table’ and ‘records’ (each row is a key-value pairs).

  • ignore_same_node – whether to ignore duplicate router-ids on the same node

pybatfish.client.asserts.assert_no_forwarding_loops(snapshot: str | None = None, soft: bool = False, session: Session | None = None, df_format: str = 'table') bool[source]

Assert that there are no forwarding loops in the snapshot.

Parameters:
  • snapshot – the snapshot on which to check the assertion

  • soft – whether this assertion is soft (i.e., generates a warning but not a failure)

  • session – Batfish session to use for the assertion

  • df_format – How to format the Dataframe content in the output message. Valid options are ‘table’ and ‘records’ (each row is a key-value pairs).

pybatfish.client.asserts.assert_no_incompatible_bgp_sessions(nodes: str | None = None, remote_nodes: str | None = None, status: str | None = None, snapshot: str | None = None, soft: bool = False, session: Session | None = None, df_format: str = 'table') bool[source]

Assert that there are no incompatible BGP sessions present in the snapshot.

Parameters:
  • nodes – search sessions with specified nodes on one side of the sessions.

  • remote_nodes – search sessions with specified remote_nodes on other side of the sessions.

  • status – select sessions matching the specified BGP session status specifier, if none is specified then all statuses other than UNIQUE_MATCH, DYNAMIC_MATCH, and UNKNOWN_REMOTE are selected.

  • snapshot – the snapshot on which to check the assertion

  • soft – whether this assertion is soft (i.e., generates a warning but not a failure)

  • session – Batfish session to use for the assertion

  • df_format – How to format the Dataframe content in the output message. Valid options are ‘table’ and ‘records’ (each row is a key-value pairs).

pybatfish.client.asserts.assert_no_incompatible_ospf_sessions(nodes: str | None = None, remote_nodes: str | None = None, snapshot: str | None = None, soft: bool = False, session: Session | None = None, df_format: str = 'table') bool[source]

Assert that there are no incompatible or unestablished OSPF sessions present in the snapshot.

Parameters:
  • nodes – search sessions with specified nodes on one side of the sessions.

  • remote_nodes – search sessions with specified remote_nodes on other side of the sessions.

  • snapshot – the snapshot on which to check the assertion

  • soft – whether this assertion is soft (i.e., generates a warning but not a failure)

  • session – Batfish session to use for the assertion

  • df_format – How to format the Dataframe content in the output message. Valid options are ‘table’ and ‘records’ (each row is a key-value pairs).

pybatfish.client.asserts.assert_no_undefined_references(snapshot: str | None = None, soft: bool = False, session: Session | None = None, df_format: str = 'table') bool[source]

Assert that there are no undefined references present in the snapshot.

Parameters:
  • snapshot – the snapshot on which to check the assertion

  • soft – whether this assertion is soft (i.e., generates a warning but not a failure)

  • session – Batfish session to use for the assertion

  • df_format – How to format the Dataframe content in the output message. Valid options are ‘table’ and ‘records’ (each row is a key-value pairs).

pybatfish.client.asserts.assert_no_unestablished_bgp_sessions(nodes: str | None = None, remote_nodes: str | None = None, snapshot: str | None = None, soft: bool = False, session: Session | None = None, df_format: str = 'table') bool[source]

Assert that there are no BGP sessions that are compatible but not established.

This assertion is run (only) for sessions that are compatible based on configuration settings and it will fail if any such session is not established because of routing or forwarding problems. To find sessions that are incompatible you may run the assert_no_incompatible_bgp_sessions assertion.

Parameters:
  • nodes – search sessions with specified nodes on one side of the sessions.

  • remote_nodes – search sessions with specified remote_nodes on other side of the sessions.

  • snapshot – the snapshot on which to check the assertion

  • soft – whether this assertion is soft (i.e., generates a warning but not a failure)

  • session – Batfish session to use for the assertion

  • df_format – How to format the Dataframe content in the output message. Valid options are ‘table’ and ‘records’ (each row is a key-value pairs).

pybatfish.client.asserts.assert_num_results(answer: Answer | TableAnswer | DataFrame, num: int, soft: bool = False) bool[source]

Assert an exact number of results were returned.

Parameters:
  • answer – Batfish answer or DataFrame

  • num (int) – expected number of results

  • soft (bool) – whether this assertion is soft (i.e., generates a warning but not a failure)

pybatfish.client.asserts.assert_zero_results(answer: Answer | TableAnswer | DataFrame, soft: bool = False) bool[source]

Assert no results were returned.

Parameters:
  • answer – Batfish answer or DataFrame

  • soft (bool) – whether this assertion is soft (i.e., generates a warning but not a failure)