예제 #1
0
    def prepare_config(self):
        for key in self._pop_config_keys:
            config.pop(key, None)

        config.update(
            {
                # Default to a uniform retry delay of one second
                "agent_http_retry_delay_offset": 0.1,
                "agent_http_retry_delay_factor": 0,
                "shutting_down": False,
                "jobtypes": {},
                "current_assignments": {},
                "agent_id": uuid.uuid4(),
                "agent_http_persistent_connections": False,
                "agent_shutdown_timeout": 3,
                "master": PYFARM_AGENT_MASTER,
                "agent_hostname": os.urandom(self.RAND_LENGTH).encode("hex"),
                "agent_ram": memory.total_ram(),
                "agent_cpus": cpu.total_cpus(),
                "agent_api_port": randint(10000, 50000),
                "free_ram": memory.free_ram(),
                "agent_time_offset": randint(-50, 50),
                "state": choice(AgentState),
                "start": time.time(),
                "agent_pretty_json": False,
                "agent_html_template_reload": True,
                "agent_master_reannounce": randint(5, 15),
            }
        )
예제 #2
0
    def test_accepted(self):
        # Cache the fake job type and make sure the config
        # turns off caching
        jobtype = {
            "classname": "FakeJobType",
            "code": FAKE_JOBTYPE,
            "name": self.data["jobtype"]["name"],
            "version": self.data["jobtype"]["version"]}
        JobType.cache[(self.data["jobtype"]["name"],
                       self.data["jobtype"]["version"])] = (jobtype, None)
        config.update(
            jobtype_enable_cache=False,
            current_assignments={})
        request = self.post(
            data=self.data,
            headers={"User-Agent": config["master_user_agent"]})
        assign = self.instance_class()
        result = assign.render(request)
        self.assertEqual(result, NOT_DONE_YET)
        self.assertTrue(request.finished)
        self.assertEqual(request.responseCode, ACCEPTED)
        self.assertEqual(len(request.written), 1)
        response_id = UUID(loads(request.written[0])["id"])
        self.assertIn(response_id, config["current_assignments"])

        # An assignment uuid has been added
        test_data = self.data.copy()
        current_assignment = config["current_assignments"][response_id].copy()

        # Update the original test data with the new assignment data
        # and make sure it matches
        test_data.update(id=response_id)
        # TODO: The jobtype instance is created asynchronously in a deferred, so
        # checking its behaviour from this test is quite hairy. Find a better
        # solution than simply not testing it if it's not there yet
        if "id" in current_assignment["jobtype"]:
            test_data["jobtype"].update(id=current_assignment["jobtype"]["id"])
        self.assertEqual(current_assignment, test_data)
        if "id" in current_assignment["jobtype"]:
            self.assertIn(current_assignment["jobtype"]["id"], config["jobtypes"])

            # Now trigger the started callback so we can make sure the job
            # type gets removed
            jobtype = config["jobtypes"][current_assignment["jobtype"]["id"]]
            jobtype.fake_started.callback(None)
            jobtype.fake_stopped.callback(None)
            self.assertNotIn(response_id, config["current_assignments"])
예제 #3
0
 def prepare_config(self):
     super(TestAssign, self).prepare_config()
     config.update({
         "cpus": randint(1, 16),
         "agent_id": randint(1, 2048)})
예제 #4
0
 def prepare_config(self):
     super(TestStatus, self).prepare_config()
     config.update(
         state=AgentState.ONLINE, pids=[1, 2, 3])
예제 #5
0
 def tearDown(self):
     super(TestStatus, self).tearDown()
     config.update(self._config)
예제 #6
0
 def prepare_config(self):
     super(TestStop, self).prepare_config()
     config.update(run_control_file="/tmp/pyfarm/agent/should_be_running")
예제 #7
0
    def __call__(self):
        logger.debug("Parsing command line arguments")
        self.args = self.parser.parse_args()

        # No daemon mode must be set with --pdb-on-unhanded, without this
        # you could end up with a blocking agent and no way of knowing
        # about it.
        if not self.args.no_daemon and self.args.pdb_on_unhandled:
            self.parser.error(
                "You cannot set --pdb-on-unhandled without --no-daemon")

        if self.args.pdb_on_unhandled:
            Observer.PDB_ON_UNHANDLED_ERROR = True

            def excepthook(exctype, value, traceback):
                pdb.set_trace()

            sys.excepthook = excepthook

        if not config["master"] and self.args.target_name == "start":
            self.parser.error(
                "--master must be provided (ex. "
                "'pyfarm-agent --master=foobar start')")

        # if we're on windows, produce some warnings about
        # flags which are not supported
        if WINDOWS and self.args.uid:
            logger.warning("--uid is not currently supported on Windows")

        if WINDOWS and self.args.gid:
            logger.warning("--gid is not currently supported on Windows")

        if WINDOWS and self.args.no_daemon:
            logger.warning("--no-daemon is not currently supported on Windows")

        if self.args.agent_id is None:
            agent_id = AgentUUID.load(self.args.agent_id_file)

            # No agent id saved, generate one then try to save it.  If we
            # can't then an error will be raised when AgentUUID.save is called.
            if agent_id is None:
                agent_id = AgentUUID.generate()
                AgentUUID.save(agent_id, self.args.agent_id_file)

            self.args.agent_id = agent_id

        # A custom --agent-id was provided, warn if it varies from one
        # we load from disk.  We won't try to save it however because
        # that could cause conflicts if someone is using --agent-id
        # and trying to run multiple agents.
        else:
            saved_agent_id = AgentUUID.load(self.args.agent_id_file)
            if (saved_agent_id is not None
                    and saved_agent_id != self.args.agent_id):
                logger.warning(
                    "Custom agent ID has been provided by --agent-id")

        config["agent_id"] = self.args.agent_id

        if self.args.target_name == "start":
            # update configuration with values from the command line
            config_flags = {
                "state": self.args.state,
                "pids": {
                    "parent": os.getpid()}}

            config.update(config_flags)

        return_code = self.args.target_func()

        # If a specific return code is provided then use it
        # directly in sys.exit
        if isinstance(return_code, INTEGER_TYPES):
            sys.exit(return_code)
 def prepare_config(self):
     super(TestEnvironment, self).prepare_config()
     config.update({"state": AgentState.ONLINE, "agent_id": 0})