blob: 83a2f753598fd2bdb733f5a302355db501ee5767 [file] [log] [blame]
Neels Hofmeyr38b051c2017-06-13 16:26:06 +02001# osmo_gsm_tester: specifics for running an osmo-stp
2#
3# Copyright (C) 2017 by sysmocom - s.f.m.c. GmbH
4#
5# Author: Neels Hofmeyr <neels@hofmeyr.de>
6#
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as
9# published by the Free Software Foundation, either version 3 of the
10# License, or (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20import os
21import pprint
22
Pau Espin Pedrole1a58bd2020-04-10 20:46:07 +020023from ..core import log, util, config, template, process
Pau Espin Pedrole8bbcbf2020-04-10 19:51:31 +020024from . import pcap_recorder
Neels Hofmeyr38b051c2017-06-13 16:26:06 +020025
26class OsmoStp(log.Origin):
Neels Hofmeyr38b051c2017-06-13 16:26:06 +020027
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020028 def __init__(self, testenv, ip_address):
Neels Hofmeyr38b051c2017-06-13 16:26:06 +020029 super().__init__(log.C_RUN, 'osmo-stp_%s' % ip_address.get('addr'))
Pau Espin Pedrol58603672018-08-09 13:45:55 +020030 self.run_dir = None
31 self.config_file = None
32 self.process = None
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020033 self.testenv = testenv
Neels Hofmeyr38b051c2017-06-13 16:26:06 +020034 self.ip_address = ip_address
35
36 def start(self):
37 self.log('Starting osmo-stp')
Neels Hofmeyr38b051c2017-06-13 16:26:06 +020038 self.configure()
39
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020040 inst = util.Dir(os.path.abspath(self.testenv.suite().trial().get_inst('osmo-stp')))
Neels Hofmeyr38b051c2017-06-13 16:26:06 +020041
42 binary = inst.child('bin', 'osmo-stp')
43 if not os.path.isfile(binary):
44 raise RuntimeError('Binary missing: %r' % binary)
45 lib = inst.child('lib')
46 if not os.path.isdir(lib):
47 raise RuntimeError('No lib/ in %r' % inst)
48
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020049 pcap_recorder.PcapRecorder(self.testenv, self.run_dir.new_dir('pcap'), None,
Pau Espin Pedrol1e1d3812017-11-16 18:06:37 +010050 'host %s and port not 22' % self.addr())
Neels Hofmeyr38b051c2017-06-13 16:26:06 +020051
52 env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) }
53
54 self.dbg(run_dir=self.run_dir, binary=binary, env=env)
55 self.process = process.Process(self.name(), self.run_dir,
56 (binary, '-c',
57 os.path.abspath(self.config_file)),
58 env=env)
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020059 self.testenv.remember_to_stop(self.process)
Neels Hofmeyr38b051c2017-06-13 16:26:06 +020060 self.process.launch()
61
62 def configure(self):
Pau Espin Pedrol166dc102020-06-04 18:44:42 +020063 self.run_dir = util.Dir(self.testenv.test().get_run_dir().new_dir(self.name()))
Neels Hofmeyr38b051c2017-06-13 16:26:06 +020064 self.config_file = self.run_dir.new_file('osmo-stp.cfg')
65 self.dbg(config_file=self.config_file)
66
67 values = dict(stp=config.get_defaults('stp'))
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020068 config.overlay(values, self.testenv.suite().config())
Neels Hofmeyr38b051c2017-06-13 16:26:06 +020069 config.overlay(values, dict(stp=dict(ip_address=self.ip_address)))
70
71 self.dbg('STP CONFIG:\n' + pprint.pformat(values))
72
73 with open(self.config_file, 'w') as f:
74 r = template.render('osmo-stp.cfg', values)
75 self.dbg(r)
76 f.write(r)
77
Pau Espin Pedrol1e1d3812017-11-16 18:06:37 +010078 def conf_for_client(self):
79 return dict(stp=dict(ip_address=self.ip_address))
80
Neels Hofmeyr38b051c2017-06-13 16:26:06 +020081 def addr(self):
82 return self.ip_address.get('addr')
83
84 def running(self):
85 return not self.process.terminated()
86
87# vim: expandtab tabstop=4 shiftwidth=4