Example #1
0
 def wait_for_match(self, images, similar_degree=0.98, timeout=300):
     """
     Compare VM screenshot with given images, if any image in the list
     matched, then return the image index, or return -1.
     """
     end_time = time.time() + timeout
     image_matched = False
     cropped_image = os.path.join(data_dir.get_tmp_dir(), "croped.ppm")
     while time.time() < end_time:
         vm_screenshot = self.get_screenshot()
         ppm_utils.image_crop_save(vm_screenshot, vm_screenshot)
         img_index = 0
         for image in images:
             logging.debug("Compare vm screenshot with image %s", image)
             ppm_utils.image_crop_save(image, cropped_image)
             h_degree = ppm_utils.image_histogram_compare(cropped_image, vm_screenshot)
             if h_degree >= similar_degree:
                 logging.debug("Image %s matched", image)
                 image_matched = True
                 break
             img_index += 1
         if image_matched:
             break
         time.sleep(1)
     if os.path.exists(cropped_image):
         os.unlink(cropped_image)
     if os.path.exists(vm_screenshot):
         os.unlink(vm_screenshot)
     if image_matched:
         return img_index
     else:
         return -1
Example #2
0
    def execute(self):
        super(GitRepoParamHelper, self).execute()

        cwd = os.path.curdir
        os.chdir(self.destination_dir)
        utils.system('git remote add origin %s' % self.uri, ignore_status=True)
        if self.recursive == 'yes':
            utils.system('git submodule init')
            utils.system('git submodule update')

        if self.tag:
            utils.system('git checkout %s' % self.tag)
            if self.key_file is not None:
                try:
                    gnupg_home = os.path.join(data_dir.get_tmp_dir(),
                                              'gnupg')
                    if not os.path.isdir(gnupg_home):
                        os.makedirs(gnupg_home)
                    os.environ['GNUPGHOME'] = gnupg_home
                    utils.system('gpg --import %s' % self.key_file)
                    logging.debug('Verifying if tag is actually signed with '
                                  'GPG key ID %s' % self.key_file)
                    utils.system('git tag -v %s' % self.tag)
                except error.CmdError:
                    raise error.TestError("GPG signature check for git repo "
                                          "%s failed" % self.name)

        # Log the top commit message, good for quick reference
        utils.system('git log -1')

        os.chdir(cwd)
 def test_dir_pool(self):
     # Used for auto cleanup
     tmp_dir = data_dir.get_tmp_dir()
     self.pool_name = "AUTOTEST_POOLTEST"
     self.assertTrue(self.sp.define_dir_pool(self.pool_name, tmp_dir))
     self.assertTrue(self.sp.build_pool(self.pool_name))
     self.assertTrue(self.sp.start_pool(self.pool_name))
     self.assertTrue(self.sp.set_pool_autostart(self.pool_name))
     self.assertTrue(self.sp.delete_pool(self.pool_name))
 def test_dir_pool(self):
     # Used for auto cleanup
     tmp_dir = data_dir.get_tmp_dir()
     self.pool_name = "AUTOTEST_POOLTEST"
     self.assertTrue(self.sp.define_dir_pool(self.pool_name, tmp_dir))
     self.assertTrue(self.sp.build_pool(self.pool_name))
     self.assertTrue(self.sp.start_pool(self.pool_name))
     self.assertTrue(self.sp.set_pool_autostart(self.pool_name))
     self.assertTrue(self.sp.delete_pool(self.pool_name))
Example #5
0
    def __init__(
        self,
        address,
        client,
        username,
        password,
        port,
        remote_path,
        limit="",
        log_filename=None,
        verbose=False,
        timeout=600,
    ):
        """
        Initialization of RemoteFile class.

        :param address: Address of remote host(guest)
        :param client: Type of transfer client
        :param username: Username (if required)
        :param password: Password (if requried)
        :param remote_path: Path of file which we want to edit on remote.
        :param limit: Speed limit of file transfer.
        :param log_filename: If specified, log all output to this file(SCP only)
        :param verbose: If True, log some stats using logging.debug (RSS only)
        :param timeout: The time duration (in seconds) to wait for the
                        transfer tocomplete.
        """
        self.address = address
        self.client = client
        self.username = username
        self.password = password
        self.port = port
        self.remote_path = remote_path
        self.limit = limit
        self.log_filename = log_filename
        self.verbose = verbose
        self.timeout = timeout

        # Get a local_path and all actions is taken on it.
        filename = os.path.basename(self.remote_path)

        # Get a local_path.
        tmp_dir = data_dir.get_tmp_dir()
        local_file = tempfile.NamedTemporaryFile(prefix=("%s_" % filename), dir=tmp_dir)
        self.local_path = local_file.name
        local_file.close()

        # Get a backup_path.
        backup_file = tempfile.NamedTemporaryFile(prefix=("%s_" % filename), dir=tmp_dir)
        self.backup_path = backup_file.name
        backup_file.close()

        # Get file from remote.
        self._pull_file()
        # Save a backup.
        shutil.copy(self.local_path, self.backup_path)
