def test(self):
        """
        Start testing
        """
        controller_name = 'devices'
        controller_list = []
        controller_list.append(controller_name)
        cgroup_name = "test"
        property_value_before = {"devices.deny": "a 8:0 r"}
        property_value_after = {"devices.allow": "a 8:0 r"}
        # Must belong to "8:0"(sda)
        tmp_file = tempfile.NamedTemporaryFile(dir="/").name
        file_size = "20M"
        try:
            if os.path.exists(tmp_file):
                os.remove(tmp_file)
            utils_cgroup.all_cgroup_delete()
            modules = utils_cgroup.CgroupModules(self.cgroup_dir)
            modules.init(controller_list)
            cgroup = utils_cgroup.Cgroup(controller_name, None)
            cgroup.initialize(modules)
            cgroup.cgdelete_all_cgroups()

            # Create cgroup
            cgroup_index = cgroup.mk_cgroup(cgroup=cgroup_name)

            # Set property_before value
            for pro in property_value_before:
                cgroup.set_property(pro, property_value_before.get(pro),
                                    cgroup_index, check=False)

            # Execute dd command
            cmd_args = []
            cmd_args.append("if=/dev/zero")
            cmd_args.append("of=%s" % tmp_file)
            cmd_args.append("bs=%s" % file_size)
            cmd_args.append("count=1")
            status_before, _ = cgroup.cgexec(cgroup_name, "dd", cmd_args)
            if not status_before or os.path.exists(tmp_file):
                raise error.TestFail("File should not be created!")

            # Set property_after value
            for pro in property_value_after:
                cgroup.set_property(pro, property_value_after.get(pro),
                                    cgroup_index, check=False)
            status_after, _ = cgroup.cgexec(cgroup_name, "dd", cmd_args)
            if status_after and not os.path.exists(tmp_file):
                raise error.TestFail("File created failed!")
        finally:
            if "cgroup" in dir():
                del cgroup
            if "modules" in dir():
                del modules
            # Recover environment
            if os.path.exists(tmp_file):
                os.remove(tmp_file)
            utils_cgroup.cgconfig_restart()
    def test(self):
        """
        Start testing
        """
        controller_name = 'cpu,cpuset'
        controller_list = controller_name.split(",")
        cgroup_name1 = "test1"
        cgroup_name2 = "test2"
        cgconfig_file = "/etc/cgconfig.conf"
        property_value1 = {
            "cpu.shares": "500",
            "cpuset.cpus": "1",
            "cpuset.mems": "0"
        }
        property_value2 = {
            "cpu.shares": "1000",
            "cpuset.cpus": "1",
            "cpuset.mems": "0"
        }
        tmp_file1 = tempfile.NamedTemporaryFile(dir=self.tmpdir).name + ".sh"
        tmp_file2 = tempfile.NamedTemporaryFile(dir=self.tmpdir).name + ".sh"
        cpu_use_flle(tmp_file1)
        cpu_use_flle(tmp_file2)
        backup_cmd = "mv -f %s %s.bak" % (cgconfig_file, cgconfig_file)
        recover_cmd = "mv -f %s.bak %s" % (cgconfig_file, cgconfig_file)
        if utils.run(backup_cmd, ignore_status=True).exit_status != 0:
            raise error.TestNAError("Backup cgconfig file failed!")
        try:
            utils_cgroup.all_cgroup_delete()
            modules = utils_cgroup.CgroupModules(self.cgroup_dir)
            modules.init([controller_name])
            cgroup = utils_cgroup.Cgroup(controller_name, None)
            cgroup.initialize(modules)
            cgroup.cgdelete_all_cgroups()

            dic_cgroup_property = {}
            dic_cgroup_property[cgroup_name1] = mk_dic_property(
                controller_list, property_value1)
            dic_cgroup_property[cgroup_name2] = mk_dic_property(
                controller_list, property_value2)
            cgconfig_file_modify(cgconfig_file, controller_list,
                                 dic_cgroup_property, cgroup)

            utils_cgroup.cgconfig_restart()
            # After cgconfig restart, there are some cgroups created automatically
            cgroup.refresh_cgroups()
            cgroup_index1 = cgroup.get_cgroup_index(cgroup=cgroup_name1)
            cgroup_index2 = cgroup.get_cgroup_index(cgroup=cgroup_name2)
            threads = []
            cgroup_name = []
            cgroup_name.append(cgroup_name1)
            cgroup_name.append(cgroup_name2)
            sh_path = []
            sh_path.append(tmp_file1)
            sh_path.append(tmp_file2)
            for i in range(0, 2):
                thd = threading.Thread(target=cgroup.cgexec,
                                       args=(cgroup_name[i], sh_path[i]))
                threads.append(thd)
            # Start process
            for i in range(0, 2):
                threads[i].start()
            time.sleep(3)
            pid_list1 = cgroup.get_pids(cgroup_index1)
            pid_list2 = cgroup.get_pids(cgroup_index2)
            pid_rate1 = get_cpu_rate(pid_list1)
            pid_rate2 = get_cpu_rate(pid_list2)
            kill_pids(pid_list1)
            kill_pids(pid_list2)
            del cgroup
            del modules
            if pid_rate1.get(pid_list1[-1]) <= pid_rate2.get(pid_list1[-1]):
                raise error.TestFail("cpu rate test pid rate failed!")
        finally:
            # Recover environment
            utils.run(recover_cmd)
            utils_cgroup.cgconfig_restart()
    def test(self):
        """
        Start testing
        """
        controller_name = 'blkio'
        controller_list = []
        controller_list.append(controller_name)
        # cmd: cgcreate. dir: mkdir
        create_mode_list = ["cmd", "dir"]
        # file: file write directly. cgset: cgset command
        set_mode_list = ["file", "cgset"]
        cgroup_name = "test"
        property_value = {
            'blkio.throttle.read_bps_device': '8:0  1048576',
            'blkio.throttle.write_bps_device': '8:0  524288'
        }
        # Must belong to "8:0"(sda)
        tmp_file = tempfile.NamedTemporaryFile(dir="/").name
        file_size = "20M"
        try:
            for item in itertools.product(create_mode_list, set_mode_list):
                if os.path.exists(tmp_file):
                    os.remove(tmp_file)
                utils_cgroup.all_cgroup_delete()
                modules = utils_cgroup.CgroupModules(self.cgroup_dir)
                modules.init(controller_list)
                cgroup = utils_cgroup.Cgroup(controller_name, None)
                cgroup.initialize(modules)
                cgroup.cgdelete_all_cgroups()

                cgroup_create_mode = item[0]
                property_set_mode = item[1]
                # Create cgroup
                if cgroup_create_mode == "dir":
                    cgroup_index = cgroup.mk_cgroup(cgroup=cgroup_name)
                elif cgroup_create_mode == "cmd":
                    cgroup_index = cgroup.mk_cgroup_cgcreate(
                        cgroup=cgroup_name)

                # Set property value
                if property_set_mode == "cgset":
                    for pro in property_value:
                        cgroup.cgset_property(pro,
                                              property_value.get(pro),
                                              cgroup_index,
                                              check=False)
                elif property_set_mode == "file":
                    for pro in property_value:
                        cgroup.set_property(pro,
                                            property_value.get(pro),
                                            cgroup_index,
                                            check=False)

                # Execute dd command
                cmd_args1 = []
                cmd_args1.append("if=/dev/zero")
                cmd_args1.append("of=%s" % tmp_file)
                cmd_args1.append("bs=%s" % file_size)
                cmd_args1.append("count=1")
                cmd_args1.append("oflag=direct")
                cmd_args2 = []
                cmd_args2.append("if=%s" % tmp_file)
                cmd_args2.append("of=/dev/null")
                cmd_args2.append("bs=%s" % file_size)
                cmd_args2.append("count=1")
                cmd_args2.append("iflag=direct")
                status_write, output_write = cgroup.cgexec(
                    cgroup_name, "dd", cmd_args1)
                if status_write and not os.path.exists(tmp_file):
                    raise error.TestError("File wirte failed!")
                rate_write = self.get_create_rate(output_write)
                _, output_read = cgroup.cgexec(cgroup_name, "dd", cmd_args2)
                if status_write:
                    raise error.TestError("File read failed!")
                rate_read = self.get_create_rate(output_read)
                del cgroup
                del modules
                if rate_write is None or rate_read is None:
                    raise error.TestFail("Get file write/read rate failed!")
                if (rate_write < (512.0 - 20)) or (rate_write > (512.0 + 20)):
                    raise error.TestFail("File create rate test failed!"
                                         "\nrate_write = %s" % rate_write)
                if (rate_read < 0.9) or (rate_read > 1.1):
                    raise error.TestFail("File create rate test failed!"
                                         "\nrate_read = %s" % rate_read)
        finally:
            # Recover environment
            if os.path.exists(tmp_file):
                os.remove(tmp_file)
            utils_cgroup.cgconfig_restart()
