#
# Cgroup class for the libcgroup functional tests
#
-# Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
+# Copyright (c) 2019-2021 Oracle and/or its affiliates.
# Author: Tom Hromatka <tom.hromatka@oracle.com>
#
return True
@staticmethod
- def build_cmd_path(in_container, cmd):
- if in_container:
- return os.path.join(consts.LIBCG_MOUNT_POINT,
- 'src/tools/{}'.format(cmd))
- else:
- return cmd
+ def build_cmd_path(cmd):
+ return os.path.join(consts.LIBCG_MOUNT_POINT,
+ 'src/tools/{}'.format(cmd))
# TODO - add support for all of the cgcreate options
@staticmethod
- def create(config, controller_list, cgname, in_container=True):
+ def create(config, controller_list, cgname):
if isinstance(controller_list, str):
controller_list = [controller_list]
cmd = list()
- cmd.append(Cgroup.build_cmd_path(in_container, 'cgcreate'))
+ cmd.append(Cgroup.build_cmd_path('cgcreate'))
controllers_and_path = '{}:{}'.format(
','.join(controller_list), cgname)
cmd.append('-g')
cmd.append(controllers_and_path)
- if in_container:
+ if config.args.container:
config.container.run(cmd)
else:
Run.run(cmd)
@staticmethod
- def delete(config, controller_list, cgname, in_container=True, recursive=False):
+ def delete(config, controller_list, cgname, recursive=False):
if isinstance(controller_list, str):
controller_list = [controller_list]
cmd = list()
- cmd.append(Cgroup.build_cmd_path(in_container, 'cgdelete'))
+ cmd.append(Cgroup.build_cmd_path('cgdelete'))
if recursive:
cmd.append('-r')
cmd.append('-g')
cmd.append(controllers_and_path)
- if in_container:
+ if config.args.container:
config.container.run(cmd)
else:
Run.run(cmd)
@staticmethod
- def set(config, cgname, setting, value, in_container=True):
+ def set(config, cgname, setting, value):
cmd = list()
- cmd.append(Cgroup.build_cmd_path(in_container, 'cgset'))
+ cmd.append(Cgroup.build_cmd_path('cgset'))
if isinstance(setting, str) and isinstance(value, str):
cmd.append('-r')
cmd.append(cgname)
- if in_container:
+ if config.args.container:
config.container.run(cmd)
else:
Run.run(cmd)
# Read all of the settings from a cgroup at a specific path
# cgget -g memory:tomcgroup/tomcgroup
def get(config, controller=None, cgname=None, setting=None,
- in_container=True, print_headers=True, values_only=False,
+ print_headers=True, values_only=False,
all_controllers=False):
cmd = list()
- cmd.append(Cgroup.build_cmd_path(in_container, 'cgget'))
+ cmd.append(Cgroup.build_cmd_path('cgget'))
if not print_headers:
cmd.append('-n')
for cg in cgname:
cmd.append(cg)
- if in_container:
+ if config.args.container:
ret = config.container.run(cmd)
else:
ret = Run.run(cmd)
@staticmethod
def classify(config, controller, cgname, pid_list, sticky=False,
- cancel_sticky=False, in_container=True):
+ cancel_sticky=False):
cmd = list()
- cmd.append(Cgroup.build_cmd_path(in_container, 'cgclassify'))
+ cmd.append(Cgroup.build_cmd_path('cgclassify'))
cmd.append('-g')
cmd.append('{}:{}'.format(controller, cgname))
for pid in pid_list:
cmd.append(pid)
- if in_container:
+ if config.args.container:
config.container.run(cmd)
else:
Run.run(cmd)
return cgdict
@staticmethod
- def snapshot(config, controller=None, in_container=True):
+ def snapshot(config, controller=None):
cmd = list()
- cmd.append(Cgroup.build_cmd_path(in_container, 'cgsnapshot'))
+ cmd.append(Cgroup.build_cmd_path('cgsnapshot'))
if controller is not None:
cmd.append(controller)
- if in_container:
+ if config.args.container:
# if we're running in a container, it's unlikely that libcgroup
# was installed and thus /etc/cgsnapshot_blacklist.conf likely
# doesn't exist. Let's make it
config.container.run(['sudo', 'touch', '/etc/cgsnapshot_blacklist.conf'])
- if in_container:
+ if config.args.container:
res = config.container.run(cmd)
else:
res = Run.run(cmd)
#
# Config class for the libcgroup functional tests
#
-# Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
+# Copyright (c) 2019-2021 Oracle and/or its affiliates.
# Author: Tom Hromatka <tom.hromatka@oracle.com>
#
def __init__(self, args, container=None):
self.args = args
- if container:
- self.container = container
- else:
- # Use the default container settings
- self.container = Container(name=consts.DEFAULT_CONTAINER_NAME,
- stop_timeout=args.timeout, arch=None,
- distro=args.distro, release=args.release)
+ if self.args.container:
+ if container:
+ self.container = container
+ else:
+ # Use the default container settings
+ self.container = Container(name=consts.DEFAULT_CONTAINER_NAME,
+ stop_timeout=args.timeout, arch=None,
+ distro=args.distro, release=args.release)
self.ftest_dir = os.path.dirname(os.path.abspath(__file__))
self.libcg_dir = os.path.dirname(self.ftest_dir)
def __str__(self):
out_str = "Configuration"
- out_str += "\n\tcontainer = {}".format(self.container)
+ if self.args.container:
+ out_str += "\n\tcontainer = {}".format(self.container)
return out_str
#
# Main entry point for the libcgroup functional tests
#
-# Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
+# Copyright (c) 2019-2021 Oracle and/or its affiliates.
# Author: Tom Hromatka <tom.hromatka@oracle.com>
#
parser.add_argument('-s', '--suite',
help='Test suite to run, e.g. cpuset', required=False,
default=consts.TESTS_RUN_ALL_SUITES, type=str)
+
+ container_parser = parser.add_mutually_exclusive_group(required=False)
+ container_parser.add_argument('--container', action='store_true',
+ help='Run the tests in a container. '
+ 'Note that some tests cannot be run in a container.',
+ dest='container')
+ container_parser.add_argument('--no-container', action='store_false',
+ help='Do not run the tests in a container. '
+ 'Note that some tests are destructive and will modify your cgroup hierarchy.',
+ dest='container')
+ parser.set_defaults(container=True)
+
parser.add_argument('-v', '--verbose',
help='Print all information about this test run',
default=True, required=False, action="store_false")
# log but ignore all exceptions
Log.log_debug(e)
- # this command initializes the lxd storage, networking, etc.
- Run.run(['sudo', 'lxd', 'init', '--auto'])
- update_host_subuid()
- update_host_subgid()
+ if config.args.container:
+ # this command initializes the lxd storage, networking, etc.
+ Run.run(['sudo', 'lxd', 'init', '--auto'])
+ update_host_subuid()
+ update_host_subgid()
- config.container.create()
- config.container.config()
- config.container.start()
+ config.container.create()
+ config.container.config()
+ config.container.start()
- # LXC on Ubuntu 20.04 put sed in a different spot. Add a symlink
- config.container.run(['ln', '-s', '/bin/sed', '/usr/bin/sed'])
+ # LXC on Ubuntu 20.04 put sed in a different spot. Add a symlink
+ config.container.run(['ln', '-s', '/bin/sed', '/usr/bin/sed'])
+
+ # add the libcgroup library to the container's ld
+ echo_cmd = ['bash', '-c', 'echo {} >> /etc/ld.so.conf.d/libcgroup.conf'.format(
+ os.path.join(consts.LIBCG_MOUNT_POINT, 'src/.libs'))]
+ config.container.run(echo_cmd)
+ config.container.run('ldconfig')
- # add the libcgroup library to the container's ld
- echo_cmd = ['bash', '-c', 'echo {} >> /etc/ld.so.conf.d/libcgroup.conf'.format(
- os.path.join(consts.LIBCG_MOUNT_POINT, 'src/.libs'))]
- config.container.run(echo_cmd)
- config.container.run('ldconfig')
if record_time:
setup_time = time.time() - start_time
Process.join_children()
- try:
- config.container.stop()
- except Exception as e:
- # log but ignore all exceptions
- Log.log_debug(e)
- try:
- config.container.delete()
- except Exception as e:
- # log but ignore all exceptions
- Log.log_debug(e)
+ if config.args.container:
+ try:
+ config.container.stop()
+ except Exception as e:
+ # log but ignore all exceptions
+ Log.log_debug(e)
+ try:
+ config.container.delete()
+ except Exception as e:
+ # log but ignore all exceptions
+ Log.log_debug(e)
if record_time:
teardown_time = time.time() - start_time
#
# Cgroup class for the libcgroup functional tests
#
-# Copyright (c) 2020 Oracle and/or its affiliates.
+# Copyright (c) 2020-2021 Oracle and/or its affiliates.
# Author: Tom Hromatka <tom.hromatka@oracle.com>
#
class Process(object):
@staticmethod
- def __infinite_loop(config, sleep_time=1, in_container=True):
+ def __infinite_loop(config, sleep_time=1):
cmd = ['nohup', 'perl', '-e', '\'while(1){{sleep({})}};\''.format(sleep_time), '&']
- if in_container:
+ if config.args.container:
config.container.run(cmd, shell_bool=True)
else:
Run.run(cmd, shell_bool=True)
@staticmethod
- def create_process(config, in_container=True):
+ def create_process(config):
# To allow for multiple processes to be created, each new process
# sleeps for a different amount of time. This lets us uniquely find
# each process later in this function
# get the PID of the newly spawned infinite loop
cmd = 'ps x | grep perl | grep "sleep({})" | awk \'{{print $1}}\''.format(sleep_time)
- if in_container:
+ if config.args.container:
pid = config.container.run(cmd, shell_bool=True)
else:
pid = Run.run(cmd, shell_bool=True)
# Create a simple process in the requested cgroup
@staticmethod
- def create_process_in_cgroup(config, controller, cgname, in_container=True):
- child_pid = Process.create_process(config, in_container=in_container)
- Cgroup.classify(config, controller, cgname, child_pid,
- in_container=in_container)
+ def create_process_in_cgroup(config, controller, cgname):
+ child_pid = Process.create_process(config)
+ Cgroup.classify(config, controller, cgname, child_pid)
# The caller will block until all children are stopped.
@staticmethod