Example #6
0
    def __init__(self,
                 address,
                 client,
                 username,
                 password,
                 port,
                 remote_path,
                 limit="",
                 log_filename=None,
                 verbose=False,
                 timeout=600):
        """
        Initialization of RemoteFile class.

        :param address: Address of remote host(guest)
        :param client: Type of transfer client
        :param username: Username (if required)
        :param password: Password (if requried)
        :param remote_path: Path of file which we want to edit on remote.
        :param limit: Speed limit of file transfer.
        :param log_filename: If specified, log all output to this file(SCP only)
        :param verbose: If True, log some stats using logging.debug (RSS only)
        :param timeout: The time duration (in seconds) to wait for the
                        transfer tocomplete.
        """
        self.address = address
        self.client = client
        self.username = username
        self.password = password
        self.port = port
        self.remote_path = remote_path
        self.limit = limit
        self.log_filename = log_filename
        self.verbose = verbose
        self.timeout = timeout

        # Get a local_path and all actions is taken on it.
        filename = os.path.basename(self.remote_path)

        # Get a local_path.
        tmp_dir = data_dir.get_tmp_dir()
        local_file = tempfile.NamedTemporaryFile(prefix=("%s_" % filename),
                                                 dir=tmp_dir)
        self.local_path = local_file.name
        local_file.close()

        # Get a backup_path.
        backup_file = tempfile.NamedTemporaryFile(prefix=("%s_" % filename),
                                                  dir=tmp_dir)
        self.backup_path = backup_file.name
        backup_file.close()

        # Get file from remote.
        self._pull_file()
        # Save a backup.
        shutil.copy(self.local_path, self.backup_path)
Example #7
0
def passfd_setup(output_dir=OUTPUT_DIR):
    '''
    Compiles the passfd python extension.

    :param output_dir: where the _passfd.so module will be saved
    :return: None
    '''
    if output_dir is None:
        output_dir = OUTPUT_DIR

    output_file = os.path.join(output_dir, SHARED_OBJECT)

    c = distutils.ccompiler.new_compiler()
    objects = c.compile(SOURCES, include_dirs=[PYTHON_HEADERS],
                        output_dir=data_dir.get_tmp_dir(),
                        extra_postargs=['-fPIC'])
    c.link_shared_object(objects, output_file, libraries=[PYTHON_LIB])
Example #8
0
def passfd_setup(output_dir=OUTPUT_DIR):
    '''
    Compiles the passfd python extension.

    :param output_dir: where the _passfd.so module will be saved
    :return: None
    '''
    if output_dir is None:
        output_dir = OUTPUT_DIR

    output_file = os.path.join(output_dir, SHARED_OBJECT)

    c = distutils.ccompiler.new_compiler()
    distutils.sysconfig.customize_compiler(c)
    objects = c.compile(SOURCES, include_dirs=[PYTHON_HEADERS],
                        output_dir=data_dir.get_tmp_dir(),
                        extra_postargs=['-fPIC'])
    c.link_shared_object(objects, output_file, libraries=[PYTHON_LIB])
Example #9
0
 def get_screenshot(self):
     """
     Do virsh screenshot of the vm and fetch the image if the VM in
     remote host.
     """
     sshot_file = os.path.join(data_dir.get_tmp_dir(), "vm_screenshot.ppm")
     if self.target == "ovirt":
         vm_sshot = "/tmp/vm_screenshot.ppm"
     else:
         vm_sshot = sshot_file
     virsh.screenshot(self.name, vm_sshot, session_id=self.virsh_session_id)
     if self.target == "ovirt":
         remote_ip = self.params.get("remote_ip")
         remote_user = self.params.get("remote_user")
         remote_pwd = self.params.get("remote_pwd")
         remote.scp_from_remote(remote_ip, "22", remote_user, remote_pwd, vm_sshot, sshot_file)
         r_runner = remote.RemoteRunner(host=remote_ip, username=remote_user, password=remote_pwd)
         r_runner.run("rm -f %s" % vm_sshot)
     return sshot_file
Example #10
0
    def test_CA(self):
        tmp_dir = tempfile.mkdtemp(dir=data_dir.get_tmp_dir())

        cakey_path = '%s/tcakey.pem' % tmp_dir
        cainfo_path = '%s/ca.info' % tmp_dir
        cacert_path = '%s/cacert.pem' % tmp_dir

        utils_conn.build_CA(tmp_dir)
        self.assertTrue(os.path.exists(cakey_path))
        self.assertTrue(os.path.exists(cainfo_path))
        self.assertTrue(os.path.exists(cacert_path))

        expect_info = ["cn = AUTOTEST.VIRT\n",
                       "ca\n",
                       "cert_signing_key\n"]
        info_file = open(cainfo_path)
        lines = info_file.readlines()
        self.assertEqual(expect_info, lines)
        shutil.rmtree(tmp_dir)
Example #11
0
    def test_CA(self):
        tmp_dir = tempfile.mkdtemp(dir=data_dir.get_tmp_dir())

        cakey_path = '%s/tcakey.pem' % tmp_dir
        cainfo_path = '%s/ca.info' % tmp_dir
        cacert_path = '%s/cacert.pem' % tmp_dir

        utils_conn.build_CA(tmp_dir)
        self.assertTrue(os.path.exists(cakey_path))
        self.assertTrue(os.path.exists(cainfo_path))
        self.assertTrue(os.path.exists(cacert_path))

        expect_info = ["cn = AUTOTEST.VIRT\n",
                       "ca\n",
                       "cert_signing_key\n"]
        info_file = open(cainfo_path)
        lines = info_file.readlines()
        self.assertEqual(expect_info, lines)
        shutil.rmtree(tmp_dir)
Example #12
0
    def test_server_key(self):
        tmp_dir = tempfile.mkdtemp(dir=data_dir.get_tmp_dir())

        utils_conn.build_CA(tmp_dir)

        serverkey_path = '%s/serverkey.pem' % tmp_dir
        servercert_path = '%s/servercert.pem' % tmp_dir
        serverinfo_path = '%s/server.info' % tmp_dir

        utils_conn.build_server_key(tmp_dir)
        self.assertTrue(os.path.exists(serverkey_path))
        self.assertTrue(os.path.exists(servercert_path))
        self.assertTrue(os.path.exists(serverinfo_path))

        expect_info = ["organization = AUTOTEST.VIRT\n",
                       "cn = TLSServer\n",
                       "tls_www_server\n",
                       "encryption_key\n",
                       "signing_key\n"]
        info_file = open(serverinfo_path)
        lines = info_file.readlines()
        self.assertEqual(expect_info, lines)

        shutil.rmtree(tmp_dir)
