blob: 19241ccfd7a768c96ae7a1dbc4c98ce741d6d36c [file] [log] [blame]
Neels Hofmeyr798e5922017-05-18 15:24:02 +02001# osmo_gsm_tester: specifics for running an osmo-hlr
2#
3# Copyright (C) 2016-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
Harald Welte27205342017-06-03 09:51:45 +02008# it under the terms of the GNU General Public License as
Neels Hofmeyr798e5922017-05-18 15:24:02 +02009# 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
Harald Welte27205342017-06-03 09:51:45 +020015# GNU General Public License for more details.
Neels Hofmeyr798e5922017-05-18 15:24:02 +020016#
Harald Welte27205342017-06-03 09:51:45 +020017# You should have received a copy of the GNU General Public License
Neels Hofmeyr798e5922017-05-18 15:24:02 +020018# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20import os
21import re
22import pprint
23
24from . import log, util, config, template, process, osmo_ctrl, pcap_recorder
25
26class OsmoHlr(log.Origin):
27 suite_run = None
28 ip_address = None
29 run_dir = None
30 config_file = None
31 process = None
32 next_subscriber_id = 1
33
34 def __init__(self, suite_run, ip_address):
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020035 super().__init__(log.C_RUN, 'osmo-hlr_%s' % ip_address.get('addr'))
Neels Hofmeyr798e5922017-05-18 15:24:02 +020036 self.suite_run = suite_run
37 self.ip_address = ip_address
Neels Hofmeyr798e5922017-05-18 15:24:02 +020038 self.bts = []
39
40 def start(self):
41 self.log('Starting osmo-hlr')
Pau Espin Pedrold0912332017-06-14 13:27:08 +020042 self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name()))
Neels Hofmeyr798e5922017-05-18 15:24:02 +020043 self.configure()
44
45 inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-hlr')))
46
47 binary = inst.child('bin', 'osmo-hlr')
48 if not os.path.isfile(binary):
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020049 raise log.Error('Binary missing:', binary)
Neels Hofmeyr798e5922017-05-18 15:24:02 +020050 lib = inst.child('lib')
51 if not os.path.isdir(lib):
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020052 raise log.Error('No lib/ in', inst)
Neels Hofmeyr798e5922017-05-18 15:24:02 +020053
54 # bootstrap an empty hlr.db
55 self.db_file = self.run_dir.new_file('hlr.db')
56 sql_input = inst.child('share/doc/osmo-hlr/hlr.sql')
57 if not os.path.isfile(sql_input):
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020058 raise log.Error('hlr.sql missing:', sql_input)
Neels Hofmeyr798e5922017-05-18 15:24:02 +020059 self.run_local('create_hlr_db', ('/bin/sh', '-c', 'sqlite3 %r < %r' % (self.db_file, sql_input)))
60
61 iface = util.ip_to_iface(self.addr())
62 pcap_recorder.PcapRecorder(self.suite_run, self.run_dir.new_dir('pcap'), iface,
63 'host %s' % self.addr())
64
65 env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) }
66
67 self.dbg(run_dir=self.run_dir, binary=binary, env=env)
68 self.process = process.Process(self.name(), self.run_dir,
69 (binary,
70 '-c', os.path.abspath(self.config_file),
71 '--database', self.db_file),
72 env=env)
73 self.suite_run.remember_to_stop(self.process)
74 self.process.launch()
75
76 def configure(self):
77 self.config_file = self.run_dir.new_file('osmo-hlr.cfg')
78 self.dbg(config_file=self.config_file)
79
80 values = dict(hlr=config.get_defaults('hlr'))
81 config.overlay(values, self.suite_run.config())
82 config.overlay(values, dict(hlr=dict(ip_address=self.ip_address)))
83
84 self.dbg('HLR CONFIG:\n' + pprint.pformat(values))
85
86 with open(self.config_file, 'w') as f:
87 r = template.render('osmo-hlr.cfg', values)
88 self.dbg(r)
89 f.write(r)
90
91 def addr(self):
92 return self.ip_address.get('addr')
93
94 def running(self):
95 return not self.process.terminated()
96
97 def run_local(self, name, popen_args):
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020098 run_dir = self.run_dir.new_dir(name)
99 proc = process.Process(name, run_dir, popen_args)
100 proc.launch()
101 proc.wait()
102 if proc.result != 0:
103 log.ctx(proc)
104 raise log.Error('Exited in error')
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200105
106 def run_sql_file(self, name, sql_file):
107 self.run_local(name, ('/bin/sh', '-c', 'sqlite3 %r < %r' % (self.db_file, sql_file)))
108
109 def run_sql(self, name, sql):
110 self.dbg('SQL:', repr(sql))
111 sql_file = self.run_dir.new_file(name + '.sql')
112 with open(sql_file, 'w') as f:
113 f.write(sql)
114 self.run_sql_file(name, sql_file)
115
116 def subscriber_add(self, modem, msisdn=None):
117 if msisdn is None:
118 msisdn = self.suite_run.resources_pool.next_msisdn(modem)
119 modem.set_msisdn(msisdn)
120 subscriber_id = self.next_subscriber_id
121 self.next_subscriber_id += 1
122 self.log('Add subscriber', msisdn=msisdn, imsi=modem.imsi(), subscriber_id=subscriber_id)
123 self.run_sql('add_subscriber',
124 'insert into subscriber (id, imsi, msisdn) values (%r, %r, %r);'
125 % (subscriber_id, modem.imsi(), modem.msisdn))
126 return subscriber_id
127
Neels Hofmeyr7b02ed02017-06-08 23:08:59 +0200128 def conf_for_msc(self):
129 return dict(hlr=dict(ip_address=self.ip_address))
130
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200131# vim: expandtab tabstop=4 shiftwidth=4