blob: 8a18f843e07989324eba8d38778fbe45aa753518 [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 os
18import os.path
19import time
20import sys
21import tempfile
22
23import osmopy.obscvty as obscvty
24import osmopy.osmoutil as osmoutil
25
26
27# Return true iff all the tests for the given config pass
28def test_config(app_desc, config, tmpdir, verbose=True):
29 try:
30 test_config_atest(app_desc, config, verify_doc, verbose)
31
32 newconfig = copy_config(tmpdir, config)
33 test_config_atest(app_desc, newconfig, write_config, verbose)
34 test_config_atest(app_desc, newconfig, token_vty_command, verbose)
35 return 0
36
37 # If there's a socket error, skip the rest of the tests for this config
38 except IOError:
39 return 1
40
41
42def test_config_atest(app_desc, config, run_test, verbose=True):
43 proc = None
44 ret = None
45 try:
46 cmd = [app_desc[1], "-c", config]
47 if verbose:
48 print "Verifying %s, test %s" % (' '.join(cmd), run_test.__name__)
49
50 proc = osmoutil.popen_devnull(cmd)
51 time.sleep(1)
52 end = app_desc[2]
53 port = app_desc[0]
54 vty = obscvty.VTYInteract(end, "127.0.0.1", port)
55 ret = run_test(vty)
56
57 except IOError as se:
58 print >> sys.stderr, "Failed to verify %s" % ' '.join(cmd)
Kat0248d3b2013-04-05 20:19:17 +020059 print >> sys.stderr, "Current directory: %s" % os.getcwd()
Kata7185c62013-04-04 17:31:13 +020060 print >> sys.stderr, "Error was %s" % se
61 raise se
62
63 finally:
64 if proc:
65 osmoutil.end_proc(proc)
66
67 return ret
68
69
70def copy_config(dirname, config):
71 try:
72 os.stat(dirname)
73 except OSError:
74 os.mkdir(dirname)
75 else:
76 remove_tmpdir(dirname)
77 os.mkdir(dirname)
78
79 prefix = os.path.basename(config)
80 tmpfile = tempfile.NamedTemporaryFile(
81 dir=dirname, prefix=prefix, delete=False)
82 tmpfile.write(open(config).read())
83 tmpfile.close()
84 # This works around the precautions NamedTemporaryFile is made for...
85 return tmpfile.name
86
87
88def write_config(vty):
89 new_config = vty.enabled_command("write")
90 return new_config.split(' ')[-1]
91
92
93# The only purpose of this function is to verify a working vty
94def token_vty_command(vty):
95 vty.command("help")
96 return True
97
98
99# This may warn about the same doc missing multiple times, by design
100def verify_doc(vty):
101 xml = vty.command("show online-help")
102 split_at = "<command"
103 all_errs = []
104 for command in xml.split(split_at):
105 if "(null)" in command:
106 lines = command.split("\n")
107 cmd_line = split_at + lines[0]
108 err_lines = []
109 for line in lines:
110 if '(null)' in line:
111 err_lines.append(line)
112
113 all_errs.append(err_lines)
114
115 print >> sys.stderr, \
116 "Documentation error (missing docs): \n%s\n%s\n" % (
117 cmd_line, '\n'.join(err_lines))
118
119 return (len(all_errs), all_errs)
120
121
122# Skip testing the configurations of anything that hasn't been compiled
123def app_exists(app_desc):
124 cmd = app_desc[1]
125 return os.path.exists(cmd)
126
127
128def remove_tmpdir(tmpdir):
129 files = os.listdir(tmpdir)
130 for f in files:
131 os.unlink(os.path.join(tmpdir, f))
132 os.rmdir(tmpdir)
133
134
135def check_configs_tested(basedir, app_configs):
136 configs = []
137 for root, dirs, files in os.walk(basedir):
138 for f in files:
139 if f.endswith(".cfg"):
140 configs.append(os.path.join(root, f))
141 for config in configs:
142 found = False
143 for app in app_configs:
144 if config in app_configs[app]:
145 found = True
146 if not found:
147 print >> sys.stderr, "Warning: %s is not being tested" % config
148
149
150def test_all_apps(apps, app_configs, tmpdir="writtenconfig", verbose=True,
Kat0248d3b2013-04-05 20:19:17 +0200151 confpath=".", rmtmp=False):
Kata7185c62013-04-04 17:31:13 +0200152 check_configs_tested("doc/examples/", app_configs)
153 errors = 0
154 for app in apps:
155 if not app_exists(app):
156 print >> sys.stderr, "Skipping app %s (not found)" % app[1]
157 continue
158
159 configs = app_configs[app[3]]
160 for config in configs:
Kat0248d3b2013-04-05 20:19:17 +0200161 config = os.path.join(confpath, config)
Kata7185c62013-04-04 17:31:13 +0200162 errors |= test_config(app, config, tmpdir, verbose)
163
Kat0248d3b2013-04-05 20:19:17 +0200164 if rmtmp or not errors:
Kata7185c62013-04-04 17:31:13 +0200165 remove_tmpdir(tmpdir)
166
167 return errors
168
169
170if __name__ == '__main__':
171 import argparse
172
173 confpath = "."
Kat0248d3b2013-04-05 20:19:17 +0200174 wordir = "."
Kata7185c62013-04-04 17:31:13 +0200175
176 parser = argparse.ArgumentParser()
177 parser.add_argument("--e1nitb", action="store_true", dest="e1nitb")
178 parser.add_argument("-v", "--verbose", dest="verbose",
179 action="store_true", help="verbose mode")
180 parser.add_argument("-p", "--pythonconfpath", dest="p",
181 help="searchpath for config")
Kat0248d3b2013-04-05 20:19:17 +0200182 parser.add_argument("-w", "--workdir", dest="w",
183 help="Working directory to run in")
184
Kata7185c62013-04-04 17:31:13 +0200185 args = parser.parse_args()
186
187 if args.p:
188 confpath = args.p
189
Kat0248d3b2013-04-05 20:19:17 +0200190 if args.w:
191 workdir = args.w
192
Kata7185c62013-04-04 17:31:13 +0200193 osmoappdesc = None
194 try:
195 osmoappdesc = osmoutil.importappconf(confpath, "osmoappdesc")
196 except ImportError as e:
197 print >> sys.stderr, "osmoappdesc not found, set searchpath with -p"
198 sys.exit(1)
199
200 apps = osmoappdesc.apps
201 configs = osmoappdesc.app_configs
202
203 if args.e1nitb:
204 configs['nitb'].extend(osmoappdesc.nitb_e1_configs)
205
Kat0248d3b2013-04-05 20:19:17 +0200206 os.chdir(workdir)
207 sys.exit(test_all_apps(apps, configs, confpath=confpath,
208 verbose=args.verbose))