blob: 8d9f3c480242c89654b56143467d2fae38271303 [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):
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
69if __name__ == '__main__':
70 import argparse
71 import os
72 import sys
73
Kat0248d3b2013-04-05 20:19:17 +020074 workdir = '.'
Kata7185c62013-04-04 17:31:13 +020075
76 parser = argparse.ArgumentParser()
Kat0d114c82013-04-05 18:21:39 +020077 parser.add_argument("-v", "--verbose", dest="verbose",
78 action="store_true", help="verbose mode")
Kata7185c62013-04-04 17:31:13 +020079 parser.add_argument("-p", "--pythonconfpath", dest="p",
80 help="searchpath for config")
81 parser.add_argument("-w", "--workdir", dest="w",
Kat0248d3b2013-04-05 20:19:17 +020082 help="Working directory")
Kata7185c62013-04-04 17:31:13 +020083 args = parser.parse_args()
84
Kat0d114c82013-04-05 18:21:39 +020085 verbose_level = 1
86 if args.verbose:
87 verbose_level = 2
88
Kata7185c62013-04-04 17:31:13 +020089 if args.w:
90 workdir = args.w
91
92 if args.p:
93 confpath = args.p
Kat0270be42013-04-05 21:34:52 +020094 osmoappdesc = osmoutil.importappconf_or_quit(confpath, "osmoappdesc",
95 args.p)
Kata7185c62013-04-04 17:31:13 +020096
Kat0248d3b2013-04-05 20:19:17 +020097 print "confpath %s, workdir %s" % (confpath, workdir)
Kata7185c62013-04-04 17:31:13 +020098 os.chdir(workdir)
Kat0d114c82013-04-05 18:21:39 +020099 print "Running tests for specific VTY commands"
Kata7185c62013-04-04 17:31:13 +0200100 suite = unittest.TestLoader().loadTestsFromTestCase(TestVTY)
Kat0d114c82013-04-05 18:21:39 +0200101 res = unittest.TextTestRunner(verbosity=verbose_level).run(suite)
Kata7185c62013-04-04 17:31:13 +0200102 sys.exit(len(res.errors) + len(res.failures))