blob: 6d8ca6f48966cbd1e86f0df3eea9e386fd39aa09 [file] [log] [blame]
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +02001#!/usr/bin/env python
2
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')
36 self.assert_(res.find(' exit\r') > 0)
37 #print 'found "exit"\nlooking for "end"\n'
38 self.assert_(res.find(' end\r') > 0)
39 #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:
57 print >> sys.stderr, "Current directory: %s" % os.getcwd()
58 print >> sys.stderr, "Consider setting -b"
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
68 osmoutil.end_proc(self.proc)
69
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020070class TestVTYMSC(TestVTYBase):
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +020071
72 def vty_command(self):
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020073 return ["./src/osmo-msc/osmo-msc", "-c",
74 "doc/examples/osmo-msc/osmo-msc.cfg"]
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +020075
76 def vty_app(self):
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020077 return (4254, "./src/osmo-msc/osmo-msc", "OsmoMSC", "msc")
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +020078
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020079 def testConfigNetworkTree(self, include_bsc_items=True):
80 self.vty.enable()
81 self.assertTrue(self.vty.verify("configure terminal",['']))
82 self.assertEquals(self.vty.node(), 'config')
83 self.checkForEndAndExit()
84 self.assertTrue(self.vty.verify("network",['']))
85 self.assertEquals(self.vty.node(), 'config-net')
86 self.checkForEndAndExit()
87 self.vty.command("write terminal")
88 self.assertTrue(self.vty.verify("exit",['']))
89 self.assertEquals(self.vty.node(), 'config')
90 self.assertTrue(self.vty.verify("exit",['']))
91 self.assertTrue(self.vty.node() is None)
Jacob Erlbeck96903c42013-09-02 13:17:14 +020092
Holger Hans Peter Freyther0df1ab92013-09-02 20:58:38 +020093 def checkForSmpp(self):
94 """SMPP is not always enabled, check if it is"""
95 res = self.vty.command("list")
96 return "smpp" in res
97
Holger Hans Peter Freyther42cf2e02015-07-06 16:41:30 +020098 def testSmppFirst(self):
Holger Hans Peter Freyther42cf2e02015-07-06 16:41:30 +020099 # enable the configuration
100 self.vty.enable()
101 self.vty.command("configure terminal")
Holger Hans Peter Freythera2c41c42015-07-13 11:23:53 +0200102
103 if not self.checkForSmpp():
104 return
105
Holger Hans Peter Freyther42cf2e02015-07-06 16:41:30 +0200106 self.vty.command("smpp")
107
108 # check the default
109 res = self.vty.command("write terminal")
110 self.assert_(res.find(' no smpp-first') > 0)
111
112 self.vty.verify("smpp-first", [''])
113 res = self.vty.command("write terminal")
114 self.assert_(res.find(' smpp-first') > 0)
115 self.assertEquals(res.find('no smpp-first'), -1)
116
117 self.vty.verify("no smpp-first", [''])
118 res = self.vty.command("write terminal")
119 self.assert_(res.find('no smpp-first') > 0)
120
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200121 def testVtyTree(self):
122 self.vty.enable()
123 self.assertTrue(self.vty.verify("configure terminal", ['']))
124 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck6e919db2013-10-29 09:30:31 +0100125 self.checkForEndAndExit()
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200126 self.assertTrue(self.vty.verify('mncc-int', ['']))
127 self.assertEquals(self.vty.node(), 'config-mncc-int')
128 self.checkForEndAndExit()
129 self.assertTrue(self.vty.verify('exit', ['']))
Holger Hans Peter Freyther0df1ab92013-09-02 20:58:38 +0200130
131 if self.checkForSmpp():
132 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck75877272013-10-23 11:24:14 +0200133 self.assertTrue(self.vty.verify('smpp', ['']))
134 self.assertEquals(self.vty.node(), 'config-smpp')
Jacob Erlbeck6e919db2013-10-29 09:30:31 +0100135 self.checkForEndAndExit()
Jacob Erlbeck75877272013-10-23 11:24:14 +0200136 self.assertTrue(self.vty.verify("exit", ['']))
Holger Hans Peter Freyther0df1ab92013-09-02 20:58:38 +0200137
Jacob Erlbeck0ae92a92013-09-02 13:17:16 +0200138 self.assertEquals(self.vty.node(), 'config')
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200139 self.assertTrue(self.vty.verify("exit", ['']))
140 self.assertTrue(self.vty.node() is None)
141
142 # Check searching for outer node's commands
143 self.vty.command("configure terminal")
144 self.vty.command('mncc-int')
Holger Hans Peter Freyther0df1ab92013-09-02 20:58:38 +0200145
146 if self.checkForSmpp():
147 self.vty.command('smpp')
148 self.assertEquals(self.vty.node(), 'config-smpp')
149 self.vty.command('mncc-int')
150
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200151 self.assertEquals(self.vty.node(), 'config-mncc-int')
152
Max0c1bc262016-04-20 12:06:06 +0200153 def testSi2Q(self):
154 self.vty.enable()
155 self.vty.command("configure terminal")
156 self.vty.command("network")
157 self.vty.command("bts 0")
158 before = self.vty.command("show running-config")
159 self.vty.command("si2quater neighbor-list add earfcn 1911 threshold 11 2")
160 self.vty.command("si2quater neighbor-list add earfcn 1924 threshold 11 3")
161 self.vty.command("si2quater neighbor-list add earfcn 2111 threshold 11")
162 self.vty.command("si2quater neighbor-list del earfcn 1911")
163 self.vty.command("si2quater neighbor-list del earfcn 1924")
164 self.vty.command("si2quater neighbor-list del earfcn 2111")
165 self.assertEquals(before, self.vty.command("show running-config"))
Max26679e02016-04-20 15:57:13 +0200166 self.vty.command("si2quater neighbor-list add uarfcn 1976 13 1")
167 self.vty.command("si2quater neighbor-list add uarfcn 1976 38 1")
168 self.vty.command("si2quater neighbor-list add uarfcn 1976 44 1")
169 self.vty.command("si2quater neighbor-list add uarfcn 1976 120 1")
170 self.vty.command("si2quater neighbor-list add uarfcn 1976 140 1")
171 self.vty.command("si2quater neighbor-list add uarfcn 1976 163 1")
172 self.vty.command("si2quater neighbor-list add uarfcn 1976 166 1")
173 self.vty.command("si2quater neighbor-list add uarfcn 1976 217 1")
174 self.vty.command("si2quater neighbor-list add uarfcn 1976 224 1")
175 self.vty.command("si2quater neighbor-list add uarfcn 1976 225 1")
176 self.vty.command("si2quater neighbor-list add uarfcn 1976 226 1")
177 self.vty.command("si2quater neighbor-list del uarfcn 1976 13")
178 self.vty.command("si2quater neighbor-list del uarfcn 1976 38")
179 self.vty.command("si2quater neighbor-list del uarfcn 1976 44")
180 self.vty.command("si2quater neighbor-list del uarfcn 1976 120")
181 self.vty.command("si2quater neighbor-list del uarfcn 1976 140")
182 self.vty.command("si2quater neighbor-list del uarfcn 1976 163")
183 self.vty.command("si2quater neighbor-list del uarfcn 1976 166")
184 self.vty.command("si2quater neighbor-list del uarfcn 1976 217")
185 self.vty.command("si2quater neighbor-list del uarfcn 1976 224")
186 self.vty.command("si2quater neighbor-list del uarfcn 1976 225")
187 self.vty.command("si2quater neighbor-list del uarfcn 1976 226")
188 self.assertEquals(before, self.vty.command("show running-config"))
Max0c1bc262016-04-20 12:06:06 +0200189
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200190 def testEnableDisablePeriodicLU(self):
191 self.vty.enable()
192 self.vty.command("configure terminal")
193 self.vty.command("network")
194 self.vty.command("bts 0")
195
196 # Test invalid input
197 self.vty.verify("periodic location update 0", ['% Unknown command.'])
198 self.vty.verify("periodic location update 5", ['% Unknown command.'])
199 self.vty.verify("periodic location update 1531", ['% Unknown command.'])
200
201 # Enable periodic lu..
202 self.vty.verify("periodic location update 60", [''])
203 res = self.vty.command("write terminal")
Holger Hans Peter Freytherc0438e32013-07-27 22:23:25 +0200204 self.assert_(res.find('periodic location update 60') > 0)
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200205 self.assertEquals(res.find('no periodic location update'), -1)
206
207 # Now disable it..
208 self.vty.verify("no periodic location update", [''])
209 res = self.vty.command("write terminal")
210 self.assertEquals(res.find('periodic location update 60'), -1)
Holger Hans Peter Freytherc0438e32013-07-27 22:23:25 +0200211 self.assert_(res.find('no periodic location update') > 0)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200212
Ciabyec6e4f82014-03-06 17:20:55 +0100213 def testShowNetwork(self):
214 res = self.vty.command("show network")
215 self.assert_(res.startswith('BSC is on Country Code') >= 0)
216
Max49364482016-04-13 11:36:39 +0200217def ipa_handle_small(x, verbose = False):
218 s = data2str(x.recv(4))
Neels Hofmeyrba1468e2017-02-03 04:23:46 +0100219 if len(s) != 4*2:
220 raise Exception("expected to receive 4 bytes, but got %d (%r)" % (len(s)/2, s))
Max49364482016-04-13 11:36:39 +0200221 if "0001fe00" == s:
222 if (verbose):
223 print "\tBSC <- NAT: PING?"
Max3e676892016-11-16 14:55:21 +0100224 x.send(IPA().pong())
Max49364482016-04-13 11:36:39 +0200225 elif "0001fe06" == s:
226 if (verbose):
227 print "\tBSC <- NAT: IPA ID ACK"
Max3e676892016-11-16 14:55:21 +0100228 x.send(IPA().id_ack())
Max49364482016-04-13 11:36:39 +0200229 elif "0001fe00" == s:
230 if (verbose):
231 print "\tBSC <- NAT: PONG!"
232 else:
233 if (verbose):
234 print "\tBSC <- NAT: ", s
235
Neels Hofmeyrbcfee2a2017-02-03 16:09:17 +0100236def ipa_handle_resp(x, tk, verbose = False, proc=None):
Max49364482016-04-13 11:36:39 +0200237 s = data2str(x.recv(38))
238 if "0023fe040108010701020103010401050101010011" in s:
Neels Hofmeyr7d17c3e2017-01-26 23:04:28 +0100239 retries = 3
240 while True:
241 print "\tsending IPA identity(%s) at %s" % (tk, time.strftime("%T"))
242 try:
243 x.send(IPA().id_resp(IPA().identity(name = tk.encode('utf-8'))))
244 print "\tdone sending IPA identity(%s) at %s" % (tk,
245 time.strftime("%T"))
246 break
247 except:
248 print "\tfailed sending IPA identity at", time.strftime("%T")
Neels Hofmeyrbcfee2a2017-02-03 16:09:17 +0100249 if proc:
250 print "\tproc.poll() = %r" % proc.poll()
Neels Hofmeyr7d17c3e2017-01-26 23:04:28 +0100251 if retries < 1:
252 print "\tgiving up"
253 raise
Neels Hofmeyre02e1e72017-02-03 05:55:26 +0100254 print "\tretrying (%d attempts left)" % retries
Neels Hofmeyr7d17c3e2017-01-26 23:04:28 +0100255 retries -= 1
Max49364482016-04-13 11:36:39 +0200256 else:
257 if (verbose):
258 print "\tBSC <- NAT: ", s
259
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200260if __name__ == '__main__':
261 import argparse
262 import sys
263
264 workdir = '.'
265
266 parser = argparse.ArgumentParser()
267 parser.add_argument("-v", "--verbose", dest="verbose",
268 action="store_true", help="verbose mode")
269 parser.add_argument("-p", "--pythonconfpath", dest="p",
270 help="searchpath for config")
271 parser.add_argument("-w", "--workdir", dest="w",
272 help="Working directory")
Neels Hofmeyr3f8a8f72017-02-28 02:43:29 +0100273 parser.add_argument("test_name", nargs="*", help="(parts of) test names to run, case-insensitive")
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200274 args = parser.parse_args()
275
276 verbose_level = 1
277 if args.verbose:
278 verbose_level = 2
279
280 if args.w:
281 workdir = args.w
282
283 if args.p:
284 confpath = args.p
285
286 print "confpath %s, workdir %s" % (confpath, workdir)
287 os.chdir(workdir)
288 print "Running tests for specific VTY commands"
289 suite = unittest.TestSuite()
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200290 suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestVTYMSC))
Neels Hofmeyr3f8a8f72017-02-28 02:43:29 +0100291
292 if args.test_name:
293 osmoutil.pick_tests(suite, *args.test_name)
294
Neels Hofmeyr0a45c1e2016-09-28 23:48:02 +0200295 res = unittest.TextTestRunner(verbosity=verbose_level, stream=sys.stdout).run(suite)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200296 sys.exit(len(res.errors) + len(res.failures))
Neels Hofmeyr23d37c92016-09-26 03:18:32 +0200297
Neels Hofmeyr0867b722016-09-28 23:28:06 +0200298# vim: shiftwidth=4 expandtab nocin ai