def test_returns_processes_when_running_in_container(self): proc_path = self.make_dir() self.make_init_process(proc_path, in_container=True) command = factory.make_name("command") pids_running_command = set(random.randint(2, 999) for _ in range(3)) for pid in pids_running_command: self.make_process(proc_path, pid, in_container=True, command=command) pids_not_running_command = set( random.randint(1000, 1999) for _ in range(3)) for pid in pids_not_running_command: self.make_process( proc_path, pid, in_container=True, command=factory.make_name("command"), ) mock_running_in_container = self.patch(ps_module, "running_in_container") mock_running_in_container.return_value = True self.assertItemsEqual( pids_running_command, get_running_pids_with_command(command, proc_path=proc_path), )
def test_ignores_process_that_have_been_removed(self): proc_path = self.make_dir() self.make_init_process(proc_path) command = factory.make_name("command") pids_running_command = set(random.randint(2, 999) for _ in range(3)) for pid in pids_running_command: self.make_process(proc_path, pid, command=command) # Remove the comm file to test the exception handling. os.remove(os.path.join(proc_path, str(pid), "comm")) self.assertItemsEqual([], get_running_pids_with_command( command, proc_path=proc_path))
def get_dhclient_info(proc_path="/proc"): """Return dictionary mapping interfaces to assigned address from dhclient. """ dhclient_pids = get_running_pids_with_command("dhclient", proc_path=proc_path) dhclient_info = {} for pid in dhclient_pids: cmdline = read_text_file(os.path.join(proc_path, str(pid), "cmdline")).split("\x00") if "-lf" in cmdline: idx_lf = cmdline.index("-lf") lease_path = cmdline[idx_lf + 1] # After '-lf' option. interface_name = cmdline[-2] # Last argument. ip_address = get_lastest_fixed_address(lease_path) if (ip_address is not None and len(ip_address) > 0 and not ip_address.isspace()): dhclient_info[interface_name] = ip_address return dhclient_info