summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorAdrian Moreno <amorenoz@redhat.com>2022-07-08 20:03:16 +0200
committerIlya Maximets <i.maximets@ovn.org>2022-07-15 20:14:24 +0200
commit73ba04fd77a1c4ea98dd289268b9db9067ce3519 (patch)
tree827c8957cda6b324799987de161607c949b2fbde /python
parent686bb5e729761f1053c36d91d4efc6c4193ebf10 (diff)
downloadopenvswitch-73ba04fd77a1c4ea98dd289268b9db9067ce3519.tar.gz
python: Add unit tests for filtering engine.
Add unit test for OFFilter class. Acked-by: Eelco Chaudron <echaudro@redhat.com> Signed-off-by: Adrian Moreno <amorenoz@redhat.com> Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
Diffstat (limited to 'python')
-rw-r--r--python/automake.mk1
-rw-r--r--python/ovs/tests/test_filter.py221
2 files changed, 222 insertions, 0 deletions
diff --git a/python/automake.mk b/python/automake.mk
index f1721b8d7..d00911828 100644
--- a/python/automake.mk
+++ b/python/automake.mk
@@ -55,6 +55,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