]> git.proxmox.com Git - mirror_lxc.git/blob - src/tests/cgpath.c
tests: fix get_item tests
[mirror_lxc.git] / src / tests / cgpath.c
1 /* liblxcapi
2 *
3 * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>.
4 * Copyright © 2012 Canonical Ltd.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2, as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 #include <lxc/lxccontainer.h>
20
21 #include <limits.h>
22 #include <unistd.h>
23 #include <signal.h>
24 #include <stdio.h>
25 #include <sys/types.h>
26 #include <sys/wait.h>
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <string.h>
30 #include <sys/stat.h>
31
32 #include "cgroup.h"
33 #include "lxc.h"
34 #include "commands.h"
35
36 #define MYNAME "lxctest1"
37
38 #define TSTERR(fmt, ...) do { \
39 fprintf(stderr, "%s:%d " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__); \
40 } while (0)
41
42 /*
43 * test_running_container: test cgroup functions against a running container
44 *
45 * @group : name of the container group or NULL for default "lxc"
46 * @name : name of the container
47 */
48 static int test_running_container(const char *lxcpath,
49 const char *group, const char *name)
50 {
51 int ret = -1;
52 struct lxc_container *c = NULL;
53 char *cgrelpath;
54 char relpath[PATH_MAX+1];
55 char value[NAME_MAX], value_save[NAME_MAX];
56
57 sprintf(relpath, "%s/%s", group ? group : "lxc", name);
58
59 if ((c = lxc_container_new(name, lxcpath)) == NULL) {
60 TSTERR("container %s couldn't instantiate", name);
61 goto err1;
62 }
63 if (!c->is_defined(c)) {
64 TSTERR("container %s does not exist", name);
65 goto err2;
66 }
67
68 cgrelpath = lxc_cmd_get_cgroup_path(c->name, c->config_path, "freezer");
69 if (!cgrelpath) {
70 TSTERR("lxc_cmd_get_cgroup_path returned NULL");
71 goto err2;
72 }
73 if (!strstr(cgrelpath, relpath)) {
74 TSTERR("lxc_cmd_get_cgroup_path %s not in %s", relpath, cgrelpath);
75 goto err3;
76 }
77
78 /* test get/set value using memory.soft_limit_in_bytes file */
79 ret = lxc_cgroup_get("memory.soft_limit_in_bytes", value, sizeof(value),
80 c->name, c->config_path);
81 if (ret < 0) {
82 TSTERR("lxc_cgroup_get failed");
83 goto err3;
84 }
85 strcpy(value_save, value);
86
87 ret = lxc_cgroup_set("memory.soft_limit_in_bytes", "512M", c->name, c->config_path);
88 if (ret < 0) {
89 TSTERR("lxc_cgroup_set failed %d %d", ret, errno);
90 goto err3;
91 }
92 ret = lxc_cgroup_get("memory.soft_limit_in_bytes", value, sizeof(value),
93 c->name, c->config_path);
94 if (ret < 0) {
95 TSTERR("lxc_cgroup_get failed");
96 goto err3;
97 }
98 if (strcmp(value, "536870912\n")) {
99 TSTERR("lxc_cgroup_set_bypath failed to set value >%s<", value);
100 goto err3;
101 }
102
103 /* restore original value */
104 ret = lxc_cgroup_set("memory.soft_limit_in_bytes", value_save,
105 c->name, c->config_path);
106 if (ret < 0) {
107 TSTERR("lxc_cgroup_set failed");
108 goto err3;
109 }
110
111 ret = 0;
112
113 err3:
114 free(cgrelpath);
115 err2:
116 lxc_container_put(c);
117 err1:
118 return ret;
119 }
120
121 static int test_container(const char *lxcpath,
122 const char *group, const char *name,
123 const char *template)
124 {
125 int ret;
126 struct lxc_container *c = NULL;
127
128 if (lxcpath) {
129 ret = mkdir(lxcpath, 0755);
130 if (ret < 0 && errno != EEXIST) {
131 TSTERR("failed to mkdir %s %s", lxcpath, strerror(errno));
132 goto out1;
133 }
134 }
135 ret = -1;
136
137 if ((c = lxc_container_new(name, lxcpath)) == NULL) {
138 TSTERR("instantiating container %s", name);
139 goto out1;
140 }
141 if (c->is_defined(c)) {
142 c->stop(c);
143 c->destroy(c);
144 c = lxc_container_new(name, lxcpath);
145 }
146 c->set_config_item(c, "lxc.network.type", "empty");
147 if (!c->createl(c, template, NULL, NULL, 0, NULL)) {
148 TSTERR("creating container %s", name);
149 goto out2;
150 }
151 c->load_config(c, NULL);
152 c->want_daemonize(c, true);
153 if (!c->startl(c, 0, NULL)) {
154 TSTERR("starting container %s", name);
155 goto out3;
156 }
157
158 ret = test_running_container(lxcpath, group, name);
159
160 c->stop(c);
161 out3:
162 c->destroy(c);
163 out2:
164 lxc_container_put(c);
165 out1:
166 return ret;
167 }
168
169 int main()
170 {
171 int ret = EXIT_FAILURE;
172
173 /* won't require privilege necessarily once users are classified by
174 * pam_cgroup */
175 if (geteuid() != 0) {
176 TSTERR("requires privilege");
177 exit(0);
178 }
179
180 #if TEST_ALREADY_RUNNING_CT
181
182 /*
183 * This is useful for running with valgrind to test for memory
184 * leaks. The container should already be running, we can't start
185 * the container ourselves because valgrind gets confused by lxc's
186 * internal calls to clone.
187 */
188 if (test_running_container(NULL, NULL, "bb01") < 0)
189 goto out;
190 printf("Running container cgroup tests...Passed\n");
191
192 #else
193
194 if (test_container(NULL, NULL, MYNAME, "busybox") < 0)
195 goto out;
196 printf("Container creation tests...Passed\n");
197
198 if (test_container("/var/lib/lxctest2", NULL, MYNAME, "busybox") < 0)
199 goto out;
200 printf("Container creation with LXCPATH tests...Passed\n");
201
202 #endif
203
204 ret = EXIT_SUCCESS;
205 out:
206 return ret;
207 }