]>
git.proxmox.com Git - mirror_lxc.git/blob - src/tests/shortlived.c
3 * Copyright © 2017 Christian Brauner <christian.brauner@ubuntu.com>.
4 * Copyright © 2017 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>
24 #include <sys/types.h>
31 #define MYNAME "shortlived"
33 static int destroy_container(void)
43 execlp("lxc-destroy", "lxc-destroy", "-f", "-n", MYNAME
, NULL
);
47 ret
= waitpid(pid
, &status
, 0);
56 if (!WIFEXITED(status
)) { // did not exit normally
57 fprintf(stderr
, "%d: lxc-create exited abnormally\n", __LINE__
);
60 return WEXITSTATUS(status
);
63 static int create_container(void)
73 execlp("lxc-create", "lxc-create", "-t", "busybox", "-n", MYNAME
, NULL
);
77 ret
= waitpid(pid
, &status
, 0);
86 if (!WIFEXITED(status
)) { // did not exit normally
87 fprintf(stderr
, "%d: lxc-create exited abnormally\n", __LINE__
);
90 return WEXITSTATUS(status
);
93 int main(int argc
, char *argv
[])
95 struct lxc_container
*c
;
102 /* test a real container */
103 c
= lxc_container_new(MYNAME
, NULL
);
105 fprintf(stderr
, "%d: error creating lxc_container %s\n", __LINE__
, MYNAME
);
110 if (c
->is_defined(c
)) {
111 fprintf(stderr
, "%d: %s thought it was defined\n", __LINE__
, MYNAME
);
115 ret
= create_container();
117 fprintf(stderr
, "%d: failed to create a container\n", __LINE__
);
121 b
= c
->is_defined(c
);
123 fprintf(stderr
, "%d: %s thought it was not defined\n", __LINE__
, MYNAME
);
128 if (!s
|| strcmp(s
, "STOPPED")) {
129 fprintf(stderr
, "%d: %s is in state %s, not in STOPPED.\n", __LINE__
, c
->name
, s
? s
: "undefined");
133 b
= c
->load_config(c
, NULL
);
135 fprintf(stderr
, "%d: %s failed to read its config\n", __LINE__
, c
->name
);
139 if (!c
->set_config_item(c
, "lxc.init.cmd", "echo hello")) {
140 fprintf(stderr
, "%d: failed setting lxc.init.cmd\n", __LINE__
);
144 c
->want_daemonize(c
, true);
146 /* Test whether we can start a really short-lived daemonized container.
148 for (i
= 0; i
< 10; i
++) {
149 if (!c
->startl(c
, 0, NULL
)) {
150 fprintf(stderr
, "%d: %s failed to start\n", __LINE__
, c
->name
);
156 if (!c
->set_config_item(c
, "lxc.init.cmd", "you-shall-fail")) {
157 fprintf(stderr
, "%d: failed setting lxc.init.cmd\n", __LINE__
);
161 /* Test whether we catch the start failure of a really short-lived
162 * daemonized container.
164 for (i
= 0; i
< 10; i
++) {
165 if (c
->startl(c
, 0, NULL
)) {
166 fprintf(stderr
, "%d: %s failed to start\n", __LINE__
, c
->name
);
174 fprintf(stderr
, "all lxc_container tests passed for %s\n", c
->name
);
182 lxc_container_put(c
);