diff mbox series

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

Message ID 20211122112256.2011194-19-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 fail github build: failed

Commit Message

Adrian Moreno Nov. 22, 2021, 11:22 a.m. UTC
Signed-off-by: Adrian Moreno <amorenoz@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

Comments

Eelco Chaudron Dec. 24, 2021, 1:22 p.m. UTC | #1
On 22 Nov 2021, at 12:22, Adrian Moreno wrote:

> Signed-off-by: Adrian Moreno <amorenoz@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

Add in alphabetical order.

Rest looks fine.

>
>  # 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()
> +
> +
> +@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
diff mbox series

Patch

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()
+
+
+@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