Example #13
0
    def test_server_key(self):
        tmp_dir = tempfile.mkdtemp(dir=data_dir.get_tmp_dir())

        utils_conn.build_CA(tmp_dir)

        serverkey_path = '%s/serverkey.pem' % tmp_dir
        servercert_path = '%s/servercert.pem' % tmp_dir
        serverinfo_path = '%s/server.info' % tmp_dir

        utils_conn.build_server_key(tmp_dir)
        self.assertTrue(os.path.exists(serverkey_path))
        self.assertTrue(os.path.exists(servercert_path))
        self.assertTrue(os.path.exists(serverinfo_path))

        expect_info = ["organization = AUTOTEST.VIRT\n",
                       "cn = TLSServer\n",
                       "tls_www_server\n",
                       "encryption_key\n",
                       "signing_key\n"]
        info_file = open(serverinfo_path)
        lines = info_file.readlines()
        self.assertEqual(expect_info, lines)

        shutil.rmtree(tmp_dir)
Example #14
0
def bootstrap(test_name,
              test_dir,
              base_dir,
              default_userspace_paths,
              check_modules,
              online_docs_url,
              restore_image=False,
              download_image=True,
              interactive=True,
              selinux=False,
              verbose=False):
    """
    Common virt test assistant module.

    :param test_name: Test name, such as "qemu".
    :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 a
            wiki page.
    :param restore_image: Whether to restore the image from the pristine.
    :param interactive: Whether to ask for confirmation.
    :param verbose: Verbose output.
    :param selinux: Whether setup SELinux contexts for shared/data

    :raise error.CmdError: If JeOS image failed to uncompress
    :raise ValueError: If 7za was not found
    """
    if interactive:
        logging_manager.configure_logging(utils_misc.VirtLoggingConfig(),
                                          verbose=verbose)
    logging.info("%s test config helper", test_name)
    step = 0

    logging.info("")
    step += 1
    logging.info("%d - Checking the mandatory programs and headers", step)
    verify_mandatory_programs(test_name)

    logging.info("")
    step += 1
    logging.info("%d - Checking the recommended programs", step)
    verify_recommended_programs(test_name)

    logging.info("")
    step += 1
    logging.info("%d - Verifying directories", step)
    shared_dir = os.path.dirname(data_dir.get_data_dir())
    sub_dir_list = ["images", "isos", "steps_data", "gpg"]
    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)

    datadir = data_dir.get_data_dir()
    if test_name == 'libvirt':
        create_config_files(test_dir, shared_dir, interactive, step)
        create_subtests_cfg(test_name)
        create_guest_os_cfg(test_name)
        # Don't bother checking if changes can't be made
        if os.getuid() == 0:
            verify_selinux(datadir, os.path.join(datadir, 'images'),
                           os.path.join(datadir, 'isos'),
                           data_dir.get_tmp_dir(), interactive, selinux)

    # lvsb test doesn't use any shared configs
    elif test_name == 'lvsb':
        create_subtests_cfg(test_name)
        if os.getuid() == 0:
            # Don't bother checking if changes can't be made
            verify_selinux(datadir, os.path.join(datadir, 'images'),
                           os.path.join(datadir, 'isos'),
                           data_dir.get_tmp_dir(), interactive, selinux)
    else:  # Some other test
        create_config_files(test_dir, shared_dir, interactive, step)
        create_subtests_cfg(test_name)
        create_guest_os_cfg(test_name)

    if download_image or restore_image:
        logging.info("")
        step += 2
        logging.info("%s - Verifying (and possibly downloading) guest image",
                     step)
        asset.download_asset('jeos-19-64',
                             interactive=interactive,
                             restore_image=restore_image)

    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 - If you wish, take a look at the online docs for "
            "more info", step)
        logging.info("")
        logging.info(online_docs_url)
