Change in ...osmo-python-tests[master]: Add osmo_interact_exec.py

pespin gerrit-no-reply at lists.osmocom.org
Fri Aug 23 13:52:42 UTC 2019


Hello osmith,

I'd like you to do a code review. Please visit

    https://gerrit.osmocom.org/c/python/osmo-python-tests/+/15273

to review the following change.


Change subject: Add osmo_interact_exec.py
......................................................................

Add osmo_interact_exec.py

Change-Id: If38243171c94d8ce9d6cdc269da59c9ebc9942bb
---
M osmopy/osmo_interact/common.py
A osmopy/osmo_interact/exec.py
A scripts/osmo_interact_exec.py
3 files changed, 132 insertions(+), 7 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/python/osmo-python-tests refs/changes/73/15273/1

diff --git a/osmopy/osmo_interact/common.py b/osmopy/osmo_interact/common.py
index 39163a2..bc2b461 100644
--- a/osmopy/osmo_interact/common.py
+++ b/osmopy/osmo_interact/common.py
@@ -374,7 +374,7 @@
 
     return passed
 
-def common_parser(doc=None):
+def common_parser(doc=None, host=True):
     parser = argparse.ArgumentParser(description=doc,
                                      formatter_class=argparse.RawDescriptionHelpFormatter)
     parser.add_argument('-r', '--run', dest='run_app_str',
@@ -383,8 +383,9 @@
                         ' application is launched.')
     parser.add_argument('-p', '--port', dest='port',
                         help="Port to reach the application at.")
-    parser.add_argument('-H', '--host', dest='host', default='localhost',
-                        help="Host to reach the application at.")
+    if host:
+        parser.add_argument('-H', '--host', dest='host', default='localhost',
+                            help="Host to reach the application at.")
     return parser
 
 def parser_add_verify_args(parser):
@@ -397,17 +398,30 @@
     parser.add_argument('transcript_files', nargs='*', help='transcript file(s) to verify')
     return parser
 
-def parser_add_run_args(parser):
+def parser_add_run_args(parser, cmd_files=True):
     parser.add_argument('-O', '--output', dest='output_path',
                         help="Write command results to a file instead of stdout."
                         "('-O -' writes to stdout and is the default)")
     parser.add_argument('-c', '--command', dest='cmd_str',
                         help="Run this command (before reading input files, if any)."
                         " multiple commands may be separated by ';'")
-    parser.add_argument('cmd_files', nargs='*', help='file(s) with plain commands to run')
+    if cmd_files:
+        parser.add_argument('cmd_files', nargs='*', help='file(s) with plain commands to run')
     return parser
 
-def main_run_commands(run_app_str, output_path, cmd_str, cmd_files, interact):
+def parser_require_args(parser, dests):
+    for dest in dests:
+        found = False
+        for action in parser._actions:
+            if action.dest == dest:
+                action.required = True
+                found = True
+                break
+        if not found:
+            raise RuntimeError("Could not find argument with dest: " + dest)
+
+def main_run_commands(run_app_str, output_path, cmd_str, cmd_files, interact,
+                      purge_output=True):
     to_stdout = False
     if not output_path or output_path == '-':
         to_stdout = True
@@ -418,7 +432,7 @@
     application = None
 
     if run_app_str:
-        application = Application(run_app_str, quiet=to_stdout)
+        application = Application(run_app_str, quiet=to_stdout, purge_output=purge_output)
         application.run()
 
     try:
diff --git a/osmopy/osmo_interact/exec.py b/osmopy/osmo_interact/exec.py
new file mode 100644
index 0000000..c2f2f08
--- /dev/null
+++ b/osmopy/osmo_interact/exec.py
@@ -0,0 +1,84 @@
+#!/usr/bin/env python3
+#
+# (C) 2019 by sysmocom s.f.m.c. GmbH <info at sysmocom.de>
+# All rights reserved.
+#
+# Author: Oliver Smith <osmith at sysmocom.de>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+# SPDX-License-Identifier: GPL-3.0-or-later
+
+'''
+Wait until a given application listens for TCP connections, then run C tests or
+other programs against it.
+'''
+
+import subprocess
+from .common import *
+
+class InteractExec(Interact):
+    def __init__(self, port, verbose=False, update=False):
+        super().__init__(Interact.StepBase, host="localhost", port=port,
+                         verbose=verbose, update=update)
+
+    def command(self, command):
+        print("Launching: " + command)
+
+        try:
+            # Do not allow commands with arguments, as these would behave
+            # unexpectedly: the commands get split by ';', no matter if in
+            # quotes or not.
+            output = subprocess.check_output([command],
+                                             stderr=subprocess.STDOUT)
+            output = output.decode("utf-8")
+        except subprocess.CalledProcessError as e:
+            # Print output on error too
+            print(e.output.decode("utf-8"))
+            print("---")
+            sys.stdout.flush()
+            sys.stderr.flush()
+            raise
+
+        print(output)
+        sys.stdout.flush()
+        sys.stderr.flush()
+        return ("$ " + command + "\n" + output).split("\n")
+
+def main_interact_exec():
+    '''
+Wait until a given application listens for TCP connections, then run C tests or
+other programs against it.
+
+Example:
+  osmo_interact_exec.py \\
+    -r 'osmo-hlr -c /etc/osmocom/osmo-hlr.cfg -l /tmp/hlr.db' \\
+    -p 4222 \\
+    -c 'tests/gsup_client_session/gsup_client_session_test'
+'''
+    parser = common_parser(main_interact_exec.__doc__, host=False)
+    parser_add_run_args(parser, cmd_files=False)
+    parser_require_args(parser, ("run_app_str", "port", "cmd_str"))
+    parser.add_argument('-v', '--verbose', action='store_true',
+                        help='print run command (from -r) output')
+    args = parser.parse_args()
+
+    interact = InteractExec(args.port, verbose=args.verbose, update=False)
+
+    main_run_commands(args.run_app_str, args.output_path, args.cmd_str,
+                      cmd_files=[], interact=interact,
+                      purge_output=not args.verbose)
+
+
+# vim: tabstop=4 shiftwidth=4 expandtab nocin ai
diff --git a/scripts/osmo_interact_exec.py b/scripts/osmo_interact_exec.py
new file mode 100755
index 0000000..af44e21
--- /dev/null
+++ b/scripts/osmo_interact_exec.py
@@ -0,0 +1,27 @@
+#!/usr/bin/env python3
+#
+# (C) 2019 by sysmocom s.f.m.c. GmbH <info at sysmocom.de>
+# All rights reserved.
+#
+# Author: Oliver Smith <osmith at sysmocom.de>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+# SPDX-License-Identifier: GPL-3.0-or-later
+
+from osmopy.osmo_interact.exec import main_interact_exec
+
+main_interact_exec()
+
+# vim: tabstop=4 shiftwidth=4 expandtab nocin ai

-- 
To view, visit https://gerrit.osmocom.org/c/python/osmo-python-tests/+/15273
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: python/osmo-python-tests
Gerrit-Branch: master
Gerrit-Change-Id: If38243171c94d8ce9d6cdc269da59c9ebc9942bb
Gerrit-Change-Number: 15273
Gerrit-PatchSet: 1
Gerrit-Owner: pespin <pespin at sysmocom.de>
Gerrit-Reviewer: osmith <osmith at sysmocom.de>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20190823/7d441a85/attachment.html>


More information about the gerrit-log mailing list