summaryrefslogtreecommitdiffstats
path: root/tests/libcgrouptest01.c
blob: 0c4d20bab658dec9ea185eeb2a9b0d5a436c1893 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
 * Copyright IBM Corporation. 2008
 *
 * Author:	Sudhir Kumar <skumar@linux.vnet.ibm.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2.1 of the GNU Lesser General Public License
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it would be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * Description: This file contains the test code for testing libcgroup apis.
 */

#include "libcgrouptest.h"

int main(int argc, char *argv[])
{
	int fs_mounted, retval, i = 0, pass = 0;
	pid_t curr_tid, tid;
	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], group[FILENAME_MAX];

	if ((argc < 3) || (atoi(argv[1]) < 0)) {
		printf("ERROR: Wrong no of parameters recieved from script\n");
		printf("Exiting the libcgroup testset\n");
		exit(1);
	}
	fs_mounted = atoi(argv[1]);
	strcpy(mountpoint, argv[2]);
	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
	 * mounted. Call different apis in these different scenarios.
	 */

	switch (fs_mounted) {

	case FS_NOT_MOUNTED:

		/*
		 * Test01: call cgroup_init() and check return values
		 * Exp outcome: error ECGROUPNOTMOUNTED
		 */

		retval = cgroup_init();
		if (retval == ECGROUPNOTMOUNTED)
			printf("Test[0:%2d]\tPASS: cgroup_init() retval= %d:\n",\
								 ++i, retval);
		else
			printf("Test[0:%2d]\tFAIL: cgroup_init() retval= %d:\n",\
								 ++i, retval);

		/*
		 * Test02: call cgroup_attach_task() with null group
		 * Exp outcome: error non zero return value
		 */
		retval = cgroup_attach_task(nullcgroup);
		if (retval != 0)
			printf("Test[0:%2d]\tPASS: cgroup_attach_task() ret: %d\n",\
								 ++i, retval);
		else
			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:

		/*
		 * Test01: call cgroup_attach_task() with null group
		 * without calling cgroup_inti(). We can check other apis too.
		 * Exp outcome: error ECGROUPNOTINITIALIZED
		 */
		retval = cgroup_attach_task(nullcgroup);
		if (retval == ECGROUPNOTINITIALIZED)
			printf("Test[1:%2d]\tPASS: cgroup_attach_task() ret: %d\n",\
								 ++i, retval);
		else
			printf("Test[1:%2d]\tFAIL: cgroup_attach_task() ret: %d\n",\
								 ++i, retval);


		/*
		 * Test02: call cgroup_init() and check return values
		 * Exp outcome:  no error. return value 0
		 */

		retval = cgroup_init();
		if (retval == 0)
			printf("Test[1:%2d]\tPASS: cgroup_init() retval= %d:\n",\
								 ++i, retval);
		else
			printf("Test[1:%2d]\tFAIL: cgroup_init() retval= %d:\n",\
								 ++i, retval);

		/*
		 * Test03: Call cgroup_attach_task() with null group and check if
		 * return values are correct. If yes check if task exists in
		 * root group tasks file
		 * TODO: This test needs some modification in script
		 * Exp outcome: current task should be attached to root group
		 */
		retval = cgroup_attach_task(nullcgroup);
		if (retval == 0) {
			strncpy(tasksfile, mountpoint, sizeof(mountpoint));
			strcat(tasksfile, "/tasks");
			file = fopen(tasksfile, "r");
			if (!file) {
				printf("ERROR: in opening %s\n", tasksfile);
				return -1;
			}

			curr_tid = cgrouptest_gettid();
			while (!feof(file)) {
				fscanf(file, "%u", &tid);
				if (tid == curr_tid) {
					pass = 1;
					break;
				}
			}
			if (pass)
				printf("Test[1:%2d]\tPASS: Task found in %s\n",\
							 ++i, tasksfile);
			else
				printf("Test[1:%2d]\tFAIL: Task not found in %s\n",\
								 ++i, tasksfile);
		} else {
			printf("Test[1:%2d]\tFAIL: cgroup_attach_task() ret: %d\n",\
								 ++i, retval);
		}

		/*
		 * Test04: Call cgroup_attach_task_pid() with null group
		 * and invalid pid
		 * Exp outcome: error
		 */
		retval = cgroup_attach_task_pid(nullcgroup, 0);
		if (retval != 0)
			printf("Test[1:%2d]\tPASS: cgroup_attach_task_pid() ret: %d\n",\
								 ++i, retval);
		else
			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:
		/*
		 * Test01: call apis and check return values
		 * Exp outcome:
		 */
		/*
		 * Scenario 1: cgroup fs is multi mounted
		 * Exp outcome: no error. 0 return value
		 */

		retval = cgroup_init();
		if (retval == 0)
			printf("Test[2:%2d]\tPASS: cgroup_init() retval= %d:\n",\
								 ++i, retval);
		else
			printf("Test[2:%2d]\tFAIL: cgroup_init() retval= %d:\n",\
								 ++i, retval);

		/*
		 * Will add further testcases in separate patchset
		 */

		break;

	default:
		fprintf(stderr, "ERROR: Wrong parameters recieved from script. \
						Exiting tests\n");
		exit(1);
		break;
	}
	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;
	}
}