blob: a4d81c11cc7dbf557c4a9f4452ab5df5c0a9540c [file] [log] [blame]
Holger Hans Peter Freytherb484aab2018-08-29 04:28:33 +01001# ms_driver: Launch OsmocomBB mobile's virtually connected to a BTS
2#
3# Copyright (C) 2018 by Holger Hans Peter Freyther
4#
5# This program is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as
7# published by the Free Software Foundation, either version 3 of the
8# License, or (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18from datetime import timedelta
19from . import log
20from osmo_ms_driver.cdf import cdfs
21from osmo_ms_driver.event_server import EventServer
22from osmo_ms_driver.simple_loop import SimpleLoop
23from osmo_ms_driver.location_update_test import MassUpdateLocationTest
24
25import os.path
26import shutil
27import tempfile
28
29class Subscriber(log.Origin):
30 def __init__(self, imsi, ki):
31 super().__init__(log.C_RUN, 'subscriber')
32 self._imsi = imsi
33 self._ki = ki
34 self._auth_algo = "comp128v1"
35 self._msisdn = None
36
37 def msisdn(self):
38 return self._msisdn
39
40 def set_msisdn(self, msisdn):
41 self._msisdn = msisdn
42
43 def imsi(self):
44 return self._imsi
45
46 def ki(self):
47 return self._ki
48
49 def auth_algo(self):
50 return self._auth_algo
51
52class MsDriver(log.Origin):
53
54 def __init__(self, suite_run):
55 super().__init__(log.C_RUN, 'ms-driver')
56 self._suite_run = suite_run
57
58 # TODO: take config out of the test scenario
59 self._num_ms = 10
60 self._time_start = timedelta(seconds=60)
61 self._time_step = timedelta(milliseconds=100)
62 self._test_duration = timedelta(seconds=120)
63 self._cdf = cdfs["ease_in_out"](self._time_start, self._time_step)
64 self._loop = SimpleLoop()
65 self._suite_run.remember_to_stop(self)
66 self._test_case = None
Holger Hans Peter Freytherd6d32062018-11-05 03:41:38 +000067 self.event_server_sk_tmp_dir = None
Holger Hans Peter Freytherb484aab2018-08-29 04:28:33 +010068
69 if len(self.event_server_path().encode()) > 107:
70 raise log.Error('Path for event_server socket is longer than max allowed len for unix socket path (107):', self.event_server_path())
71
72 def event_server_path(self):
73 if self.event_server_sk_tmp_dir is None:
74 self.event_server_sk_tmp_dir = tempfile.mkdtemp('', 'ogteventserversk')
75 return os.path.join(self.event_server_sk_tmp_dir, 'osmo_ms_driver.unix')
76
77 def configure(self):
78 """
79 Configures the subscribers, tests and registration server. Needs to be
80 called after the complete configuration of this driver.
81 """
82 event_server_path = self.event_server_path()
83
84 self._ev_server = EventServer("ev_server", event_server_path)
85 self._ev_server.listen(self._loop)
86 self._test_case = MassUpdateLocationTest("mass", self._num_ms, self._cdf, self._ev_server, self.event_server_sk_tmp_dir)
87
88 # TODO: We should pass subscribers down to the test and not get it from
89 # there.
90 self._subs = [Subscriber(imsi=mob.imsi(), ki=mob.ki()) for mob in self._test_case.mobiles()]
91
92
93 def ms_subscribers(self):
94 """
95 Returns a list of 'subscribers' that were configured in the
96 current scenario.
97 """
98 if not hasattr(self, '_subs'):
99 self.configure()
100 return self._subs
101
102 def run_test(self):
103 """
104 Runs the configured tests by starting the configured amount of mobile
105 devices according to their schedule. Returns once all tests succeeded
106 or the configured timeout has passed.
107 """
108 if not hasattr(self, '_subs'):
109 self.configure()
110 self._test_case.run_test(self._loop, self._test_duration)
111
112 def print_stats(self):
113 """
114 Prints statistics about the test run.
115 """
116 self._test_case.print_stats()
117
118 def cleanup(self):
119 """
120 Stops the testcase and all launched processes. Called by the
121 suite.
122 """
123
124 # Clean-up the temporary directory.
125 if self.event_server_sk_tmp_dir:
126 shutil.rmtree(path=self.event_server_sk_tmp_dir)
127
128 if not self._test_case:
129 return
130 self._test_case.stop_all()
131
132# vim: expandtab tabstop=4 shiftwidth=4