blob: 2132c43d56085896fb456c76ca4a689353f5525a [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
Max6c33a152016-04-13 17:29:51 +020020import sys, shutil, stat
Kata7185c62013-04-04 17:31:13 +020021import 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:
Holger Hans Peter Freyther4e98c262014-07-04 20:43:38 +020030 err = 0
Holger Hans Peter Freytherb819b572015-01-31 21:15:06 +010031 if test_config_atest(app_desc, config, verify_doc, verbose)[0] > 0:
Holger Hans Peter Freyther4e98c262014-07-04 20:43:38 +020032 err += 1
Kata7185c62013-04-04 17:31:13 +020033
34 newconfig = copy_config(tmpdir, config)
Holger Hans Peter Freyther4e98c262014-07-04 20:43:38 +020035 if test_config_atest(app_desc, newconfig, write_config, verbose) > 0:
36 err += 1
37
38 if test_config_atest(app_desc, newconfig, token_vty_command, verbose) > 0:
39 err += 1
40
41 return err
Kata7185c62013-04-04 17:31:13 +020042
43 # If there's a socket error, skip the rest of the tests for this config
44 except IOError:
45 return 1
46
47
48def test_config_atest(app_desc, config, run_test, verbose=True):
49 proc = None
50 ret = None
Neels Hofmeyr8972d062017-02-24 20:49:34 +010051 vty = None
Kata7185c62013-04-04 17:31:13 +020052 try:
Max5f4567b2016-03-30 14:58:17 +020053 cmd = app_desc[1].split(' ') + [ "-c", config]
Kata7185c62013-04-04 17:31:13 +020054 if verbose:
55 print "Verifying %s, test %s" % (' '.join(cmd), run_test.__name__)
56
57 proc = osmoutil.popen_devnull(cmd)
Kata7185c62013-04-04 17:31:13 +020058 end = app_desc[2]
59 port = app_desc[0]
60 vty = obscvty.VTYInteract(end, "127.0.0.1", port)
61 ret = run_test(vty)
62
63 except IOError as se:
64 print >> sys.stderr, "Failed to verify %s" % ' '.join(cmd)
Kat0248d3b2013-04-05 20:19:17 +020065 print >> sys.stderr, "Current directory: %s" % os.getcwd()
Kata7185c62013-04-04 17:31:13 +020066 print >> sys.stderr, "Error was %s" % se
Neels Hofmeyrcebe8f52017-02-27 01:04:43 +010067 print >> sys.stderr, "Config was\n%s" % open(config).read()
Kata7185c62013-04-04 17:31:13 +020068 raise se
69
70 finally:
71 if proc:
72 osmoutil.end_proc(proc)
Neels Hofmeyr8972d062017-02-24 20:49:34 +010073 if vty:
74 vty._close_socket()
Kata7185c62013-04-04 17:31:13 +020075
76 return ret
77
Kata7185c62013-04-04 17:31:13 +020078def copy_config(dirname, config):
Max334d6802016-04-07 14:11:25 +020079 shutil.rmtree(dirname, True)
80 ign = shutil.ignore_patterns('*.cfg')
81 shutil.copytree(os.path.dirname(config), dirname, ignore=ign)
Max6c33a152016-04-13 17:29:51 +020082 os.chmod(dirname, stat.S_IRWXU)
Max334d6802016-04-07 14:11:25 +020083
Kata7185c62013-04-04 17:31:13 +020084 try:
85 os.stat(dirname)
86 except OSError:
87 os.mkdir(dirname)
Kata7185c62013-04-04 17:31:13 +020088
89 prefix = os.path.basename(config)
90 tmpfile = tempfile.NamedTemporaryFile(
91 dir=dirname, prefix=prefix, delete=False)
92 tmpfile.write(open(config).read())
93 tmpfile.close()
94 # This works around the precautions NamedTemporaryFile is made for...
95 return tmpfile.name
96
97
98def write_config(vty):
99 new_config = vty.enabled_command("write")
Holger Hans Peter Freyther0edf0c92016-08-15 12:31:46 +0200100 if not new_config.startswith("Configuration saved to "):
101 print(new_config)
102 return 1, [new_config]
Holger Hans Peter Freytherb819b572015-01-31 21:15:06 +0100103 return 0
Kata7185c62013-04-04 17:31:13 +0200104
105
106# The only purpose of this function is to verify a working vty
107def token_vty_command(vty):
108 vty.command("help")
Holger Hans Peter Freyther4e98c262014-07-04 20:43:38 +0200109 return 0
Kata7185c62013-04-04 17:31:13 +0200110
111
112# This may warn about the same doc missing multiple times, by design
113def verify_doc(vty):
114 xml = vty.command("show online-help")
115 split_at = "<command"
116 all_errs = []
117 for command in xml.split(split_at):
118 if "(null)" in command:
119 lines = command.split("\n")
120 cmd_line = split_at + lines[0]
121 err_lines = []
122 for line in lines:
123 if '(null)' in line:
124 err_lines.append(line)
125
126 all_errs.append(err_lines)
127
128 print >> sys.stderr, \
129 "Documentation error (missing docs): \n%s\n%s\n" % (
130 cmd_line, '\n'.join(err_lines))
131
132 return (len(all_errs), all_errs)
133
134
135# Skip testing the configurations of anything that hasn't been compiled
136def app_exists(app_desc):
Max5f4567b2016-03-30 14:58:17 +0200137 cmd = app_desc[1].split(' ')[0]
Kata7185c62013-04-04 17:31:13 +0200138 return os.path.exists(cmd)
139
140
141def remove_tmpdir(tmpdir):
142 files = os.listdir(tmpdir)
143 for f in files:
144 os.unlink(os.path.join(tmpdir, f))
145 os.rmdir(tmpdir)
146
147
Maxd401cc12016-03-30 14:58:16 +0200148def check_configs_tested(basedir, app_configs, ignore_configs):
Kata7185c62013-04-04 17:31:13 +0200149 configs = []
150 for root, dirs, files in os.walk(basedir):
151 for f in files:
Maxd401cc12016-03-30 14:58:16 +0200152 if f.endswith(".cfg") and f not in ignore_configs:
Kata7185c62013-04-04 17:31:13 +0200153 configs.append(os.path.join(root, f))
154 for config in configs:
155 found = False
156 for app in app_configs:
157 if config in app_configs[app]:
158 found = True
159 if not found:
160 print >> sys.stderr, "Warning: %s is not being tested" % config
161
162
163def test_all_apps(apps, app_configs, tmpdir="writtenconfig", verbose=True,
Maxd401cc12016-03-30 14:58:16 +0200164 confpath=".", rmtmp=False, ignore_configs=[]):
165 check_configs_tested("doc/examples/", app_configs, ignore_configs)
Kata7185c62013-04-04 17:31:13 +0200166 errors = 0
167 for app in apps:
168 if not app_exists(app):
169 print >> sys.stderr, "Skipping app %s (not found)" % app[1]
170 continue
171
172 configs = app_configs[app[3]]
173 for config in configs:
Kat0248d3b2013-04-05 20:19:17 +0200174 config = os.path.join(confpath, config)
Neels Hofmeyrbaa6f122017-02-27 01:06:44 +0100175 errors += test_config(app, config, tmpdir, verbose)
Kata7185c62013-04-04 17:31:13 +0200176
Kat0248d3b2013-04-05 20:19:17 +0200177 if rmtmp or not errors:
Kata7185c62013-04-04 17:31:13 +0200178 remove_tmpdir(tmpdir)
179
Neels Hofmeyrbaa6f122017-02-27 01:06:44 +0100180 if errors:
181 print >> sys.stderr, "ERRORS: %d" % errors
Kata7185c62013-04-04 17:31:13 +0200182 return errors
183
184
185if __name__ == '__main__':
186 import argparse
187
188 confpath = "."
Kat0248d3b2013-04-05 20:19:17 +0200189 wordir = "."
Kata7185c62013-04-04 17:31:13 +0200190
191 parser = argparse.ArgumentParser()
192 parser.add_argument("--e1nitb", action="store_true", dest="e1nitb")
193 parser.add_argument("-v", "--verbose", dest="verbose",
194 action="store_true", help="verbose mode")
195 parser.add_argument("-p", "--pythonconfpath", dest="p",
196 help="searchpath for config")
Kat0248d3b2013-04-05 20:19:17 +0200197 parser.add_argument("-w", "--workdir", dest="w",
198 help="Working directory to run in")
199
Kata7185c62013-04-04 17:31:13 +0200200 args = parser.parse_args()
201
202 if args.p:
203 confpath = args.p
204
Kat0248d3b2013-04-05 20:19:17 +0200205 if args.w:
206 workdir = args.w
207
Kat0270be42013-04-05 21:34:52 +0200208 osmoappdesc = osmoutil.importappconf_or_quit(confpath, "osmoappdesc",
209 args.p)
Kata7185c62013-04-04 17:31:13 +0200210
211 apps = osmoappdesc.apps
212 configs = osmoappdesc.app_configs
Maxd401cc12016-03-30 14:58:16 +0200213 ignores = getattr(osmoappdesc, 'ignore_configs', [])
Kata7185c62013-04-04 17:31:13 +0200214
215 if args.e1nitb:
216 configs['nitb'].extend(osmoappdesc.nitb_e1_configs)
217
Kat0248d3b2013-04-05 20:19:17 +0200218 os.chdir(workdir)
Maxd401cc12016-03-30 14:58:16 +0200219 sys.exit(test_all_apps(apps, configs, ignore_configs=ignores,
220 confpath=confpath, verbose=args.verbose))