Holger Hans Peter Freyther | 6539752 | 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 | |
| 58 | |
| 59 | class TestVTYNAT(TestVTYBase): |
| 60 | |
| 61 | def vty_command(self): |
| 62 | return ["./src/osmo-bsc_nat/osmo-bsc_nat", "-c", |
| 63 | "doc/examples/osmo-bsc_nat/osmo-bsc_nat.cfg"] |
| 64 | |
| 65 | def vty_app(self): |
| 66 | return (4244, "src/osmo-bsc_nat/osmo-bsc_nat", "OsmoBSCNAT", "nat") |
| 67 | |
| 68 | def testMoo(self): |
| 69 | pass |
| 70 | |
| 71 | |
| 72 | def add_nat_test(suite, workdir): |
| 73 | if not os.path.isfile(os.path.join(workdir, "src/osmo-bsc_nat/osmo-bsc_nat")): |
| 74 | print("Skipping the NAT test") |
| 75 | return |
| 76 | test = unittest.TestLoader().loadTestsFromTestCase(TestVTYNAT) |
| 77 | suite.addTest(test) |
| 78 | |
| 79 | if __name__ == '__main__': |
| 80 | import argparse |
| 81 | import sys |
| 82 | |
| 83 | workdir = '.' |
| 84 | |
| 85 | parser = argparse.ArgumentParser() |
| 86 | parser.add_argument("-v", "--verbose", dest="verbose", |
| 87 | action="store_true", help="verbose mode") |
| 88 | parser.add_argument("-p", "--pythonconfpath", dest="p", |
| 89 | help="searchpath for config") |
| 90 | parser.add_argument("-w", "--workdir", dest="w", |
| 91 | help="Working directory") |
| 92 | args = parser.parse_args() |
| 93 | |
| 94 | verbose_level = 1 |
| 95 | if args.verbose: |
| 96 | verbose_level = 2 |
| 97 | |
| 98 | if args.w: |
| 99 | workdir = args.w |
| 100 | |
| 101 | if args.p: |
| 102 | confpath = args.p |
| 103 | |
| 104 | print "confpath %s, workdir %s" % (confpath, workdir) |
| 105 | os.chdir(workdir) |
| 106 | print "Running tests for specific VTY commands" |
| 107 | suite = unittest.TestSuite() |
| 108 | add_nat_test(suite, workdir) |
| 109 | res = unittest.TextTestRunner(verbosity=verbose_level).run(suite) |
| 110 | sys.exit(len(res.errors) + len(res.failures)) |