]>
git.proxmox.com Git - mirror_lxc.git/blob - src/tests/cgpath.c
3 * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>.
4 * Copyright © 2012 Canonical Ltd.
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.
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.
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.
19 #include <lxc/lxccontainer.h>
25 #include <sys/types.h>
29 #include "../lxc/cgroup.h"
30 #include "../lxc/lxc.h"
31 #include "../lxc/commands.h"
33 #define MYNAME "lxctest1"
35 #define TSTERR(fmt, ...) do { \
36 fprintf(stderr, "%s:%d " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__); \
40 * test_running_container: test cgroup functions against a running container
42 * @group : name of the container group or NULL for default "lxc"
43 * @name : name of the container
45 static int test_running_container(const char *lxcpath
,
46 const char *group
, const char *name
)
49 struct lxc_container
*c
= NULL
;
52 char relpath
[PATH_MAX
+1];
53 char abspath
[PATH_MAX
+1];
54 char value
[NAME_MAX
], value_save
[NAME_MAX
];
56 sprintf(relpath
, "%s/%s", group
? group
: "lxc", name
);
58 if ((c
= lxc_container_new(name
, lxcpath
)) == NULL
) {
59 TSTERR("container %s couldn't instantiate", name
);
62 if (!c
->is_defined(c
)) {
63 TSTERR("container %s does not exist", name
);
67 cgrelpath
= lxc_cmd_get_cgroup_path(c
->name
, c
->config_path
, "freezer");
69 TSTERR("lxc_cmd_get_cgroup_path returned NULL");
72 if (!strstr(cgrelpath
, relpath
)) {
73 TSTERR("lxc_cmd_get_cgroup_path %s not in %s", relpath
, cgrelpath
);
77 /* test get/set value using memory.soft_limit_in_bytes file */
78 ret
= lxc_cgroup_get("memory.soft_limit_in_bytes", value
, sizeof(value
),
79 c
->name
, c
->config_path
);
81 TSTERR("lxc_cgroup_get failed");
84 strcpy(value_save
, value
);
86 ret
= lxc_cgroup_set("memory.soft_limit_in_bytes", "512M", c
->name
, c
->config_path
);
88 TSTERR("lxc_cgroup_set failed %d %d", ret
, errno
);
91 ret
= lxc_cgroup_get("memory.soft_limit_in_bytes", value
, sizeof(value
),
92 c
->name
, c
->config_path
);
94 TSTERR("lxc_cgroup_get failed");
97 if (strcmp(value
, "536870912\n")) {
98 TSTERR("lxc_cgroup_set_bypath failed to set value >%s<", value
);
102 /* restore original value */
103 ret
= lxc_cgroup_set("memory.soft_limit_in_bytes", value_save
,
104 c
->name
, c
->config_path
);
106 TSTERR("lxc_cgroup_set failed");
110 cgabspath
= lxc_cgroup_path_get("freezer", c
->name
, c
->config_path
);
112 TSTERR("lxc_cgroup_path_get returned NULL");
115 sprintf(abspath
, "%s/%s/%s", "freezer", group
? group
: "lxc", c
->name
);
116 if (!strstr(cgabspath
, abspath
)) {
117 TSTERR("lxc_cgroup_path_get %s not in %s", abspath
, cgabspath
);
122 cgabspath
= lxc_cgroup_path_get("freezer.state", c
->name
, c
->config_path
);
124 TSTERR("lxc_cgroup_path_get returned NULL");
127 sprintf(abspath
, "%s/%s/%s", "freezer", group
? group
: "lxc", c
->name
);
128 if (!strstr(cgabspath
, abspath
)) {
129 TSTERR("lxc_cgroup_path_get %s not in %s", abspath
, cgabspath
);
139 lxc_container_put(c
);
144 static int test_container(const char *lxcpath
,
145 const char *group
, const char *name
,
146 const char *template)
149 struct lxc_container
*c
= NULL
;
152 ret
= mkdir(lxcpath
, 0755);
153 if (ret
< 0 && errno
!= EEXIST
) {
154 TSTERR("failed to mkdir %s %s", lxcpath
, strerror(errno
));
160 if ((c
= lxc_container_new(name
, lxcpath
)) == NULL
) {
161 TSTERR("instantiating container %s", name
);
164 if (c
->is_defined(c
)) {
167 c
= lxc_container_new(name
, lxcpath
);
169 c
->set_config_item(c
, "lxc.network.type", "empty");
170 if (!c
->createl(c
, template, NULL
, NULL
, 0, NULL
)) {
171 TSTERR("creating container %s", name
);
174 c
->load_config(c
, NULL
);
175 c
->want_daemonize(c
);
176 if (!c
->startl(c
, 0, NULL
)) {
177 TSTERR("starting container %s", name
);
181 ret
= test_running_container(lxcpath
, group
, name
);
187 lxc_container_put(c
);
194 int ret
= EXIT_FAILURE
;
196 /* won't require privilege necessarily once users are classified by
198 if (geteuid() != 0) {
199 TSTERR("requires privilege");
203 #if TEST_ALREADY_RUNNING_CT
206 * This is useful for running with valgrind to test for memory
207 * leaks. The container should already be running, we can't start
208 * the container ourselves because valgrind gets confused by lxc's
209 * internal calls to clone.
211 if (test_running_container(NULL
, NULL
, "bb01") < 0)
213 printf("Running container cgroup tests...Passed\n");
217 if (test_container(NULL
, NULL
, MYNAME
, "busybox") < 0)
219 printf("Container creation tests...Passed\n");
221 if (test_container("/var/lib/lxctest2", NULL
, MYNAME
, "busybox") < 0)
223 printf("Container creation with LXCPATH tests...Passed\n");