blob: c22ca863c022b8c150abecf980f80db399d4a6c9 [file] [log] [blame]
Pau Espin Pedrol30ceb5c2017-08-31 18:30:11 +02001# osmo_gsm_tester: specifics for running an openggsn
2#
3# Copyright (C) 2016-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
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
Pau Espin Pedrol30ceb5c2017-08-31 18:30:11 +020025
26class OsmoGgsn(log.Origin):
Pau Espin Pedrol30ceb5c2017-08-31 18:30:11 +020027
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020028 def __init__(self, testenv, ip_address):
Pau Espin Pedrol30ceb5c2017-08-31 18:30:11 +020029 super().__init__(log.C_RUN, 'osmo-ggsn_%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
Pau Espin Pedrol30ceb5c2017-08-31 18:30:11 +020034 self.ip_address = ip_address
35
36 def start(self):
37 self.log('Starting osmo-ggsn')
Pau Espin Pedrol2a2d8462020-05-11 10:56:52 +020038 self.run_dir = util.Dir(self.testenv.test().get_run_dir().new_dir(self.name()))
Pau Espin Pedrol30ceb5c2017-08-31 18:30:11 +020039 self.configure()
40
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020041 inst = util.Dir(os.path.abspath(self.testenv.suite().trial().get_inst('osmo-ggsn')))
Pau Espin Pedrol30ceb5c2017-08-31 18:30:11 +020042
43 binary = inst.child('bin', 'osmo-ggsn')
44 if not os.path.isfile(binary):
45 raise log.Error('Binary missing:', binary)
46 lib = inst.child('lib')
47 if not os.path.isdir(lib):
48 raise log.Error('No lib/ in', inst)
49
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020050 pcap_recorder.PcapRecorder(self.testenv, self.run_dir.new_dir('pcap'), None,
Pau Espin Pedrol30ceb5c2017-08-31 18:30:11 +020051 'host %s' % self.addr())
52
Pau Espin Pedrolf0b8e372017-12-13 16:08:35 +010053 env = {}
54
55 # setting capabilities will later disable use of LD_LIBRARY_PATH from ELF loader -> modify RPATH instead.
56 self.log('Setting RPATH for osmo-ggsn')
57 util.change_elf_rpath(binary, util.prepend_library_path(lib), self.run_dir.new_dir('patchelf'))
58 # osmo-ggsn requires CAP_NET_ADMIN to create tunnel devices: ioctl(TUNSETIFF):
59 self.log('Applying CAP_NET_ADMIN capability to osmo-ggsn')
60 util.setcap_net_admin(binary, self.run_dir.new_dir('setcap_net_admin'))
Pau Espin Pedrol30ceb5c2017-08-31 18:30:11 +020061
62 self.dbg(run_dir=self.run_dir, binary=binary, env=env)
63 self.process = process.Process(self.name(), self.run_dir,
64 (binary,
65 '-c', os.path.abspath(self.config_file)),
66 env=env)
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020067 self.testenv.remember_to_stop(self.process)
Pau Espin Pedrol30ceb5c2017-08-31 18:30:11 +020068 self.process.launch()
69
70 def configure(self):
71 self.config_file = self.run_dir.new_file('osmo-ggsn.cfg')
72 self.dbg(config_file=self.config_file)
73
74 values = dict(ggsn=config.get_defaults('ggsn'))
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020075 config.overlay(values, self.testenv.suite().config())
Pau Espin Pedrol30ceb5c2017-08-31 18:30:11 +020076 config.overlay(values, dict(ggsn=dict(ip_address=self.ip_address)))
77 config.overlay(values, dict(ggsn=dict(statedir=self.run_dir.new_dir('statedir'))))
78
79 self.dbg('GGSN CONFIG:\n' + pprint.pformat(values))
80
81 with open(self.config_file, 'w') as f:
82 r = template.render('osmo-ggsn.cfg', values)
83 self.dbg(r)
84 f.write(r)
85
86 def conf_for_client(self):
87 return dict(ggsn=dict(ip_address=self.ip_address))
88
89 def addr(self):
90 return self.ip_address.get('addr')
91
92 def running(self):
93 return not self.process.terminated()
94
95# vim: expandtab tabstop=4 shiftwidth=4