def test_success(self): """Test success cases. This tests for both giving explicitly the shard number, and for autodetecting it. """ self.assertEqual(get_safe_shard("Service", 0), 0) self.assertEqual(get_safe_shard("Service", 1), 1) self.assertEqual(get_safe_shard("Service", None), 1)
def _is_service_proc(service, cmdline): """Returns if cmdline can be the command line of service. service (ServiceCoord): the service. cmdline ([string]): a command line. return (bool): whether service could have been launched with the command line cmdline. """ if not cmdline: return False start_index = 0 if cmdline[0] == "/usr/bin/env": start_index = 1 if len(cmdline) - start_index < 2: return False cl_interpreter = cmdline[start_index] cl_service = cmdline[start_index + 1] if "python" not in cl_interpreter or \ not cl_service.endswith("cms%s" % service.name): return False # We assume that apart from the shard, all other # options are in the form "-<something> <something>". shard = None for i in xrange(start_index + 2, len(cmdline), 2): if cmdline[i].isdigit(): shard = int(cmdline[i]) break try: if get_safe_shard(service.name, shard) != service.shard: return False except ValueError: return False return True
def find(self, service, cpu_times=None): """Returns the pid of a given service running on this machine. service (ServiceCoord): the service we are interested in. cpu_times ({ServiceCoord: object}|None): if not None, a dict to update with the cputimes of the found process, if found. return (psutil.Process|None): the process of service, or None if not found """ logger.debug("ProcessMatcher.find %s", service) if self._procs is None: self._procs = ProcessMatcher._get_interesting_running_processes() shards = self._procs.get(service.name, {}) for shard, proc in shards.items(): if get_safe_shard(service.name, shard) == service.shard: logger.debug("Found %s", service) if cpu_times is not None: cpu_times[service] = proc.cpu_times() return proc return None
def find(self, service, cpu_times=None): """Returns the pid of a given service running on this machine. service (ServiceCoord): the service we are interested in. cpu_times ({ServiceCoord: object}|None): if not None, a dict to update with the cputimes of the found process, if found. return (psutil.Process|None): the process of service, or None if not found """ logger.debug("ProcessMatcher.find %s", service) if self._procs is None: self._procs = ProcessMatcher._get_interesting_running_processes() shards = self._procs.get(service.name, {}) for shard, proc in shards.iteritems(): if get_safe_shard(service.name, shard) == service.shard: logger.debug("Found %s", service) if cpu_times is not None: cpu_times[service] = proc.cpu_times() return proc return None
def test_shard_not_present(self): """Test failure when the given shard is not in the config.""" with self.assertRaises(ValueError): get_safe_shard("Service", 2)
def test_no_autodetect(self): """Test failure when no shard is given and autodetect fails.""" # Setting up non-matching IPs. _set_up_ip_addresses(["1.1.1.1", "0.0.0.2"]) with self.assertRaises(ValueError): get_safe_shard("Service", None)
def test_service_not_present(self): """Test failure when the given service is not in the config.""" with self.assertRaises(ValueError): get_safe_shard("ServiceNotPresent", 0)