#!/usr/bin/env python3 # (C) 2013 by Katerina Barone-Adesi # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program. If not, see . import os import os.path import shutil import time import sys, shutil, stat import tempfile import osmopy.obscvty as obscvty import osmopy.osmoutil as osmoutil # Run all tests for a given config, raise error on failure def test_config(app_desc, config, tmpdir, verbose=True): if test_config_atest(app_desc, config, verify_doc, verbose)[0] > 0: raise RuntimeError(f"{config}: verify_doc() failed") newconfig = copy_config(tmpdir, config) if test_config_atest(app_desc, newconfig, write_config, verbose) > 0: raise RuntimeError(f"{config}: write_config() failed") if test_config_atest(app_desc, newconfig, token_vty_command, verbose) > 0: raise RuntimeError(f"{config}: token_vty_command() failed") def test_config_atest(app_desc, config, run_test, verbose=True): proc = None ret = None vty = None try: cmd = app_desc[1].split(' ') + [ "-c", config] if verbose: print("Verifying %s, test %s" % (' '.join(cmd), run_test.__name__)) proc = osmoutil.popen_devnull(cmd) end = app_desc[2] port = app_desc[0] vty = obscvty.VTYInteract(end, "127.0.0.1", port) ret = run_test(vty) except IOError as se: print("Failed to verify %s" % ' '.join(cmd), file=sys.stderr) print("Current directory: %s" % os.getcwd(), file=sys.stderr) print("Error was %s" % se, file=sys.stderr) print("Config was\n%s" % open(config).read(), file=sys.stderr) raise se finally: if proc: osmoutil.end_proc(proc) if vty: vty._close_socket() return ret def copy_config(dirname, config): if os.path.exists(dirname): shutil.rmtree(dirname) os.mkdir(dirname) prefix = os.path.basename(config) tmpfile = tempfile.NamedTemporaryFile( dir=dirname, prefix=prefix, delete=False) tmpfile.write(open(config).read().encode()) tmpfile.close() # This works around the precautions NamedTemporaryFile is made for... return tmpfile.name def write_config(vty): new_config = vty.enabled_command("write") if not new_config.startswith("Configuration saved to "): print(new_config) return 1, [new_config] return 0 # The only purpose of this function is to verify a working vty def token_vty_command(vty): vty.command("help") return 0 # This may warn about the same doc missing multiple times, by design def verify_doc(vty): xml = vty.command("show online-help") split_at = "