diff mbox series

[net-next,3/6] selftests: net: py: extract tool logic

Message ID 20240426232400.624864-4-kuba@kernel.org (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series selftests: net: page_poll allocation error injection | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 8 this patch: 8
netdev/build_tools success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 1 maintainers not CCed: shuah@kernel.org
netdev/build_clang success Errors and warnings before: 8 this patch: 8
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 8 this patch: 8
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 25 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
netdev/contest fail net-next-2024-04-29--12-00 (tests: 1000)

Commit Message

Jakub Kicinski April 26, 2024, 11:23 p.m. UTC
The main use of the ip() wrapper over cmd() is that it can parse JSON.
cmd("ip -j link show") will return stdout as a string, and test has
to call json.loads(). With ip("link show", json=True) the return value
will be already parsed.

More tools (ethtool, bpftool etc.) support the --json switch.
To avoid having to wrap all of them individually create a tool()
helper.

Switch from -j to --json (for ethtool).
While at it consume the netns attribute at the ip() level.

Signed-off-by: Jakub Kicinski <kuba@kernel.org>
---
 tools/testing/selftests/net/lib/py/utils.py | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

Comments

Willem de Bruijn April 27, 2024, 1:51 p.m. UTC | #1
Jakub Kicinski wrote:
> The main use of the ip() wrapper over cmd() is that it can parse JSON.
> cmd("ip -j link show") will return stdout as a string, and test has
> to call json.loads(). With ip("link show", json=True) the return value
> will be already parsed.
> 
> More tools (ethtool, bpftool etc.) support the --json switch.
> To avoid having to wrap all of them individually create a tool()
> helper.
> 
> Switch from -j to --json (for ethtool).
> While at it consume the netns attribute at the ip() level.
> 
> Signed-off-by: Jakub Kicinski <kuba@kernel.org>
> ---
>  tools/testing/selftests/net/lib/py/utils.py | 12 +++++++++---
>  1 file changed, 9 insertions(+), 3 deletions(-)
> 
> diff --git a/tools/testing/selftests/net/lib/py/utils.py b/tools/testing/selftests/net/lib/py/utils.py
> index d3715e6c21f2..11b588a2bb9d 100644
> --- a/tools/testing/selftests/net/lib/py/utils.py
> +++ b/tools/testing/selftests/net/lib/py/utils.py
> @@ -56,10 +56,10 @@ import time
>          return self.process(terminate=self.terminate)
>  
>  
> -def ip(args, json=None, ns=None, host=None):
> -    cmd_str = "ip "
> +def tool(name, args, json=None, ns=None, host=None):
> +    cmd_str = name + " "
>      if json:
> -        cmd_str += '-j '
> +        cmd_str += '--json '
>      cmd_str += args
>      cmd_obj = cmd(cmd_str, ns=ns, host=host)
>      if json:
> @@ -67,6 +67,12 @@ import time
>      return cmd_obj
>  
>  
> +def ip(args, json=None, ns=None, host=None):
> +    if ns:
> +        args = '-netns ' + ns + " " + args

Minor: inconsistent use of single and double comma strings. Maybe
there's a reasoning that I'm just missing.

> +    return tool("ip", args, json=json, host=host)
> +
> +
>  def rand_port():
>      """
>      Get unprivileged port, for now just random, one day we may decide to check if used.
> -- 
> 2.44.0
>
Jakub Kicinski April 29, 2024, 2:44 p.m. UTC | #2
On Sat, 27 Apr 2024 09:51:01 -0400 Willem de Bruijn wrote:
> > +def ip(args, json=None, ns=None, host=None):
> > +    if ns:
> > +        args = '-netns ' + ns + " " + args  
> 
> Minor: inconsistent use of single and double comma strings. Maybe
> there's a reasoning that I'm just missing.

I also need to coerce ns to be a string, it may be class NetNS.
v2 coming in 3... 2... 1...
diff mbox series

Patch

diff --git a/tools/testing/selftests/net/lib/py/utils.py b/tools/testing/selftests/net/lib/py/utils.py
index d3715e6c21f2..11b588a2bb9d 100644
--- a/tools/testing/selftests/net/lib/py/utils.py
+++ b/tools/testing/selftests/net/lib/py/utils.py
@@ -56,10 +56,10 @@  import time
         return self.process(terminate=self.terminate)
 
 
-def ip(args, json=None, ns=None, host=None):
-    cmd_str = "ip "
+def tool(name, args, json=None, ns=None, host=None):
+    cmd_str = name + " "
     if json:
-        cmd_str += '-j '
+        cmd_str += '--json '
     cmd_str += args
     cmd_obj = cmd(cmd_str, ns=ns, host=host)
     if json:
@@ -67,6 +67,12 @@  import time
     return cmd_obj
 
 
+def ip(args, json=None, ns=None, host=None):
+    if ns:
+        args = '-netns ' + ns + " " + args
+    return tool("ip", args, json=json, host=host)
+
+
 def rand_port():
     """
     Get unprivileged port, for now just random, one day we may decide to check if used.