blob: 05bc3bf89cd0a00c186eaa370b6fa2e4d4336db6 [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
26
Neels Hofmeyr476c4bb2017-02-24 17:55:11 +010027# add $top_srcdir/contrib to find ipa.py
28sys.path.append(os.path.join(sys.path[0], '..', 'contrib'))
29
Max3e676892016-11-16 14:55:21 +010030from ipa import IPA
31
Neels Hofmeyr476c4bb2017-02-24 17:55:11 +010032# to be able to find $top_srcdir/doc/...
33confpath = os.path.join(sys.path[0], '..')
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +020034
35class TestVTYBase(unittest.TestCase):
36
Neels Hofmeyra1756f32016-05-20 21:59:55 +020037 def checkForEndAndExit(self):
38 res = self.vty.command("list")
39 #print ('looking for "exit"\n')
40 self.assert_(res.find(' exit\r') > 0)
41 #print 'found "exit"\nlooking for "end"\n'
42 self.assert_(res.find(' end\r') > 0)
43 #print 'found "end"\n'
44
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +020045 def vty_command(self):
46 raise Exception("Needs to be implemented by a subclass")
47
48 def vty_app(self):
49 raise Exception("Needs to be implemented by a subclass")
50
51 def setUp(self):
52 osmo_vty_cmd = self.vty_command()[:]
53 config_index = osmo_vty_cmd.index('-c')
54 if config_index:
55 cfi = config_index + 1
56 osmo_vty_cmd[cfi] = os.path.join(confpath, osmo_vty_cmd[cfi])
57
58 try:
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +020059 self.proc = osmoutil.popen_devnull(osmo_vty_cmd)
60 except OSError:
61 print >> sys.stderr, "Current directory: %s" % os.getcwd()
62 print >> sys.stderr, "Consider setting -b"
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +020063
64 appstring = self.vty_app()[2]
65 appport = self.vty_app()[0]
66 self.vty = obscvty.VTYInteract(appstring, "127.0.0.1", appport)
67
68 def tearDown(self):
Neels Hofmeyr40a91b32017-02-24 17:54:22 +010069 if self.vty:
70 self.vty._close_socket()
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +020071 self.vty = None
72 osmoutil.end_proc(self.proc)
73
Holger Hans Peter Freyther8d998a72014-07-04 20:23:56 +020074class TestVTYMGCP(TestVTYBase):
75 def vty_command(self):
76 return ["./src/osmo-bsc_mgcp/osmo-bsc_mgcp", "-c",
77 "doc/examples/osmo-bsc_mgcp/mgcp.cfg"]
78
79 def vty_app(self):
80 return (4243, "./src/osmo-bsc_mgcp/osmo-bsc_mgcp", "OpenBSC MGCP", "mgcp")
81
82 def testForcePtime(self):
Neels Hofmeyr0867b722016-09-28 23:28:06 +020083 self.vty.enable()
84 res = self.vty.command("show running-config")
85 self.assert_(res.find(' rtp force-ptime 20\r') > 0)
86 self.assertEquals(res.find(' no rtp force-ptime\r'), -1)
Holger Hans Peter Freyther8d998a72014-07-04 20:23:56 +020087
Neels Hofmeyr0867b722016-09-28 23:28:06 +020088 self.vty.command("configure terminal")
89 self.vty.command("mgcp")
90 self.vty.command("no rtp force-ptime")
91 res = self.vty.command("show running-config")
92 self.assertEquals(res.find(' rtp force-ptime 20\r'), -1)
93 self.assertEquals(res.find(' no rtp force-ptime\r'), -1)
Holger Hans Peter Freyther8d998a72014-07-04 20:23:56 +020094
Holger Hans Peter Freyther619b0142014-11-19 16:04:45 +010095 def testOmitAudio(self):
96 self.vty.enable()
Neels Hofmeyr0867b722016-09-28 23:28:06 +020097 res = self.vty.command("show running-config")
98 self.assert_(res.find(' sdp audio-payload send-name\r') > 0)
99 self.assertEquals(res.find(' no sdp audio-payload send-name\r'), -1)
Holger Hans Peter Freyther619b0142014-11-19 16:04:45 +0100100
Neels Hofmeyr0867b722016-09-28 23:28:06 +0200101 self.vty.command("configure terminal")
102 self.vty.command("mgcp")
103 self.vty.command("no sdp audio-payload send-name")
104 res = self.vty.command("show running-config")
105 self.assertEquals(res.find(' rtp sdp audio-payload send-name\r'), -1)
106 self.assert_(res.find(' no sdp audio-payload send-name\r') > 0)
Holger Hans Peter Freyther619b0142014-11-19 16:04:45 +0100107
108 # TODO: test it for the trunk!
109
Holger Hans Peter Freytherc390ae82015-08-20 15:15:50 +0200110 def testBindAddr(self):
111 self.vty.enable()
112
Neels Hofmeyr0867b722016-09-28 23:28:06 +0200113 self.vty.command("configure terminal")
114 self.vty.command("mgcp")
Holger Hans Peter Freytherc390ae82015-08-20 15:15:50 +0200115
116 # enable.. disable bts-bind-ip
117 self.vty.command("rtp bts-bind-ip 254.253.252.250")
118 res = self.vty.command("show running-config")
119 self.assert_(res.find('rtp bts-bind-ip 254.253.252.250') > 0)
120 self.vty.command("no rtp bts-bind-ip")
121 res = self.vty.command("show running-config")
122 self.assertEquals(res.find(' rtp bts-bind-ip'), -1)
123
124 # enable.. disable net-bind-ip
125 self.vty.command("rtp net-bind-ip 254.253.252.250")
126 res = self.vty.command("show running-config")
127 self.assert_(res.find('rtp net-bind-ip 254.253.252.250') > 0)
128 self.vty.command("no rtp net-bind-ip")
129 res = self.vty.command("show running-config")
130 self.assertEquals(res.find(' rtp net-bind-ip'), -1)
131
Jacob Erlbeck96903c42013-09-02 13:17:14 +0200132
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200133if __name__ == '__main__':
134 import argparse
135 import sys
136
137 workdir = '.'
138
139 parser = argparse.ArgumentParser()
140 parser.add_argument("-v", "--verbose", dest="verbose",
141 action="store_true", help="verbose mode")
142 parser.add_argument("-p", "--pythonconfpath", dest="p",
143 help="searchpath for config")
144 parser.add_argument("-w", "--workdir", dest="w",
145 help="Working directory")
Neels Hofmeyr3f8a8f72017-02-28 02:43:29 +0100146 parser.add_argument("test_name", nargs="*", help="(parts of) test names to run, case-insensitive")
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200147 args = parser.parse_args()
148
149 verbose_level = 1
150 if args.verbose:
151 verbose_level = 2
152
153 if args.w:
154 workdir = args.w
155
156 if args.p:
157 confpath = args.p
158
159 print "confpath %s, workdir %s" % (confpath, workdir)
160 os.chdir(workdir)
161 print "Running tests for specific VTY commands"
162 suite = unittest.TestSuite()
Holger Hans Peter Freyther8d998a72014-07-04 20:23:56 +0200163 suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestVTYMGCP))
Neels Hofmeyr3f8a8f72017-02-28 02:43:29 +0100164
165 if args.test_name:
166 osmoutil.pick_tests(suite, *args.test_name)
167
Neels Hofmeyr0a45c1e2016-09-28 23:48:02 +0200168 res = unittest.TextTestRunner(verbosity=verbose_level, stream=sys.stdout).run(suite)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200169 sys.exit(len(res.errors) + len(res.failures))
Neels Hofmeyr23d37c92016-09-26 03:18:32 +0200170
Neels Hofmeyr0867b722016-09-28 23:28:06 +0200171# vim: shiftwidth=4 expandtab nocin ai