Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # (C) 2013 by Katerina Barone-Adesi <kat.obsc@gmail.com> |
| 4 | # This program is free software: you can redistribute it and/or modify |
| 5 | # it under the terms of the GNU General Public License as published by |
| 6 | # the Free Software Foundation, either version 3 of the License, or |
| 7 | # (at your option) any later version. |
| 8 | |
| 9 | # This program is distributed in the hope that it will be useful, |
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | # GNU General Public License for more details. |
| 13 | |
| 14 | # You should have received a copy of the GNU General Public License |
| 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | |
Kat | 0248d3b | 2013-04-05 20:19:17 +0200 | [diff] [blame^] | 17 | import os |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 18 | import time |
| 19 | import unittest |
| 20 | |
| 21 | import osmopy.obscvty as obscvty |
| 22 | import osmopy.osmoutil as osmoutil |
| 23 | |
Kat | 0248d3b | 2013-04-05 20:19:17 +0200 | [diff] [blame^] | 24 | confpath = '.' |
| 25 | |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 26 | """Test a VTY. Warning: osmoappdesc must be imported first.""" |
| 27 | |
| 28 | |
| 29 | class TestVTY(unittest.TestCase): |
| 30 | def setUp(self): |
Kat | 0248d3b | 2013-04-05 20:19:17 +0200 | [diff] [blame^] | 31 | osmo_vty_cmd = osmoappdesc.vty_command[:] |
| 32 | config_index = osmo_vty_cmd.index('-c') |
| 33 | if config_index: |
| 34 | cfi = config_index + 1 |
| 35 | osmo_vty_cmd[cfi] = os.path.join(confpath, osmo_vty_cmd[cfi]) |
| 36 | |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 37 | try: |
Kat | 0248d3b | 2013-04-05 20:19:17 +0200 | [diff] [blame^] | 38 | print "Launch: %s from %s" % (' '.join(osmo_vty_cmd), os.getcwd()) |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 39 | self.proc = osmoutil.popen_devnull(osmo_vty_cmd) |
| 40 | except OSError: |
| 41 | print >> sys.stderr, "Current directory: %s" % os.getcwd() |
Kat | 0248d3b | 2013-04-05 20:19:17 +0200 | [diff] [blame^] | 42 | print >> sys.stderr, "Consider setting -b" |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 43 | time.sleep(1) |
| 44 | |
| 45 | appstring = osmoappdesc.vty_app[2] |
| 46 | appport = osmoappdesc.vty_app[0] |
| 47 | self.vty = obscvty.VTYInteract(appstring, "127.0.0.1", appport) |
| 48 | |
| 49 | def tearDown(self): |
| 50 | self.vty = None |
| 51 | osmoutil.end_proc(self.proc) |
| 52 | |
| 53 | def test_history(self): |
| 54 | t1 = "show version" |
| 55 | self.vty.command(t1) |
| 56 | test_str = "show history" |
| 57 | assert(self.vty.w_verify(test_str, [t1])) |
| 58 | |
| 59 | def test_unknown_command(self): |
| 60 | test_str = "help show" |
| 61 | assert(self.vty.verify(test_str, ['% Unknown command.'])) |
| 62 | |
| 63 | def test_terminal_length(self): |
| 64 | test_str = "terminal length 20" |
| 65 | assert(self.vty.verify(test_str, [''])) |
| 66 | |
| 67 | |
| 68 | if __name__ == '__main__': |
| 69 | import argparse |
| 70 | import os |
| 71 | import sys |
| 72 | |
Kat | 0248d3b | 2013-04-05 20:19:17 +0200 | [diff] [blame^] | 73 | workdir = '.' |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 74 | |
| 75 | parser = argparse.ArgumentParser() |
Kat | 0d114c8 | 2013-04-05 18:21:39 +0200 | [diff] [blame] | 76 | parser.add_argument("-v", "--verbose", dest="verbose", |
| 77 | action="store_true", help="verbose mode") |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 78 | parser.add_argument("-p", "--pythonconfpath", dest="p", |
| 79 | help="searchpath for config") |
| 80 | parser.add_argument("-w", "--workdir", dest="w", |
Kat | 0248d3b | 2013-04-05 20:19:17 +0200 | [diff] [blame^] | 81 | help="Working directory") |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 82 | args = parser.parse_args() |
| 83 | |
Kat | 0d114c8 | 2013-04-05 18:21:39 +0200 | [diff] [blame] | 84 | verbose_level = 1 |
| 85 | if args.verbose: |
| 86 | verbose_level = 2 |
| 87 | |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 88 | if args.w: |
| 89 | workdir = args.w |
| 90 | |
| 91 | if args.p: |
| 92 | confpath = args.p |
| 93 | osmoappdesc = None |
| 94 | try: |
| 95 | osmoappdesc = osmoutil.importappconf(confpath, "osmoappdesc") |
| 96 | except ImportError as e: |
| 97 | print >> sys.stderr, "osmoappdesc not found, set searchpath with -p" |
| 98 | sys.exit(1) |
| 99 | |
Kat | 0248d3b | 2013-04-05 20:19:17 +0200 | [diff] [blame^] | 100 | print "confpath %s, workdir %s" % (confpath, workdir) |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 101 | os.chdir(workdir) |
Kat | 0d114c8 | 2013-04-05 18:21:39 +0200 | [diff] [blame] | 102 | print "Running tests for specific VTY commands" |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 103 | suite = unittest.TestLoader().loadTestsFromTestCase(TestVTY) |
Kat | 0d114c8 | 2013-04-05 18:21:39 +0200 | [diff] [blame] | 104 | res = unittest.TextTestRunner(verbosity=verbose_level).run(suite) |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 105 | sys.exit(len(res.errors) + len(res.failures)) |