blob: f02c61032f1345a9cee734d052793e38ddb568bb [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):
30 def setUp(self):
Kat0248d3b2013-04-05 20:19:17 +020031 osmo_vty_cmd = osmoappdesc.vty_command[:]
32 config_index = osmo_vty_cmd.index('-c')
33 if config_index:
34 cfi = config_index + 1
35 osmo_vty_cmd[cfi] = os.path.join(confpath, osmo_vty_cmd[cfi])
36
Kata7185c62013-04-04 17:31:13 +020037 try:
Kat0248d3b2013-04-05 20:19:17 +020038 print "Launch: %s from %s" % (' '.join(osmo_vty_cmd), os.getcwd())
Kata7185c62013-04-04 17:31:13 +020039 self.proc = osmoutil.popen_devnull(osmo_vty_cmd)
40 except OSError:
41 print >> sys.stderr, "Current directory: %s" % os.getcwd()
Kat0248d3b2013-04-05 20:19:17 +020042 print >> sys.stderr, "Consider setting -b"
Kata7185c62013-04-04 17:31:13 +020043 time.sleep(1)
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):
50 self.vty = None
51 osmoutil.end_proc(self.proc)
52
53 def test_history(self):
54 t1 = "show version"
55 self.vty.command(t1)
56 test_str = "show history"
57 assert(self.vty.w_verify(test_str, [t1]))
58
59 def test_unknown_command(self):
60 test_str = "help show"
61 assert(self.vty.verify(test_str, ['% Unknown command.']))
62
63 def test_terminal_length(self):
64 test_str = "terminal length 20"
65 assert(self.vty.verify(test_str, ['']))
66
67
68if __name__ == '__main__':
69 import argparse
70 import os
71 import sys
72
Kat0248d3b2013-04-05 20:19:17 +020073 workdir = '.'
Kata7185c62013-04-04 17:31:13 +020074
75 parser = argparse.ArgumentParser()
Kat0d114c82013-04-05 18:21:39 +020076 parser.add_argument("-v", "--verbose", dest="verbose",
77 action="store_true", help="verbose mode")
Kata7185c62013-04-04 17:31:13 +020078 parser.add_argument("-p", "--pythonconfpath", dest="p",
79 help="searchpath for config")
80 parser.add_argument("-w", "--workdir", dest="w",
Kat0248d3b2013-04-05 20:19:17 +020081 help="Working directory")
Kata7185c62013-04-04 17:31:13 +020082 args = parser.parse_args()
83
Kat0d114c82013-04-05 18:21:39 +020084 verbose_level = 1
85 if args.verbose:
86 verbose_level = 2
87
Kata7185c62013-04-04 17:31:13 +020088 if args.w:
89 workdir = args.w
90
91 if args.p:
92 confpath = args.p
93 osmoappdesc = None
94 try:
95 osmoappdesc = osmoutil.importappconf(confpath, "osmoappdesc")
96 except ImportError as e:
97 print >> sys.stderr, "osmoappdesc not found, set searchpath with -p"
98 sys.exit(1)
99
Kat0248d3b2013-04-05 20:19:17 +0200100 print "confpath %s, workdir %s" % (confpath, workdir)
Kata7185c62013-04-04 17:31:13 +0200101 os.chdir(workdir)
Kat0d114c82013-04-05 18:21:39 +0200102 print "Running tests for specific VTY commands"
Kata7185c62013-04-04 17:31:13 +0200103 suite = unittest.TestLoader().loadTestsFromTestCase(TestVTY)
Kat0d114c82013-04-05 18:21:39 +0200104 res = unittest.TextTestRunner(verbosity=verbose_level).run(suite)
Kata7185c62013-04-04 17:31:13 +0200105 sys.exit(len(res.errors) + len(res.failures))