예제 #1
0
    def worker(self, index, run_test_func):
        """
        The worker function.

        Waits for commands from the scheduler and processes them.

        @param index: The index of this worker (in the range 0..num_workers-1).
        @param run_test_func: A function to be called to run a test
                (e.g. job.run_test).
        """
        r = self.s2w_r[index]
        w = self.w2s_w[index]
        self_dict = self.worker_dicts[index]

        # Inform the scheduler this worker is ready
        w.write("ready\n")

        while True:
            cmd = r.readline().split()
            if not cmd:
                continue

            # The scheduler wants this worker to run a test
            if cmd[0] == "run":
                test_index = int(cmd[1])
                test = self.tests[test_index].copy()
                test.update(self_dict)
                test_iterations = int(test.get("iterations", 1))
                status = run_test_func("kvm", params=test,
                                       tag=test.get("shortname"),
                                       iterations=test_iterations)
                w.write("done %s %s\n" % (test_index, status))
                w.write("ready\n")

            # The scheduler wants this worker to free its used resources
            elif cmd[0] == "cleanup":
                env_filename = os.path.join(self.bindir, self_dict["env"])
                env = kvm_utils.load_env(env_filename, {})
                for obj in env.values():
                    if isinstance(obj, kvm_vm.VM):
                        obj.destroy()
                    elif isinstance(obj, kvm_subprocess.kvm_spawn):
                        obj.close()
                kvm_utils.dump_env(env, env_filename)
                w.write("cleanup_done\n")
                w.write("ready\n")

            # There's no more work for this worker
            elif cmd[0] == "terminate":
                break
예제 #2
0
파일: kvm.py 프로젝트: wenhann/chromiumos
    def run_once(self, params):
        # Report the parameters we've received and write them as keyvals
        logging.debug("Test parameters:")
        keys = params.keys()
        keys.sort()
        for key in keys:
            logging.debug("    %s = %s", key, params[key])
            self.write_test_keyval({key: params[key]})

        # Open the environment file
        env_filename = os.path.join(self.bindir, params.get("env", "env"))
        env = kvm_utils.load_env(env_filename, {})
        logging.debug("Contents of environment: %s" % str(env))

        try:
            try:
                # Get the test routine corresponding to the specified test type
                type = params.get("type")
                # Verify if we have the correspondent source file for it
                module_path = os.path.join(self.subtest_dir, '%s.py' % type)
                if not os.path.isfile(module_path):
                    raise error.TestError("No %s.py test file found" % type)
                # Load the tests directory (which was turned into a py module)
                try:
                    test_module = __import__("tests.%s" % type)
                except ImportError, e:
                    raise error.TestError("Failed to import test %s: %s" %
                                          (type, e))

                # Preprocess
                kvm_preprocessing.preprocess(self, params, env)
                kvm_utils.dump_env(env, env_filename)
                # Run the test function
                eval("test_module.%s.run_%s(self, params, env)" % (type, type))
                kvm_utils.dump_env(env, env_filename)

            except Exception, e:
                logging.error("Test failed: %s", e)
                logging.debug("Postprocessing on error...")
                kvm_preprocessing.postprocess_on_error(self, params, env)
                kvm_utils.dump_env(env, env_filename)
                raise
예제 #3
0
파일: kvm.py 프로젝트: wenhann/chromiumos
                # Verify if we have the correspondent source file for it
                module_path = os.path.join(self.subtest_dir, '%s.py' % type)
                if not os.path.isfile(module_path):
                    raise error.TestError("No %s.py test file found" % type)
                # Load the tests directory (which was turned into a py module)
                try:
                    test_module = __import__("tests.%s" % type)
                except ImportError, e:
                    raise error.TestError("Failed to import test %s: %s" %
                                          (type, e))

                # Preprocess
                kvm_preprocessing.preprocess(self, params, env)
                kvm_utils.dump_env(env, env_filename)
                # Run the test function
                eval("test_module.%s.run_%s(self, params, env)" % (type, type))
                kvm_utils.dump_env(env, env_filename)

            except Exception, e:
                logging.error("Test failed: %s", e)
                logging.debug("Postprocessing on error...")
                kvm_preprocessing.postprocess_on_error(self, params, env)
                kvm_utils.dump_env(env, env_filename)
                raise

        finally:
            # Postprocess
            kvm_preprocessing.postprocess(self, params, env)
            logging.debug("Contents of environment: %s", str(env))
            kvm_utils.dump_env(env, env_filename)
예제 #4
0
    def run_once(self, params):
        # Report the parameters we've received and write them as keyvals
        logging.debug("Test parameters:")
        keys = params.keys()
        keys.sort()
        for key in keys:
            logging.debug("    %s = %s", key, params[key])
            self.write_test_keyval({key: params[key]})

        # Set the log file dir for the logging mechanism used by kvm_subprocess
        # (this must be done before unpickling env)
        kvm_utils.set_log_file_dir(self.debugdir)

        # Open the environment file
        logging.info("Unpickling env. You may see some harmless error "
                     "messages.")
        env_filename = os.path.join(self.bindir, params.get("env", "env"))
        env = kvm_utils.load_env(env_filename, self.env_version)
        logging.debug("Contents of environment: %s", env)

        test_passed = False

        try:
            try:
                try:
                    # Get the test routine corresponding to the specified
                    # test type
                    t_type = params.get("type")
                    # Verify if we have the correspondent source file for it
                    subtest_dir = os.path.join(self.bindir, "tests")
                    module_path = os.path.join(subtest_dir, "%s.py" % t_type)
                    if not os.path.isfile(module_path):
                        raise error.TestError("No %s.py test file found" %
                                              t_type)
                    # Load the test module
                    f, p, d = imp.find_module(t_type, [subtest_dir])
                    test_module = imp.load_module(t_type, f, p, d)
                    f.close()

                    # Preprocess
                    try:
                        kvm_preprocessing.preprocess(self, params, env)
                    finally:
                        kvm_utils.dump_env(env, env_filename)
                    # Run the test function
                    run_func = getattr(test_module, "run_%s" % t_type)
                    try:
                        run_func(self, params, env)
                    finally:
                        kvm_utils.dump_env(env, env_filename)
                    test_passed = True

                except Exception, e:
                    logging.error("Test failed: %s: %s",
                                  e.__class__.__name__, e)
                    try:
                        kvm_preprocessing.postprocess_on_error(
                            self, params, env)
                    finally:
                        kvm_utils.dump_env(env, env_filename)
                    raise

            finally:
                # Postprocess
                try:
                    try:
                        kvm_preprocessing.postprocess(self, params, env)
                    except Exception, e:
                        if test_passed:
                            raise
                        logging.error("Exception raised during "
                                      "postprocessing: %s", e)
                finally:
                    kvm_utils.dump_env(env, env_filename)
                    logging.debug("Contents of environment: %s", env)

        except Exception, e:
            if params.get("abort_on_error") != "yes":
                raise
            # Abort on error
            logging.info("Aborting job (%s)", e)
            for vm in kvm_utils.env_get_all_vms(env):
                if vm.is_dead():
                    continue
                logging.info("VM '%s' is alive.", vm.name)
                for m in vm.monitors:
                    logging.info("'%s' has a %s monitor unix socket at: %s",
                                 vm.name, m.protocol, m.filename)
                logging.info("The command line used to start '%s' was:\n%s",
                             vm.name, vm.make_qemu_command())
            raise error.JobError("Abort requested (%s)" % e)