diff mbox series

[ovs-dev,v4,17/17] python: add unit tests for filtering engine

Message ID 20220616063247.517147-18-amorenoz@redhat.com
State Changes Requested
Headers show
Series python: add flow parsing library | expand

Checks

Context Check Description
ovsrobot/apply-robot success apply and check: success
ovsrobot/github-robot-_Build_and_Test success github build: passed
ovsrobot/intel-ovs-compilation success test: success

Commit Message

Adrian Moreno June 16, 2022, 6:32 a.m. UTC
Add unit test for OFFilter class.

Acked-by: Eelco Chaudron <echaudro@redhat.com>
Signed-off-by: Adrian Moreno <amorenoz@redhat.com>
---
 python/automake.mk              |   1 +
 python/ovs/tests/test_filter.py | 221 ++++++++++++++++++++++++++++++++
 2 files changed, 222 insertions(+)
 create mode 100644 python/ovs/tests/test_filter.py
diff mbox series

Patch

diff --git a/python/automake.mk b/python/automake.mk
index fe41b653d..6a0561cbb 100644
--- a/python/automake.mk
+++ b/python/automake.mk
@@ -54,6 +54,7 @@  ovs_pyfiles = \
 
 ovs_pytests = \
 	python/ovs/tests/test_decoders.py \
+	python/ovs/tests/test_filter.py \
 	python/ovs/tests/test_kv.py \
 	python/ovs/tests/test_list.py \
 	python/ovs/tests/test_odp.py \
