[ovs-dev] [PATCH v1 18/18] python: add unit tests for filtering engine

Adrian Moreno amorenoz at redhat.com
Mon Nov 22 11:22:56 UTC 2021


Signed-off-by: Adrian Moreno <amorenoz at redhat.com>
---
 python/automake.mk              |   3 +-
 python/ovs/tests/test_filter.py | 225 ++++++++++++++++++++++++++++++++
 2 files changed, 227 insertions(+), 1 deletion(-)
 create mode 100644 python/ovs/tests/test_filter.py

diff --git a/python/automake.mk b/python/automake.mk
index cb8697b12..56cd9b7dc 100644
--- a/python/automake.mk
+++ b/python/automake.mk
@@ -57,7 +57,8 @@ ovs_tests = \
 	python/ovs/tests/test_kv.py \
 	python/ovs/tests/test_list.py \
 	python/ovs/tests/test_ofp.py \
-	python/ovs/tests/test_odp.py
+	python/ovs/tests/test_odp.py \
+	python/ovs/tests/test_filter.py
 
 
 # These python files are used at build time but not runtime,
diff --git a/python/ovs/tests/test_filter.py b/python/ovs/tests/test_filter.py
new file mode 100644
index 000000000..e2aed9d77
--- /dev/null
+++ b/python/ovs/tests/test_filter.py
@@ -0,0 +1,225 @@
+import pytest
+
+from ovs.flows.filter import OFFilter
+from ovs.flows.ofp import OFPFlowFactory
+from ovs.flows.odp import ODPFlowFactory
+
+
+ofp_factory = OFPFlowFactory()
+odp_factory = ODPFlowFactory()
+
+
+ at pytest.mark.parametrize(
+    "expr,flow,expected,match",
+    [
+        (
+            "nw_src=192.168.1.1 && tcp_dst=80",
+            ofp_factory.from_string(
+                "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",
+            ofp_factory.from_string(
+                "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",
+            ofp_factory.from_string(
+                "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",
+            ofp_factory.from_string(
+                "nw_src=192.168.1.1,tcp_dst=80 actions=drop"
+            ),
+            False,
+            ["nw_src", "tcp_dst"],
+        ),
+        (
+            "nw_src=192.168.1.1",
+            ofp_factory.from_string(
+                "nw_src=192.168.1.0/24,tcp_dst=80 actions=drop"
+            ),
+            False,
+            ["nw_src"],
+        ),
+        (
+            "nw_src~=192.168.1.1",
+            ofp_factory.from_string(
+                "nw_src=192.168.1.0/24,tcp_dst=80 actions=drop"
+            ),
+            True,
+            ["nw_src"],
+        ),
+        (
+            "nw_src~=192.168.1.1/30",
+            ofp_factory.from_string(
+                "nw_src=192.168.1.0/24,tcp_dst=80 actions=drop"
+            ),
+            True,
+            ["nw_src"],
+        ),
+        (
+            "nw_src~=192.168.1.0/16",
+            ofp_factory.from_string(
+                "nw_src=192.168.1.0/24,tcp_dst=80 actions=drop"
+            ),
+            False,
+            ["nw_src"],
+        ),
+        (
+            "nw_src~=192.168.1.0/16",
+            ofp_factory.from_string(
+                "nw_src=192.168.1.0/24,tcp_dst=80 actions=drop"
+            ),
+            False,
+            ["nw_src"],
+        ),
+        (
+            "n_bytes=100",
+            ofp_factory.from_string(
+                "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",
+            ofp_factory.from_string(
+                "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",
+            ofp_factory.from_string(
+                "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",
+            ofp_factory.from_string(
+                "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",
+            ofp_factory.from_string(
+                "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",
+            ofp_factory.from_string(
+                "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",
+            ofp_factory.from_string(
+                "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",
+            ofp_factory.from_string(
+                "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",
+            ofp_factory.from_string(
+                "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",
+            ofp_factory.from_string(
+                "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",
+            ofp_factory.from_string(
+                "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",
+            ofp_factory.from_string(
+                "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",
+            ofp_factory.from_string(
+                "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",
+            odp_factory.from_string(
+                "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",
+            odp_factory.from_string(
+                "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",
+            odp_factory.from_string(
+                "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
-- 
2.31.1



More information about the dev mailing list