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 | |
| 99 | def testMoo(self): |
| 100 | pass |
| 101 | |
| 102 | |
| 103 | def add_nat_test(suite, workdir): |
| 104 | if not os.path.isfile(os.path.join(workdir, "src/osmo-bsc_nat/osmo-bsc_nat")): |
| 105 | print("Skipping the NAT test") |
| 106 | return |
| 107 | test = unittest.TestLoader().loadTestsFromTestCase(TestVTYNAT) |
| 108 | suite.addTest(test) |
| 109 | |
| 110 | if __name__ == '__main__': |
| 111 | import argparse |
| 112 | import sys |
| 113 | |
| 114 | workdir = '.' |
| 115 | |
| 116 | parser = argparse.ArgumentParser() |
| 117 | parser.add_argument("-v", "--verbose", dest="verbose", |
| 118 | action="store_true", help="verbose mode") |
| 119 | parser.add_argument("-p", "--pythonconfpath", dest="p", |
| 120 | help="searchpath for config") |
| 121 | parser.add_argument("-w", "--workdir", dest="w", |
| 122 | help="Working directory") |
| 123 | args = parser.parse_args() |
| 124 | |
| 125 | verbose_level = 1 |
| 126 | if args.verbose: |
| 127 | verbose_level = 2 |
| 128 | |
| 129 | if args.w: |
| 130 | workdir = args.w |
| 131 | |
| 132 | if args.p: |
| 133 | confpath = args.p |
| 134 | |
| 135 | print "confpath %s, workdir %s" % (confpath, workdir) |
| 136 | os.chdir(workdir) |
| 137 | print "Running tests for specific VTY commands" |
| 138 | suite = unittest.TestSuite() |
Holger Hans Peter Freyther | c63f6f1 | 2013-07-27 21:07:57 +0200 | [diff] [blame] | 139 | suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestVTYNITB)) |
Holger Hans Peter Freyther | eb0acb6 | 2013-06-24 15:47:34 +0200 | [diff] [blame] | 140 | add_nat_test(suite, workdir) |
| 141 | res = unittest.TextTestRunner(verbosity=verbose_level).run(suite) |
| 142 | sys.exit(len(res.errors) + len(res.failures)) |