Example #15
0
def create_subtests_cfg(t_type):
    root_dir = data_dir.get_root_dir()

    specific_test_list = []
    specific_file_list = []
    specific_subdirs = asset.get_test_provider_subdirs(t_type)
    provider_names_specific = asset.get_test_provider_names(t_type)

    provider_info_specific = []
    for specific_provider in provider_names_specific:
        provider_info_specific.append(asset.get_test_provider_info(specific_provider))

    for subdir in specific_subdirs:
        specific_test_list += data_dir.SubdirGlobList(subdir,
                                                      '*.py',
                                                      test_filter)
        specific_file_list += data_dir.SubdirGlobList(subdir,
                                                      '*.cfg',
                                                      config_filter)

    shared_test_list = []
    shared_file_list = []
    shared_subdirs = asset.get_test_provider_subdirs('generic')
    provider_names_shared = asset.get_test_provider_names('generic')

    provider_info_shared = []
    for shared_provider in provider_names_shared:
        provider_info_shared.append(asset.get_test_provider_info(shared_provider))

    if not t_type == 'lvsb':
        for subdir in shared_subdirs:
            shared_test_list += data_dir.SubdirGlobList(subdir,
                                                        '*.py',
                                                        test_filter)
            shared_file_list += data_dir.SubdirGlobList(subdir,
                                                        '*.cfg',
                                                        config_filter)

    all_specific_test_list = []
    for test in specific_test_list:
        for p in provider_info_specific:
            provider_base_path = p['backends'][t_type]['path']
            if provider_base_path in test:
                provider_name = p['name']
                break

        basename = os.path.basename(test)
        if basename != "__init__.py":
            all_specific_test_list.append("%s.%s" %
                                          (provider_name,
                                           basename.split(".")[0]))
    all_shared_test_list = []
    for test in shared_test_list:
        for p in provider_info_shared:
            provider_base_path = p['backends']['generic']['path']
            if provider_base_path in test:
                provider_name = p['name']
                break

        basename = os.path.basename(test)
        if basename != "__init__.py":
            all_shared_test_list.append("%s.%s" %
                                        (provider_name,
                                         basename.split(".")[0]))

    all_specific_test_list.sort()
    all_shared_test_list.sort()
    all_test_list = set(all_specific_test_list + all_shared_test_list)

    first_subtest_file = []
    last_subtest_file = []
    non_dropin_tests = []
    tmp = []

    for shared_file in shared_file_list:
        provider_name = None
        for p in provider_info_shared:
            provider_base_path = p['backends']['generic']['path']
            if provider_base_path in shared_file:
                provider_name = p['name']
                break

        shared_file_obj = open(shared_file, 'r')
        for line in shared_file_obj.readlines():
            line = line.strip()
            if line.startswith("type"):
                cartesian_parser = cartesian_config.Parser()
                cartesian_parser.parse_string(line)
                td = cartesian_parser.get_dicts().next()
                values = td['type'].split(" ")
                for value in values:
                    if t_type not in non_dropin_tests:
                        non_dropin_tests.append("%s.%s" %
                                                (provider_name, value))

        shared_file_name = os.path.basename(shared_file)
        shared_file_name = shared_file_name.split(".")[0]
        if shared_file_name in first_subtest[t_type]:
            if [provider_name, shared_file] not in first_subtest_file:
                first_subtest_file.append([provider_name, shared_file])
        elif shared_file_name in last_subtest[t_type]:
            if [provider_name, shared_file] not in last_subtest_file:
                last_subtest_file.append([provider_name, shared_file])
        else:
            if [provider_name, shared_file] not in tmp:
                tmp.append([provider_name, shared_file])
    shared_file_list = tmp

    tmp = []
    for shared_file in specific_file_list:
        provider_name = None
        for p in provider_info_specific:
            provider_base_path = p['backends'][t_type]['path']
            if provider_base_path in shared_file:
                provider_name = p['name']
                break

        shared_file_obj = open(shared_file, 'r')
        for line in shared_file_obj.readlines():
            line = line.strip()
            if line.startswith("type"):
                cartesian_parser = cartesian_config.Parser()
                cartesian_parser.parse_string(line)
                td = cartesian_parser.get_dicts().next()
                values = td['type'].split(" ")
                for value in values:
                    if value not in non_dropin_tests:
                        non_dropin_tests.append("%s.%s" %
                                                (provider_name, value))

        shared_file_name = os.path.basename(shared_file)
        shared_file_name = shared_file_name.split(".")[0]
        if shared_file_name in first_subtest[t_type]:
            if [provider_name, shared_file] not in first_subtest_file:
                first_subtest_file.append([provider_name, shared_file])
        elif shared_file_name in last_subtest[t_type]:
            if [provider_name, shared_file] not in last_subtest_file:
                last_subtest_file.append([provider_name, shared_file])
        else:
            if [provider_name, shared_file] not in tmp:
                tmp.append([provider_name, shared_file])
    specific_file_list = tmp

    non_dropin_tests.sort()
    non_dropin_tests = set(non_dropin_tests)
    dropin_tests = all_test_list - non_dropin_tests
    dropin_file_list = []
    tmp_dir = data_dir.get_tmp_dir()
    if not os.path.isdir(tmp_dir):
        os.makedirs(tmp_dir)

    for dropin_test in dropin_tests:
        provider = dropin_test.split(".")[0]
        d_type = dropin_test.split(".")[-1]
        autogen_cfg_path = os.path.join(tmp_dir,
                                        '%s.cfg' % dropin_test)
        autogen_cfg_file = open(autogen_cfg_path, 'w')
        autogen_cfg_file.write("# Drop-in test - auto generated snippet\n")
        autogen_cfg_file.write("- %s:\n" % dropin_test)
        autogen_cfg_file.write("    virt_test_type = %s\n" % t_type)
        autogen_cfg_file.write("    type = %s\n" % d_type)
        autogen_cfg_file.close()
        dropin_file_list.append([provider, autogen_cfg_path])

    dropin_file_list_2 = []
    dropin_tests = os.listdir(os.path.join(data_dir.get_root_dir(), "dropin"))
    dropin_cfg_path = os.path.join(tmp_dir, 'dropin.cfg')
    dropin_cfg_file = open(dropin_cfg_path, 'w')
    dropin_cfg_file.write("# Auto generated snippet for dropin tests\n")
    dropin_cfg_file.write("- dropin:\n")
    dropin_cfg_file.write("    variants:\n")
    for dropin_test in dropin_tests:
        if dropin_test == "README":
            continue
        dropin_cfg_file.write("        - %s:\n" % dropin_test)
        dropin_cfg_file.write("            virt_test_type = %s\n" % t_type)
        dropin_cfg_file.write("            type = dropin\n")
        dropin_cfg_file.write("            start_vm = no\n")
        dropin_cfg_file.write("            dropin_path = %s\n" % dropin_test)
    dropin_cfg_file.close()
    dropin_file_list_2.append(['io-github-autotest-qemu', dropin_cfg_path])

    subtests_cfg = os.path.join(root_dir, 'backends', t_type, 'cfg',
                                'subtests.cfg')
    subtests_file = open(subtests_cfg, 'w')
    subtests_file.write(
        "# Do not edit, auto generated file from subtests config\n")

    subtests_file.write("variants subtest:\n")
    write_subtests_files(first_subtest_file, subtests_file)
    write_subtests_files(specific_file_list, subtests_file, t_type)
    write_subtests_files(shared_file_list, subtests_file)
    write_subtests_files(dropin_file_list, subtests_file)
    write_subtests_files(dropin_file_list_2, subtests_file)
    write_subtests_files(last_subtest_file, subtests_file)

    subtests_file.close()
