diff mbox series

[ovs-dev,v3,5/8] python: tests: Refactor test_odp section testing.

Message ID 20240105114702.443465-6-amorenoz@redhat.com
State Changes Requested
Delegated to: Simon Horman
Headers show
Series python: Miscelaneous flow parsing fixes. | 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 Jan. 5, 2024, 11:46 a.m. UTC
Avoid code duplication by moving the section testing code to its own
function.

Signed-off-by: Adrian Moreno <amorenoz@redhat.com>
---
 python/ovs/tests/test_odp.py | 66 ++++++++++++++----------------------
 1 file changed, 26 insertions(+), 40 deletions(-)

Comments

Simon Horman Jan. 5, 2024, 3:21 p.m. UTC | #1
On Fri, Jan 05, 2024 at 12:46:57PM +0100, Adrian Moreno wrote:
> Avoid code duplication by moving the section testing code to its own
> function.
> 
> Signed-off-by: Adrian Moreno <amorenoz@redhat.com>

Acked-by: Simon Horman <horms@ovn.org>
diff mbox series

Patch

diff --git a/python/ovs/tests/test_odp.py b/python/ovs/tests/test_odp.py
index d60947a5c..8147a31d1 100644
--- a/python/ovs/tests/test_odp.py
+++ b/python/ovs/tests/test_odp.py
@@ -13,6 +13,30 @@  from ovs.flow.decoders import (
 )
 
 
+def do_test_section(input_string, section, expected):
+    flow = ODPFlow(input_string)
+    kv_list = flow.section(section).data
+
+    for i in range(len(expected)):
+        assert expected[i].key == kv_list[i].key
+        assert expected[i].value == kv_list[i].value
+
+        # Assert positions relative to action string are OK.
+        pos = flow.section(section).pos
+        string = flow.section(section).string
+
+        kpos = kv_list[i].meta.kpos
+        kstr = kv_list[i].meta.kstring
+        vpos = kv_list[i].meta.vpos
+        vstr = kv_list[i].meta.vstring
+        assert string[kpos : kpos + len(kstr)] == kstr
+        if vpos != -1:
+            assert string[vpos : vpos + len(vstr)] == vstr
+
+        # Assert string meta is correct.
+        assert input_string[pos : pos + len(string)] == string
+
+
 @pytest.mark.parametrize(
     "input_string,expected",
     [
@@ -109,26 +133,7 @@  from ovs.flow.decoders import (
     ],
 )
 def test_odp_fields(input_string, expected):
-    odp = ODPFlow(input_string)
-    match = odp.match_kv
-    for i in range(len(expected)):
-        assert expected[i].key == match[i].key
-        assert expected[i].value == match[i].value
-
-        # Assert positions relative to action string are OK.
-        mpos = odp.section("match").pos
-        mstring = odp.section("match").string
-
-        kpos = match[i].meta.kpos
-        kstr = match[i].meta.kstring
-        vpos = match[i].meta.vpos
-        vstr = match[i].meta.vstring
-        assert mstring[kpos : kpos + len(kstr)] == kstr
-        if vpos != -1:
-            assert mstring[vpos : vpos + len(vstr)] == vstr
-
-        # Assert mstring meta is correct.
-        assert input_string[mpos : mpos + len(mstring)] == mstring
+    do_test_section(input_string, "match", expected)
 
 
 @pytest.mark.parametrize(
@@ -549,23 +554,4 @@  def test_odp_fields(input_string, expected):
     ],
 )
 def test_odp_actions(input_string, expected):
-    odp = ODPFlow(input_string)
-    actions = odp.actions_kv
-    for i in range(len(expected)):
-        assert expected[i].key == actions[i].key
-        assert expected[i].value == actions[i].value
-
-        # Assert positions relative to action string are OK.
-        apos = odp.section("actions").pos
-        astring = odp.section("actions").string
-
-        kpos = actions[i].meta.kpos
-        kstr = actions[i].meta.kstring
-        vpos = actions[i].meta.vpos
-        vstr = actions[i].meta.vstring
-        assert astring[kpos : kpos + len(kstr)] == kstr
-        if vpos != -1:
-            assert astring[vpos : vpos + len(vstr)] == vstr
-
-        # Assert astring meta is correct.
-        assert input_string[apos : apos + len(astring)] == astring
+    do_test_section(input_string, "actions", expected)