blob: 91b4684d4adb44f3d23ebac06c6fc7a93fab2d02 [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
18import os
19import time
20import unittest
21
22import osmopy.obscvty as obscvty
23import osmopy.osmoutil as osmoutil
24
25confpath = '.'
26
27class TestVTYBase(unittest.TestCase):
28
29 def vty_command(self):
30 raise Exception("Needs to be implemented by a subclass")
31
32 def vty_app(self):
33 raise Exception("Needs to be implemented by a subclass")
34
35 def setUp(self):
36 osmo_vty_cmd = self.vty_command()[:]
37 config_index = osmo_vty_cmd.index('-c')
38 if config_index:
39 cfi = config_index + 1
40 osmo_vty_cmd[cfi] = os.path.join(confpath, osmo_vty_cmd[cfi])
41
42 try:
43 print "Launch: %s from %s" % (' '.join(osmo_vty_cmd), os.getcwd())
44 self.proc = osmoutil.popen_devnull(osmo_vty_cmd)
45 except OSError:
46 print >> sys.stderr, "Current directory: %s" % os.getcwd()
47 print >> sys.stderr, "Consider setting -b"
48 time.sleep(1)
49
50 appstring = self.vty_app()[2]
51 appport = self.vty_app()[0]
52 self.vty = obscvty.VTYInteract(appstring, "127.0.0.1", appport)
53
54 def tearDown(self):
55 self.vty = None
56 osmoutil.end_proc(self.proc)
57
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +020058class TestVTYNITB(TestVTYBase):
59
60 def vty_command(self):
61 return ["./src/osmo-nitb/osmo-nitb", "-c",
62 "doc/examples/osmo-nitb/nanobts/openbsc.cfg"]
63
64 def vty_app(self):
65 return (4242, "./src/osmo-nitb/osmo-nitb", "OpenBSC", "nitb")
66
67 def testEnableDisablePeriodicLU(self):
68 self.vty.enable()
69 self.vty.command("configure terminal")
70 self.vty.command("network")
71 self.vty.command("bts 0")
72
73 # Test invalid input
74 self.vty.verify("periodic location update 0", ['% Unknown command.'])
75 self.vty.verify("periodic location update 5", ['% Unknown command.'])
76 self.vty.verify("periodic location update 1531", ['% Unknown command.'])
77
78 # Enable periodic lu..
79 self.vty.verify("periodic location update 60", [''])
80 res = self.vty.command("write terminal")
Holger Hans Peter Freytherc0438e32013-07-27 22:23:25 +020081 self.assert_(res.find('periodic location update 60') > 0)
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +020082 self.assertEquals(res.find('no periodic location update'), -1)
83
84 # Now disable it..
85 self.vty.verify("no periodic location update", [''])
86 res = self.vty.command("write terminal")
87 self.assertEquals(res.find('periodic location update 60'), -1)
Holger Hans Peter Freytherc0438e32013-07-27 22:23:25 +020088 self.assert_(res.find('no periodic location update') > 0)
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +020089
90class TestVTYNAT(TestVTYBase):
91
92 def vty_command(self):
93 return ["./src/osmo-bsc_nat/osmo-bsc_nat", "-c",
94 "doc/examples/osmo-bsc_nat/osmo-bsc_nat.cfg"]
95
96 def vty_app(self):
97 return (4244, "src/osmo-bsc_nat/osmo-bsc_nat", "OsmoBSCNAT", "nat")
98
Holger Hans Peter Freytherb718ad32013-06-25 09:08:02 +020099 def testRewriteNoRewrite(self):
100 self.vty.enable()
101 res = self.vty.command("configure terminal")
102 res = self.vty.command("nat")
103 res = self.vty.command("number-rewrite rewrite.cfg")
104 res = self.vty.command("no number-rewrite")
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200105
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +0200106 def testPrefixTreeLoading(self):
107 cfg = os.path.join(confpath, "tests/bsc-nat-trie/prefixes.csv")
108
109 self.vty.enable()
110 self.vty.command("configure terminal")
111 self.vty.command("nat")
112 res = self.vty.command("prefix-tree %s" % cfg)
113 self.assertEqual(res, "% prefix-tree loaded 17 rules.")
114 self.vty.command("end")
115
116 res = self.vty.command("show prefix-tree")
117 self.assertEqual(res, '1,1\r\n12,2\r\n123,3\r\n1234,4\r\n12345,5\r\n123456,6\r\n1234567,7\r\n12345678,8\r\n123456789,9\r\n1234567890,10\r\n13,11\r\n14,12\r\n15,13\r\n16,14\r\n82,16\r\n823455,15\r\n+49123,17')
118
119 self.vty.command("configure terminal")
120 self.vty.command("nat")
121 self.vty.command("no prefix-tree")
122 self.vty.command("end")
123
124 res = self.vty.command("show prefix-tree")
125 self.assertEqual(res, "% there is now prefix tree loaded.")
126
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200127
128def add_nat_test(suite, workdir):
129 if not os.path.isfile(os.path.join(workdir, "src/osmo-bsc_nat/osmo-bsc_nat")):
130 print("Skipping the NAT test")
131 return
132 test = unittest.TestLoader().loadTestsFromTestCase(TestVTYNAT)
133 suite.addTest(test)
134
135if __name__ == '__main__':
136 import argparse
137 import sys
138
139 workdir = '.'
140
141 parser = argparse.ArgumentParser()
142 parser.add_argument("-v", "--verbose", dest="verbose",
143 action="store_true", help="verbose mode")
144 parser.add_argument("-p", "--pythonconfpath", dest="p",
145 help="searchpath for config")
146 parser.add_argument("-w", "--workdir", dest="w",
147 help="Working directory")
148 args = parser.parse_args()
149
150 verbose_level = 1
151 if args.verbose:
152 verbose_level = 2
153
154 if args.w:
155 workdir = args.w
156
157 if args.p:
158 confpath = args.p
159
160 print "confpath %s, workdir %s" % (confpath, workdir)
161 os.chdir(workdir)
162 print "Running tests for specific VTY commands"
163 suite = unittest.TestSuite()
Holger Hans Peter Freytherc63f6f12013-07-27 21:07:57 +0200164 suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestVTYNITB))
Holger Hans Peter Freythereb0acb62013-06-24 15:47:34 +0200165 add_nat_test(suite, workdir)
166 res = unittest.TextTestRunner(verbosity=verbose_level).run(suite)
167 sys.exit(len(res.errors) + len(res.failures))