Example #16
0
        else:
            logging.debug("Dir %s exists, not creating",
                          sub_dir_path)

    test_dir = data_dir.get_backend_dir(options.vt_type)
    if options.vt_type == 'libvirt':
        step = create_config_files(test_dir, shared_dir, interactive, step,
                                   force_update=options.vt_update_config)
        create_subtests_cfg(options.vt_type)
        create_guest_os_cfg(options.vt_type)
        # Don't bother checking if changes can't be made
        if os.getuid() == 0:
            verify_selinux(datadir,
                           os.path.join(datadir, 'images'),
                           os.path.join(datadir, 'isos'),
                           data_dir.get_tmp_dir(),
                           interactive, options.vt_selinux_setup)

    # lvsb test doesn't use any shared configs
    elif options.vt_type == 'lvsb':
        create_subtests_cfg(options.vt_type)
        if os.getuid() == 0:
            # Don't bother checking if changes can't be made
            verify_selinux(datadir,
                           os.path.join(datadir, 'images'),
                           os.path.join(datadir, 'isos'),
                           data_dir.get_tmp_dir(),
                           interactive, options.vt_selinux_setup)
    else:  # Some other test
        step = create_config_files(test_dir, shared_dir, interactive, step,
                                   force_update=options.vt_update_config)
Example #17
0
def bootstrap(test_name, test_dir, base_dir, default_userspace_paths,
              check_modules, online_docs_url, restore_image=False,
              download_image=True, interactive=True, selinux=False,
              verbose=False, update_providers=False,
              guest_os=defaults.DEFAULT_GUEST_OS):
    """
    Common virt test assistant module.

    :param test_name: Test name, such as "qemu".
    :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 a
            wiki page.
    :param restore_image: Whether to restore the image from the pristine.
    :param interactive: Whether to ask for confirmation.
    :param verbose: Verbose output.
    :param selinux: Whether setup SELinux contexts for shared/data.
    :param update_providers: Whether to update test providers if they are already
            downloaded.
    :param guest_os: Specify the guest image used for bootstrapping. By default
            the JeOS image is used.

    :raise error.CmdError: If JeOS image failed to uncompress
    :raise ValueError: If 7za was not found
    """
    if interactive:
        logging_manager.configure_logging(utils_misc.VirtLoggingConfig(),
                                          verbose=verbose)
    logging.info("%s test config helper", test_name)
    step = 0

    logging.info("")
    step += 1
    logging.info("%d - Updating all test providers", step)
    asset.download_all_test_providers(update_providers)

    logging.info("")
    step += 1
    logging.info("%d - Checking the mandatory programs and headers", step)
    verify_mandatory_programs(test_name)

    logging.info("")
    step += 1
    logging.info("%d - Checking the recommended programs", step)
    verify_recommended_programs(test_name)

    logging.info("")
    step += 1
    logging.info("%d - Verifying directories", step)
    shared_dir = os.path.dirname(data_dir.get_data_dir())
    sub_dir_list = ["images", "isos", "steps_data", "gpg"]
    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)

    datadir = data_dir.get_data_dir()
    if test_name == 'libvirt':
        create_config_files(test_dir, shared_dir, interactive, step)
        create_subtests_cfg(test_name)
        create_guest_os_cfg(test_name)
        # Don't bother checking if changes can't be made
        if os.getuid() == 0:
            verify_selinux(datadir,
                           os.path.join(datadir, 'images'),
                           os.path.join(datadir, 'isos'),
                           data_dir.get_tmp_dir(),
                           interactive, selinux)

    # lvsb test doesn't use any shared configs
    elif test_name == 'lvsb':
        create_subtests_cfg(test_name)
        if os.getuid() == 0:
            # Don't bother checking if changes can't be made
            verify_selinux(datadir,
                           os.path.join(datadir, 'images'),
                           os.path.join(datadir, 'isos'),
                           data_dir.get_tmp_dir(),
                           interactive, selinux)
    else:  # Some other test
        create_config_files(test_dir, shared_dir, interactive, step)
        create_subtests_cfg(test_name)
        create_guest_os_cfg(test_name)

    if download_image or restore_image:
        logging.info("")
        step += 2
        logging.info("%s - Verifying (and possibly downloading) guest image",
                     step)
        for os_info in get_guest_os_info_list(test_name, guest_os):
            os_asset = os_info['asset']
            asset.download_asset(os_asset, interactive=interactive,
                                 restore_image=restore_image)

    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 - If you wish, take a look at the online docs for "
                     "more info", step)
        logging.info("")
        logging.info(online_docs_url)
