osmo-python-tests/osmopy/osmo_verify_transcript_ctrl.py

121 lines
3.8 KiB
Python
Raw Normal View History

add osmo_verify_transcript_{vty,ctrl}.py for easier vty and ctrl testing While adding VTY and CTRL tests to new programs like OsmoHLR, I wanted to have a simple way to translate a VTY interaction transcript to a VTY python test. It is fairly trivial to simply read in a transcript, extract both the commands to send as well as the expected results, and to verify these without having to write one line of application-specific code. From there it was just a little step to allow the same for CTRL interaction. With osmo_verify_transcript_vty.py and osmo_verify_transcript_ctrl.py, it is possible to have a simple text file of a telnet VTY or CTRL interface interaction and run it against a given application. With the --update option, the scripts run the given command and rewrite the transcript file to whatever the application currently produces as response. Backed by version control, it is super easy to tweak commands, --update the test results and verify that only the desired bits changed. A '...' wildcard can skip any number of lines in the expected result and is usually preserved during --update. This python3 implementation is independent from the previous obscvty implementations. Take the opportunity to clarify/fix a few aspects: for example, it is now possible to verify the hints that the interactive VTY displays when the user enters '?' in various places, and to evaluate the prompt character '>'/'#'. Unitl now, code is duplicated/scattered across various vty_test_runner.py scripts in different git repositories. Now, a VTY or CTRL transcript is enough to put a complete test in place. The simplest invocation is directly from the Makefile, feeding an application commandline, the proper port number to contact it and e.g. a VTY prompt name. This new code is also usable as python modules, to be able to build more complex tests that require specialized intermediate actions, possibly coordinating launch of applications or data manipulation. The first repository to employ this is osmo-hlr.git. See change-ids I42b3b70a0439a8f2e4964d7cc31e593c1f0d7537 for VTY and Iff93abe370b8f3ecf42082d1d0eaa1fbeca5b122 for CTRL. Change-Id: Id47331009910e651372b9c9c76e12f2e8964cc2c
2017-10-15 01:01:09 +00:00
#!/usr/bin/env python3
#
# (C) 2017 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
# All rights reserved.
#
# Author: Neels Hofmeyr <nhofmeyr@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/>.
'''
Run CTRL test transcripts against a given application.
A CTRL transcript contains CTRL commands and their expected results.
It looks like:
"
SET 1 var val
SET_REPLY 1 var OK
GET 2 var
GET_REPLY 2 var val
"
The application to be tested is described by
- a binary to run,
- command line arguments to pass to the binary,
- the CTRL port.
This module can either be run directly to run or update a given CTRL transcript,
or it can be imported as a module to run more complex setups.
'''
import re
from osmopy.osmo_verify_transcript_common import *
from osmopy.osmo_ipa import Ctrl, IPA
class InteractCtrl(Interact):
next_id = 1
keep_ids = True
re_command = re.compile('^(SET|GET) ([^ ]*) (.*)$')
class CtrlStep(Interact.StepBase):
@staticmethod
def is_next_step(line, interact_instance):
m = InteractCtrl.re_command.match(line)
if not m:
return None
next_step = InteractCtrl.CtrlStep()
set_get = m.group(1)
cmd_id = m.group(2)
var_val = m.group(3)
if not interact_instance.keep_ids:
cmd_id = interact_instance.next_id
interact_instance.next_id += 1
next_step.command = '%s %s %s' % (set_get, cmd_id, var_val)
return next_step
def __init__(self, port, host, verbose=False, update=False, keep_ids=True):
if not update:
keep_ids = True
self.keep_ids = keep_ids
super().__init__(InteractCtrl.CtrlStep, port=port, host=host, verbose=verbose, update=update)
def connect(self):
self.next_id = 1
super().connect()
def send(self, data):
data = Ctrl().add_header(data)
return self.socket.send(data) == len(data)
def receive(self):
responses = []
data = self.socket.recv(4096)
while (len(data)>0):
(response_with_header, data) = IPA().split_combined(data)
response = Ctrl().rem_header(response_with_header)
responses.append(response.decode('utf-8'))
return responses
def command(self, command):
assert self.send(command)
res = self.receive()
split_responses = []
for r in res:
split_responses.extend(r.splitlines())
sys.stdout.flush()
sys.stderr.flush()
return split_responses
if __name__ == '__main__':
parser = common_parser()
parser.add_argument('-i', '--keep-ids', dest='keep_ids', action='store_true',
help='With --update, default is to overwrite the command IDs'
' so that they are consecutive numbers starting from 1.'
' With --keep-ids, do not change these command IDs.')
args = parser.parse_args()
interact = InteractCtrl(args.port, args.host, args.verbose, args.update, args.keep_ids)
main(command_str=args.command_str,
transcript_files=args.transcript_files,
interact=interact,
verbose=args.verbose)
# vim: tabstop=4 shiftwidth=4 expandtab nocin ai