#!/usr/bin/env python # (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 . from __future__ import print_function import os import os.path import time import sys, shutil, stat import tempfile import osmopy.obscvty as obscvty import osmopy.osmoutil as osmoutil # Return true iff all the tests for the given config pass def test_config(app_desc, config, tmpdir, verbose=True): try: err = 0 if test_config_atest(app_desc, config, verify_doc, verbose)[0] > 0: err += 1 newconfig = copy_config(tmpdir, config) if test_config_atest(app_desc, newconfig, write_config, verbose) > 0: err += 1 if test_config_atest(app_desc, newconfig, token_vty_command, verbose) > 0: err += 1 return err # If there's a socket error, skip the rest of the tests for this config except IOError: return 1 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): shutil.rmtree(dirname, True) ign = shutil.ignore_patterns('*.cfg') shutil.copytree(os.path.dirname(config), dirname, ignore=ign) os.chmod(dirname, stat.S_IRWXU) try: os.stat(dirname) except OSError: os.mkdir(dirname) prefix = os.path.basename(config) tmpfile = tempfile.NamedTemporaryFile( dir=dirname, prefix=prefix, delete=False) tmpfile.write(open(config).read()) 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 = "