]>
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>
36 #define MYNAME "lxctest1"
38 #define TSTERR(fmt, ...) do { \
39 fprintf(stderr, "%s:%d " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__); \
43 * test_running_container: test cgroup functions against a running container
45 * @group : name of the container group or NULL for default "lxc"
46 * @name : name of the container
48 static int test_running_container(const char *lxcpath
,
49 const char *group
, const char *name
)
52 struct lxc_container
*c
= NULL
;
54 char relpath
[PATH_MAX
+1];
55 char value
[NAME_MAX
], value_save
[NAME_MAX
];
57 sprintf(relpath
, "%s/%s", group
? group
: "lxc", name
);
59 if ((c
= lxc_container_new(name
, lxcpath
)) == NULL
) {
60 TSTERR("container %s couldn't instantiate", name
);
63 if (!c
->is_defined(c
)) {
64 TSTERR("container %s does not exist", name
);
68 cgrelpath
= lxc_cmd_get_cgroup_path(c
->name
, c
->config_path
, "freezer");
70 TSTERR("lxc_cmd_get_cgroup_path returned NULL");
73 if (!strstr(cgrelpath
, relpath
)) {
74 TSTERR("lxc_cmd_get_cgroup_path %s not in %s", relpath
, cgrelpath
);
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
);
82 TSTERR("lxc_cgroup_get failed");
85 strcpy(value_save
, value
);
87 ret
= lxc_cgroup_set("memory.soft_limit_in_bytes", "512M", c
->name
, c
->config_path
);
89 TSTERR("lxc_cgroup_set failed %d %d", ret
, errno
);
92 ret
= lxc_cgroup_get("memory.soft_limit_in_bytes", value
, sizeof(value
),
93 c
->name
, c
->config_path
);
95 TSTERR("lxc_cgroup_get failed");
98 if (strcmp(value
, "536870912\n")) {
99 TSTERR("lxc_cgroup_set_bypath failed to set value >%s<", value
);
103 /* restore original value */
104 ret
= lxc_cgroup_set("memory.soft_limit_in_bytes", value_save
,
105 c
->name
, c
->config_path
);
107 TSTERR("lxc_cgroup_set failed");
116 lxc_container_put(c
);
121 static int test_container(const char *lxcpath
,
122 const char *group
, const char *name
,
123 const char *template)
126 struct lxc_container
*c
= NULL
;
129 ret
= mkdir(lxcpath
, 0755);
130 if (ret
< 0 && errno
!= EEXIST
) {
131 TSTERR("failed to mkdir %s %s", lxcpath
, strerror(errno
));
137 if ((c
= lxc_container_new(name
, lxcpath
)) == NULL
) {
138 TSTERR("instantiating container %s", name
);
141 if (c
->is_defined(c
)) {
144 c
= lxc_container_new(name
, lxcpath
);
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
);
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
);
158 ret
= test_running_container(lxcpath
, group
, name
);
164 lxc_container_put(c
);
171 int ret
= EXIT_FAILURE
;
173 /* won't require privilege necessarily once users are classified by
175 if (geteuid() != 0) {
176 TSTERR("requires privilege");
180 #if TEST_ALREADY_RUNNING_CT
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.
188 if (test_running_container(NULL
, NULL
, "bb01") < 0)
190 printf("Running container cgroup tests...Passed\n");
194 if (test_container(NULL
, NULL
, MYNAME
, "busybox") < 0)
196 printf("Container creation tests...Passed\n");
198 if (test_container("/var/lib/lxctest2", NULL
, MYNAME
, "busybox") < 0)
200 printf("Container creation with LXCPATH tests...Passed\n");