blob: 2ee877a3788d9433f1ce13231557732ce2b40f6e [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
17import time
18import unittest
19
20import osmopy.obscvty as obscvty
21import osmopy.osmoutil as osmoutil
22
23"""Test a VTY. Warning: osmoappdesc must be imported first."""
24
25
26class TestVTY(unittest.TestCase):
27 def setUp(self):
28 osmo_vty_cmd = osmoappdesc.vty_command
29 try:
30 self.proc = osmoutil.popen_devnull(osmo_vty_cmd)
31 except OSError:
32 print >> sys.stderr, "Current directory: %s" % os.getcwd()
33 print >> sys.stderr, "Consider setting -w"
34 time.sleep(1)
35
36 appstring = osmoappdesc.vty_app[2]
37 appport = osmoappdesc.vty_app[0]
38 self.vty = obscvty.VTYInteract(appstring, "127.0.0.1", appport)
39
40 def tearDown(self):
41 self.vty = None
42 osmoutil.end_proc(self.proc)
43
44 def test_history(self):
45 t1 = "show version"
46 self.vty.command(t1)
47 test_str = "show history"
48 assert(self.vty.w_verify(test_str, [t1]))
49
50 def test_unknown_command(self):
51 test_str = "help show"
52 assert(self.vty.verify(test_str, ['% Unknown command.']))
53
54 def test_terminal_length(self):
55 test_str = "terminal length 20"
56 assert(self.vty.verify(test_str, ['']))
57
58
59if __name__ == '__main__':
60 import argparse
61 import os
62 import sys
63
64 workdir = "."
65 confpath = "."
66
67 parser = argparse.ArgumentParser()
68 parser.add_argument("-p", "--pythonconfpath", dest="p",
69 help="searchpath for config")
70 parser.add_argument("-w", "--workdir", dest="w",
71 help="Working directory to run in")
72 args = parser.parse_args()
73
74 if args.w:
75 workdir = args.w
76
77 if args.p:
78 confpath = args.p
79 osmoappdesc = None
80 try:
81 osmoappdesc = osmoutil.importappconf(confpath, "osmoappdesc")
82 except ImportError as e:
83 print >> sys.stderr, "osmoappdesc not found, set searchpath with -p"
84 sys.exit(1)
85
86 os.chdir(workdir)
87 suite = unittest.TestLoader().loadTestsFromTestCase(TestVTY)
88 res = unittest.TextTestRunner(verbosity=1).run(suite)
89 sys.exit(len(res.errors) + len(res.failures))