Oliver Smith | 14ec634 | 2019-12-10 13:07:27 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Holger Hans Peter Freyther | 6539752 | 2013-06-24 15:47:34 +0200 | [diff] [blame] | 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 | |
Max | 5fd9894 | 2016-11-16 14:55:21 +0100 | [diff] [blame] | 18 | import os, sys |
Holger Hans Peter Freyther | 6539752 | 2013-06-24 15:47:34 +0200 | [diff] [blame] | 19 | import time |
| 20 | import unittest |
Jacob Erlbeck | 4684eb6 | 2013-08-14 11:10:34 +0200 | [diff] [blame] | 21 | import socket |
Neels Hofmeyr | d81df4f | 2017-02-03 16:09:17 +0100 | [diff] [blame] | 22 | import subprocess |
Holger Hans Peter Freyther | 6539752 | 2013-06-24 15:47:34 +0200 | [diff] [blame] | 23 | |
| 24 | import osmopy.obscvty as obscvty |
| 25 | import osmopy.osmoutil as osmoutil |
Max | 8a58bcd | 2017-12-19 18:12:11 +0100 | [diff] [blame] | 26 | from osmopy.osmo_ipa import IPA |
Max | 5fd9894 | 2016-11-16 14:55:21 +0100 | [diff] [blame] | 27 | |
Neels Hofmeyr | 6226a36 | 2017-02-24 17:55:11 +0100 | [diff] [blame] | 28 | # to be able to find $top_srcdir/doc/... |
| 29 | confpath = os.path.join(sys.path[0], '..') |
Holger Hans Peter Freyther | 6539752 | 2013-06-24 15:47:34 +0200 | [diff] [blame] | 30 | |
| 31 | class TestVTYBase(unittest.TestCase): |
| 32 | |
Neels Hofmeyr | d489ea3 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 33 | def checkForEndAndExit(self): |
| 34 | res = self.vty.command("list") |
| 35 | #print ('looking for "exit"\n') |
Oliver Smith | 14ec634 | 2019-12-10 13:07:27 +0100 | [diff] [blame] | 36 | self.assertTrue(res.find(' exit\r') > 0) |
Neels Hofmeyr | d489ea3 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 37 | #print 'found "exit"\nlooking for "end"\n' |
Oliver Smith | 14ec634 | 2019-12-10 13:07:27 +0100 | [diff] [blame] | 38 | self.assertTrue(res.find(' end\r') > 0) |
Neels Hofmeyr | d489ea3 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 39 | #print 'found "end"\n' |
| 40 | |
Holger Hans Peter Freyther | 6539752 | 2013-06-24 15:47:34 +0200 | [diff] [blame] | 41 | def vty_command(self): |
| 42 | raise Exception("Needs to be implemented by a subclass") |
| 43 | |
| 44 | def vty_app(self): |
| 45 | raise Exception("Needs to be implemented by a subclass") |
| 46 | |
| 47 | def setUp(self): |
| 48 | osmo_vty_cmd = self.vty_command()[:] |
| 49 | config_index = osmo_vty_cmd.index('-c') |
| 50 | if config_index: |
| 51 | cfi = config_index + 1 |
| 52 | osmo_vty_cmd[cfi] = os.path.join(confpath, osmo_vty_cmd[cfi]) |
| 53 | |
| 54 | try: |
Holger Hans Peter Freyther | 6539752 | 2013-06-24 15:47:34 +0200 | [diff] [blame] | 55 | self.proc = osmoutil.popen_devnull(osmo_vty_cmd) |
| 56 | except OSError: |
Oliver Smith | 14ec634 | 2019-12-10 13:07:27 +0100 | [diff] [blame] | 57 | print("Current directory: %s" % os.getcwd(), file=sys.stderr) |
| 58 | print("Consider setting -b", file=sys.stderr) |
Holger Hans Peter Freyther | 6539752 | 2013-06-24 15:47:34 +0200 | [diff] [blame] | 59 | |
| 60 | appstring = self.vty_app()[2] |
| 61 | appport = self.vty_app()[0] |
| 62 | self.vty = obscvty.VTYInteract(appstring, "127.0.0.1", appport) |
| 63 | |
| 64 | def tearDown(self): |
Neels Hofmeyr | d5834d1 | 2017-02-24 17:54:22 +0100 | [diff] [blame] | 65 | if self.vty: |
| 66 | self.vty._close_socket() |
Holger Hans Peter Freyther | 6539752 | 2013-06-24 15:47:34 +0200 | [diff] [blame] | 67 | self.vty = None |
| 68 | osmoutil.end_proc(self.proc) |
| 69 | |
Jacob Erlbeck | 768a7c3 | 2013-09-02 13:17:14 +0200 | [diff] [blame] | 70 | |
Neels Hofmeyr | 62507c5 | 2017-07-13 02:03:50 +0200 | [diff] [blame] | 71 | class TestVTYGbproxy(TestVTYBase): |
Jacob Erlbeck | 7553d1c | 2013-10-23 11:24:15 +0200 | [diff] [blame] | 72 | |
| 73 | def vty_command(self): |
Harald Welte | 936dfd7 | 2021-01-31 19:36:06 +0100 | [diff] [blame] | 74 | return ["./src/osmo-gbproxy", "-c", |
Jacob Erlbeck | 7553d1c | 2013-10-23 11:24:15 +0200 | [diff] [blame] | 75 | "doc/examples/osmo-gbproxy/osmo-gbproxy.cfg"] |
| 76 | |
| 77 | def vty_app(self): |
Harald Welte | 936dfd7 | 2021-01-31 19:36:06 +0100 | [diff] [blame] | 78 | return (4246, "./src/osmo-gbproxy", "OsmoGbProxy", "gbproxy") |
Jacob Erlbeck | 7553d1c | 2013-10-23 11:24:15 +0200 | [diff] [blame] | 79 | |
| 80 | def testVtyTree(self): |
| 81 | self.vty.enable() |
| 82 | self.assertTrue(self.vty.verify('configure terminal', [''])) |
Oliver Smith | 14ec634 | 2019-12-10 13:07:27 +0100 | [diff] [blame] | 83 | self.assertEqual(self.vty.node(), 'config') |
Jacob Erlbeck | a423591 | 2013-10-29 09:30:31 +0100 | [diff] [blame] | 84 | self.checkForEndAndExit() |
Jacob Erlbeck | 7553d1c | 2013-10-23 11:24:15 +0200 | [diff] [blame] | 85 | self.assertTrue(self.vty.verify('ns', [''])) |
Oliver Smith | 14ec634 | 2019-12-10 13:07:27 +0100 | [diff] [blame] | 86 | self.assertEqual(self.vty.node(), 'config-ns') |
Jacob Erlbeck | 7553d1c | 2013-10-23 11:24:15 +0200 | [diff] [blame] | 87 | self.checkForEndAndExit() |
| 88 | self.assertTrue(self.vty.verify('exit', [''])) |
Oliver Smith | 14ec634 | 2019-12-10 13:07:27 +0100 | [diff] [blame] | 89 | self.assertEqual(self.vty.node(), 'config') |
Jacob Erlbeck | 7553d1c | 2013-10-23 11:24:15 +0200 | [diff] [blame] | 90 | self.assertTrue(self.vty.verify('gbproxy', [''])) |
Oliver Smith | 14ec634 | 2019-12-10 13:07:27 +0100 | [diff] [blame] | 91 | self.assertEqual(self.vty.node(), 'config-gbproxy') |
Jacob Erlbeck | 7553d1c | 2013-10-23 11:24:15 +0200 | [diff] [blame] | 92 | self.checkForEndAndExit() |
| 93 | self.assertTrue(self.vty.verify('exit', [''])) |
Oliver Smith | 14ec634 | 2019-12-10 13:07:27 +0100 | [diff] [blame] | 94 | self.assertEqual(self.vty.node(), 'config') |
Jacob Erlbeck | 7553d1c | 2013-10-23 11:24:15 +0200 | [diff] [blame] | 95 | |
| 96 | def testVtyShow(self): |
| 97 | res = self.vty.command("show ns") |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 98 | self.assertTrue(res.find('UDP bind') >= 0) |
Jacob Erlbeck | 7553d1c | 2013-10-23 11:24:15 +0200 | [diff] [blame] | 99 | |
Harald Welte | befe1c3 | 2020-12-12 15:15:34 +0100 | [diff] [blame] | 100 | res = self.vty.command("show gbproxy bvc bss stats") |
Oliver Smith | 14ec634 | 2019-12-10 13:07:27 +0100 | [diff] [blame] | 101 | self.assertTrue(res.find('GBProxy Global Statistics') >= 0) |
Jacob Erlbeck | 7553d1c | 2013-10-23 11:24:15 +0200 | [diff] [blame] | 102 | |
Jacob Erlbeck | 7fee972 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 103 | def testVtyDeletePeer(self): |
| 104 | self.vty.enable() |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 105 | self.assertTrue(self.vty.verify('delete-gbproxy-peer 9999 bvci 7777', ['NSE not found'])) |
Jacob Erlbeck | 7fee972 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 106 | res = self.vty.command("delete-gbproxy-peer 9999 all dry-run") |
Oliver Smith | 14ec634 | 2019-12-10 13:07:27 +0100 | [diff] [blame] | 107 | self.assertTrue(res.find('Not Deleted 0 BVC') >= 0) |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 108 | self.assertTrue(res.find('NSEI not found') >= 0) |
Jacob Erlbeck | 7fee972 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 109 | res = self.vty.command("delete-gbproxy-peer 9999 only-bvc dry-run") |
Oliver Smith | 14ec634 | 2019-12-10 13:07:27 +0100 | [diff] [blame] | 110 | self.assertTrue(res.find('Not Deleted 0 BVC') >= 0) |
Jacob Erlbeck | 7fee972 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 111 | res = self.vty.command("delete-gbproxy-peer 9999 only-nsvc dry-run") |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 112 | self.assertTrue(res.find('NSEI not found') >= 0) |
Jacob Erlbeck | 7fee972 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 113 | res = self.vty.command("delete-gbproxy-peer 9999 all") |
Oliver Smith | 14ec634 | 2019-12-10 13:07:27 +0100 | [diff] [blame] | 114 | self.assertTrue(res.find('Deleted 0 BVC') >= 0) |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 115 | self.assertTrue(res.find('NSEI not found') >= 0) |
Jacob Erlbeck | 7fee972 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 116 | |
Jacob Erlbeck | 7553d1c | 2013-10-23 11:24:15 +0200 | [diff] [blame] | 117 | def add_gbproxy_test(suite, workdir): |
Harald Welte | 936dfd7 | 2021-01-31 19:36:06 +0100 | [diff] [blame] | 118 | assert os.path.isfile(os.path.join(workdir, "src/osmo-gbproxy")) |
Jacob Erlbeck | 7553d1c | 2013-10-23 11:24:15 +0200 | [diff] [blame] | 119 | test = unittest.TestLoader().loadTestsFromTestCase(TestVTYGbproxy) |
| 120 | suite.addTest(test) |
| 121 | |
Holger Hans Peter Freyther | 6539752 | 2013-06-24 15:47:34 +0200 | [diff] [blame] | 122 | if __name__ == '__main__': |
| 123 | import argparse |
| 124 | import sys |
| 125 | |
| 126 | workdir = '.' |
| 127 | |
| 128 | parser = argparse.ArgumentParser() |
| 129 | parser.add_argument("-v", "--verbose", dest="verbose", |
| 130 | action="store_true", help="verbose mode") |
| 131 | parser.add_argument("-p", "--pythonconfpath", dest="p", |
| 132 | help="searchpath for config") |
| 133 | parser.add_argument("-w", "--workdir", dest="w", |
| 134 | help="Working directory") |
Neels Hofmeyr | b21619e | 2017-02-28 02:43:29 +0100 | [diff] [blame] | 135 | parser.add_argument("test_name", nargs="*", help="(parts of) test names to run, case-insensitive") |
Holger Hans Peter Freyther | 6539752 | 2013-06-24 15:47:34 +0200 | [diff] [blame] | 136 | args = parser.parse_args() |
| 137 | |
| 138 | verbose_level = 1 |
| 139 | if args.verbose: |
| 140 | verbose_level = 2 |
| 141 | |
| 142 | if args.w: |
| 143 | workdir = args.w |
| 144 | |
| 145 | if args.p: |
| 146 | confpath = args.p |
| 147 | |
Oliver Smith | 14ec634 | 2019-12-10 13:07:27 +0100 | [diff] [blame] | 148 | print("confpath %s, workdir %s" % (confpath, workdir)) |
Holger Hans Peter Freyther | 6539752 | 2013-06-24 15:47:34 +0200 | [diff] [blame] | 149 | os.chdir(workdir) |
Oliver Smith | 14ec634 | 2019-12-10 13:07:27 +0100 | [diff] [blame] | 150 | print("Running tests for specific VTY commands") |
Holger Hans Peter Freyther | 6539752 | 2013-06-24 15:47:34 +0200 | [diff] [blame] | 151 | suite = unittest.TestSuite() |
Jacob Erlbeck | 7553d1c | 2013-10-23 11:24:15 +0200 | [diff] [blame] | 152 | add_gbproxy_test(suite, workdir) |
Neels Hofmeyr | b21619e | 2017-02-28 02:43:29 +0100 | [diff] [blame] | 153 | |
| 154 | if args.test_name: |
| 155 | osmoutil.pick_tests(suite, *args.test_name) |
| 156 | |
Neels Hofmeyr | 58b99ae | 2016-09-28 23:48:02 +0200 | [diff] [blame] | 157 | res = unittest.TextTestRunner(verbosity=verbose_level, stream=sys.stdout).run(suite) |
Holger Hans Peter Freyther | 6539752 | 2013-06-24 15:47:34 +0200 | [diff] [blame] | 158 | sys.exit(len(res.errors) + len(res.failures)) |
Neels Hofmeyr | 558af7a | 2016-09-26 03:18:32 +0200 | [diff] [blame] | 159 | |
Neels Hofmeyr | b993563 | 2016-09-28 23:28:06 +0200 | [diff] [blame] | 160 | # vim: shiftwidth=4 expandtab nocin ai |