diff options
author | Dhaval Giani <dhaval@linux.vnet.ibm.com> | 2008-07-01 13:51:52 +0000 |
---|---|---|
committer | Dhaval Giani <dhaval@linux.vnet.ibm.com> | 2008-07-01 13:51:52 +0000 |
commit | 952183580cc874db99230534f7e38b75f57f3d88 (patch) | |
tree | fa55c60b047c678d091ee75df9e421e90286b205 | |
parent | 2fd38347726e379945d5506136f7e1fe656bcde7 (diff) | |
download | libcg-952183580cc874db99230534f7e38b75f57f3d88.tar.gz libcg-952183580cc874db99230534f7e38b75f57f3d88.tar.xz libcg-952183580cc874db99230534f7e38b75f57f3d88.zip |
From: Sudhir Kumar <skumar@linux.vnet.ibm.com>
libcgroup: libcgroup testcases for cgroup_attach_task()
This patch adds testcases for libcgroup API cgroup_create_cgroup() and
cgroup_delete_cgroup() testing. The APIs are called under the same but two
conditions(FS_MOUNTED=0/1) and return values are checked. The cgroup is
created and freed using the wrapper APIs.
Testcases for FS_MOUNTED=2 have to be added.
Signed-off-by: Sudhir Kumar <skumar@linux.vnet.ibm.com>
Signed-off-by: Dhaval Giani <dhaval@linux.vnet.ibm.com>
git-svn-id: https://libcg.svn.sourceforge.net/svnroot/libcg/trunk@90 4f4bb910-9a46-0410-90c8-c897d4f1cd53
-rw-r--r-- | tests/libcgrouptest.h | 15 | ||||
-rw-r--r-- | tests/libcgrouptest01.c | 186 |
2 files changed, 199 insertions, 2 deletions
diff --git a/tests/libcgrouptest.h b/tests/libcgrouptest.h index b35c7a9..9d3da5d 100644 --- a/tests/libcgrouptest.h +++ b/tests/libcgrouptest.h @@ -18,6 +18,8 @@ #ifndef __LIBCGROUPTEST_H #define __LIBCGROUPTEST_H +#include <errno.h> +#include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -25,12 +27,25 @@ #include <libcgroup.h> +int cpu = 0, memory = 0; + enum cgroup_mount_t { FS_NOT_MOUNTED, FS_MOUNTED, FS_MULTI_MOUNTED, }; +enum controller_t { + MEMORY, + CPU, + /* Add new controllers here */ +}; + +void get_controllers(char *name, int *exist); +static int group_exist(char *path_group); +static int set_controller(int controller, char *controller_name, + char *control_file, char *control_val, char *value); + static inline pid_t cgrouptest_gettid() { return syscall(__NR_gettid); diff --git a/tests/libcgrouptest01.c b/tests/libcgrouptest01.c index 394c883..0c4d20b 100644 --- a/tests/libcgrouptest01.c +++ b/tests/libcgrouptest01.c @@ -20,9 +20,12 @@ int main(int argc, char *argv[]) { int fs_mounted, retval, i = 0, pass = 0; pid_t curr_tid, tid; - struct cgroup *nullcgroup = NULL; + struct cgroup *cgroup1, *nullcgroup = NULL; + struct cgroup_controller *controller1; + char controller_name[FILENAME_MAX], control_file[FILENAME_MAX], + control_val[FILENAME_MAX], path_group[FILENAME_MAX]; FILE *file; - char mountpoint[FILENAME_MAX], tasksfile[FILENAME_MAX]; + char mountpoint[FILENAME_MAX], tasksfile[FILENAME_MAX], group[FILENAME_MAX]; if ((argc < 3) || (atoi(argv[1]) < 0)) { printf("ERROR: Wrong no of parameters recieved from script\n"); @@ -35,6 +38,17 @@ int main(int argc, char *argv[]) dbg("C:DBG: mountpoint as recieved from script=%s\n", mountpoint); /* + * get the list of supported controllers + */ + get_controllers("cpu", &cpu); + get_controllers("memory", &memory); + if (cpu == 0 && memory == 0) { + fprintf(stderr, "controllers are not enabled in kernel\n"); + fprintf(stderr, "Exiting the libcgroup testcases......\n"); + exit(1); + } + + /* * Testsets: Testcases are broadly devided into 3 categories based on * filesystem(fs) mount scenario. fs not mounted, fs mounted, fs multi * mounted. Call different apis in these different scenarios. @@ -69,6 +83,57 @@ int main(int argc, char *argv[]) printf("Test[0:%2d]\tFAIL: cgroup_attach_task() ret: %d\n",\ ++i, retval); + /* + * Test03: Create a valid cgroup and check all return values + * Exp outcome: no error. 0 return value + */ + + strncpy(group, "group1", sizeof(group)); + retval = set_controller(MEMORY, controller_name, + control_file, control_val, "40960000"); + if (retval) + fprintf(stderr, "Setting controller failled\n"); + + cgroup1 = cgroup_new_cgroup(group, 0, 0, 0, 0); + if (cgroup1) { + controller1 = cgroup_add_controller(cgroup1, controller_name); + if (controller1) { + retval = cgroup_add_value_string(controller1, + control_file, control_val); + if (!retval) + printf("Test[0:%2d]\tPASS: cgroup_new_cgroup() success\n", ++i); + else + printf("Test[0:%2d]\tFAIL: cgroup_add_value_string()\n", ++i); + } + else + printf("Test[0:%2d]\tFAIL: cgroup_add_controller()\n", ++i); + } + else + printf("Test[0:%2d]\tFAIL: cgroup_new_cgroup() fails\n", ++i); + + /* + * Test04: Then Call cgroup_create_cgroup() with this valid group + * Exp outcome: non zero return value + */ + retval = cgroup_create_cgroup(cgroup1, 1); + if (retval) + printf("Test[0:%2d]\tPASS: cgroup_create_cgroup() retval=%d\n", ++i, retval); + else + printf("Test[0:%2d]\tFAIL: cgroup_create_cgroup() retval=%d\n", ++i, retval); + + /* + * Test05: delete cgroup + * Exp outcome: non zero return value but what ? + */ + retval = cgroup_delete_cgroup(cgroup1, 1); + if (retval) + printf("Test[0:%2d]\tPASS: cgroup_delete_cgroup() retval=%d\n", ++i, retval); + else + printf("Test[0:%2d]\tFAIL: cgroup_delete_cgroup() retval=%d\n", ++i, retval); + + cgroup_free(&nullcgroup); + cgroup_free(&cgroup1); + break; case FS_MOUNTED: @@ -149,6 +214,66 @@ int main(int argc, char *argv[]) printf("Test[1:%2d]\tFAIL: cgroup_attach_task_pid() ret: %d\n",\ ++i, retval); + /* + * Test05: Create a valid cgroup structure + * Exp outcome: no error. 0 return value + */ + strncpy(group, "group1", sizeof(group)); + retval = set_controller(MEMORY, controller_name, + control_file, control_val, "40960000"); + if (retval) + fprintf(stderr, "Setting controller failled\n"); + + cgroup1 = cgroup_new_cgroup(group, 0, 0, 0, 0); + if (cgroup1) { + controller1 = cgroup_add_controller(cgroup1, controller_name); + if (controller1) { + retval = cgroup_add_value_string(controller1, + control_file, control_val); + if (!retval) + printf("Test[1:%2d]\tPASS: cgroup_new_cgroup() success\n", ++i); + else + printf("Test[1:%2d]\tFAIL: cgroup_add_value_string()\n", ++i); + } + else + printf("Test[1:%2d]\tFAIL: cgroup_add_controller()\n", ++i); + } + else + printf("Test[1:%2d]\tFAIL: cgroup_new_cgroup() fails\n", ++i); + + /* + * Test06: Then Call cgroup_create_cgroup() with this valid group + * Exp outcome: zero return value + */ + retval = cgroup_create_cgroup(cgroup1, 1); + if (!retval) { + /* Check if the group exists in the dir tree */ + strncpy(path_group, mountpoint, sizeof(mountpoint)); + strncat(path_group, "/group1", sizeof("group1")); + if (group_exist(path_group) == 0) + printf("Test[1:%2d]\tPASS: cgroup_create_cgroup() retval=%d\n", + ++i, retval); + } else + printf("Test[1:%2d]\tFAIL: cgroup_create_cgroup() retval=%d\n", ++i, retval); + + /* + * Test07: delete cgroup + * Exp outcome: zero return value + */ + retval = cgroup_delete_cgroup(cgroup1, 1); + if (!retval) { + /* Check if the group is deleted from the dir tree */ + strncpy(path_group, mountpoint, sizeof(mountpoint)); + strncat(path_group, "/group1", sizeof("group1")); + if (group_exist(path_group) == -1) + printf("Test[1:%2d]\tPASS: cgroup_delete_cgroup() retval=%d\n", + ++i, retval); + } else + printf("Test[1:%2d]\tFAIL: cgroup_delete_cgroup() retval=%d\n", ++i, retval); + + cgroup_free(&nullcgroup); + cgroup_free(&cgroup1); + break; case FS_MULTI_MOUNTED: @@ -183,3 +308,60 @@ int main(int argc, char *argv[]) } return 0; } + +void get_controllers(char *name, int *exist) +{ + int hierarchy, num_cgroups, enabled; + FILE *fd; + char subsys_name[FILENAME_MAX]; + fd = fopen("/proc/cgroups", "r"); + if (!fd) + return; + + while (!feof(fd)) { + fscanf(fd, "%s, %d, %d, %d", subsys_name, + &hierarchy, &num_cgroups, &enabled); + if (strncmp(name, subsys_name, sizeof(*name)) == 0) + *exist = 1; + } +} + +static int group_exist(char *path_group) +{ + int ret; + ret = open(path_group, O_DIRECTORY); + if (ret == -1) + return ret; + return 0; +} + +static int set_controller(int controller, char *controller_name, + char *control_file, char *control_val, char *value) +{ + switch (controller) { + case MEMORY: + if (memory == 0) + return 1; + + strncpy(controller_name, "memory", FILENAME_MAX); + strncpy(control_file, "memory.limit_in_bytes", FILENAME_MAX); + strncpy(control_val, value, FILENAME_MAX); + return 0; + break; + + case CPU: + if (cpu == 0) + return 1; + + strncpy(controller_name, "cpu", FILENAME_MAX); + strncpy(control_file, "cpu.shares", FILENAME_MAX); + strncpy(control_val, value, FILENAME_MAX); + return 0; + break; + /* Future controllers can be added here */ + + default: + return 1; + break; + } +} |