def test_SetupSessionStepDetail_from_dict(): d = { "sessionScope": { "incomingInterfaces": ["reth0.6"] }, "sessionAction": { "type": "Accept" }, "matchCriteria": { "ipProtocol": "ICMP", "srcIp": "2.2.2.2", "dstIp": "3.3.3.3" }, "transformation": [{ "fieldName": "srcIp", "oldValue": "2.2.2.2", "newValue": "1.1.1.1" }], } assert SetupSessionStepDetail.from_dict(d) == SetupSessionStepDetail( IncomingSessionScope(["reth0.6"]), Accept(), SessionMatchExpr("ICMP", "2.2.2.2", "3.3.3.3"), [FlowDiff("srcIp", "2.2.2.2", "1.1.1.1")], )
def test_MatchSessionStepDetail_from_dict_bw_compat(): """ For backward compatibility, allow "incomingInterfaces" instead of "sessionScope". """ d = { "incomingInterfaces": ["reth0.6"], "sessionAction": { "type": "Accept" }, "matchCriteria": { "ipProtocol": "ICMP", "srcIp": "2.2.2.2", "dstIp": "3.3.3.3" }, "transformation": [{ "fieldName": "srcIp", "oldValue": "2.2.2.2", "newValue": "1.1.1.1" }], } assert MatchSessionStepDetail.from_dict(d) == MatchSessionStepDetail( IncomingSessionScope(["reth0.6"]), Accept(), SessionMatchExpr("ICMP", "2.2.2.2", "3.3.3.3"), [FlowDiff("srcIp", "2.2.2.2", "1.1.1.1")], )
def test_SessionAction_str(): assert str(Accept()) == "Accept" assert str(PostNatFibLookup()) == "PostNatFibLookup" assert str(PreNatFibLookup()) == "PreNatFibLookup" assert str(ForwardOutInterface("1.1.1.1", "iface1", "iface2")) == ( "ForwardOutInterface(Next Hop: 1.1.1.1, Next Hop Interface: iface1, Outgoing Interface: iface2)" )
def test_MatchSessionStepDetail_str(): detail = MatchSessionStepDetail( IncomingSessionScope(["reth0.6"]), Accept(), SessionMatchExpr("ICMP", "2.2.2.2", "3.3.3.3"), [FlowDiff("srcIp", "2.2.2.2", "1.1.1.1")], ) assert str(detail) == ( "Incoming Interfaces: [reth0.6], " "Action: Accept, " "Match Criteria: [ipProtocol=ICMP, srcIp=2.2.2.2, dstIp=3.3.3.3], " "Transformation: [srcIp: 2.2.2.2 -> 1.1.1.1]") detail = MatchSessionStepDetail( IncomingSessionScope(["reth0.6"]), Accept(), SessionMatchExpr("ICMP", "2.2.2.2", "3.3.3.3"), ) assert str(detail) == ( "Incoming Interfaces: [reth0.6], " "Action: Accept, " "Match Criteria: [ipProtocol=ICMP, srcIp=2.2.2.2, dstIp=3.3.3.3]")
def test_SessionAction_from_dict(): assert SessionAction.from_dict({"type": "Accept"}) == Accept() assert SessionAction.from_dict({"type": "FibLookup"}) == FibLookup() d = { "type": "ForwardOutInterface", "nextHop": { "hostname": "1.1.1.1", "interface": "iface1" }, "outgoingInterface": "iface2", } assert SessionAction.from_dict(d) == ForwardOutInterface( "1.1.1.1", "iface1", "iface2") with pytest.raises(ValueError): SessionAction.from_dict({"type": "NotAType"})