Example #4
0
    def test(self):
        """
        Start testing
        """
        controller_name = 'memory'
        controller_list = [controller_name]
        cgroup_name1 = "test1"
        cgroup_name2 = "test2"
        memory_use = 60  # M
        test_memory1 = memory_use + 10  # M
        test_memory2 = memory_use - 10  # M
        property_values1 = {
            'memory.move_charge_at_immigrate': '1',
            'memory.limit_in_bytes': '%dM' % test_memory1,
            'memory.memsw.limit_in_bytes': '%dM' % test_memory1,
            'memory.swappiness': '0'
        }
        property_values2 = {
            'memory.move_charge_at_immigrate': '1',
            'memory.limit_in_bytes': '%dM' % test_memory2,
            'memory.memsw.limit_in_bytes': '%dM' % test_memory2,
            'memory.swappiness': '0'
        }
        get_property_list = [
            'memory.limit_in_bytes', 'memory.max_usage_in_bytes',
            'memory.memsw.usage_in_bytes', 'memory.memsw.max_usage_in_bytes'
        ]
        memory_file = os.path.join(self.bindir, "memory_use.c")
        binary_file = os.path.join(self.tmpdir, "memory_use.o")

        def get_property_dict(cgroup_index, get_property_list):
            """
            Get all property value in desired cgroup

            @param: cgroup_index: Desired cgroup index
            @param: get_property_list: Property list
            @return property dict:{property1:value1, property2,value2}
            """
            output_property_dic = {}
            for pro in get_property_list:
                output = cgroup.get_property(pro, cgroup_index)
                output_property_dic[pro] = output[0]
            return output_property_dic

        try:
            # Apply for memory
            pid = execute_stresser(memory_use, memory_file, binary_file)
            utils_cgroup.all_cgroup_delete()
            modules = utils_cgroup.CgroupModules(self.cgroup_dir)
            modules.init(controller_list)
            cgroup = utils_cgroup.Cgroup(controller_name, None)
            cgroup.initialize(modules)
            cgroup.cgdelete_all_cgroups()

            # Create cgroup
            cgroup_index1 = cgroup.mk_cgroup(cgroup=cgroup_name1)
            cgroup_index2 = cgroup.mk_cgroup(cgroup=cgroup_name2)

            # Set property value
            # 'memory.limit_in_bytes' must be set first, if not,
            # 'memory.memsw.limit_in_bytes' will fail
            _pro = 'memory.limit_in_bytes'
            cgroup.cgset_property(_pro,
                                  property_values1.get(_pro),
                                  cgroup_index1,
                                  check=False)
            for property, value in property_values1.iteritems():
                cgroup.cgset_property(property,
                                      value,
                                      cgroup_index1,
                                      check=False)
            cgroup.cgset_property(_pro,
                                  property_values2.get(_pro),
                                  cgroup_index2,
                                  check=False)
            for property, value in property_values2.iteritems():
                cgroup.cgset_property(property,
                                      value,
                                      cgroup_index2,
                                      check=False)

            # Classify pid to cgroup_name1
            cgroup.cgclassify_cgroup(pid, cgroup_name1)

            # Apply enough time to get memory use
            time.sleep(3)
            all_property_dict = {}
            property_dict1 = get_property_dict(cgroup_index1,
                                               get_property_list)
            all_property_dict[cgroup_name1] = property_dict1
            # Kill process to free memory
            if os.path.exists("/proc/%d/stat" % pid):
                logging.debug("Kill process %d to free memory" % pid)
                os.kill(pid, 9)
            pid = execute_stresser(memory_use, memory_file, binary_file)
            # Classify pid to cgroup_name2
            cgroup.cgclassify_cgroup(pid, cgroup_name2)

            # Apply enough time to get memory use
            time.sleep(3)
            property_dict2 = get_property_dict(cgroup_index2,
                                               get_property_list)
            all_property_dict[cgroup_name2] = property_dict2
            if os.path.exists("/proc/%d/stat" % pid):
                logging.debug("Kill process %d to free memory" % pid)
                os.kill(pid, 0)
            # Check output
            for sub_pro in all_property_dict:
                property_check(all_property_dict.get(sub_pro), memory_use)
        finally:
            # Recover environment
            if "modules" in dir():
                del modules
            if "pid" in dir():
                if os.path.exists("/proc/%d/stat" % pid):
                    os.kill(pid, 9)
            utils_cgroup.cgconfig_restart()
    def test(self):
        """
        Start testing
        """
        controller_name = 'freezer'
        cgroup_name = "test"
        property_value_frozen = {'freezer.state': 'FROZEN'}
        property_value_thawed = {'freezer.state': 'THAWED'}
        tmp_file = tempfile.NamedTemporaryFile(dir=self.tmpdir).name
        shell_file = tmp_file + ".sh"
        pid = stress_process(shell_file)
        try:
            if os.path.exists(tmp_file):
                os.remove(tmp_file)
            utils_cgroup.all_cgroup_delete()
            modules = utils_cgroup.CgroupModules(self.cgroup_dir)
            modules.init([controller_name])
            cgroup = utils_cgroup.Cgroup(controller_name, None)
            cgroup.initialize(modules)
            cgroup.cgdelete_all_cgroups()

            # Create cgroup
            cgroup_index = cgroup.mk_cgroup_cgcreate(cgroup=cgroup_name)

            # Classify pid to cgroup_name
            cgroup.cgclassify_cgroup(pid, cgroup_name)
            if str(pid) not in cgroup.get_pids(cgroup_index):
                raise error.TestFail("Classify pid '%d' into %s failed" %
                                     (pid, cgroup_name))
            pid_state = get_pid_state([pid])
            # Defaultly freezer.state is "THAWED",
            # process should be running normally
            if pid_state.get(pid).strip() != RUNNING:
                raise error.TestFail("It should be running!")

            # Set property value to frozen process
            for pro in property_value_frozen:
                cgroup.cgset_property(pro,
                                      property_value_frozen.get(pro),
                                      cgroup_index,
                                      check=False)
            pid_state_frozen = get_pid_state([pid])
            if pid_state_frozen.get(pid).strip() != SLEEPING:
                # If freezer.state isn't set to 'THAWED',
                # pid cannot be killed.
                for pro in property_value_thawed:
                    cgroup.cgset_property(pro,
                                          property_value_thawed.get(pro),
                                          cgroup_index,
                                          check=False)
                raise error.TestFail("It should be sleeping!")

            # Set property value to thawed process
            for pro in property_value_thawed:
                cgroup.cgset_property(pro,
                                      property_value_thawed.get(pro),
                                      cgroup_index,
                                      check=False)
            pid_state_thawed = get_pid_state([pid])
            if pid_state_thawed.get(pid).strip() != RUNNING:
                raise error.TestFail("It should be running!")
        finally:
            # Recover environment
            if "cgroup" in dir():
                del cgroup
            if "modules" in dir():
                del modules
            os.kill(pid, signal.SIGUSR1)
            utils_cgroup.cgconfig_restart()
    def test(self):
        """
        Start testing
        """
        controller_name = 'memory'
        controller_list = []
        controller_list.append(controller_name)
        # cmd: cgcreate. dir: mkdir
        create_mode_list = ["cmd", "dir"]
        # file: file write directly. cgset: cgset command
        set_mode_list = ["file", "cgset"]
        cgroup_name1 = "test1"
        cgroup_name2 = "test2"
        property_value1 = {'memory.move_charge_at_immigrate': '0'}
        property_value2 = {'memory.move_charge_at_immigrate': '1'}
        get_property = "memory.usage_in_bytes"
        memory_use = 10  # M
        tmp_file = tempfile.NamedTemporaryFile(dir=self.tmpdir).name
        memory_file = tmp_file + ".c"
        binary_file = tmp_file + ".o"
        # Apply for memory
        pid = self.memory_use_flle(memory_use, memory_file, binary_file)
        try:
            for item in itertools.product(create_mode_list, set_mode_list):
                if os.path.exists(tmp_file):
                    os.remove(tmp_file)
                utils_cgroup.all_cgroup_delete()
                modules = utils_cgroup.CgroupModules(self.cgroup_dir)
                modules.init(controller_list)
                cgroup = utils_cgroup.Cgroup(controller_name, None)
                cgroup.initialize(modules)
                cgroup.cgdelete_all_cgroups()

                cgroup_create_mode = item[0]
                property_set_mode = item[1]
                # Create cgroup
                if cgroup_create_mode == "dir":
                    cgroup_index1 = cgroup.mk_cgroup(cgroup=cgroup_name1)
                    cgroup_index2 = cgroup.mk_cgroup(cgroup=cgroup_name2)
                elif cgroup_create_mode == "cmd":
                    cgroup_index1 = cgroup.mk_cgroup_cgcreate(
                        cgroup=cgroup_name1)
                    cgroup_index2 = cgroup.mk_cgroup_cgcreate(
                        cgroup=cgroup_name2)

                # Set property value
                if property_set_mode == "cgset":
                    for pro in property_value1:
                        cgroup.cgset_property(pro,
                                              property_value1.get(pro),
                                              cgroup_index1,
                                              check=False)
                    for pro in property_value2:
                        cgroup.cgset_property(pro,
                                              property_value2.get(pro),
                                              cgroup_index2,
                                              check=False)
                elif property_set_mode == "file":
                    for pro in property_value1:
                        cgroup.set_property(pro,
                                            property_value1.get(pro),
                                            cgroup_index1,
                                            check=False)
                    for pro in property_value2:
                        cgroup.set_property(pro,
                                            property_value2.get(pro),
                                            cgroup_index2,
                                            check=False)

                # Classify pid to cgroup_name1
                cgroup.cgclassify_cgroup(pid, cgroup_name1)
                if str(pid) not in cgroup.get_pids(cgroup_index1):
                    raise error.TestFail("Classify pid '%d' into %s failed" %
                                         (pid, cgroup_name1))
                # Apply enough time to get memory use
                time.sleep(3)
                output1 = cgroup.get_property(get_property, cgroup_index1)

                # Classify pid to cgroup_name2
                cgroup.cgclassify_cgroup(pid, cgroup_name2)
                if str(pid) not in cgroup.get_pids(cgroup_index2):
                    raise error.TestFail("Classify pid '%d' into %s failed" %
                                         (pid, cgroup_name2))
                # Apply enough time to get memory use
                time.sleep(3)
                output2 = cgroup.get_property(get_property, cgroup_index2)
                del cgroup
                del modules
                if output1[0] != '0':
                    raise error.TestFail(
                        "move_charge_at_immigrate doesn't work"
                        " in %s" % cgroup_name1)
                if output2[0] == '0':
                    raise error.TestFail(
                        "move_charge_at_immigrate doesn't work"
                        " in %s" % cgroup_name2)
        finally:
            # Recover environment
            os.kill(pid, signal.SIGUSR1)
            utils_cgroup.cgconfig_restart()