Pau Espin Pedrol | c9817a5 | 2017-12-13 19:52:27 +0100 | [diff] [blame] | 1 | # osmo_gsm_tester: base classes to share code among BTS subclasses. |
| 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 | |
| 20 | import os |
Pau Espin Pedrol | c9817a5 | 2017-12-13 19:52:27 +0100 | [diff] [blame] | 21 | import tempfile |
| 22 | from abc import ABCMeta, abstractmethod |
Pau Espin Pedrol | e1a58bd | 2020-04-10 20:46:07 +0200 | [diff] [blame] | 23 | from ..core import log |
Pau Espin Pedrol | e8bbcbf | 2020-04-10 19:51:31 +0200 | [diff] [blame] | 24 | from . import bts, pcu_osmo |
Pau Espin Pedrol | c9817a5 | 2017-12-13 19:52:27 +0100 | [diff] [blame] | 25 | |
Pau Espin Pedrol | 52ad3a6 | 2018-03-08 17:50:14 +0100 | [diff] [blame] | 26 | class OsmoBts(bts.Bts, metaclass=ABCMeta): |
Pau Espin Pedrol | c9817a5 | 2017-12-13 19:52:27 +0100 | [diff] [blame] | 27 | |
| 28 | ############## |
| 29 | # PROTECTED |
| 30 | ############## |
Pau Espin Pedrol | e519462 | 2018-05-07 13:36:58 +0200 | [diff] [blame] | 31 | def __init__(self, suite_run, conf, name, defaults_cfg_name): |
| 32 | super().__init__(suite_run, conf, name, defaults_cfg_name) |
Pau Espin Pedrol | 5860367 | 2018-08-09 13:45:55 +0200 | [diff] [blame] | 33 | self._pcu = None |
| 34 | self.proc_bts = None |
Pau Espin Pedrol | c9817a5 | 2017-12-13 19:52:27 +0100 | [diff] [blame] | 35 | if len(self.pcu_socket_path().encode()) > 107: |
| 36 | raise log.Error('Path for pcu socket is longer than max allowed len for unix socket path (107):', self.pcu_socket_path()) |
| 37 | |
| 38 | ######################## |
| 39 | # PUBLIC - INTERNAL API |
| 40 | ######################## |
| 41 | @abstractmethod |
| 42 | def conf_for_bsc(self): |
Pau Espin Pedrol | 52ad3a6 | 2018-03-08 17:50:14 +0100 | [diff] [blame] | 43 | # coming from bts.Bts, we forward the implementation to children. |
Pau Espin Pedrol | c9817a5 | 2017-12-13 19:52:27 +0100 | [diff] [blame] | 44 | pass |
| 45 | |
| 46 | @abstractmethod |
| 47 | def pcu_socket_path(self): |
| 48 | 'Used by pcu objects to get path to socket.' |
| 49 | pass |
| 50 | |
| 51 | @abstractmethod |
| 52 | def create_pcu(self): |
| 53 | 'Used by base class. Subclass can create different pcu implementations.' |
| 54 | pass |
| 55 | |
Pau Espin Pedrol | c9817a5 | 2017-12-13 19:52:27 +0100 | [diff] [blame] | 56 | ################### |
| 57 | # PUBLIC (test API included) |
| 58 | ################### |
| 59 | @abstractmethod |
Pau Espin Pedrol | b1526b9 | 2018-05-22 20:32:30 +0200 | [diff] [blame] | 60 | def start(self, keepalive=False): |
Pau Espin Pedrol | 52ad3a6 | 2018-03-08 17:50:14 +0100 | [diff] [blame] | 61 | # coming from bts.Bts, we forward the implementation to children. |
Pau Espin Pedrol | c9817a5 | 2017-12-13 19:52:27 +0100 | [diff] [blame] | 62 | pass |
| 63 | |
Pau Espin Pedrol | f2e5972 | 2018-10-02 14:41:22 +0200 | [diff] [blame] | 64 | @abstractmethod |
Pau Espin Pedrol | 4fbdc35 | 2018-03-08 17:47:35 +0100 | [diff] [blame] | 65 | def ready_for_pcu(self): |
Pau Espin Pedrol | f2e5972 | 2018-10-02 14:41:22 +0200 | [diff] [blame] | 66 | 'Used by tests to know when BTS is prepared and PCU can be started.' |
| 67 | pass |
Pau Espin Pedrol | 4fbdc35 | 2018-03-08 17:47:35 +0100 | [diff] [blame] | 68 | |
Pau Espin Pedrol | c9817a5 | 2017-12-13 19:52:27 +0100 | [diff] [blame] | 69 | def pcu(self): |
| 70 | if self._pcu is None: |
Pau Espin Pedrol | c1b3278 | 2017-12-15 11:49:01 +0100 | [diff] [blame] | 71 | self._pcu = self.create_pcu() |
Pau Espin Pedrol | c9817a5 | 2017-12-13 19:52:27 +0100 | [diff] [blame] | 72 | return self._pcu |
| 73 | |
Pau Espin Pedrol | c9817a5 | 2017-12-13 19:52:27 +0100 | [diff] [blame] | 74 | class OsmoBtsMainUnit(OsmoBts, metaclass=ABCMeta): |
| 75 | ############## |
| 76 | # PROTECTED |
| 77 | ############## |
Pau Espin Pedrol | c9817a5 | 2017-12-13 19:52:27 +0100 | [diff] [blame] | 78 | |
Pau Espin Pedrol | e519462 | 2018-05-07 13:36:58 +0200 | [diff] [blame] | 79 | def __init__(self, suite_run, conf, name, defaults_cfg_name): |
Pau Espin Pedrol | 5860367 | 2018-08-09 13:45:55 +0200 | [diff] [blame] | 80 | self.pcu_sk_tmp_dir = None |
Pau Espin Pedrol | e519462 | 2018-05-07 13:36:58 +0200 | [diff] [blame] | 81 | super().__init__(suite_run, conf, name, defaults_cfg_name) |
Pau Espin Pedrol | c9817a5 | 2017-12-13 19:52:27 +0100 | [diff] [blame] | 82 | |
| 83 | ######################## |
| 84 | # PUBLIC - INTERNAL API |
| 85 | ######################## |
| 86 | @abstractmethod |
| 87 | def conf_for_bsc(self): |
Pau Espin Pedrol | 52ad3a6 | 2018-03-08 17:50:14 +0100 | [diff] [blame] | 88 | # coming from bts.Bts, we forward the implementation to children. |
Pau Espin Pedrol | c9817a5 | 2017-12-13 19:52:27 +0100 | [diff] [blame] | 89 | pass |
| 90 | |
| 91 | def cleanup(self): |
| 92 | if self.pcu_sk_tmp_dir: |
| 93 | try: |
| 94 | os.remove(self.pcu_socket_path()) |
| 95 | except OSError: |
| 96 | pass |
| 97 | os.rmdir(self.pcu_sk_tmp_dir) |
| 98 | |
| 99 | def create_pcu(self): |
| 100 | return pcu_osmo.OsmoPcu(self.suite_run, self, self.conf) |
| 101 | |
| 102 | def pcu_socket_path(self): |
| 103 | if self.pcu_sk_tmp_dir is None: |
| 104 | self.pcu_sk_tmp_dir = tempfile.mkdtemp('', 'ogtpcusk') |
| 105 | return os.path.join(self.pcu_sk_tmp_dir, 'pcu_bts') |
| 106 | |
| 107 | ################### |
| 108 | # PUBLIC (test API included) |
| 109 | ################### |
Pau Espin Pedrol | f2e5972 | 2018-10-02 14:41:22 +0200 | [diff] [blame] | 110 | def ready_for_pcu(self): |
| 111 | if not self.proc_bts or not self.proc_bts.is_running: |
| 112 | return False |
| 113 | return os.path.exists(self.pcu_socket_path()) |
| 114 | |
Pau Espin Pedrol | c9817a5 | 2017-12-13 19:52:27 +0100 | [diff] [blame] | 115 | @abstractmethod |
Pau Espin Pedrol | b1526b9 | 2018-05-22 20:32:30 +0200 | [diff] [blame] | 116 | def start(self, keepalive=False): |
Pau Espin Pedrol | 52ad3a6 | 2018-03-08 17:50:14 +0100 | [diff] [blame] | 117 | # coming from bts.Bts, we forward the implementation to children. |
Pau Espin Pedrol | c9817a5 | 2017-12-13 19:52:27 +0100 | [diff] [blame] | 118 | pass |