diff --git a/python/ovs/tests/test_filter.py b/python/ovs/tests/test_filter.py
new file mode 100644
index 000000000..47d9d9420
--- /dev/null
+++ b/python/ovs/tests/test_filter.py
@@ -0,0 +1,221 @@ 
+import pytest
+
+from ovs.flow.filter import OFFilter
+from ovs.flow.ofp import OFPFlow
+from ovs.flow.odp import ODPFlow
+
+
+@pytest.mark.parametrize(
+    "expr,flow,expected,match",
+    [
+        (
+            "nw_src=192.168.1.1 && tcp_dst=80",
+            OFPFlow(
+                "nw_src=192.168.1.1,tcp_dst=80 actions=drop"
+            ),
+            True,
+            ["nw_src", "tcp_dst"],
+        ),
+        (
+            "nw_src=192.168.1.2 || tcp_dst=80",
+            OFPFlow(
+                "nw_src=192.168.1.1,tcp_dst=80 actions=drop"
+            ),
+            True,
+            ["nw_src", "tcp_dst"],
+        ),
+        (
+            "nw_src=192.168.1.1 || tcp_dst=90",
+            OFPFlow(
+                "nw_src=192.168.1.1,tcp_dst=80 actions=drop"
+            ),
+            True,
+            ["nw_src", "tcp_dst"],
+        ),
+        (
+            "nw_src=192.168.1.2 && tcp_dst=90",
+            OFPFlow(
+                "nw_src=192.168.1.1,tcp_dst=80 actions=drop"
+            ),
+            False,
+            ["nw_src", "tcp_dst"],
+        ),
+        (
+            "nw_src=192.168.1.1",
+            OFPFlow(
+                "nw_src=192.168.1.0/24,tcp_dst=80 actions=drop"
+            ),
+            False,
+            ["nw_src"],
+        ),
+        (
+            "nw_src~=192.168.1.1",
+            OFPFlow(
+                "nw_src=192.168.1.0/24,tcp_dst=80 actions=drop"
+            ),
+            True,
+            ["nw_src"],
+        ),
+        (
+            "nw_src~=192.168.1.1/30",
+            OFPFlow(
+                "nw_src=192.168.1.0/24,tcp_dst=80 actions=drop"
+            ),
+            True,
+            ["nw_src"],
+        ),
+        (
+            "nw_src~=192.168.1.0/16",
+            OFPFlow(
+                "nw_src=192.168.1.0/24,tcp_dst=80 actions=drop"
+            ),
+            False,
+            ["nw_src"],
+        ),
+        (
+            "nw_src~=192.168.1.0/16",
+            OFPFlow(
+                "nw_src=192.168.1.0/24,tcp_dst=80 actions=drop"
+            ),
+            False,
+            ["nw_src"],
+        ),
+        (
+            "n_bytes=100",
+            OFPFlow(
+                "n_bytes=100 priority=100,nw_src=192.168.1.0/24,tcp_dst=80 actions=drop"  # noqa: E501
+            ),
+            True,
+            ["n_bytes"],
+        ),
+        (
+            "n_bytes>10",
+            OFPFlow(
+                "n_bytes=100 priority=100,nw_src=192.168.1.0/24,tcp_dst=80 actions=drop"  # noqa: E501
+            ),
+            True,
+            ["n_bytes"],
+        ),
+        (
+            "n_bytes>100",
+            OFPFlow(
+                "n_bytes=100 priority=100,nw_src=192.168.1.0/24,tcp_dst=80 actions=drop"  # noqa: E501
+            ),
+            False,
+            ["n_bytes"],
+        ),
+        (
+            "n_bytes<100",
+            OFPFlow(
+                "n_bytes=100 priority=100,nw_src=192.168.1.0/24,tcp_dst=80 actions=drop"  # noqa: E501
+            ),
+            False,
+            ["n_bytes"],
+        ),
+        (
+            "n_bytes<1000",
+            OFPFlow(
+                "n_bytes=100 priority=100,nw_src=192.168.1.0/24,tcp_dst=80 actions=drop"  # noqa: E501
+            ),
+            True,
+            ["n_bytes"],
+        ),
+        (
+            "n_bytes>0 && drop=true",
+            OFPFlow(
+                "n_bytes=100 priority=100,nw_src=192.168.1.0/24,tcp_dst=80 actions=drop"  # noqa: E501
+            ),
+            True,
+            ["n_bytes", "drop"],
+        ),
+        (
+            "n_bytes>0 && drop=true",
+            OFPFlow(
+                "n_bytes=100 priority=100,nw_src=192.168.1.0/24,tcp_dst=80 actions=2"  # noqa: E501
+            ),
+            False,
+            ["n_bytes"],
+        ),
+        (
+            "n_bytes>10 && !output.port=3",
+            OFPFlow(
+                "n_bytes=100 priority=100,nw_src=192.168.1.0/24,tcp_dst=80 actions=2"  # noqa: E501
+            ),
+            True,
+            ["n_bytes", "output"],
+        ),
+        (
+            "dl_src=00:11:22:33:44:55",
+            OFPFlow(
+                "n_bytes=100 priority=100,dl_src=00:11:22:33:44:55,nw_src=192.168.1.0/24,tcp_dst=80 actions=2"   # noqa: E501
+            ),
+            True,
+            ["dl_src"],
+        ),
+        (
+            "dl_src~=00:11:22:33:44:55",
+            OFPFlow(
+                "n_bytes=100 priority=100,dl_src=00:11:22:33:44:55/ff:ff:ff:ff:ff:00,nw_src=192.168.1.0/24,tcp_dst=80 actions=2"  # noqa: E501
+            ),
+            True,
+            ["dl_src"],
+        ),
+        (
+            "dl_src~=00:11:22:33:44:66",
+            OFPFlow(
+                "n_bytes=100 priority=100,dl_src=00:11:22:33:44:55/ff:ff:ff:ff:ff:00,nw_src=192.168.1.0/24,tcp_dst=80 actions=2"  # noqa: E501
+            ),
+            True,
+            ["dl_src"],
+        ),
+        (
+            "dl_src~=00:11:22:33:44:66 && tp_dst=1000",
+            OFPFlow(
+                "n_bytes=100 priority=100,dl_src=00:11:22:33:44:55/ff:ff:ff:ff:ff:00,nw_src=192.168.1.0/24,tp_dst=0x03e8/0xfff8 actions=2"  # noqa: E501
+            ),
+            False,
+            ["dl_src", "tp_dst"],
+        ),
+        (
+            "dl_src~=00:11:22:33:44:66 && tp_dst~=1000",
+            OFPFlow(
+                "n_bytes=100 priority=100,dl_src=00:11:22:33:44:55/ff:ff:ff:ff:ff:00,nw_src=192.168.1.0/24,tp_dst=0x03e8/0xfff8 actions=2"  # noqa: E501
+            ),
+            True,
+            ["dl_src", "tp_dst"],
+        ),
+        (
+            "encap",
+            ODPFlow(
+                "encap(eth_type(0x0800),ipv4(src=10.76.23.240/255.255.255.248,dst=10.76.23.106,proto=17,tos=0/0,ttl=64,frag=no)) actions:drop"  # noqa: E501
+            ),
+            True,
+            ["encap"],
+        ),
+        (
+            "encap.ipv4.src=10.76.23.240",
+            ODPFlow(
+                "encap(eth_type(0x0800),ipv4(src=10.76.23.240/255.255.255.248,dst=10.76.23.106,proto=17,tos=0/0,ttl=64,frag=no)) actions:drop"  # noqa: E501
+            ),
+            False,
+            ["encap"],
+        ),
+        (
+            "encap.ipv4.src~=10.76.23.240",
+            ODPFlow(
+                "encap(eth_type(0x0800),ipv4(src=10.76.23.240/255.255.255.248,dst=10.76.23.106,proto=17,tos=0/0,ttl=64,frag=no)) actions:drop"  # noqa: E501
+            ),
+            True,
+            ["encap"],
+        ),
+    ],
+)
+def test_filter(expr, flow, expected, match):
+    ffilter = OFFilter(expr)
+    result = ffilter.evaluate(flow)
+    if expected:
+        assert result
+    else:
+        assert not result
+
+    assert [kv.key for kv in result.kv] == match