def test_flows_fail(): """Confirm flows-fail assert passes and fails as expected when specifying a session.""" startLocation = "node1" headers = HeaderConstraints(srcIps="1.1.1.1") bf = Session(load_questions=False) with patch.object(bf.q, "reachability", create=True) as reachability: # Test success reachability.return_value = MockQuestion() assert_flows_fail(startLocation, headers, session=bf) reachability.assert_called_with( pathConstraints=PathConstraints(startLocation=startLocation), headers=headers, actions="success", ) # Test failure mock_df = DataFrame.from_records([{"Flow": "found", "More": "data"}]) reachability.return_value = MockQuestion(MockTableAnswer(mock_df)) with pytest.raises(BatfishAssertException) as excinfo: assert_flows_fail(startLocation, headers, session=bf) # Ensure found answer is printed assert mock_df.to_string() in str(excinfo.value) reachability.assert_called_with( pathConstraints=PathConstraints(startLocation=startLocation), headers=headers, actions="success", )
def test_flows_fail_no_session(): """ Confirm flows-fail assert passes and fails as expected when not specifying a session. For reverse compatibility. """ startLocation = "node1" headers = HeaderConstraints(srcIps='1.1.1.1') with patch.object(bfq, 'reachability', create=True) as reachability: # Test success reachability.return_value = MockQuestion() assert_flows_fail(startLocation, headers) reachability.assert_called_with( pathConstraints=PathConstraints(startLocation=startLocation), headers=headers, actions='success') # Test failure mock_df = DataFrame.from_records([{'Flow': 'found', 'More': 'data'}]) reachability.return_value = MockQuestion( MockTableAnswer(mock_df)) with pytest.raises(BatfishAssertException) as excinfo: assert_flows_fail(startLocation, headers) # Ensure found answer is printed assert mock_df.to_string() in str(excinfo.value) reachability.assert_called_with( pathConstraints=PathConstraints(startLocation=startLocation), headers=headers, actions='success')
def assert_flows_fail(self, startLocation, headers, soft=False, snapshot=None, df_format="table"): # type: (str, HeaderConstraints, bool, Optional[str], str) -> bool """ Check if the specified set of flows, denoted by starting locations and headers, fail. :param startLocation: LocationSpec indicating where the flow starts :param headers: :py:class:`~pybatfish.datamodel.flow.HeaderConstraints` :param soft: whether this assertion is soft (i.e., generates a warning but not a failure) :param snapshot: the snapshot on which to check the assertion :param 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). :return: True if the assertion passes """ return assert_flows_fail(startLocation, headers, soft, snapshot, self.session, df_format)