Esempio n. 1
0
def test_flows_fail_from_session():
    """Confirm flows-fail assert passes and fails as expected when called from 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()
        bf.asserts.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:
            bf.asserts.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')
Esempio n. 2
0
def test_flows_succeed_no_session():
    """
    Confirm flows-succeed 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_succeed(startLocation, headers)
        reachability.assert_called_with(
            pathConstraints=PathConstraints(startLocation=startLocation),
            headers=headers,
            actions='failure')
        # 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_succeed(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='failure')
Esempio n. 3
0
def test_flows_succeed():
    """Confirm flows-succeed 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_succeed(startLocation, headers, session=bf)
        reachability.assert_called_with(
            pathConstraints=PathConstraints(startLocation=startLocation),
            headers=headers,
            actions="failure",
        )
        # 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_succeed(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="failure",
        )
Esempio n. 4
0
def assert_flows_succeed(startLocation,
                         headers,
                         soft=False,
                         snapshot=None,
                         session=None,
                         df_format="table"):
    # type: (str, HeaderConstraints, bool, Optional[str], Optional[Session], str) -> bool
    """
    Check if the specified set of flows, denoted by starting locations and headers, succeed.

    :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 session: Batfish session to use for 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
    """
    __tracebackhide__ = operator.methodcaller("errisinstance",
                                              BatfishAssertException)

    kwargs = dict(pathConstraints=PathConstraints(startLocation=startLocation),
                  headers=headers,
                  actions="failure")

    df = _get_question_object(session, 'reachability').reachability(
        **kwargs).answer(snapshot).frame()  # type: ignore
    if len(df) > 0:
        return _raise_common(
            "Found a flow that failed, when expected to succeed\n{}".format(
                _format_df(df, df_format)), soft)
    return True