]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
From: Sudhir Kumar <skumar@linux.vnet.ibm.com>
authorDhaval Giani <dhaval@linux.vnet.ibm.com>
Tue, 1 Jul 2008 13:51:52 +0000 (13:51 +0000)
committerDhaval Giani <dhaval@linux.vnet.ibm.com>
Tue, 1 Jul 2008 13:51:52 +0000 (13:51 +0000)
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

tests/libcgrouptest.h
tests/libcgrouptest01.c

index b35c7a9b209b43ff1d59a41d6d98a32d73ed0fb8..9d3da5dea1b8eda5cca88b5b27084aa10c1c4a69 100644 (file)
@@ -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>
 
 #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);
index 394c883b2d52eac6dec5aff4dbfb40f075add439..0c4d20bab658dec9ea185eeb2a9b0d5a436c1893 100644 (file)
@@ -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");
@@ -34,6 +37,17 @@ int main(int argc, char *argv[])
        dbg("C:DBG: fs_mounted as recieved from script=%d\n", fs_mounted);
        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
@@ -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;
+       }
+}