keep Ctrl connections open for bsc, msc and nitb objects

The pattern to use 'with' to keep a CTRL connection open adds indents to
every test script that wants to avoid multiple reconnections to the
CTRL. Instead, keeping a single open connection that is cleaned up on
{bsc,msc,nitb} object cleanup ensures that a) the program started up
successfully and opened a CTRL port, b) always has a CTRL open without
having to worry about it and c) keeps test scripts less
complex/indented/crufted.

(These are all current users of the OsmoCtrl API.)

Change-Id: I53fedbe569c5ccbc4b1a17dafe1f8d1bb8200b24
This commit is contained in:
Neels Hofmeyr 2020-12-08 17:36:04 +01:00
parent 5b04ef213e
commit 012a17da6a
4 changed files with 37 additions and 14 deletions

View File

@ -50,6 +50,7 @@ class OsmoBsc(log.Origin):
self.mgw = mgw self.mgw = mgw
self.stp = stp self.stp = stp
self.vty = None self.vty = None
self.ctrl = None
def start(self): def start(self):
self.log('Starting osmo-bsc') self.log('Starting osmo-bsc')
@ -84,6 +85,9 @@ class OsmoBsc(log.Origin):
self.vty = OsmoBscVty(self) self.vty = OsmoBscVty(self)
self.vty.connect() self.vty.connect()
self.ctrl = OsmoBscCtrl(self)
self.ctrl.connect()
def configure(self): def configure(self):
self.config_file = self.run_dir.new_file('osmo-bsc.cfg') self.config_file = self.run_dir.new_file('osmo-bsc.cfg')
self.dbg(config_file=self.config_file) self.dbg(config_file=self.config_file)
@ -149,10 +153,8 @@ class OsmoBsc(log.Origin):
# over this list, we have a 1:1 match in indexes. # over this list, we have a 1:1 match in indexes.
return self.bts.index(bts) return self.bts.index(bts)
def bts_is_connected(self, bts, use_ctrl=None): def bts_is_connected(self, bts):
if use_ctrl is None: return self.ctrl.bts_is_connected(self.bts_num(bts))
use_ctrl = self.ctrl()
return use_ctrl.bts_is_connected(self.bts_num(bts))
def running(self): def running(self):
return not self.process.terminated() return not self.process.terminated()
@ -161,9 +163,9 @@ class OsmoBsc(log.Origin):
if self.vty is not None: if self.vty is not None:
self.vty.disconnect() self.vty.disconnect()
self.vty = None self.vty = None
if self.ctrl is not None:
def ctrl(self): self.ctrl.disconnect()
return OsmoBscCtrl(self) self.ctrl = None
class OsmoBscCtrl(osmo_ctrl.OsmoCtrl): class OsmoBscCtrl(osmo_ctrl.OsmoCtrl):
def __init__(self, bsc, port=4249): def __init__(self, bsc, port=4249):

View File

@ -49,6 +49,7 @@ class OsmoMsc(log.Origin):
self.mgw = mgw self.mgw = mgw
self.stp = stp self.stp = stp
self.smsc = smsc.Smsc((ip_address.get('addr'), 2775)) self.smsc = smsc.Smsc((ip_address.get('addr'), 2775))
self.ctrl = None
def start(self): def start(self):
self.log('Starting osmo-msc') self.log('Starting osmo-msc')
@ -75,6 +76,9 @@ class OsmoMsc(log.Origin):
self.testenv.remember_to_stop(self.process) self.testenv.remember_to_stop(self.process)
self.process.launch() self.process.launch()
self.ctrl = OsmoMscCtrl(self)
self.ctrl.connect()
def configure(self): def configure(self):
self.config_file = self.run_dir.new_file('osmo-msc.cfg') self.config_file = self.run_dir.new_file('osmo-msc.cfg')
self.dbg(config_file=self.config_file) self.dbg(config_file=self.config_file)
@ -147,7 +151,7 @@ class OsmoMsc(log.Origin):
return all([(imsi in attached) for imsi in imsis]) return all([(imsi in attached) for imsi in imsis])
def imsi_list_attached(self): def imsi_list_attached(self):
return OsmoMscCtrl(self).subscriber_list_active() return self.ctrl.subscriber_list_active()
def set_emergency_call_msisdn(self, msisdn): def set_emergency_call_msisdn(self, msisdn):
self.dbg('Setting Emergency Call MSISDN', msisdn=msisdn) self.dbg('Setting Emergency Call MSISDN', msisdn=msisdn)
@ -156,6 +160,11 @@ class OsmoMsc(log.Origin):
def running(self): def running(self):
return not self.process.terminated() return not self.process.terminated()
def cleanup(self):
if self.ctrl is not None:
self.ctrl.disconnect()
self.ctrl = None
class OsmoMscCtrl(osmo_ctrl.OsmoCtrl): class OsmoMscCtrl(osmo_ctrl.OsmoCtrl):
def __init__(self, msc, port=4255): def __init__(self, msc, port=4255):

