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/>. |
Max | 2f266e0 | 2018-01-03 17:35:59 +0100 | [diff] [blame^] | 16 | from __future__ import print_function |
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): |
Kat | 0270be4 | 2013-04-05 21:34:52 +0200 | [diff] [blame] | 30 | |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 31 | def setUp(self): |
Kat | 0248d3b | 2013-04-05 20:19:17 +0200 | [diff] [blame] | 32 | osmo_vty_cmd = osmoappdesc.vty_command[:] |
| 33 | config_index = osmo_vty_cmd.index('-c') |
| 34 | if config_index: |
| 35 | cfi = config_index + 1 |
| 36 | osmo_vty_cmd[cfi] = os.path.join(confpath, osmo_vty_cmd[cfi]) |
| 37 | |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 38 | try: |
Max | 2f266e0 | 2018-01-03 17:35:59 +0100 | [diff] [blame^] | 39 | print("Launch: %s from %s" % (' '.join(osmo_vty_cmd), os.getcwd())) |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 40 | self.proc = osmoutil.popen_devnull(osmo_vty_cmd) |
| 41 | except OSError: |
Max | 2f266e0 | 2018-01-03 17:35:59 +0100 | [diff] [blame^] | 42 | print("Current directory: %s" % os.getcwd(), file=sys.stderr) |
| 43 | print("Consider setting -b", file=sys.stderr) |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 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): |
Holger Hans Peter Freyther | 99b5c56 | 2017-02-13 20:06:44 +0700 | [diff] [blame] | 50 | self.vty._close_socket() |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 51 | self.vty = None |
| 52 | osmoutil.end_proc(self.proc) |
| 53 | |
| 54 | def test_history(self): |
| 55 | t1 = "show version" |
| 56 | self.vty.command(t1) |
| 57 | test_str = "show history" |
| 58 | assert(self.vty.w_verify(test_str, [t1])) |
| 59 | |
| 60 | def test_unknown_command(self): |
| 61 | test_str = "help show" |
| 62 | assert(self.vty.verify(test_str, ['% Unknown command.'])) |
| 63 | |
| 64 | def test_terminal_length(self): |
| 65 | test_str = "terminal length 20" |
| 66 | assert(self.vty.verify(test_str, [''])) |
| 67 | |
| 68 | |
| 69 | if __name__ == '__main__': |
| 70 | import argparse |
| 71 | import os |
| 72 | import sys |
| 73 | |
Kat | 0248d3b | 2013-04-05 20:19:17 +0200 | [diff] [blame] | 74 | workdir = '.' |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 75 | |
| 76 | parser = argparse.ArgumentParser() |
Kat | 0d114c8 | 2013-04-05 18:21:39 +0200 | [diff] [blame] | 77 | parser.add_argument("-v", "--verbose", dest="verbose", |
| 78 | action="store_true", help="verbose mode") |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 79 | parser.add_argument("-p", "--pythonconfpath", dest="p", |
| 80 | help="searchpath for config") |
| 81 | parser.add_argument("-w", "--workdir", dest="w", |
Kat | 0248d3b | 2013-04-05 20:19:17 +0200 | [diff] [blame] | 82 | help="Working directory") |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 83 | args = parser.parse_args() |
| 84 | |
Kat | 0d114c8 | 2013-04-05 18:21:39 +0200 | [diff] [blame] | 85 | verbose_level = 1 |
| 86 | if args.verbose: |
| 87 | verbose_level = 2 |
| 88 | |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 89 | if args.w: |
| 90 | workdir = args.w |
| 91 | |
| 92 | if args.p: |
| 93 | confpath = args.p |
Kat | 0270be4 | 2013-04-05 21:34:52 +0200 | [diff] [blame] | 94 | osmoappdesc = osmoutil.importappconf_or_quit(confpath, "osmoappdesc", |
| 95 | args.p) |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 96 | |
Max | 2f266e0 | 2018-01-03 17:35:59 +0100 | [diff] [blame^] | 97 | print("confpath %s, workdir %s" % (confpath, workdir)) |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 98 | os.chdir(workdir) |
Max | 2f266e0 | 2018-01-03 17:35:59 +0100 | [diff] [blame^] | 99 | print("Running tests for specific VTY commands") |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 100 | suite = unittest.TestLoader().loadTestsFromTestCase(TestVTY) |
Kat | 0d114c8 | 2013-04-05 18:21:39 +0200 | [diff] [blame] | 101 | res = unittest.TextTestRunner(verbosity=verbose_level).run(suite) |
Kat | a7185c6 | 2013-04-04 17:31:13 +0200 | [diff] [blame] | 102 | sys.exit(len(res.errors) + len(res.failures)) |