Pau Espin Pedrol | e9d4af8 | 2017-11-09 13:01:00 +0100 | [diff] [blame] | 1 | # osmo_gsm_tester: specifics for running an osmo-mgw |
| 2 | # |
| 3 | # Copyright (C) 2017 by sysmocom - s.f.m.c. GmbH |
| 4 | # |
| 5 | # Author: Pau Espin Pedrol <pespin@sysmocom.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 | |
| 20 | import os |
| 21 | import pprint |
| 22 | |
Pau Espin Pedrol | e1a58bd | 2020-04-10 20:46:07 +0200 | [diff] [blame] | 23 | from ..core import log, util, config, template, process |
Pau Espin Pedrol | e8bbcbf | 2020-04-10 19:51:31 +0200 | [diff] [blame] | 24 | from . import pcap_recorder |
Pau Espin Pedrol | e9d4af8 | 2017-11-09 13:01:00 +0100 | [diff] [blame] | 25 | |
| 26 | class OsmoMgw(log.Origin): |
Pau Espin Pedrol | e9d4af8 | 2017-11-09 13:01:00 +0100 | [diff] [blame] | 27 | |
Pau Espin Pedrol | a442cb8 | 2020-05-05 12:54:37 +0200 | [diff] [blame] | 28 | def __init__(self, testenv, ip_address): |
Pau Espin Pedrol | e9d4af8 | 2017-11-09 13:01:00 +0100 | [diff] [blame] | 29 | super().__init__(log.C_RUN, 'osmo-mgw_%s' % ip_address.get('addr')) |
Pau Espin Pedrol | 5860367 | 2018-08-09 13:45:55 +0200 | [diff] [blame] | 30 | self.run_dir = None |
| 31 | self.config_file = None |
| 32 | self.process = None |
Pau Espin Pedrol | a442cb8 | 2020-05-05 12:54:37 +0200 | [diff] [blame] | 33 | self.testenv = testenv |
Pau Espin Pedrol | e9d4af8 | 2017-11-09 13:01:00 +0100 | [diff] [blame] | 34 | self.ip_address = ip_address |
Pau Espin Pedrol | fed578e | 2019-06-19 17:18:43 +0200 | [diff] [blame] | 35 | self.use_osmux = "off" |
Pau Espin Pedrol | e9d4af8 | 2017-11-09 13:01:00 +0100 | [diff] [blame] | 36 | |
| 37 | def start(self): |
| 38 | self.log('Starting osmo-mgw') |
Pau Espin Pedrol | 2a2d846 | 2020-05-11 10:56:52 +0200 | [diff] [blame] | 39 | self.run_dir = util.Dir(self.testenv.test().get_run_dir().new_dir(self.name())) |
Pau Espin Pedrol | e9d4af8 | 2017-11-09 13:01:00 +0100 | [diff] [blame] | 40 | self.configure() |
Pau Espin Pedrol | a442cb8 | 2020-05-05 12:54:37 +0200 | [diff] [blame] | 41 | inst = util.Dir(os.path.abspath(self.testenv.suite().trial().get_inst('osmo-mgw'))) |
Pau Espin Pedrol | e9d4af8 | 2017-11-09 13:01:00 +0100 | [diff] [blame] | 42 | binary = inst.child('bin', 'osmo-mgw') |
| 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 Pedrol | a442cb8 | 2020-05-05 12:54:37 +0200 | [diff] [blame] | 49 | pcap_recorder.PcapRecorder(self.testenv, self.run_dir.new_dir('pcap'), None, |
Pau Espin Pedrol | e9d4af8 | 2017-11-09 13:01:00 +0100 | [diff] [blame] | 50 | 'host %s and port not 22' % self.addr()) |
| 51 | |
| 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 Pedrol | a442cb8 | 2020-05-05 12:54:37 +0200 | [diff] [blame] | 59 | self.testenv.remember_to_stop(self.process) |
Pau Espin Pedrol | e9d4af8 | 2017-11-09 13:01:00 +0100 | [diff] [blame] | 60 | self.process.launch() |
| 61 | |
| 62 | def configure(self): |
| 63 | self.config_file = self.run_dir.new_file('osmo-mgw.cfg') |
| 64 | self.dbg(config_file=self.config_file) |
| 65 | |
| 66 | values = dict(mgw=config.get_defaults('mgw')) |
Pau Espin Pedrol | a442cb8 | 2020-05-05 12:54:37 +0200 | [diff] [blame] | 67 | config.overlay(values, self.testenv.suite().config()) |
Pau Espin Pedrol | fed578e | 2019-06-19 17:18:43 +0200 | [diff] [blame] | 68 | config.overlay(values, { 'mgw': { |
| 69 | 'ip_address': self.ip_address, |
| 70 | 'use_osmux': self.use_osmux |
| 71 | } |
| 72 | }) |
Pau Espin Pedrol | e9d4af8 | 2017-11-09 13:01:00 +0100 | [diff] [blame] | 73 | |
| 74 | self.dbg('MGW CONFIG:\n' + pprint.pformat(values)) |
| 75 | |
| 76 | with open(self.config_file, 'w') as f: |
| 77 | r = template.render('osmo-mgw.cfg', values) |
| 78 | self.dbg(r) |
| 79 | f.write(r) |
| 80 | |
| 81 | def addr(self): |
| 82 | return self.ip_address.get('addr') |
| 83 | |
| 84 | def conf_for_client(self): |
| 85 | return dict(mgw=dict(ip_address=self.ip_address)) |
| 86 | |
| 87 | def running(self): |
| 88 | return not self.process.terminated() |
| 89 | |
Pau Espin Pedrol | fed578e | 2019-06-19 17:18:43 +0200 | [diff] [blame] | 90 | def set_use_osmux(self, use=False, force=False): |
| 91 | if not use: |
| 92 | self.use_osmux = "off" |
| 93 | else: |
| 94 | if not force: |
| 95 | self.use_osmux = "on" |
| 96 | else: |
| 97 | self.use_osmux = "only" |
| 98 | |
| 99 | |
Pau Espin Pedrol | e9d4af8 | 2017-11-09 13:01:00 +0100 | [diff] [blame] | 100 | # vim: expandtab tabstop=4 shiftwidth=4 |