# Author: Tom Hromatka <tom.hromatka@oracle.com>
#
+from process import Process
from cgroup import Cgroup
-from run import Run
import consts
import ftests
import sys
def teardown(config):
pids = Cgroup.get_pids_in_cgroup(config, CGNAME, CONTROLLER)
- if pids:
- for p in pids.splitlines():
- if config.args.container:
- config.container.run(['kill', '-9', p])
- else:
- Run.run(['sudo', 'kill', '-9', p])
+ Process.kill(config, pids)
Cgroup.delete(config, CONTROLLER, CGNAME)
#
from cgroup import Cgroup, CgroupVersion
-from run import Run
+from process import Process
import consts
import ftests
import sys
def teardown(config):
# destroy the child processes
pids = Cgroup.get_pids_in_cgroup(config, PARENT_CGNAME, CONTROLLER)
- if pids:
- for p in pids.splitlines():
- Run.run(['sudo', 'kill', '-9', p])
+ Process.kill(config, pids)
Cgroup.delete(config, CONTROLLER, PARENT_CGNAME, recursive=True)
#
from cgroup import Cgroup, CgroupVersion
-from run import Run
+from process import Process
import consts
import ftests
import sys
def teardown(config):
pids = Cgroup.get_pids_in_cgroup(config, CGNAME, CONTROLLER)
- if pids:
- for p in pids.splitlines():
- if config.args.container:
- config.container.run(['kill', '-9', p])
- else:
- Run.run(['sudo', 'kill', '-9', p])
+ Process.kill(config, pids)
Cgroup.delete(config, None, CGNAME)
from cgroup import Cgroup as CgroupCli
from cgroup import CgroupVersion
-from run import Run, RunError
from libcgroup import Cgroup
from systemd import Systemd
+from process import Process
+from run import RunError
import ftests
import consts
import sys
def teardown(config, result):
pid = CgroupCli.get(config, cgname=os.path.join(SLICE, SCOPE), setting='cgroup.procs',
print_headers=False, values_only=True)
- if pid is not None:
- Run.run(['kill', '-9', str(pid)], shell_bool=True)
+ Process.kill(config, pid)
if result != consts.TEST_PASSED:
# Something went wrong. Let's force the removal of the cgroups just to be safe.
from run import Run, RunError
from libcgroup import Cgroup
from systemd import Systemd
+from process import Process
import ftests
import consts
import sys
def teardown(config, result):
global pid
- if pid is not None:
- Run.run(['kill', '-9', str(pid)], shell_bool=True)
+ Process.kill(config, pid)
if result != consts.TEST_PASSED:
# Something went wrong. Let's force the removal of the cgroups just to be safe.
found = False
pids = CgroupCli.get_pids_in_cgroup(config, CGNAME, CONTROLLER)
- for pid in pids.splitlines():
- if int(pid) == os.getpid():
+ for pid in pids:
+ if pid == os.getpid():
# our process was successfully added to the cgroup
found = True
found = False
pids = CgroupCli.get_pids_in_cgroup(config, CGNAME, CONTROLLER)
- for pid in pids.splitlines():
- if int(pid) == os.getpid():
+ for pid in pids:
+ if pid == os.getpid():
# our process was successfully added to the cgroup
found = True
cg = Cgroup(CGNAME, Version.CGROUP_V2)
cg.get()
- cg.attach(int(child_pid))
+ cg.attach(child_pid)
found = False
pids = CgroupCli.get_pids_in_cgroup(config, CGNAME, CONTROLLER)
- for pid in pids.splitlines():
+ for pid in pids:
if pid == child_pid:
# our process was successfully added to the cgroup
found = True
# now let's attach the child process to the root cgroup and ensure we no longer
# are in CGNAME
- cg.attach(pid=int(child_pid), root_cgroup=True)
+ cg.attach(pid=child_pid, root_cgroup=True)
found = False
pids = CgroupCli.get_pids_in_cgroup(config, CGNAME, CONTROLLER)
- for pid in pids.splitlines():
+ for pid in pids:
if pid == child_pid:
# our process was successfully added to the cgroup
found = True
from cgroup import CgroupVersion as CgroupCliVersion
from cgroup import Cgroup as CgroupCli
from libcgroup import Cgroup, Version
-from run import Run, RunError
from systemd import Systemd
+from process import Process
+from run import RunError
import ftests
import consts
import utils
result = consts.TEST_PASSED
cause = None
- pid = int(config.process.create_process(config))
+ pid = config.process.create_process(config)
cg = Cgroup(CGNAME, Version.CGROUP_V2)
def teardown(config, result):
global pid
- if pid is not None:
- Run.run(['kill', '-9', str(pid)], shell_bool=True)
+ Process.kill(config, pid)
if result != consts.TEST_PASSED:
# Something went wrong. Let's force the removal of the cgroups just to be safe.
# Copyright (c) 2023 Oracle and/or its affiliates.
# Author: Kamalesh Babulal <kamalesh.babulal@oracle.com>
-from run import Run, RunError
from systemd import Systemd
+from process import Process
from cgroup import Cgroup
+from run import RunError
import consts
import ftests
import time
result = consts.TEST_FAILED
cause = 'Creation of systemd default slice/scope erroneously succeeded'
- # killing the pid, should remove the scope cgroup too.
- Run.run(['sudo', 'kill', '-9', pid])
+ # killing the pid should remove the scope cgroup too.
+ Process.kill(config, pid)
# Let's pause and wait for the systemd to remove the scope.
time.sleep(1)
#
from cgroup import Cgroup, CgroupVersion
-from run import Run, RunError
from systemd import Systemd
+from process import Process
+from run import RunError
import consts
import ftests
import time
return pids, result, cause
-def terminate_process(config, pids):
- if pids:
- for p in pids.splitlines():
- Run.run(['sudo', 'kill', '-9', p])
-
-
def test(config):
global SYSTEMD_PIDS, OTHER_PIDS
try:
Cgroup.classify(config, CONTROLLER, SYSTEMD_CGNAME, OTHER_PIDS, ignore_systemd=True)
except RunError as re:
- err_str = 'Error changing group of pid {}: Cgroup does not exist'.format(OTHER_PIDS)
+ err_str = 'Error changing group of pid {}: Cgroup does not exist'.format(OTHER_PIDS[0])
if re.stderr != err_str:
raise re
else:
result = consts.TEST_FAILED
- cause = 'Changing group of pid {} erroneously succeeded'.format(OTHER_PIDS)
+ cause = 'Changing group of pid {} erroneously succeeded'.format(OTHER_PIDS[0])
# classify a task from the systemd scope cgroup (SYSTEMD_CGNAME) to
# non-systemd scope cgroup (OTHER_CGNAME). Migration should fail due
try:
Cgroup.classify(config, CONTROLLER, OTHER_CGNAME, SYSTEMD_PIDS)
except RunError as re:
- err_str = 'Error changing group of pid {}: Cgroup does not exist'.format(SYSTEMD_PIDS)
+ err_str = 'Error changing group of pid {}: Cgroup does not exist'.format(SYSTEMD_PIDS[0])
if re.stderr != err_str:
raise re
else:
result = consts.TEST_FAILED
- tmp_cause = 'Changing group of pid {} erroneously succeeded'.format(SYSTEMD_PIDS)
+ tmp_cause = 'Changing group of pid {} erroneously succeeded'.format(SYSTEMD_PIDS[0])
cause = '\n'.join(filter(None, [cause, tmp_cause]))
# classify the task from the non-systemd scope cgroup to systemd scope cgroup.
def teardown(config):
global SYSTEMD_PIDS, OTHER_PIDS
- terminate_process(config, SYSTEMD_PIDS)
- terminate_process(config, OTHER_PIDS)
+ Process.kill(config, SYSTEMD_PIDS)
+ Process.kill(config, OTHER_PIDS)
# We need a pause, so that cgroup.procs gets updated.
time.sleep(1)
from cgroup import Cgroup, CgroupVersion
from systemd import Systemd
-from run import Run, RunError
+from process import Process
+from run import RunError
import consts
import ftests
import time
return pids, result, cause
-def terminate_process(config, pids):
- if pids:
- for p in pids.splitlines():
- Run.run(['sudo', 'kill', '-9', p])
-
-
def test(config):
global SYSTEMD_PIDS, OTHER_PIDS
if result == consts.TEST_FAILED:
return result, cause
- # SYSTEMD_CGNAME already has the pid of the task, that scope was created
- # with and killing it will remove the scope, be careful and pick the newly
- # spawned task
- SYSTEMD_PIDS = SYSTEMD_PIDS.split('\n')[1]
-
# classify a task from the non-systemd scope cgroup (OTHER_CGNAME) to
# systemd scope cgroup (SYSTEMD_CGNAME). Migration should fail due to
# the incorrect destination cgroup path that gets constructed, without
try:
Cgroup.classify(config, CONTROLLER, SYSTEMD_CGNAME, OTHER_PIDS, ignore_systemd=True)
except RunError as re:
- err_str = 'Error changing group of pid {}: No such file or directory'.format(OTHER_PIDS)
+ err_str = 'Error changing group of pid {}: No such file or directory'.format(OTHER_PIDS[0])
if re.stderr != err_str:
raise re
else:
result = consts.TEST_FAILED
- cause = 'Changing group of pid {} erroneously succeeded'.format(OTHER_PIDS)
+ cause = 'Changing group of pid {} erroneously succeeded'.format(OTHER_PIDS[0])
# classify a task from the systemd scope cgroup (SYSTEMD_CGNAME) to
# non-systemd scope cgroup (OTHER_CGNAME). Migration should fail due
# to the incorrect destination cgroup path that gets constructed, with
# the systemd slice/scope when ignore_systemd=False)
try:
- Cgroup.classify(config, CONTROLLER, OTHER_CGNAME, SYSTEMD_PIDS)
+ Cgroup.classify(config, CONTROLLER, OTHER_CGNAME, SYSTEMD_PIDS[1])
except RunError as re:
- err_str = 'Error changing group of pid {}: No such file or directory'.format(SYSTEMD_PIDS)
+ err_str = 'Error changing group of pid {}: No such file or directory'.format(
+ SYSTEMD_PIDS[1])
if re.stderr != err_str:
raise re
else:
result = consts.TEST_FAILED
- tmp_cause = 'Changing group of pid {} erroneously succeeded'.format(SYSTEMD_PIDS)
+ tmp_cause = 'Changing group of pid {} erroneously succeeded'.format(SYSTEMD_PIDS[1])
cause = '\n'.join(filter(None, [cause, tmp_cause]))
# classify the task from the non-systemd scope cgroup to systemd scope cgroup.
def teardown(config):
global SYSTEMD_PIDS, OTHER_PIDS
- terminate_process(config, SYSTEMD_PIDS)
- terminate_process(config, OTHER_PIDS)
+ Process.kill(config, SYSTEMD_PIDS)
+ Process.kill(config, OTHER_PIDS)
# We need a pause, so that cgroup.procs gets updated.
time.sleep(1)
- Systemd.remove_scope_slice_conf(config, SLICE, SCOPE, CONTROLLER, CONFIG_FILE_NAME)
+ try:
+ Cgroup.delete(config, CONTROLLER, cgname=SLICE, ignore_systemd=True)
+ except RunError as re:
+ if 'No such file or directory' not in re.stderr:
+ raise re
# Incase the error occurs before the creation of OTHER_CGNAME,
# let's ignore the exception
from cgroup import Cgroup, CgroupVersion
from systemd import Systemd
-from run import Run, RunError
+from process import Process
+from run import RunError
import consts
import ftests
import time
return pids, result, cause
-def terminate_process(config, pids):
- if pids:
- for p in pids.splitlines():
- Run.run(['sudo', 'kill', '-9', p])
-
-
def test(config):
global SYSTEMD_PIDS, OTHER_PIDS
def teardown(config):
global SYSTEMD_PIDS, OTHER_PIDS
- terminate_process(config, SYSTEMD_PIDS)
- terminate_process(config, OTHER_PIDS)
+ Process.kill(config, SYSTEMD_PIDS)
+ Process.kill(config, OTHER_PIDS)
# We need a pause, so that cgroup.procs gets updated.
time.sleep(1)
#
from cgroup import Cgroup, CgroupVersion
-from run import Run, RunError
from systemd import Systemd
+from process import Process
+from run import RunError
import consts
import ftests
import time
CONFIG_FILE_NAME = os.path.join(os.getcwd(), '068cgconfig.conf')
-SYSTEMD_PIDS = ''
-OTHER_PIDS = ''
+SYSTEMD_PIDS = None
+OTHER_PIDS = None
def prereqs(config):
return pids, result, cause
-def terminate_process(config, pids):
- if pids:
- for p in pids.splitlines():
- Run.run(['kill', '-9', p])
-
-
def test(config):
global SYSTEMD_PIDS, OTHER_PIDS
# SYSTEMD_CGNAME already has the pid of the task, that scope was created
# with and killing it will remove the scope, be careful and pick the newly
# spawned task
- SYSTEMD_PIDS = SYSTEMD_PIDS.split('\n')[1]
+ SYSTEMD_PIDS = SYSTEMD_PIDS[1]
return result, cause
def teardown(config):
global SYSTEMD_PIDS, OTHER_PIDS
- terminate_process(config, SYSTEMD_PIDS)
- terminate_process(config, OTHER_PIDS)
+ Process.kill(config, SYSTEMD_PIDS)
+ Process.kill(config, OTHER_PIDS)
# We need a pause, so that cgroup.procs gets updated.
time.sleep(1)
def __set(config, cmd, cgname=None, setting=None, value=None,
copy_from=None, cghelp=False, ignore_systemd=False):
if setting is not None or value is not None:
- if isinstance(setting, str) and isinstance(value, str):
+ if isinstance(setting, str) and (isinstance(value, str) or isinstance(value, int)):
cmd.append('-r')
- cmd.append('{}={}'.format(setting, value))
+ cmd.append('{}={}'.format(setting, str(value)))
elif isinstance(setting, list) and isinstance(value, list):
if len(setting) != len(value):
raise ValueError(
for idx, stg in enumerate(setting):
cmd.append('-r')
- cmd.append('{}={}'.format(stg, value[idx]))
+ cmd.append('{}={}'.format(stg, str(value[idx])))
else:
raise ValueError(
'Invalid inputs to cgget:\nsetting: {}\n'
cmd.append(str(pid_list))
elif isinstance(pid_list, list):
for pid in pid_list:
- cmd.append(pid)
+ cmd.append(str(pid))
if config.args.container:
config.container.run(cmd)
if re.ret == 0 and \
'neither blacklisted nor whitelisted' in re.stderr:
res = re.stdout
+ elif re.ret == 0 and 'ERROR: can\'t get' in re.stderr:
+ res = re.stdout
else:
raise(re)
cmd.append(cmdline)
elif isinstance(cmdline, list):
for entry in cmdline:
- cmd.append(entry)
+ cmd.append(str(entry))
if cghelp:
cmd.append('-h')
cmd = ['cat', proc_file]
if config.args.container:
- return config.container.run(cmd, shell_bool=True)
+ pids = config.container.run(cmd, shell_bool=True)
else:
- return Run.run(cmd, shell_bool=True)
+ pids = Run.run(cmd, shell_bool=True)
+
+ pid_list = list()
+ for pid in pids.splitlines():
+ pid_list.append(int(pid))
+
+ return pid_list
return None
pid = Run.run(cmd, shell_bool=True)
for _pid in pid.splitlines():
- self.children_pids.append(_pid)
+ self.children_pids.append(int(_pid))
if pid.find('\n') >= 0:
# The second pid in the list contains the actual perl process
''.format(pid)
)
- return pid
+ return int(pid)
def create_process(self, config):
# To allow for multiple processes to be created, each new process
raise ValueError("get_cgroup() shouldn't reach this point")
+ @staticmethod
+ def kill(config, pids):
+ if not pids:
+ return
+
+ if type(pids) == str:
+ pids = [int(pid) for pid in pids.splitlines()]
+ elif type(pids) == int:
+ pids = [pids]
+
+ for pid in pids:
+ if config.args.container:
+ config.container.run(['kill', '-9', str(pid)])
+ else:
+ Run.run(['kill', '-9', str(pid)])
+
# vim: set et ts=4 sw=4: