As most of the get_started.py code could be very well shared among virt tests, moved the bulk of the code to virt_utils.py. Now we have the function virt_test_assistant(), that is generic enough so other tests can implement their variation of the assistant script. Signed-off-by: Lucas Meneghel Rodrigues <lmr@xxxxxxxxxx> --- client/tests/kvm/get_started.py | 162 +++---------------------------------- client/virt/virt_utils.py | 171 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 181 insertions(+), 152 deletions(-) diff --git a/client/tests/kvm/get_started.py b/client/tests/kvm/get_started.py index 4a40925..830c2a9 100755 --- a/client/tests/kvm/get_started.py +++ b/client/tests/kvm/get_started.py @@ -4,159 +4,19 @@ Program to help setup kvm test environment @copyright: Red Hat 2010 """ - -import os, sys, logging, shutil, glob +import os, sys import common -from autotest_lib.client.common_lib import logging_manager -from autotest_lib.client.bin import utils from autotest_lib.client.virt import virt_utils - -def check_iso(url, destination, hash): - """ - Verifies if ISO that can be find on url is on destination with right hash. - - This function will verify the SHA1 hash of the ISO image. If the file - turns out to be missing or corrupted, let the user know we can download it. - - @param url: URL where the ISO file can be found. - @param destination: Directory in local disk where we'd like the iso to be. - @param hash: SHA1 hash for the ISO image. - """ - file_ok = False - if not destination: - os.makedirs(destination) - iso_path = os.path.join(destination, os.path.basename(url)) - if not os.path.isfile(iso_path): - logging.warning("File %s not found", iso_path) - logging.warning("Expected SHA1 sum: %s", hash) - answer = utils.ask("Would you like to download it from %s?" % url) - if answer == 'y': - try: - utils.unmap_url_cache(destination, url, hash, method="sha1") - file_ok = True - except EnvironmentError, e: - logging.error(e) - else: - logging.warning("Missing file %s", iso_path) - logging.warning("Please download it or put an exsiting copy on the " - "appropriate location") - return - else: - logging.info("Found %s", iso_path) - logging.info("Expected SHA1 sum: %s", hash) - answer = utils.ask("Would you like to check %s? It might take a while" % - iso_path) - if answer == 'y': - try: - utils.unmap_url_cache(destination, url, hash, method="sha1") - file_ok = True - except EnvironmentError, e: - logging.error(e) - else: - logging.info("File %s present, but chose to not verify it", - iso_path) - return - - if file_ok: - logging.info("%s present, with proper checksum", iso_path) - +test_name = "kvm" +test_dir = os.path.dirname(sys.modules[__name__].__file__) +test_dir = os.path.abspath(test_dir) +base_dir = "/tmp/kvm_autotest_root" +default_userspace_paths = ["/usr/bin/qemu-kvm", "/usr/bin/qemu-img"] +check_modules = ["kvm", "kvm-%s" % virt_utils.get_cpu_vendor(verbose=False)] +online_docs_url = "https://github.com/autotest/autotest/wiki/KVMAutotest-GetStartedClient" if __name__ == "__main__": - logging_manager.configure_logging(virt_utils.VirtLoggingConfig(), - verbose=True) - logging.info("KVM test config helper") - - logging.info("") - logging.info("1 - Verifying directories (check if the directory structure " - "expected by the default test config is there)") - base_dir = "/tmp/kvm_autotest_root" - sub_dir_list = ["images", "isos", "steps_data"] - for sub_dir in sub_dir_list: - sub_dir_path = os.path.join(base_dir, sub_dir) - if not os.path.isdir(sub_dir_path): - logging.debug("Creating %s", sub_dir_path) - os.makedirs(sub_dir_path) - else: - logging.debug("Dir %s exists, not creating" % - sub_dir_path) - logging.info("") - logging.info("2 - Creating config files from samples (copy the default " - "config samples to actual config files)") - kvm_test_dir = os.path.dirname(sys.modules[__name__].__file__) - kvm_test_dir = os.path.abspath(kvm_test_dir) - config_file_list = glob.glob(os.path.join(kvm_test_dir, "*.cfg.sample")) - for config_file in config_file_list: - src_file = config_file - dst_file = config_file.rstrip(".sample") - if not os.path.isfile(dst_file): - logging.debug("Creating config file %s from sample", dst_file) - shutil.copyfile(src_file, dst_file) - else: - logging.debug("Config file %s exists, not touching" % dst_file) - - logging.info("") - logging.info("3 - Verifying iso (make sure we have the OS ISO needed for " - "the default test set)") - - iso_name = "Fedora-15-x86_64-DVD.iso" - fedora_dir = "pub/fedora/linux/releases/15/Fedora/x86_64/iso" - url = os.path.join("http://download.fedoraproject.org/", fedora_dir, - iso_name) - hash = "61b3407f62bac22d3a3b2e919c7fc960116012d7" - destination = os.path.join(base_dir, 'isos', 'linux') - path = os.path.join(destination, iso_name) - check_iso(url, destination, hash) - - logging.info("") - logging.info("4 - Verifying winutils.iso (make sure we have the utility " - "ISO needed for Windows testing)") - - logging.info("In order to run the KVM autotests in Windows guests, we " - "provide you an ISO that this script can download") - - url = "http://people.redhat.com/mrodrigu/kvm/winutils.iso" - hash = "02930224756510e383c44c49bffb760e35d6f892" - destination = os.path.join(base_dir, 'isos', 'windows') - path = os.path.join(destination, iso_name) - check_iso(url, destination, hash) - - logging.info("") - logging.info("5 - Checking if qemu is installed (certify qemu and qemu-kvm " - "are in the place the default config expects)") - qemu_default_paths = ['/usr/bin/qemu-kvm', '/usr/bin/qemu-img'] - for qemu_path in qemu_default_paths: - if not os.path.isfile(qemu_path): - logging.warning("No %s found. You might need to install qemu-kvm.", - qemu_path) - else: - logging.debug("%s present", qemu_path) - logging.info("If you wish to change qemu-kvm to qemu or other binary path, " - "you will have to modify tests.cfg") - - logging.info("") - logging.info("6 - Checking for the KVM module (make sure kvm is loaded " - "to accelerate qemu-kvm)") - if not utils.module_is_loaded("kvm"): - logging.warning("KVM module is not loaded. You might want to load it") - else: - logging.debug("KVM module loaded") - - logging.info("") - logging.info("7 - Verify needed packages to get started") - logging.info("Please take a look at the online documentation " - "http://www.linux-kvm.org/page/KVM-Autotest/Client_Install " - "(session 'Install Prerequisite packages')") - - client_dir = os.path.abspath(os.path.join(kvm_test_dir, "..", "..")) - autotest_bin = os.path.join(client_dir, 'bin', 'autotest') - control_file = os.path.join(kvm_test_dir, 'control') - - logging.info("") - logging.info("When you are done fixing eventual warnings found, " - "you can run the kvm test using the command line AS ROOT:") - logging.info("%s %s", autotest_bin, control_file) - logging.info("Autotest prints the results dir, so you can look at DEBUG " - "logs if something went wrong") - logging.info("You can also edit the test config files (see output of " - "step 2 for a list)") + virt_utils.virt_test_assistant(test_name, test_dir, base_dir, + default_userspace_paths, check_modules, + online_docs_url) diff --git a/client/virt/virt_utils.py b/client/virt/virt_utils.py index 962e2c3..b76e087 100644 --- a/client/virt/virt_utils.py +++ b/client/virt/virt_utils.py @@ -6,9 +6,10 @@ KVM test utility functions. import time, string, random, socket, os, signal, re, logging, commands, cPickle import fcntl, shelve, ConfigParser, threading, sys, UserDict, inspect, tarfile -import struct, shutil +import struct, shutil, glob from autotest_lib.client.bin import utils, os_dep from autotest_lib.client.common_lib import error, logging_config +from autotest_lib.client.common_lib import logging_manager import rss_client, aexpect try: import koji @@ -3287,3 +3288,171 @@ def if_set_macaddress(ifname, mac): logging.info(e) raise HwAddrSetError(ifname, mac) ctrl_sock.close() + + +def check_iso(url, destination, hash): + """ + Verifies if ISO that can be find on url is on destination with right hash. + + This function will verify the SHA1 hash of the ISO image. If the file + turns out to be missing or corrupted, let the user know we can download it. + + @param url: URL where the ISO file can be found. + @param destination: Directory in local disk where we'd like the iso to be. + @param hash: SHA1 hash for the ISO image. + """ + file_ok = False + if not destination: + os.makedirs(destination) + iso_path = os.path.join(destination, os.path.basename(url)) + if not os.path.isfile(iso_path): + logging.warning("File %s not found", iso_path) + logging.warning("Expected SHA1 sum: %s", hash) + answer = utils.ask("Would you like to download it from %s?" % url) + if answer == 'y': + try: + utils.unmap_url_cache(destination, url, hash, method="sha1") + file_ok = True + except EnvironmentError, e: + logging.error(e) + else: + logging.warning("Missing file %s", iso_path) + logging.warning("Please download it or put an exsiting copy on the " + "appropriate location") + return + else: + logging.info("Found %s", iso_path) + logging.info("Expected SHA1 sum: %s", hash) + answer = utils.ask("Would you like to check %s? It might take a while" % + iso_path) + if answer == 'y': + try: + utils.unmap_url_cache(destination, url, hash, method="sha1") + file_ok = True + except EnvironmentError, e: + logging.error(e) + else: + logging.info("File %s present, but chose to not verify it", + iso_path) + return + + if file_ok: + logging.info("%s present, with proper checksum", iso_path) + + +def virt_test_assistant(test_name, test_dir, base_dir, default_userspace_paths, + check_modules, online_docs_url): + """ + Common virt test assistant module. + + @param test_name: Test name, such as "kvm". + @param test_dir: Path with the test directory. + @param base_dir: Base directory used to hold images and isos. + @param default_userspace_paths: Important programs for a successful test + execution. + @param check_modules: Whether we want to verify if a given list of modules + is loaded in the system. + @param online_docs_url: URL to an online documentation system, such as an + wiki page. + """ + logging_manager.configure_logging(VirtLoggingConfig(), verbose=True) + logging.info("%s test config helper", test_name) + step = 0 + + logging.info("") + step += 1 + logging.info("%d - Verifying directories (check if the directory structure " + "expected by the default test config is there)", step) + sub_dir_list = ["images", "isos", "steps_data"] + for sub_dir in sub_dir_list: + sub_dir_path = os.path.join(base_dir, sub_dir) + if not os.path.isdir(sub_dir_path): + logging.debug("Creating %s", sub_dir_path) + os.makedirs(sub_dir_path) + else: + logging.debug("Dir %s exists, not creating" % + sub_dir_path) + logging.info("") + step += 1 + logging.info("%d - Creating config files from samples (copy the default " + "config samples to actual config files)", step) + config_file_list = glob.glob(os.path.join(test_dir, "*.cfg.sample")) + for config_file in config_file_list: + src_file = config_file + dst_file = config_file.rstrip(".sample") + if not os.path.isfile(dst_file): + logging.debug("Creating config file %s from sample", dst_file) + shutil.copyfile(src_file, dst_file) + else: + logging.debug("Config file %s exists, not touching" % dst_file) + + logging.info("") + step += 1 + logging.info("%s - Verifying iso (make sure we have the OS ISO needed for " + "the default test set)", step) + + iso_name = "Fedora-15-x86_64-DVD.iso" + fedora_dir = "pub/fedora/linux/releases/15/Fedora/x86_64/iso" + url = os.path.join("http://download.fedoraproject.org/", fedora_dir, + iso_name) + hash = "61b3407f62bac22d3a3b2e919c7fc960116012d7" + destination = os.path.join(base_dir, 'isos', 'linux') + check_iso(url, destination, hash) + + logging.info("") + step += 1 + logging.info("%d - Verifying winutils.iso (make sure we have the utility " + "ISO needed for Windows testing)", step) + + logging.info("In order to run the KVM autotests in Windows guests, we " + "provide you an ISO that this script can download") + + url = "http://people.redhat.com/mrodrigu/kvm/winutils.iso" + hash = "02930224756510e383c44c49bffb760e35d6f892" + destination = os.path.join(base_dir, 'isos', 'windows') + path = os.path.join(destination, iso_name) + check_iso(url, destination, hash) + + logging.info("") + step += 1 + logging.info("%d - Checking if the appropriate userspace programs are " + "installed", step) + for path in default_userspace_paths: + if not os.path.isfile(path): + logging.warning("No %s found. You might need to install %s.", + path, os.path.basename(path)) + else: + logging.debug("%s present", path) + logging.info("If you wish to change any userspace program path, " + "you will have to modify tests.cfg") + + if check_modules: + logging.info("") + step += 1 + logging.info("%d - Checking for modules %s", step, + ",".join(check_modules)) + for module in check_modules: + if not utils.module_is_loaded(module): + logging.warning("Module %s is not loaded. You might want to " + "load it", module) + else: + logging.debug("Module %s loaded", module) + + if online_docs_url: + logging.info("") + step += 1 + logging.info("%d - Verify needed packages to get started", step) + logging.info("Please take a look at the online documentation: %s", + online_docs_url) + + client_dir = os.path.abspath(os.path.join(test_dir, "..", "..")) + autotest_bin = os.path.join(client_dir, 'bin', 'autotest') + control_file = os.path.join(test_dir, 'control') + + logging.info("") + logging.info("When you are done fixing eventual warnings found, " + "you can run the test using this command line AS ROOT:") + logging.info("%s %s", autotest_bin, control_file) + logging.info("Autotest prints the results dir, so you can look at DEBUG " + "logs if something went wrong") + logging.info("You can also edit the test config files") -- 1.7.6.4 -- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html