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_has_no_unreachable_lines(filters, soft=False, snapshot=None, session=None, df_format='table')[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_denies(filters, headers, startLocation=None, soft=False, snapshot=None, session=None, df_format='table')[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_permits(filters, headers, startLocation=None, soft=False, snapshot=None, session=None, df_format='table')[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, headers, soft=False, snapshot=None, session=None, df_format='table')[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, headers, soft=False, snapshot=None, session=None, df_format='table')[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: Union[pandas.core.frame.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: Union[pandas.core.frame.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: Optional[str] = None, nodes: Optional[str] = None, protocols: Optional[List[str]] = None, soft: bool = False, session: Optional[Session] = 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=None, soft=False, session=None, df_format='table')[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=None, remote_nodes=None, status=None, snapshot=None, soft=False, session=None, df_format='table')[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=None, remote_nodes=None, snapshot=None, soft=False, session=None, df_format='table')[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_unestablished_bgp_sessions(nodes=None, remote_nodes=None, snapshot=None, soft=False, session=None, df_format='table')[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_no_undefined_references(snapshot=None, soft=False, session=None, df_format='table')[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_num_results(answer, num, soft=False)[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, soft=False)[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)