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

<div style="display:none"> Gerrit-Project: python/osmo-python-tests </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-Change-Id: If38243171c94d8ce9d6cdc269da59c9ebc9942bb </div>
<div style="display:none"> Gerrit-Change-Number: 15273 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: pespin <pespin@sysmocom.de> </div>
<div style="display:none"> Gerrit-Reviewer: osmith <osmith@sysmocom.de> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>