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