Ejemplo n.º 1
0
def test_filter_has_no_unreachable_lines_no_session():
    """Confirm no-unreachable-filter-lines assert passes and fails as expected when no session is specified."""
    filters = "filter1"
    with patch.object(bfq, 'filterLineReachability',
                      create=True) as filterLineReachability:
        # Test success
        filterLineReachability.return_value = MockQuestion()
        assert_filter_has_no_unreachable_lines(filters=filters)
        # Test failure
        mock_df = DataFrame.from_records(
            [{'UnreachableLine': 'found', 'More': 'data'}])
        filterLineReachability.return_value = MockQuestion(
            MockTableAnswer(mock_df))
        with pytest.raises(BatfishAssertException) as excinfo:
            assert_filter_has_no_unreachable_lines(filters=filters)
        # Ensure found answer is printed
        assert mock_df.to_string() in str(excinfo.value)
Ejemplo n.º 2
0
def test_filter_has_no_unreachable_lines():
    """Confirm filter-has-no-unreachable-lines assert passes and fails as expected when specifying a session."""
    filters = "filter1"
    bf = Session(load_questions=False)
    with patch.object(bf.q, "filterLineReachability",
                      create=True) as filterLineReachability:
        # Test success
        filterLineReachability.return_value = MockQuestion()
        assert_filter_has_no_unreachable_lines(filters=filters, session=bf)
        # Test failure
        mock_df = DataFrame.from_records([{
            "UnreachableLine": "found",
            "More": "data"
        }])
        filterLineReachability.return_value = MockQuestion(
            MockTableAnswer(mock_df))
        with pytest.raises(BatfishAssertException) as excinfo:
            assert_filter_has_no_unreachable_lines(filters=filters, session=bf)
        # Ensure found answer is printed
        assert mock_df.to_string() in str(excinfo.value)
Ejemplo n.º 3
0
    def assert_filter_has_no_unreachable_lines(self, filters, soft=False,
                                               snapshot=None,
                                               df_format="table"):
        # type: (str, bool, Optional[str], str) -> bool
        """
        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."

        :param filters: the specification for the filter (filterSpec) to check
        :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_filter_has_no_unreachable_lines(filters, soft, snapshot,
                                                      self.session, df_format)