def test_kill_stopped_sleep(self): sleep = process.run("which sleep", ignore_status=True, shell=True) if sleep.exit_status: self.skipTest("Sleep binary not found in PATH") sleep = "'%s 60'" % sleep.stdout.strip() proc = aexpect.Expect("./scripts/avocado run %s --job-results-dir %s " "--sysinfo=off --job-timeout 3" % (sleep, self.tmpdir)) proc.read_until_output_matches(["\(1/1\)"], timeout=3, internal_timeout=0.01) # We need pid of the avocado, not the shell executing it pid = int(process.get_children_pids(proc.get_pid())[0]) os.kill(pid, signal.SIGTSTP) # This freezes the process deadline = time.time() + 9 while time.time() < deadline: if not proc.is_alive(): break time.sleep(0.1) else: proc.kill(signal.SIGKILL) self.fail("Avocado process still alive 5s after job-timeout:\n%s" % proc.get_output()) output = proc.get_output() self.assertIn("ctrl+z pressed, stopping test", output, "SIGTSTP " "message not in the output, test was probably not " "stopped.") self.assertIn("TIME", output, "TIME not in the output, avocado " "probably died unexpectadly") self.assertEqual(proc.get_status(), 1, "Avocado did not finish with " "1.")
def test_kill_stopped_sleep(self): sleep = process.run("which sleep", ignore_status=True, shell=True) if sleep.exit_status: self.skipTest("Sleep binary not found in PATH") sleep = "'%s 60'" % sleep.stdout.strip() proc = aexpect.Expect("./scripts/avocado run %s --job-results-dir %s " "--sysinfo=off --job-timeout 3" % (sleep, self.tmpdir)) proc.read_until_output_matches(["\(1/1\)"], timeout=3, internal_timeout=0.01) # We need pid of the avocado, not the shell executing it pid = int(process.get_children_pids(proc.get_pid())[0]) os.kill(pid, signal.SIGTSTP) # This freezes the process deadline = time.time() + 9 while time.time() < deadline: if not proc.is_alive(): break time.sleep(0.1) else: proc.kill(signal.SIGKILL) self.fail("Avocado process still alive 5s after job-timeout:\n%s" % proc.get_output()) output = proc.get_output() self.assertIn( "ctrl+z pressed, stopping test", output, "SIGTSTP " "message not in the output, test was probably not " "stopped.") self.assertIn( "TIME", output, "TIME not in the output, avocado " "probably died unexpectadly") self.assertEqual(proc.get_status(), 1, "Avocado did not finish with " "1.")
def assign_vm_into_cgroup(vm, cgroup, pwd=None): """ Assigns all threads of VM into cgroup :param vm: desired VM :param cgroup: cgroup handler :param pwd: desired cgroup's pwd, cgroup index or None for root cgroup """ cgroup.set_cgroup(vm.get_shell_pid(), pwd) for pid in process.get_children_pids(vm.get_shell_pid()): try: cgroup.set_cgroup(int(pid), pwd) except Exception: # Process might not already exist test.fail("Failed to move all VM threads to cgroup")
def test_kill_stopped_sleep(self): sleep = process.run("which sleep", ignore_status=True, shell=True) if sleep.exit_status: self.skipTest("Sleep binary not found in PATH") sleep = "'%s 60'" % sleep.stdout.strip() proc = aexpect.Expect("./scripts/avocado run %s --job-results-dir %s " "--sysinfo=off --job-timeout 3" % (sleep, self.tmpdir)) proc.read_until_output_matches(["\(1/1\)"], timeout=3, internal_timeout=0.01) # We need pid of the avocado, not the shell executing it pid = int(process.get_children_pids(proc.get_pid())[0]) os.kill(pid, signal.SIGTSTP) # This freezes the process deadline = time.time() + 9 while time.time() < deadline: if not proc.is_alive(): break time.sleep(0.1) else: proc.kill(signal.SIGKILL) self.fail("Avocado process still alive 5s after job-timeout:\n%s" % proc.get_output()) output = proc.get_output() self.assertIn( "ctrl+z pressed, stopping test", output, "SIGTSTP " "message not in the output, test was probably not " "stopped.") self.assertIn( "TIME", output, "TIME not in the output, avocado " "probably died unexpectadly") self.assertEqual(proc.get_status(), 8, "Avocado did not finish with " "1.") sleep_dir = astring.string_to_safe_path("1-" + sleep[1:-1]) debug_log = os.path.join(self.tmpdir, "latest", "test-results", sleep_dir, "debug.log") debug_log = open(debug_log).read() self.assertIn( "Runner error occurred: Timeout reached", debug_log, "Runner error occurred: Timeout reached message not " "in the test's debug.log:\n%s" % debug_log) self.assertNotIn( "Traceback (most recent", debug_log, "Traceback " "present in the test's debug.log file, but it was " "suppose to be stopped and unable to produce it.\n" "%s" % debug_log)
def test_kill_stopped_sleep(self): sleep = process.run("which sleep", ignore_status=True, shell=True) if sleep.exit_status: self.skipTest("Sleep binary not found in PATH") sleep = "'%s 60'" % sleep.stdout.strip() proc = aexpect.Expect("./scripts/avocado run %s --job-results-dir %s " "--sysinfo=off --job-timeout 3" % (sleep, self.tmpdir)) proc.read_until_output_matches(["\(1/1\)"], timeout=3, internal_timeout=0.01) # We need pid of the avocado, not the shell executing it pid = int(process.get_children_pids(proc.get_pid())[0]) os.kill(pid, signal.SIGTSTP) # This freezes the process deadline = time.time() + 9 while time.time() < deadline: if not proc.is_alive(): break time.sleep(0.1) else: proc.kill(signal.SIGKILL) self.fail("Avocado process still alive 5s after job-timeout:\n%s" % proc.get_output()) output = proc.get_output() self.assertIn("ctrl+z pressed, stopping test", output, "SIGTSTP " "message not in the output, test was probably not " "stopped.") self.assertIn("TIME", output, "TIME not in the output, avocado " "probably died unexpectadly") self.assertEqual(proc.get_status(), 8, "Avocado did not finish with " "1.") sleep_dir = astring.string_to_safe_path("1-" + sleep[1:-1]) debug_log = os.path.join(self.tmpdir, "latest", "test-results", sleep_dir, "debug.log") debug_log = open(debug_log).read() self.assertIn("Runner error occurred: Timeout reached", debug_log, "Runner error occurred: Timeout reached message not " "in the test's debug.log:\n%s" % debug_log) self.assertNotIn("Traceback (most recent", debug_log, "Traceback " "present in the test's debug.log file, but it was " "suppose to be stopped and unable to produce it.\n" "%s" % debug_log)
def test_get_children_pids(self): ''' Gets the list of children process. Linux only. ''' self.assertGreaterEqual(len(process.get_children_pids(1)), 1)
def run(test, params, env): """ Check smbios table : 1) Run the qemu command as nobody 2) check the process is same as the user's :param test: QEMU test object. :param params: Dictionary with the test parameters. :param env: Dictionary with test environment. """ def get_user_ugid(username): """ return user uid and gid as a list """ user_uid = process.getoutput("id -u %s" % username).split() user_gid = process.getoutput("id -g %s" % username).split() return (user_uid, user_gid) def get_ugid_from_processid(pid): """ return a list[uid,euid,suid,fsuid,gid,egid,sgid,fsgid] of pid """ grep_ugid_cmd = "cat /proc/%s/status | grep -iE '^(U|G)id'" o = process.getoutput(grep_ugid_cmd % pid, shell=True) ugid = re.findall(r"(\d+)", o) # real UID, effective UID, saved set UID, and file system UID if ugid: return ugid else: test.error("Could not find the correct UID for process %s" % pid) exec_username = params.get("user_runas", "nobody") error_context.base_context("Run QEMU %s test:" % exec_username) error_context.context("Get the user uid and gid,using 'id -u/g username'") (exec_uid, exec_gid) = get_user_ugid(exec_username) error_context.context("Run the qemu as user '%s'" % exec_username) logging.info("The user %s :uid='%s', gid='%s'" % (exec_username, exec_uid, exec_gid)) params["extra_params"] = " -runas %s" % exec_username params["start_vm"] = "yes" env_process.preprocess_vm(test, params, env, params.get("main_vm")) vm = env.get_vm(params["main_vm"]) failures = [] for pid in process.get_children_pids(vm.get_shell_pid()): error_context.context( "Get the process '%s' u/gid, using 'cat " "/proc/%s/status'" % (pid, pid), logging.info) qemu_ugid = get_ugid_from_processid(pid) logging.info("Process run as uid=%s,euid=%s,suid=%s,fsuid=%s" % tuple(qemu_ugid[0:4])) logging.info("Process run as gid=%s,egid=%s,sgid=%s,fsgid=%s" % tuple(qemu_ugid[4:])) error_context.context("Check if the user %s ugid is equal to the " "process %s" % (exec_username, pid)) # generate user uid, euid, suid, fsuid, gid, egid, sgid, fsgid user_ugid_extend = exec_uid * 4 + exec_gid * 4 if cmp(user_ugid_extend, qemu_ugid) != 0: e_msg = ("Process %s error, expect ugid is %s, real is %s" % (pid, user_ugid_extend, qemu_ugid)) failures.append(e_msg) if failures: test.fail("FAIL: Test reported %s failures:\n%s" % (len(failures), "\n".join(failures)))
def sigterm_handler(signum, frame): # pylint: disable=W0613 children = process.get_children_pids(os.getpid()) for child in children: process.kill_process_tree(int(child)) raise SystemExit('Terminated')
def run(test, params, env): """ Check smbios table : 1) Run the qemu command as nobody 2) check the process is same as the user's :param test: QEMU test object. :param params: Dictionary with the test parameters. :param env: Dictionary with test environment. """ def get_user_ugid(username): """ return user uid and gid as a list """ user_uid = process.system_output("id -u %s" % username).split() user_gid = process.system_output("id -g %s" % username).split() return(user_uid, user_gid) def get_ugid_from_processid(pid): """ return a list[uid,euid,suid,fsuid,gid,egid,sgid,fsgid] of pid """ grep_ugid_cmd = "cat /proc/%s/status | grep -iE '^(U|G)id'" o = process.system_output(grep_ugid_cmd % pid.strip(), shell=True) ugid = re.findall(r"(\d+)", o) # real UID, effective UID, saved set UID, and file system UID if ugid: return ugid else: test.error("Could not find the correct UID for process %s" % pid) exec_username = params.get("user_runas", "nobody") error_context.base_context("Run QEMU %s test:" % exec_username) error_context.context("Get the user uid and gid,using 'id -u/g username'") (exec_uid, exec_gid) = get_user_ugid(exec_username) error_context.context("Run the qemu as user '%s'" % exec_username) logging.info("The user %s :uid='%s', gid='%s'" % (exec_username, exec_uid, exec_gid)) params["extra_params"] = " -runas %s" % exec_username params["start_vm"] = "yes" env_process.preprocess_vm(test, params, env, params.get("main_vm")) vm = env.get_vm(params["main_vm"]) failures = [] for pid in process.get_children_pids(vm.get_shell_pid()): error_context.context("Get the process '%s' u/gid, using 'cat " "/proc/%s/status'" % (pid, pid), logging.info) qemu_ugid = get_ugid_from_processid(pid) logging.info("Process run as uid=%s,euid=%s,suid=%s,fsuid=%s" % tuple(qemu_ugid[0:4])) logging.info("Process run as gid=%s,egid=%s,sgid=%s,fsgid=%s" % tuple(qemu_ugid[4:])) error_context.context("Check if the user %s ugid is equal to the " "process %s" % (exec_username, pid)) # generate user uid, euid, suid, fsuid, gid, egid, sgid, fsgid user_ugid_extend = exec_uid * 4 + exec_gid * 4 if cmp(user_ugid_extend, qemu_ugid) != 0: e_msg = ("Process %s error, expect ugid is %s, real is %s" % (pid, user_ugid_extend, qemu_ugid)) failures.append(e_msg) if failures: test.fail("FAIL: Test reported %s failures:\n%s" % (len(failures), "\n".join(failures)))
def test_get_children_pids(self): """ Gets the list of children process. Linux only. """ self.assertGreaterEqual(len(process.get_children_pids(os.getppid())), 1)