blob: 2dfa15534ca1b767ac4afe375d6dc32e1ef6b062 [file] [log] [blame]
Oliver Smith6dbdf142019-12-10 13:21:11 +01001#!/usr/bin/env python3
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +02002
3# (C) 2013 by Katerina Barone-Adesi <kat.obsc@gmail.com>
4# (C) 2013 by Holger Hans Peter Freyther
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 published by
7# the Free Software Foundation, either version 3 of the License, or
8# (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
Max3e676892016-11-16 14:55:21 +010018import os, sys
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +020019import time
20import unittest
Jacob Erlbeck6cb2ccc2013-08-14 11:10:34 +020021import socket
Neels Hofmeyrbcfee2a2017-02-03 16:09:17 +010022import subprocess
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +020023
24import osmopy.obscvty as obscvty
25import osmopy.osmoutil as osmoutil
Max60383a12017-11-23 16:33:33 +010026from osmopy.osmo_ipa import IPA
Max3e676892016-11-16 14:55:21 +010027
Neels Hofmeyr476c4bb2017-02-24 17:55:11 +010028# to be able to find $top_srcdir/doc/...
29confpath = os.path.join(sys.path[0], '..')
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +020030
31class TestVTYBase(unittest.TestCase):
32
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020033 def checkForEndAndExit(self):
34 res = self.vty.command("list")
35 #print ('looking for "exit"\n')
Oliver Smith6dbdf142019-12-10 13:21:11 +010036 self.assertTrue(res.find(' exit\r') > 0)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020037 #print 'found "exit"\nlooking for "end"\n'
Oliver Smith6dbdf142019-12-10 13:21:11 +010038 self.assertTrue(res.find(' end\r') > 0)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020039 #print 'found "end"\n'
40
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +020041 def vty_command(self):
42 raise Exception("Needs to be implemented by a subclass")
43
44 def vty_app(self):
45 raise Exception("Needs to be implemented by a subclass")
46
47 def setUp(self):
48 osmo_vty_cmd = self.vty_command()[:]
49 config_index = osmo_vty_cmd.index('-c')
50 if config_index:
51 cfi = config_index + 1
52 osmo_vty_cmd[cfi] = os.path.join(confpath, osmo_vty_cmd[cfi])
53
54 try:
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +020055 self.proc = osmoutil.popen_devnull(osmo_vty_cmd)
56 except OSError:
Oliver Smith6dbdf142019-12-10 13:21:11 +010057 print("Current directory: %s" % os.getcwd(), file=sys.stderr)
58 print("Consider setting -b", file=sys.stderr)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +020059
60 appstring = self.vty_app()[2]
61 appport = self.vty_app()[0]
62 self.vty = obscvty.VTYInteract(appstring, "127.0.0.1", appport)
63
64 def tearDown(self):
Neels Hofmeyr40a91b32017-02-24 17:54:22 +010065 if self.vty:
66 self.vty._close_socket()
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +020067 self.vty = None
Vadim Yanitskiyd3abcab2023-06-01 20:06:51 +070068 rc = osmoutil.end_proc(self.proc)
69 if rc is not None and rc != 0:
70 raise Exception("Process returned %d" % rc)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +020071
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020072class TestVTYMSC(TestVTYBase):
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +020073
74 def vty_command(self):
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020075 return ["./src/osmo-msc/osmo-msc", "-c",
76 "doc/examples/osmo-msc/osmo-msc.cfg"]
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +020077
78 def vty_app(self):
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020079 return (4254, "./src/osmo-msc/osmo-msc", "OsmoMSC", "msc")
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +020080
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020081 def testConfigNetworkTree(self, include_bsc_items=True):
82 self.vty.enable()
83 self.assertTrue(self.vty.verify("configure terminal",['']))
Oliver Smith6dbdf142019-12-10 13:21:11 +010084 self.assertEqual(self.vty.node(), 'config')
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020085 self.checkForEndAndExit()
86 self.assertTrue(self.vty.verify("network",['']))
Oliver Smith6dbdf142019-12-10 13:21:11 +010087 self.assertEqual(self.vty.node(), 'config-net')
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020088 self.checkForEndAndExit()
89 self.vty.command("write terminal")
90 self.assertTrue(self.vty.verify("exit",['']))
Oliver Smith6dbdf142019-12-10 13:21:11 +010091 self.assertEqual(self.vty.node(), 'config')
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020092 self.assertTrue(self.vty.verify("exit",['']))
93 self.assertTrue(self.vty.node() is None)
Jacob Erlbeck96903c42013-09-02 13:17:14 +020094
Holger Hans Peter Freyther0df1ab92013-09-02 20:58:38 +020095 def checkForSmpp(self):
96 """SMPP is not always enabled, check if it is"""
97 res = self.vty.command("list")
98 return "smpp" in res
99
Holger Hans Peter Freyther42cf2e02015-07-06 16:41:30 +0200100 def testSmppFirst(self):
Holger Hans Peter Freyther42cf2e02015-07-06 16:41:30 +0200101 # enable the configuration
102 self.vty.enable()
103 self.vty.command("configure terminal")
Holger Hans Peter Freythera2c41c42015-07-13 11:23:53 +0200104
105 if not self.checkForSmpp():
106 return
107
Holger Hans Peter Freyther42cf2e02015-07-06 16:41:30 +0200108 self.vty.command("smpp")
109
110 # check the default
111 res = self.vty.command("write terminal")
Oliver Smith6dbdf142019-12-10 13:21:11 +0100112 self.assertTrue(res.find(' no smpp-first') > 0)
Holger Hans Peter Freyther42cf2e02015-07-06 16:41:30 +0200113
114 self.vty.verify("smpp-first", [''])
115 res = self.vty.command("write terminal")
Oliver Smith6dbdf142019-12-10 13:21:11 +0100116 self.assertTrue(res.find(' smpp-first') > 0)
117 self.assertEqual(res.find('no smpp-first'), -1)
Holger Hans Peter Freyther42cf2e02015-07-06 16:41:30 +0200118
119 self.vty.verify("no smpp-first", [''])
120 res = self.vty.command("write terminal")
Oliver Smith6dbdf142019-12-10 13:21:11 +0100121 self.assertTrue(res.find('no smpp-first') > 0)
Holger Hans Peter Freyther42cf2e02015-07-06 16:41:30 +0200122
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200123 def testVtyTree(self):
124 self.vty.enable()
125 self.assertTrue(self.vty.verify("configure terminal", ['']))
Oliver Smith6dbdf142019-12-10 13:21:11 +0100126 self.assertEqual(self.vty.node(), 'config')
Jacob Erlbeck6e919db2013-10-29 09:30:31 +0100127 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200128 self.assertTrue(self.vty.verify('mncc-int', ['']))
Oliver Smith6dbdf142019-12-10 13:21:11 +0100129 self.assertEqual(self.vty.node(), 'config-mncc-int')
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200130 self.checkForEndAndExit()
131 self.assertTrue(self.vty.verify('exit', ['']))
Holger Hans Peter Freyther0df1ab92013-09-02 20:58:38 +0200132
133 if self.checkForSmpp():
Oliver Smith6dbdf142019-12-10 13:21:11 +0100134 self.assertEqual(self.vty.node(), 'config')
Jacob Erlbeck75877272013-10-23 11:24:14 +0200135 self.assertTrue(self.vty.verify('smpp', ['']))
Oliver Smith6dbdf142019-12-10 13:21:11 +0100136 self.assertEqual(self.vty.node(), 'config-smpp')
Jacob Erlbeck6e919db2013-10-29 09:30:31 +0100137 self.checkForEndAndExit()
Jacob Erlbeck75877272013-10-23 11:24:14 +0200138 self.assertTrue(self.vty.verify("exit", ['']))
Holger Hans Peter Freyther0df1ab92013-09-02 20:58:38 +0200139
Oliver Smith6dbdf142019-12-10 13:21:11 +0100140 self.assertEqual(self.vty.node(), 'config')
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200141 self.assertTrue(self.vty.verify("exit", ['']))
142 self.assertTrue(self.vty.node() is None)
143
144 # Check searching for outer node's commands
145 self.vty.command("configure terminal")
146 self.vty.command('mncc-int')
Holger Hans Peter Freyther0df1ab92013-09-02 20:58:38 +0200147
148 if self.checkForSmpp():
149 self.vty.command('smpp')
Oliver Smith6dbdf142019-12-10 13:21:11 +0100150 self.assertEqual(self.vty.node(), 'config-smpp')
Holger Hans Peter Freyther0df1ab92013-09-02 20:58:38 +0200151 self.vty.command('mncc-int')
152
Oliver Smith6dbdf142019-12-10 13:21:11 +0100153 self.assertEqual(self.vty.node(), 'config-mncc-int')
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200154
Max0c1bc262016-04-20 12:06:06 +0200155 def testSi2Q(self):
156 self.vty.enable()
157 self.vty.command("configure terminal")
158 self.vty.command("network")
159 self.vty.command("bts 0")
160 before = self.vty.command("show running-config")
161 self.vty.command("si2quater neighbor-list add earfcn 1911 threshold 11 2")
162 self.vty.command("si2quater neighbor-list add earfcn 1924 threshold 11 3")
163 self.vty.command("si2quater neighbor-list add earfcn 2111 threshold 11")
164 self.vty.command("si2quater neighbor-list del earfcn 1911")
165 self.vty.command("si2quater neighbor-list del earfcn 1924")
166 self.vty.command("si2quater neighbor-list del earfcn 2111")
Oliver Smith6dbdf142019-12-10 13:21:11 +0100167 self.assertEqual(before, self.vty.command("show running-config"))
Max26679e02016-04-20 15:57:13 +0200168 self.vty.command("si2quater neighbor-list add uarfcn 1976 13 1")
169 self.vty.command("si2quater neighbor-list add uarfcn 1976 38 1")
170 self.vty.command("si2quater neighbor-list add uarfcn 1976 44 1")
171 self.vty.command("si2quater neighbor-list add uarfcn 1976 120 1")
172 self.vty.command("si2quater neighbor-list add uarfcn 1976 140 1")
173 self.vty.command("si2quater neighbor-list add uarfcn 1976 163 1")
174 self.vty.command("si2quater neighbor-list add uarfcn 1976 166 1")
175 self.vty.command("si2quater neighbor-list add uarfcn 1976 217 1")
176 self.vty.command("si2quater neighbor-list add uarfcn 1976 224 1")
177 self.vty.command("si2quater neighbor-list add uarfcn 1976 225 1")
178 self.vty.command("si2quater neighbor-list add uarfcn 1976 226 1")
179 self.vty.command("si2quater neighbor-list del uarfcn 1976 13")
180 self.vty.command("si2quater neighbor-list del uarfcn 1976 38")
181 self.vty.command("si2quater neighbor-list del uarfcn 1976 44")
182 self.vty.command("si2quater neighbor-list del uarfcn 1976 120")
183 self.vty.command("si2quater neighbor-list del uarfcn 1976 140")
184 self.vty.command("si2quater neighbor-list del uarfcn 1976 163")
185 self.vty.command("si2quater neighbor-list del uarfcn 1976 166")
186 self.vty.command("si2quater neighbor-list del uarfcn 1976 217")
187 self.vty.command("si2quater neighbor-list del uarfcn 1976 224")
188 self.vty.command("si2quater neighbor-list del uarfcn 1976 225")
189 self.vty.command("si2quater neighbor-list del uarfcn 1976 226")
Oliver Smith6dbdf142019-12-10 13:21:11 +0100190 self.assertEqual(before, self.vty.command("show running-config"))
Max0c1bc262016-04-20 12:06:06 +0200191
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200192 def testEnableDisablePeriodicLU(self):
193 self.vty.enable()
194 self.vty.command("configure terminal")
195 self.vty.command("network")
196 self.vty.command("bts 0")
197
198 # Test invalid input
199 self.vty.verify("periodic location update 0", ['% Unknown command.'])
200 self.vty.verify("periodic location update 5", ['% Unknown command.'])
201 self.vty.verify("periodic location update 1531", ['% Unknown command.'])
202
Vadim Yanitskiyfc2b0192020-01-18 07:20:14 +0700203 depr_str = "% 'periodic location update' is now deprecated: " \
204 "use 'timer T3212' to change subscriber expiration timeout."
205 set_str = "% Setting T3212 to 121 minutes (emulating the old behaviour)."
206
207 # Enable periodic LU (deprecated command)
208 self.vty.verify("periodic location update 60", [depr_str, set_str])
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200209 res = self.vty.command("write terminal")
Vadim Yanitskiyfc2b0192020-01-18 07:20:14 +0700210 self.assertTrue(res.find('timer vlr T3212 121') > 0)
211 self.assertEqual(res.find('periodic location update 60'), -1)
Oliver Smith6dbdf142019-12-10 13:21:11 +0100212 self.assertEqual(res.find('no periodic location update'), -1)
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200213
Vadim Yanitskiyfc2b0192020-01-18 07:20:14 +0700214 # Now disable it (deprecated command)
215 self.vty.verify("no periodic location update", [depr_str])
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200216 res = self.vty.command("write terminal")
Vadim Yanitskiyfc2b0192020-01-18 07:20:14 +0700217 self.assertEqual(res.find('no periodic location update'), -1)
218 self.assertEqual(res.find('timer vlr T3212 121'), -1)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200219
Ciabyec6e4f82014-03-06 17:20:55 +0100220 def testShowNetwork(self):
221 res = self.vty.command("show network")
Oliver Smith6dbdf142019-12-10 13:21:11 +0100222 self.assertTrue(res.startswith('BSC is on Country Code') >= 0)
Ciabyec6e4f82014-03-06 17:20:55 +0100223
Max49364482016-04-13 11:36:39 +0200224def ipa_handle_small(x, verbose = False):
225 s = data2str(x.recv(4))
Neels Hofmeyrba1468e2017-02-03 04:23:46 +0100226 if len(s) != 4*2:
227 raise Exception("expected to receive 4 bytes, but got %d (%r)" % (len(s)/2, s))
Max49364482016-04-13 11:36:39 +0200228 if "0001fe00" == s:
229 if (verbose):
Oliver Smith6dbdf142019-12-10 13:21:11 +0100230 print("\tBSC <- NAT: PING?")
Max3e676892016-11-16 14:55:21 +0100231 x.send(IPA().pong())
Max49364482016-04-13 11:36:39 +0200232 elif "0001fe06" == s:
233 if (verbose):
Oliver Smith6dbdf142019-12-10 13:21:11 +0100234 print("\tBSC <- NAT: IPA ID ACK")
Max3e676892016-11-16 14:55:21 +0100235 x.send(IPA().id_ack())
Max49364482016-04-13 11:36:39 +0200236 elif "0001fe00" == s:
237 if (verbose):
Oliver Smith6dbdf142019-12-10 13:21:11 +0100238 print("\tBSC <- NAT: PONG!")
Max49364482016-04-13 11:36:39 +0200239 else:
240 if (verbose):
Oliver Smith6dbdf142019-12-10 13:21:11 +0100241 print("\tBSC <- NAT: ", s)
Max49364482016-04-13 11:36:39 +0200242
Neels Hofmeyrbcfee2a2017-02-03 16:09:17 +0100243def ipa_handle_resp(x, tk, verbose = False, proc=None):
Max49364482016-04-13 11:36:39 +0200244 s = data2str(x.recv(38))
245 if "0023fe040108010701020103010401050101010011" in s:
Neels Hofmeyr7d17c3e2017-01-26 23:04:28 +0100246 retries = 3
247 while True:
Oliver Smith6dbdf142019-12-10 13:21:11 +0100248 print("\tsending IPA identity(%s) at %s" % (tk, time.strftime("%T")))
Neels Hofmeyr7d17c3e2017-01-26 23:04:28 +0100249 try:
250 x.send(IPA().id_resp(IPA().identity(name = tk.encode('utf-8'))))
Oliver Smith6dbdf142019-12-10 13:21:11 +0100251 print("\tdone sending IPA identity(%s) at %s" % (tk,
252 time.strftime("%T")))
Neels Hofmeyr7d17c3e2017-01-26 23:04:28 +0100253 break
254 except:
Oliver Smith6dbdf142019-12-10 13:21:11 +0100255 print("\tfailed sending IPA identity at", time.strftime("%T"))
Neels Hofmeyrbcfee2a2017-02-03 16:09:17 +0100256 if proc:
Oliver Smith6dbdf142019-12-10 13:21:11 +0100257 print("\tproc.poll() = %r" % proc.poll())
Neels Hofmeyr7d17c3e2017-01-26 23:04:28 +0100258 if retries < 1:
Oliver Smith6dbdf142019-12-10 13:21:11 +0100259 print("\tgiving up")
Neels Hofmeyr7d17c3e2017-01-26 23:04:28 +0100260 raise
Oliver Smith6dbdf142019-12-10 13:21:11 +0100261 print("\tretrying (%d attempts left)" % retries)
Neels Hofmeyr7d17c3e2017-01-26 23:04:28 +0100262 retries -= 1
Max49364482016-04-13 11:36:39 +0200263 else:
264 if (verbose):
Oliver Smith6dbdf142019-12-10 13:21:11 +0100265 print("\tBSC <- NAT: ", s)
Max49364482016-04-13 11:36:39 +0200266
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200267if __name__ == '__main__':
268 import argparse
269 import sys
270
271 workdir = '.'
272
273 parser = argparse.ArgumentParser()
274 parser.add_argument("-v", "--verbose", dest="verbose",
275 action="store_true", help="verbose mode")
276 parser.add_argument("-p", "--pythonconfpath", dest="p",
277 help="searchpath for config")
278 parser.add_argument("-w", "--workdir", dest="w",
279 help="Working directory")
Neels Hofmeyr3f8a8f72017-02-28 02:43:29 +0100280 parser.add_argument("test_name", nargs="*", help="(parts of) test names to run, case-insensitive")
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200281 args = parser.parse_args()
282
283 verbose_level = 1
284 if args.verbose:
285 verbose_level = 2
286
287 if args.w:
288 workdir = args.w
289
290 if args.p:
291 confpath = args.p
292
Oliver Smith6dbdf142019-12-10 13:21:11 +0100293 print("confpath %s, workdir %s" % (confpath, workdir))
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200294 os.chdir(workdir)
Oliver Smith6dbdf142019-12-10 13:21:11 +0100295 print("Running tests for specific VTY commands")
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200296 suite = unittest.TestSuite()
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200297 suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestVTYMSC))
Neels Hofmeyr3f8a8f72017-02-28 02:43:29 +0100298
299 if args.test_name:
300 osmoutil.pick_tests(suite, *args.test_name)
301
Neels Hofmeyr0a45c1e2016-09-28 23:48:02 +0200302 res = unittest.TextTestRunner(verbosity=verbose_level, stream=sys.stdout).run(suite)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200303 sys.exit(len(res.errors) + len(res.failures))
Neels Hofmeyr23d37c92016-09-26 03:18:32 +0200304
Neels Hofmeyr0867b722016-09-28 23:28:06 +0200305# vim: shiftwidth=4 expandtab nocin ai