View File

@ -36,6 +36,7 @@ class OsmoNitb(log.Origin):
self.ip_address = ip_address self.ip_address = ip_address
self.bts = [] self.bts = []
self.smsc = smsc.Smsc((ip_address.get('addr'), 2775)) self.smsc = smsc.Smsc((ip_address.get('addr'), 2775))
self.ctrl = None
def start(self): def start(self):
self.log('Starting osmo-nitb') self.log('Starting osmo-nitb')
@ -62,6 +63,9 @@ class OsmoNitb(log.Origin):
self.testenv.remember_to_stop(self.process) self.testenv.remember_to_stop(self.process)
self.process.launch() self.process.launch()
self.ctrl = OsmoNitbCtrl(self)
self.ctrl.connect()
def configure(self): def configure(self):
self.config_file = self.run_dir.new_file('osmo-nitb.cfg') self.config_file = self.run_dir.new_file('osmo-nitb.cfg')
self.dbg(config_file=self.config_file) self.dbg(config_file=self.config_file)
@ -134,11 +138,11 @@ class OsmoNitb(log.Origin):
raise log.Error("Auth algo %r selected and no KI specified" % algo) raise log.Error("Auth algo %r selected and no KI specified" % algo)
self.log('Add subscriber', msisdn=msisdn, imsi=modem.imsi()) self.log('Add subscriber', msisdn=msisdn, imsi=modem.imsi())
OsmoNitbCtrl(self).subscriber_add(modem.imsi(), msisdn, modem.ki(), algo) return self.ctrl.subscriber_add(modem.imsi(), msisdn, modem.ki(), algo)
def subscriber_delete(self, modem): def subscriber_delete(self, modem):
self.log('Delete subscriber', imsi=modem.imsi()) self.log('Delete subscriber', imsi=modem.imsi())
OsmoNitbCtrl(self).subscriber_delete(modem.imsi()) return self.ctrl.subscriber_delete(modem.imsi())
def subscriber_attached(self, *modems): def subscriber_attached(self, *modems):
return self.imsi_attached(*[m.imsi() for m in modems]) return self.imsi_attached(*[m.imsi() for m in modems])
@ -149,14 +153,18 @@ class OsmoNitb(log.Origin):
return all([(imsi in attached) for imsi in imsis]) return all([(imsi in attached) for imsi in imsis])
def imsi_list_attached(self): def imsi_list_attached(self):
return OsmoNitbCtrl(self).subscriber_list_active() return self.ctrl.subscriber_list_active()
def bts_is_connected(self, bts): def bts_is_connected(self, bts):
return OsmoNitbCtrl(self).bts_is_connected(self.bts_num(bts)) return self.ctrl.bts_is_connected(self.bts_num(bts))
def running(self): def running(self):
return not self.process.terminated() return not self.process.terminated()
def cleanup(self):
if self.ctrl is not None:
self.ctrl.disconnect()
self.ctrl = None
class OsmoNitbCtrl(osmo_ctrl.OsmoCtrl): class OsmoNitbCtrl(osmo_ctrl.OsmoCtrl):
def __init__(self, nitb, port=4249): def __init__(self, nitb, port=4249):

View File

@ -194,7 +194,9 @@ class TestEnv(log_module.Origin):
from .obj.nitb_osmo import OsmoNitb from .obj.nitb_osmo import OsmoNitb
if ip_address is None: if ip_address is None:
ip_address = self.ip_address() ip_address = self.ip_address()
return OsmoNitb(self, ip_address) nitb_obj = OsmoNitb(self, ip_address)
self.register_for_cleanup(nitb_obj)
return nitb_obj
def hlr(self, ip_address=None): def hlr(self, ip_address=None):
from .obj.hlr_osmo import OsmoHlr from .obj.hlr_osmo import OsmoHlr
@ -230,7 +232,9 @@ class TestEnv(log_module.Origin):
from .obj import msc_osmo from .obj import msc_osmo
if ip_address is None: if ip_address is None:
ip_address = self.ip_address() ip_address = self.ip_address()
return msc_osmo.OsmoMsc(self, hlr, mgcpgw, stp, ip_address) msc_obj = msc_osmo.OsmoMsc(self, hlr, mgcpgw, stp, ip_address)
self.register_for_cleanup(msc_obj)
return msc_obj
def bsc(self, msc, mgw, stp, ip_address=None): def bsc(self, msc, mgw, stp, ip_address=None):
from .obj.bsc_osmo import OsmoBsc from .obj.bsc_osmo import OsmoBsc