blob: 9f8dd0af43ab5a134eddb7fefeaf77c20a5a935f [file] [log] [blame]
Kata7185c62013-04-04 17:31:13 +02001#!/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
Kat0248d3b2013-04-05 20:19:17 +020017import os
Kata7185c62013-04-04 17:31:13 +020018import time
19import unittest
20
21import osmopy.obscvty as obscvty
22import osmopy.osmoutil as osmoutil
23
Kat0248d3b2013-04-05 20:19:17 +020024confpath = '.'
25
Kata7185c62013-04-04 17:31:13 +020026"""Test a VTY. Warning: osmoappdesc must be imported first."""
27
28
29class TestVTY(unittest.TestCase):
Kat0270be42013-04-05 21:34:52 +020030
Kata7185c62013-04-04 17:31:13 +020031 def setUp(self):
Kat0248d3b2013-04-05 20:19:17 +020032 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
Kata7185c62013-04-04 17:31:13 +020038 try:
Kat0248d3b2013-04-05 20:19:17 +020039 print "Launch: %s from %s" % (' '.join(osmo_vty_cmd), os.getcwd())
Kata7185c62013-04-04 17:31:13 +020040 self.proc = osmoutil.popen_devnull(osmo_vty_cmd)
41 except OSError:
42 print >> sys.stderr, "Current directory: %s" % os.getcwd()
Kat0248d3b2013-04-05 20:19:17 +020043 print >> sys.stderr, "Consider setting -b"
Kata7185c62013-04-04 17:31:13 +020044 time.sleep(1)
45
46 appstring = osmoappdesc.vty_app[2]
47 appport = osmoappdesc.vty_app[0]
48 self.vty = obscvty.VTYInteract(appstring, "127.0.0.1", appport)
49
50 def tearDown(self):
Holger Hans Peter Freyther99b5c562017-02-13 20:06:44 +070051 self.vty._close_socket()
Kata7185c62013-04-04 17:31:13 +020052 self.vty = None
53 osmoutil.end_proc(self.proc)
54
55 def test_history(self):
56 t1 = "show version"
57 self.vty.command(t1)
58 test_str = "show history"
59 assert(self.vty.w_verify(test_str, [t1]))
60
61 def test_unknown_command(self):
62 test_str = "help show"
63 assert(self.vty.verify(test_str, ['% Unknown command.']))
64
65 def test_terminal_length(self):
66 test_str = "terminal length 20"
67 assert(self.vty.verify(test_str, ['']))
68
69
70if __name__ == '__main__':
71 import argparse
72 import os
73 import sys
74
Kat0248d3b2013-04-05 20:19:17 +020075 workdir = '.'
Kata7185c62013-04-04 17:31:13 +020076
77 parser = argparse.ArgumentParser()
Kat0d114c82013-04-05 18:21:39 +020078 parser.add_argument("-v", "--verbose", dest="verbose",
79 action="store_true", help="verbose mode")
Kata7185c62013-04-04 17:31:13 +020080 parser.add_argument("-p", "--pythonconfpath", dest="p",
81 help="searchpath for config")
82 parser.add_argument("-w", "--workdir", dest="w",
Kat0248d3b2013-04-05 20:19:17 +020083 help="Working directory")
Kata7185c62013-04-04 17:31:13 +020084 args = parser.parse_args()
85
Kat0d114c82013-04-05 18:21:39 +020086 verbose_level = 1
87 if args.verbose:
88 verbose_level = 2
89
Kata7185c62013-04-04 17:31:13 +020090 if args.w:
91 workdir = args.w
92
93 if args.p:
94 confpath = args.p
Kat0270be42013-04-05 21:34:52 +020095 osmoappdesc = osmoutil.importappconf_or_quit(confpath, "osmoappdesc",
96 args.p)
Kata7185c62013-04-04 17:31:13 +020097
Kat0248d3b2013-04-05 20:19:17 +020098 print "confpath %s, workdir %s" % (confpath, workdir)
Kata7185c62013-04-04 17:31:13 +020099 os.chdir(workdir)
Kat0d114c82013-04-05 18:21:39 +0200100 print "Running tests for specific VTY commands"
Kata7185c62013-04-04 17:31:13 +0200101 suite = unittest.TestLoader().loadTestsFromTestCase(TestVTY)
Kat0d114c82013-04-05 18:21:39 +0200102 res = unittest.TextTestRunner(verbosity=verbose_level).run(suite)
Kata7185c62013-04-04 17:31:13 +0200103 sys.exit(len(res.errors) + len(res.failures))