Add osmo_interact_exec.py

Change-Id: If38243171c94d8ce9d6cdc269da59c9ebc9942bb
osmith/osmo-interact-exec
Oliver Smith 4 years ago
parent a7eda7f113
commit 050f7fe631
  1. 28
      osmopy/osmo_interact/common.py
  2. 84
      osmopy/osmo_interact/exec.py
  3. 27
      scripts/osmo_interact_exec.py

@ -374,7 +374,7 @@ def verify_application(run_app_str, interact, transcript_file, verbose):
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 @@ def common_parser(doc=None):
' 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 @@ def parser_add_verify_args(parser):
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 @@ def main_run_commands(run_app_str, output_path, cmd_str, cmd_files, interact):
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:

@ -0,0 +1,84 @@
#!/usr/bin/env python3
#
# (C) 2019 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
# All rights reserved.
#
# Author: Oliver Smith <osmith@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

@ -0,0 +1,27 @@
#!/usr/bin/env python3
#
# (C) 2019 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
# All rights reserved.
#
# Author: Oliver Smith <osmith@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
Loading…
Cancel
Save