Example #18
0
            logging.debug("Dir %s exists, not creating", sub_dir_path)

    test_dir = data_dir.get_backend_dir(options.vt_type)
    if options.vt_type == 'libvirt':
        step = create_config_files(test_dir,
                                   shared_dir,
                                   interactive,
                                   step,
                                   force_update=options.vt_update_config)
        create_subtests_cfg(options.vt_type)
        create_guest_os_cfg(options.vt_type)
        # Don't bother checking if changes can't be made
        if os.getuid() == 0:
            verify_selinux(datadir, os.path.join(datadir, 'images'),
                           os.path.join(datadir,
                                        'isos'), data_dir.get_tmp_dir(),
                           interactive, options.vt_selinux_setup)

    # lvsb test doesn't use any shared configs
    elif options.vt_type == 'lvsb':
        create_subtests_cfg(options.vt_type)
        if os.getuid() == 0:
            # Don't bother checking if changes can't be made
            verify_selinux(datadir, os.path.join(datadir, 'images'),
                           os.path.join(datadir,
                                        'isos'), data_dir.get_tmp_dir(),
                           interactive, options.vt_selinux_setup)
    else:  # Some other test
        step = create_config_files(test_dir,
                                   shared_dir,
                                   interactive,
Example #19
0
def create_subtests_cfg(t_type):
    root_dir = data_dir.get_root_dir()

    specific_test = os.path.join(root_dir, t_type, 'tests')
    specific_test_list = glob.glob(os.path.join(specific_test, '*.py'))
    shared_test = os.path.join(root_dir, 'tests')
    shared_test_list = glob.glob(os.path.join(shared_test, '*.py'))
    all_specific_test_list = []
    for test in specific_test_list:
        basename = os.path.basename(test)
        if basename != "__init__.py":
            all_specific_test_list.append(basename.split(".")[0])
    all_shared_test_list = []
    for test in shared_test_list:
        basename = os.path.basename(test)
        if basename != "__init__.py":
            all_shared_test_list.append(basename.split(".")[0])

    all_specific_test_list.sort()
    all_shared_test_list.sort()
    all_test_list = set(all_specific_test_list + all_shared_test_list)

    specific_test_cfg = os.path.join(root_dir, t_type,
                                   'tests', 'cfg')
    shared_test_cfg = os.path.join(root_dir, 'tests', 'cfg')

    shared_file_list = glob.glob(os.path.join(shared_test_cfg, "*.cfg"))
    first_subtest_file = []
    last_subtest_file = []
    non_dropin_tests = []
    tmp = []
    for shared_file in shared_file_list:
        shared_file_obj = open(shared_file, 'r')
        for line in shared_file_obj.readlines():
            line = line.strip()
            if not line.startswith("#"):
                try:
                    (key, value) = line.split("=")
                    if key.strip() == 'type':
                        value = value.strip()
                        value = value.split(" ")
                        for v in value:
                            if v not in non_dropin_tests:
                                non_dropin_tests.append(v)
                except:
                    pass
        shared_file_name = os.path.basename(shared_file)
        shared_file_name = shared_file_name.split(".")[0]
        if shared_file_name in first_subtest[t_type]:
            if shared_file_name not in first_subtest_file:
                first_subtest_file.append(shared_file)
        elif shared_file_name in last_subtest[t_type]:
            if shared_file_name not in last_subtest_file:
                last_subtest_file.append(shared_file)
        else:
            if shared_file_name not in tmp:
                tmp.append(shared_file)
    shared_file_list = tmp
    shared_file_list.sort()

    specific_file_list = glob.glob(os.path.join(specific_test_cfg, "*.cfg"))
    tmp = []
    for shared_file in specific_file_list:
        shared_file_obj = open(shared_file, 'r')
        for line in shared_file_obj.readlines():
            line = line.strip()
            if not line.startswith("#"):
                try:
                    (key, value) = line.split("=")
                    if key.strip() == 'type':
                        value = value.strip()
                        value = value.split(" ")
                        for v in value:
                            if v not in non_dropin_tests:
                                non_dropin_tests.append(v)
                except:
                    pass
        shared_file_name = os.path.basename(shared_file)
        shared_file_name = shared_file_name.split(".")[0]
        if shared_file_name in first_subtest[t_type]:
            if shared_file_name not in first_subtest_file:
                first_subtest_file.append(shared_file)
        elif shared_file_name in last_subtest[t_type]:
            if shared_file_name not in last_subtest_file:
                last_subtest_file.append(shared_file)
        else:
            if shared_file_name not in tmp:
                tmp.append(shared_file)
    specific_file_list = tmp
    specific_file_list.sort()

    non_dropin_tests.sort()
    non_dropin_tests = set(non_dropin_tests)
    dropin_tests = all_test_list - non_dropin_tests
    dropin_file_list = []
    tmp_dir = data_dir.get_tmp_dir()
    if not os.path.isdir(tmp_dir):
        os.makedirs(tmp_dir)
    for dropin_test in dropin_tests:
        autogen_cfg_path = os.path.join(tmp_dir,
                                        '%s.cfg' % dropin_test)
        autogen_cfg_file = open(autogen_cfg_path, 'w')
        autogen_cfg_file.write("# Drop-in test - auto generated snippet\n")
        autogen_cfg_file.write("- %s:\n" % dropin_test)
        autogen_cfg_file.write("    virt_test_type = %s\n" % t_type)
        autogen_cfg_file.write("    type = %s\n" % dropin_test)
        autogen_cfg_file.close()
        dropin_file_list.append(autogen_cfg_path)

    subtests_cfg = os.path.join(root_dir, t_type, 'cfg', 'subtests.cfg')
    subtests_file = open(subtests_cfg, 'w')
    subtests_file.write("# Do not edit, auto generated file from subtests config\n")
    subtests_file.write("variants:\n")
    write_subtests_files(first_subtest_file, subtests_file)
    write_subtests_files(specific_file_list, subtests_file, t_type)
    write_subtests_files(shared_file_list, subtests_file)
    write_subtests_files(dropin_file_list, subtests_file)
    write_subtests_files(last_subtest_file, subtests_file)

    subtests_file.close()
Example #20
0
def create_subtests_cfg(t_type):
    root_dir = data_dir.get_root_dir()

    specific_test = os.path.join(root_dir, t_type, 'tests')
    specific_test_list = data_dir.SubdirGlobList(specific_test,
                                                 '*.py',
                                                 test_filter)
    shared_test = os.path.join(root_dir, 'tests')
    if t_type == 'lvsb':
        shared_test_list = []
    else:
        shared_test_list = data_dir.SubdirGlobList(shared_test,
                                                   '*.py',
                                                   test_filter)
    all_specific_test_list = []
    for test in specific_test_list:
        basename = os.path.basename(test)
        if basename != "__init__.py":
            all_specific_test_list.append(basename.split(".")[0])
    all_shared_test_list = []
    for test in shared_test_list:
        basename = os.path.basename(test)
        if basename != "__init__.py":
            all_shared_test_list.append(basename.split(".")[0])

    all_specific_test_list.sort()
    all_shared_test_list.sort()
    all_test_list = set(all_specific_test_list + all_shared_test_list)

    specific_test_cfg = os.path.join(root_dir, t_type,
                                     'tests', 'cfg')
    shared_test_cfg = os.path.join(root_dir, 'tests', 'cfg')

    # lvsb tests can't use VM shared tests
    if t_type == 'lvsb':
        shared_file_list = []
    else:
        shared_file_list = data_dir.SubdirGlobList(shared_test_cfg,
                                                   "*.cfg",
                                                   config_filter)
    first_subtest_file = []
    last_subtest_file = []
    non_dropin_tests = []
    tmp = []
    for shared_file in shared_file_list:
        shared_file_obj = open(shared_file, 'r')
        for line in shared_file_obj.readlines():
            line = line.strip()
            if line.startswith("type"):
                cartesian_parser = cartesian_config.Parser()
                cartesian_parser.parse_string(line)
                td = cartesian_parser.get_dicts().next()
                values = td['type'].split(" ")
                for value in values:
                    if t_type not in non_dropin_tests:
                        non_dropin_tests.append(value)

        shared_file_name = os.path.basename(shared_file)
        shared_file_name = shared_file_name.split(".")[0]
        if shared_file_name in first_subtest[t_type]:
            if shared_file_name not in first_subtest_file:
                first_subtest_file.append(shared_file)
        elif shared_file_name in last_subtest[t_type]:
            if shared_file_name not in last_subtest_file:
                last_subtest_file.append(shared_file)
        else:
            if shared_file_name not in tmp:
                tmp.append(shared_file)
    shared_file_list = tmp
    shared_file_list.sort()

    specific_file_list = data_dir.SubdirGlobList(specific_test_cfg,
                                                 "*.cfg",
                                                 config_filter)
    tmp = []
    for shared_file in specific_file_list:
        shared_file_obj = open(shared_file, 'r')
        for line in shared_file_obj.readlines():
            line = line.strip()
            if line.startswith("type"):
                cartesian_parser = cartesian_config.Parser()
                cartesian_parser.parse_string(line)
                td = cartesian_parser.get_dicts().next()
                values = td['type'].split(" ")
                for value in values:
                    if value not in non_dropin_tests:
                        non_dropin_tests.append(value)

        shared_file_name = os.path.basename(shared_file)
        shared_file_name = shared_file_name.split(".")[0]
        if shared_file_name in first_subtest[t_type]:
            if shared_file_name not in first_subtest_file:
                first_subtest_file.append(shared_file)
        elif shared_file_name in last_subtest[t_type]:
            if shared_file_name not in last_subtest_file:
                last_subtest_file.append(shared_file)
        else:
            if shared_file_name not in tmp:
                tmp.append(shared_file)
    specific_file_list = tmp
    specific_file_list.sort()

    non_dropin_tests.sort()
    non_dropin_tests = set(non_dropin_tests)
    dropin_tests = all_test_list - non_dropin_tests
    dropin_file_list = []
    tmp_dir = data_dir.get_tmp_dir()
    if not os.path.isdir(tmp_dir):
        os.makedirs(tmp_dir)
    for dropin_test in dropin_tests:
        autogen_cfg_path = os.path.join(tmp_dir,
                                        '%s.cfg' % dropin_test)
        autogen_cfg_file = open(autogen_cfg_path, 'w')
        autogen_cfg_file.write("# Drop-in test - auto generated snippet\n")
        autogen_cfg_file.write("- %s:\n" % dropin_test)
        autogen_cfg_file.write("    virt_test_type = %s\n" % t_type)
        autogen_cfg_file.write("    type = %s\n" % dropin_test)
        autogen_cfg_file.close()
        dropin_file_list.append(autogen_cfg_path)

    subtests_cfg = os.path.join(root_dir, t_type, 'cfg', 'subtests.cfg')
    subtests_file = open(subtests_cfg, 'w')
    subtests_file.write(
        "# Do not edit, auto generated file from subtests config\n")
    subtests_file.write("variants:\n")
    write_subtests_files(first_subtest_file, subtests_file)
    write_subtests_files(specific_file_list, subtests_file, t_type)
    write_subtests_files(shared_file_list, subtests_file)
    write_subtests_files(dropin_file_list, subtests_file)
    write_subtests_files(last_subtest_file, subtests_file)

    subtests_file.close()
Example #21
0
def create_subtests_cfg(t_type):
    root_dir = data_dir.get_root_dir()

    specific_test = os.path.join(root_dir, t_type, 'tests')
    specific_test_list = data_dir.SubdirGlobList(specific_test, '*.py',
                                                 test_filter)
    shared_test = os.path.join(root_dir, 'tests')
    shared_test_list = data_dir.SubdirGlobList(shared_test, '*.py',
                                               test_filter)
    all_specific_test_list = []
    for test in specific_test_list:
        basename = os.path.basename(test)
        if basename != "__init__.py":
            all_specific_test_list.append(basename.split(".")[0])
    all_shared_test_list = []
    for test in shared_test_list:
        basename = os.path.basename(test)
        if basename != "__init__.py":
            all_shared_test_list.append(basename.split(".")[0])

    all_specific_test_list.sort()
    all_shared_test_list.sort()
    all_test_list = set(all_specific_test_list + all_shared_test_list)

    specific_test_cfg = os.path.join(root_dir, t_type, 'tests', 'cfg')
    shared_test_cfg = os.path.join(root_dir, 'tests', 'cfg')

    shared_file_list = data_dir.SubdirGlobList(shared_test_cfg, "*.cfg",
                                               config_filter)
    first_subtest_file = []
    last_subtest_file = []
    non_dropin_tests = []
    tmp = []
    for shared_file in shared_file_list:
        shared_file_obj = open(shared_file, 'r')
        for line in shared_file_obj.readlines():
            line = line.strip()
            if line.startswith("type"):
                cartesian_parser = cartesian_config.Parser()
                cartesian_parser.parse_string(line)
                td = cartesian_parser.get_dicts().next()
                values = td['type'].split(" ")
                for value in values:
                    if t_type not in non_dropin_tests:
                        non_dropin_tests.append(value)

        shared_file_name = os.path.basename(shared_file)
        shared_file_name = shared_file_name.split(".")[0]
        if shared_file_name in first_subtest[t_type]:
            if shared_file_name not in first_subtest_file:
                first_subtest_file.append(shared_file)
        elif shared_file_name in last_subtest[t_type]:
            if shared_file_name not in last_subtest_file:
                last_subtest_file.append(shared_file)
        else:
            if shared_file_name not in tmp:
                tmp.append(shared_file)
    shared_file_list = tmp
    shared_file_list.sort()

    specific_file_list = data_dir.SubdirGlobList(specific_test_cfg, "*.cfg",
                                                 config_filter)
    tmp = []
    for shared_file in specific_file_list:
        shared_file_obj = open(shared_file, 'r')
        for line in shared_file_obj.readlines():
            line = line.strip()
            if line.startswith("type"):
                cartesian_parser = cartesian_config.Parser()
                cartesian_parser.parse_string(line)
                td = cartesian_parser.get_dicts().next()
                values = td['type'].split(" ")
                for value in values:
                    if value not in non_dropin_tests:
                        non_dropin_tests.append(value)

        shared_file_name = os.path.basename(shared_file)
        shared_file_name = shared_file_name.split(".")[0]
        if shared_file_name in first_subtest[t_type]:
            if shared_file_name not in first_subtest_file:
                first_subtest_file.append(shared_file)
        elif shared_file_name in last_subtest[t_type]:
            if shared_file_name not in last_subtest_file:
                last_subtest_file.append(shared_file)
        else:
            if shared_file_name not in tmp:
                tmp.append(shared_file)
    specific_file_list = tmp
    specific_file_list.sort()

    non_dropin_tests.sort()
    non_dropin_tests = set(non_dropin_tests)
    dropin_tests = all_test_list - non_dropin_tests
    dropin_file_list = []
    tmp_dir = data_dir.get_tmp_dir()
    if not os.path.isdir(tmp_dir):
        os.makedirs(tmp_dir)
    for dropin_test in dropin_tests:
        autogen_cfg_path = os.path.join(tmp_dir, '%s.cfg' % dropin_test)
        autogen_cfg_file = open(autogen_cfg_path, 'w')
        autogen_cfg_file.write("# Drop-in test - auto generated snippet\n")
        autogen_cfg_file.write("- %s:\n" % dropin_test)
        autogen_cfg_file.write("    virt_test_type = %s\n" % t_type)
        autogen_cfg_file.write("    type = %s\n" % dropin_test)
        autogen_cfg_file.close()
        dropin_file_list.append(autogen_cfg_path)

    subtests_cfg = os.path.join(root_dir, t_type, 'cfg', 'subtests.cfg')
    subtests_file = open(subtests_cfg, 'w')
    subtests_file.write(
        "# Do not edit, auto generated file from subtests config\n")
    subtests_file.write("variants:\n")
    write_subtests_files(first_subtest_file, subtests_file)
    write_subtests_files(specific_file_list, subtests_file, t_type)
    write_subtests_files(shared_file_list, subtests_file)
    write_subtests_files(dropin_file_list, subtests_file)
    write_subtests_files(last_subtest_file, subtests_file)

    subtests_file.close()
Example #22
0
class RemoteFileTest(unittest.TestCase):
    tmp_dir = data_dir.get_tmp_dir()
    test_file_path = os.path.join(tmp_dir, "remote_file")
    default_data = ["RemoteFile Test.\n", "Pattern Line."]

    def __del__(self):
        if os.path.exists(self.test_file_path):
            os.remove(self.test_file_path)

    def _new_remote_file(self):
        if os.path.exists(self.test_file_path):
            os.remove(self.test_file_path)
        test_file = open(self.test_file_path, "w")
        test_file.writelines(self.default_data)
        test_file.close()
        remote_file = remote.RemoteFile(None, "test", None, None, None,
                                        self.test_file_path)
        return remote_file

    def _read_test_file(self):
        test_file = open(self.test_file_path, "r")
        test_data = test_file.readlines()
        test_file.close()
        return test_data

    def testAdd(self):
        remote_file = self._new_remote_file()
        _add_list = ["add_line_1", "add_line_2", "add_line_3"]
        remote_file.add(_add_list)
        test_data = self._read_test_file()
        except_data = [
            "RemoteFile Test.\n", "Pattern Line.\n", "add_line_1\n",
            "add_line_2\n", "add_line_3"
        ]
        for index in range(len(except_data)):
            self.assertEqual(except_data[index], test_data[index])
        del remote_file
        test_data = self._read_test_file()
        self.assertEqual(test_data, self.default_data)

    def testSub(self):
        remote_file = self._new_remote_file()
        _pattern2repl = {r"Remote": "Local", r"^Pat.*$": "Replace Line"}
        remote_file.sub(_pattern2repl)
        test_data = self._read_test_file()
        except_data = ["LocalFile Test.\n", "Replace Line"]
        for index in range(len(except_data)):
            self.assertEqual(except_data[index], test_data[index])
        del remote_file
        test_data = self._read_test_file()
        self.assertEqual(test_data, self.default_data)

    def testRemove(self):
        remote_file = self._new_remote_file()
        _pattern_list = [r"^Pattern"]
        remote_file.remove(_pattern_list)
        test_data = self._read_test_file()
        except_data = ["RemoteFile Test."]
        for index in range(len(except_data)):
            self.assertEqual(except_data[index], test_data[index])
        del remote_file
        test_data = self._read_test_file()
        self.assertEqual(test_data, self.default_data)