Holger Hans Peter Freyther | eb0acb6 | 2013-06-24 15:47:34 +0200 | [diff] [blame] | 1 | #!/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 | |
Max | 3e67689 | 2016-11-16 14:55:21 +0100 | [diff] [blame] | 18 | import os, sys |
Holger Hans Peter Freyther | eb0acb6 | 2013-06-24 15:47:34 +0200 | [diff] [blame] | 19 | import time |
| 20 | import unittest |
Jacob Erlbeck | 6cb2ccc | 2013-08-14 11:10:34 +0200 | [diff] [blame] | 21 | import socket |
Neels Hofmeyr | bcfee2a | 2017-02-03 16:09:17 +0100 | [diff] [blame] | 22 | import subprocess |
Holger Hans Peter Freyther | eb0acb6 | 2013-06-24 15:47:34 +0200 | [diff] [blame] | 23 | |
| 24 | import osmopy.obscvty as obscvty |
| 25 | import osmopy.osmoutil as osmoutil |
| 26 | |
Neels Hofmeyr | 476c4bb | 2017-02-24 17:55:11 +0100 | [diff] [blame] | 27 | # add $top_srcdir/contrib to find ipa.py |
| 28 | sys.path.append(os.path.join(sys.path[0], '..', 'contrib')) |
| 29 | |
Max | 3e67689 | 2016-11-16 14:55:21 +0100 | [diff] [blame] | 30 | from ipa import IPA |
| 31 | |
Neels Hofmeyr | 476c4bb | 2017-02-24 17:55:11 +0100 | [diff] [blame] | 32 | # to be able to find $top_srcdir/doc/... |
| 33 | confpath = os.path.join(sys.path[0], '..') |
Holger Hans Peter Freyther | eb0acb6 | 2013-06-24 15:47:34 +0200 | [diff] [blame] | 34 | |
| 35 | class TestVTYBase(unittest.TestCase): |
| 36 | |
Neels Hofmeyr | a1756f3 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 37 | 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 Freyther | eb0acb6 | 2013-06-24 15:47:34 +0200 | [diff] [blame] | 45 | 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 Freyther | eb0acb6 | 2013-06-24 15:47:34 +0200 | [diff] [blame] | 59 | 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 Freyther | eb0acb6 | 2013-06-24 15:47:34 +0200 | [diff] [blame] | 63 | |
| 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 Hofmeyr | 40a91b3 | 2017-02-24 17:54:22 +0100 | [diff] [blame] | 69 | if self.vty: |
| 70 | self.vty._close_socket() |
Holger Hans Peter Freyther | eb0acb6 | 2013-06-24 15:47:34 +0200 | [diff] [blame] | 71 | self.vty = None |
| 72 | osmoutil.end_proc(self.proc) |
| 73 | |
Holger Hans Peter Freyther | 8d998a7 | 2014-07-04 20:23:56 +0200 | [diff] [blame] | 74 | class 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 Hofmeyr | 0867b72 | 2016-09-28 23:28:06 +0200 | [diff] [blame] | 83 | 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 Freyther | 8d998a7 | 2014-07-04 20:23:56 +0200 | [diff] [blame] | 87 | |
Neels Hofmeyr | 0867b72 | 2016-09-28 23:28:06 +0200 | [diff] [blame] | 88 | 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 Freyther | 8d998a7 | 2014-07-04 20:23:56 +0200 | [diff] [blame] | 94 | |
Holger Hans Peter Freyther | 619b014 | 2014-11-19 16:04:45 +0100 | [diff] [blame] | 95 | def testOmitAudio(self): |
| 96 | self.vty.enable() |
Neels Hofmeyr | 0867b72 | 2016-09-28 23:28:06 +0200 | [diff] [blame] | 97 | 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 Freyther | 619b014 | 2014-11-19 16:04:45 +0100 | [diff] [blame] | 100 | |
Neels Hofmeyr | 0867b72 | 2016-09-28 23:28:06 +0200 | [diff] [blame] | 101 | 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 Freyther | 619b014 | 2014-11-19 16:04:45 +0100 | [diff] [blame] | 107 | |
| 108 | # TODO: test it for the trunk! |
| 109 | |
Holger Hans Peter Freyther | c390ae8 | 2015-08-20 15:15:50 +0200 | [diff] [blame] | 110 | def testBindAddr(self): |
| 111 | self.vty.enable() |
| 112 | |
Neels Hofmeyr | 0867b72 | 2016-09-28 23:28:06 +0200 | [diff] [blame] | 113 | self.vty.command("configure terminal") |
| 114 | self.vty.command("mgcp") |
Holger Hans Peter Freyther | c390ae8 | 2015-08-20 15:15:50 +0200 | [diff] [blame] | 115 | |
| 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 Erlbeck | 96903c4 | 2013-09-02 13:17:14 +0200 | [diff] [blame] | 132 | |
Holger Hans Peter Freyther | eb0acb6 | 2013-06-24 15:47:34 +0200 | [diff] [blame] | 133 | if __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 Hofmeyr | 3f8a8f7 | 2017-02-28 02:43:29 +0100 | [diff] [blame] | 146 | parser.add_argument("test_name", nargs="*", help="(parts of) test names to run, case-insensitive") |
Holger Hans Peter Freyther | eb0acb6 | 2013-06-24 15:47:34 +0200 | [diff] [blame] | 147 | 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 Freyther | 8d998a7 | 2014-07-04 20:23:56 +0200 | [diff] [blame] | 163 | suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestVTYMGCP)) |
Neels Hofmeyr | 3f8a8f7 | 2017-02-28 02:43:29 +0100 | [diff] [blame] | 164 | |
| 165 | if args.test_name: |
| 166 | osmoutil.pick_tests(suite, *args.test_name) |
| 167 | |
Neels Hofmeyr | 0a45c1e | 2016-09-28 23:48:02 +0200 | [diff] [blame] | 168 | res = unittest.TextTestRunner(verbosity=verbose_level, stream=sys.stdout).run(suite) |
Holger Hans Peter Freyther | eb0acb6 | 2013-06-24 15:47:34 +0200 | [diff] [blame] | 169 | sys.exit(len(res.errors) + len(res.failures)) |
Neels Hofmeyr | 23d37c9 | 2016-09-26 03:18:32 +0200 | [diff] [blame] | 170 | |
Neels Hofmeyr | 0867b72 | 2016-09-28 23:28:06 +0200 | [diff] [blame] | 171 | # vim: shiftwidth=4 expandtab nocin ai |