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 | |
| 18 | import os |
| 19 | import time |
| 20 | import unittest |
| 21 | |
| 22 | import osmopy.obscvty as obscvty |
| 23 | import osmopy.osmoutil as osmoutil |
| 24 | |
| 25 | confpath = '.' |
| 26 | |
| 27 | class 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 Freyther | c63f6f1 | 2013-07-27 21:07:57 +0200 | [diff] [blame] | 58 | class 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 Freyther | c0438e3 | 2013-07-27 22:23:25 +0200 | [diff] [blame] | 81 | self.assert_(res.find('periodic location update 60') > 0) |
Holger Hans Peter Freyther | c63f6f1 | 2013-07-27 21:07:57 +0200 | [diff] [blame] | 82 | 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 Freyther | c0438e3 | 2013-07-27 22:23:25 +0200 | [diff] [blame] | 88 | self.assert_(res.find('no periodic location update') > 0) |
Holger Hans Peter Freyther | eb0acb6 | 2013-06-24 15:47:34 +0200 | [diff] [blame] | 89 | |
| 90 | class 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 Freyther | b718ad3 | 2013-06-25 09:08:02 +0200 | [diff] [blame] | 99 | 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 Freyther | eb0acb6 | 2013-06-24 15:47:34 +0200 | [diff] [blame] | 105 | |
Holger Hans Peter Freyther | 67e423c | 2013-06-25 15:38:31 +0200 | [diff] [blame] | 106 | def testRewritePostNoRewrite(self): |
| 107 | self.vty.enable() |
| 108 | self.vty.command("configure terminal") |
| 109 | self.vty.command("nat") |
| 110 | self.vty.verify("number-rewrite-post rewrite.cfg", ['']) |
| 111 | self.vty.verify("no number-rewrite-post", ['']) |
| 112 | |
| 113 | |
Holger Hans Peter Freyther | ddf191e | 2013-06-25 11:44:01 +0200 | [diff] [blame] | 114 | def testPrefixTreeLoading(self): |
| 115 | cfg = os.path.join(confpath, "tests/bsc-nat-trie/prefixes.csv") |
| 116 | |
| 117 | self.vty.enable() |
| 118 | self.vty.command("configure terminal") |
| 119 | self.vty.command("nat") |
| 120 | res = self.vty.command("prefix-tree %s" % cfg) |
| 121 | self.assertEqual(res, "% prefix-tree loaded 17 rules.") |
| 122 | self.vty.command("end") |
| 123 | |
| 124 | res = self.vty.command("show prefix-tree") |
| 125 | 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') |
| 126 | |
| 127 | self.vty.command("configure terminal") |
| 128 | self.vty.command("nat") |
| 129 | self.vty.command("no prefix-tree") |
| 130 | self.vty.command("end") |
| 131 | |
| 132 | res = self.vty.command("show prefix-tree") |
| 133 | self.assertEqual(res, "% there is now prefix tree loaded.") |
| 134 | |
Holger Hans Peter Freyther | eb0acb6 | 2013-06-24 15:47:34 +0200 | [diff] [blame] | 135 | |
| 136 | def add_nat_test(suite, workdir): |
| 137 | if not os.path.isfile(os.path.join(workdir, "src/osmo-bsc_nat/osmo-bsc_nat")): |
| 138 | print("Skipping the NAT test") |
| 139 | return |
| 140 | test = unittest.TestLoader().loadTestsFromTestCase(TestVTYNAT) |
| 141 | suite.addTest(test) |
| 142 | |
| 143 | if __name__ == '__main__': |
| 144 | import argparse |
| 145 | import sys |
| 146 | |
| 147 | workdir = '.' |
| 148 | |
| 149 | parser = argparse.ArgumentParser() |
| 150 | parser.add_argument("-v", "--verbose", dest="verbose", |
| 151 | action="store_true", help="verbose mode") |
| 152 | parser.add_argument("-p", "--pythonconfpath", dest="p", |
| 153 | help="searchpath for config") |
| 154 | parser.add_argument("-w", "--workdir", dest="w", |
| 155 | help="Working directory") |
| 156 | args = parser.parse_args() |
| 157 | |
| 158 | verbose_level = 1 |
| 159 | if args.verbose: |
| 160 | verbose_level = 2 |
| 161 | |
| 162 | if args.w: |
| 163 | workdir = args.w |
| 164 | |
| 165 | if args.p: |
| 166 | confpath = args.p |
| 167 | |
| 168 | print "confpath %s, workdir %s" % (confpath, workdir) |
| 169 | os.chdir(workdir) |
| 170 | print "Running tests for specific VTY commands" |
| 171 | suite = unittest.TestSuite() |
Holger Hans Peter Freyther | c63f6f1 | 2013-07-27 21:07:57 +0200 | [diff] [blame] | 172 | suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestVTYNITB)) |
Holger Hans Peter Freyther | eb0acb6 | 2013-06-24 15:47:34 +0200 | [diff] [blame] | 173 | add_nat_test(suite, workdir) |
| 174 | res = unittest.TextTestRunner(verbosity=verbose_level).run(suite) |
| 175 | sys.exit(len(res.errors) + len(res.failures)) |