blob: 50cf4f3b2e02c45fe6b39169a697870a78d35ed3 [file] [log] [blame]
Oliver Smith14ec6342019-12-10 13:07:27 +01001#!/usr/bin/env python3
Holger Hans Peter Freyther65397522013-06-24 15:47:34 +02002
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
Max5fd98942016-11-16 14:55:21 +010018import os, sys
Holger Hans Peter Freyther65397522013-06-24 15:47:34 +020019import time
20import unittest
Jacob Erlbeck4684eb62013-08-14 11:10:34 +020021import socket
Neels Hofmeyrd81df4f2017-02-03 16:09:17 +010022import subprocess
Holger Hans Peter Freyther65397522013-06-24 15:47:34 +020023
24import osmopy.obscvty as obscvty
25import osmopy.osmoutil as osmoutil
Max8a58bcd2017-12-19 18:12:11 +010026from osmopy.osmo_ipa import IPA
Max5fd98942016-11-16 14:55:21 +010027
Neels Hofmeyr6226a362017-02-24 17:55:11 +010028# to be able to find $top_srcdir/doc/...
29confpath = os.path.join(sys.path[0], '..')
Holger Hans Peter Freyther65397522013-06-24 15:47:34 +020030
31class TestVTYBase(unittest.TestCase):
32
Neels Hofmeyrd489ea32016-05-20 21:59:55 +020033 def checkForEndAndExit(self):
34 res = self.vty.command("list")
35 #print ('looking for "exit"\n')
Oliver Smith14ec6342019-12-10 13:07:27 +010036 self.assertTrue(res.find(' exit\r') > 0)
Neels Hofmeyrd489ea32016-05-20 21:59:55 +020037 #print 'found "exit"\nlooking for "end"\n'
Oliver Smith14ec6342019-12-10 13:07:27 +010038 self.assertTrue(res.find(' end\r') > 0)
Neels Hofmeyrd489ea32016-05-20 21:59:55 +020039 #print 'found "end"\n'
40
Holger Hans Peter Freyther65397522013-06-24 15:47:34 +020041 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 Freyther65397522013-06-24 15:47:34 +020055 self.proc = osmoutil.popen_devnull(osmo_vty_cmd)
56 except OSError:
Oliver Smith14ec6342019-12-10 13:07:27 +010057 print("Current directory: %s" % os.getcwd(), file=sys.stderr)
58 print("Consider setting -b", file=sys.stderr)
Holger Hans Peter Freyther65397522013-06-24 15:47:34 +020059
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 Hofmeyrd5834d12017-02-24 17:54:22 +010065 if self.vty:
66 self.vty._close_socket()
Holger Hans Peter Freyther65397522013-06-24 15:47:34 +020067 self.vty = None
68 osmoutil.end_proc(self.proc)
69
Jacob Erlbeck768a7c32013-09-02 13:17:14 +020070
Neels Hofmeyr62507c52017-07-13 02:03:50 +020071class TestVTYGbproxy(TestVTYBase):
Jacob Erlbeck7553d1c2013-10-23 11:24:15 +020072
73 def vty_command(self):
Harald Welte936dfd72021-01-31 19:36:06 +010074 return ["./src/osmo-gbproxy", "-c",
Jacob Erlbeck7553d1c2013-10-23 11:24:15 +020075 "doc/examples/osmo-gbproxy/osmo-gbproxy.cfg"]
76
77 def vty_app(self):
Harald Welte936dfd72021-01-31 19:36:06 +010078 return (4246, "./src/osmo-gbproxy", "OsmoGbProxy", "gbproxy")
Jacob Erlbeck7553d1c2013-10-23 11:24:15 +020079
80 def testVtyTree(self):
81 self.vty.enable()
82 self.assertTrue(self.vty.verify('configure terminal', ['']))
Oliver Smith14ec6342019-12-10 13:07:27 +010083 self.assertEqual(self.vty.node(), 'config')
Jacob Erlbecka4235912013-10-29 09:30:31 +010084 self.checkForEndAndExit()
Jacob Erlbeck7553d1c2013-10-23 11:24:15 +020085 self.assertTrue(self.vty.verify('ns', ['']))
Oliver Smith14ec6342019-12-10 13:07:27 +010086 self.assertEqual(self.vty.node(), 'config-ns')
Jacob Erlbeck7553d1c2013-10-23 11:24:15 +020087 self.checkForEndAndExit()
88 self.assertTrue(self.vty.verify('exit', ['']))
Oliver Smith14ec6342019-12-10 13:07:27 +010089 self.assertEqual(self.vty.node(), 'config')
Jacob Erlbeck7553d1c2013-10-23 11:24:15 +020090 self.assertTrue(self.vty.verify('gbproxy', ['']))
Oliver Smith14ec6342019-12-10 13:07:27 +010091 self.assertEqual(self.vty.node(), 'config-gbproxy')
Jacob Erlbeck7553d1c2013-10-23 11:24:15 +020092 self.checkForEndAndExit()
93 self.assertTrue(self.vty.verify('exit', ['']))
Oliver Smith14ec6342019-12-10 13:07:27 +010094 self.assertEqual(self.vty.node(), 'config')
Jacob Erlbeck7553d1c2013-10-23 11:24:15 +020095
96 def testVtyShow(self):
97 res = self.vty.command("show ns")
Alexander Couzens951e1332020-09-22 13:21:46 +020098 self.assertTrue(res.find('UDP bind') >= 0)
Jacob Erlbeck7553d1c2013-10-23 11:24:15 +020099
Harald Weltebefe1c32020-12-12 15:15:34 +0100100 res = self.vty.command("show gbproxy bvc bss stats")
Oliver Smith14ec6342019-12-10 13:07:27 +0100101 self.assertTrue(res.find('GBProxy Global Statistics') >= 0)
Jacob Erlbeck7553d1c2013-10-23 11:24:15 +0200102
Jacob Erlbeck7fee9722013-10-24 12:48:23 +0200103 def testVtyDeletePeer(self):
104 self.vty.enable()
Harald Weltee5209642020-12-05 19:59:45 +0100105 self.assertTrue(self.vty.verify('delete-gbproxy-peer 9999 bvci 7777', ['NSE not found']))
Jacob Erlbeck7fee9722013-10-24 12:48:23 +0200106 res = self.vty.command("delete-gbproxy-peer 9999 all dry-run")
Oliver Smith14ec6342019-12-10 13:07:27 +0100107 self.assertTrue(res.find('Not Deleted 0 BVC') >= 0)
Alexander Couzens951e1332020-09-22 13:21:46 +0200108 self.assertTrue(res.find('NSEI not found') >= 0)
Jacob Erlbeck7fee9722013-10-24 12:48:23 +0200109 res = self.vty.command("delete-gbproxy-peer 9999 only-bvc dry-run")
Oliver Smith14ec6342019-12-10 13:07:27 +0100110 self.assertTrue(res.find('Not Deleted 0 BVC') >= 0)
Jacob Erlbeck7fee9722013-10-24 12:48:23 +0200111 res = self.vty.command("delete-gbproxy-peer 9999 only-nsvc dry-run")
Alexander Couzens951e1332020-09-22 13:21:46 +0200112 self.assertTrue(res.find('NSEI not found') >= 0)
Jacob Erlbeck7fee9722013-10-24 12:48:23 +0200113 res = self.vty.command("delete-gbproxy-peer 9999 all")
Oliver Smith14ec6342019-12-10 13:07:27 +0100114 self.assertTrue(res.find('Deleted 0 BVC') >= 0)
Alexander Couzens951e1332020-09-22 13:21:46 +0200115 self.assertTrue(res.find('NSEI not found') >= 0)
Jacob Erlbeck7fee9722013-10-24 12:48:23 +0200116
Jacob Erlbeck7553d1c2013-10-23 11:24:15 +0200117def add_gbproxy_test(suite, workdir):
Harald Welte936dfd72021-01-31 19:36:06 +0100118 assert os.path.isfile(os.path.join(workdir, "src/osmo-gbproxy"))
Jacob Erlbeck7553d1c2013-10-23 11:24:15 +0200119 test = unittest.TestLoader().loadTestsFromTestCase(TestVTYGbproxy)
120 suite.addTest(test)
121
Holger Hans Peter Freyther65397522013-06-24 15:47:34 +0200122if __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 Hofmeyrb21619e2017-02-28 02:43:29 +0100135 parser.add_argument("test_name", nargs="*", help="(parts of) test names to run, case-insensitive")
Holger Hans Peter Freyther65397522013-06-24 15:47:34 +0200136 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 Smith14ec6342019-12-10 13:07:27 +0100148 print("confpath %s, workdir %s" % (confpath, workdir))
Holger Hans Peter Freyther65397522013-06-24 15:47:34 +0200149 os.chdir(workdir)
Oliver Smith14ec6342019-12-10 13:07:27 +0100150 print("Running tests for specific VTY commands")
Holger Hans Peter Freyther65397522013-06-24 15:47:34 +0200151 suite = unittest.TestSuite()
Jacob Erlbeck7553d1c2013-10-23 11:24:15 +0200152 add_gbproxy_test(suite, workdir)
Neels Hofmeyrb21619e2017-02-28 02:43:29 +0100153
154 if args.test_name:
155 osmoutil.pick_tests(suite, *args.test_name)
156
Neels Hofmeyr58b99ae2016-09-28 23:48:02 +0200157 res = unittest.TextTestRunner(verbosity=verbose_level, stream=sys.stdout).run(suite)
Holger Hans Peter Freyther65397522013-06-24 15:47:34 +0200158 sys.exit(len(res.errors) + len(res.failures))
Neels Hofmeyr558af7a2016-09-26 03:18:32 +0200159
Neels Hofmeyrb9935632016-09-28 23:28:06 +0200160# vim: shiftwidth=4 expandtab nocin ai