]>
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>
34 #define MYNAME "shortlived"
36 static int destroy_container(void)
46 execlp("lxc-destroy", "lxc-destroy", "-f", "-n", MYNAME
, NULL
);
50 ret
= waitpid(pid
, &status
, 0);
59 if (!WIFEXITED(status
)) { // did not exit normally
60 fprintf(stderr
, "%d: lxc-create exited abnormally\n", __LINE__
);
63 return WEXITSTATUS(status
);
66 static int create_container(void)
76 execlp("lxc-create", "lxc-create", "-t", "busybox", "-n", MYNAME
, NULL
);
80 ret
= waitpid(pid
, &status
, 0);
89 if (!WIFEXITED(status
)) { // did not exit normally
90 fprintf(stderr
, "%d: lxc-create exited abnormally\n", __LINE__
);
93 return WEXITSTATUS(status
);
96 int main(int argc
, char *argv
[])
101 struct lxc_container
*c
;
103 char template[sizeof(P_tmpdir
"/shortlived_XXXXXX")];
104 int ret
= EXIT_FAILURE
;
106 strcpy(template, P_tmpdir
"/shortlived_XXXXXX");
107 i
= lxc_make_tmpfile(template, false);
109 lxc_error("Failed to create temporary log file for container %s\n", MYNAME
);
112 lxc_debug("Using \"%s\" as temporary log file for container %s\n", template, MYNAME
);
119 log
.prefix
= "shortlived";
122 if (lxc_log_init(&log
))
125 /* test a real container */
126 c
= lxc_container_new(MYNAME
, NULL
);
128 fprintf(stderr
, "%d: error creating lxc_container %s\n", __LINE__
, MYNAME
);
132 if (c
->is_defined(c
)) {
133 fprintf(stderr
, "%d: %s thought it was defined\n", __LINE__
, MYNAME
);
137 if (create_container() < 0) {
138 fprintf(stderr
, "%d: failed to create a container\n", __LINE__
);
142 b
= c
->is_defined(c
);
144 fprintf(stderr
, "%d: %s thought it was not defined\n", __LINE__
, MYNAME
);
149 if (!s
|| strcmp(s
, "STOPPED")) {
150 fprintf(stderr
, "%d: %s is in state %s, not in STOPPED.\n", __LINE__
, c
->name
, s
? s
: "undefined");
154 b
= c
->load_config(c
, NULL
);
156 fprintf(stderr
, "%d: %s failed to read its config\n", __LINE__
, c
->name
);
160 if (!c
->set_config_item(c
, "lxc.init.cmd", "echo hello")) {
161 fprintf(stderr
, "%d: failed setting lxc.init.cmd\n", __LINE__
);
165 if (!c
->set_config_item(c
, "lxc.execute.cmd", "echo hello")) {
166 fprintf(stderr
, "%d: failed setting lxc.execute.cmd\n", __LINE__
);
170 c
->want_daemonize(c
, true);
172 /* Test whether we can start a really short-lived daemonized container. */
173 for (i
= 0; i
< 10; i
++) {
174 if (!c
->startl(c
, 0, NULL
)) {
175 fprintf(stderr
, "%d: %s failed to start on %dth iteration\n", __LINE__
, c
->name
, i
);
179 if (!c
->wait(c
, "STOPPED", 30)) {
180 fprintf(stderr
, "%d: %s failed to wait on %dth iteration\n", __LINE__
, c
->name
, i
);
185 /* Test whether we can start a really short-lived daemonized container with lxc-init. */
186 for (i
= 0; i
< 10; i
++) {
187 if (!c
->startl(c
, 1, NULL
)) {
188 fprintf(stderr
, "%d: %s failed to start on %dth iteration\n", __LINE__
, c
->name
, i
);
192 if (!c
->wait(c
, "STOPPED", 30)) {
193 fprintf(stderr
, "%d: %s failed to wait on %dth iteration\n", __LINE__
, c
->name
, i
);
198 if (!c
->set_config_item(c
, "lxc.init.cmd", "you-shall-fail")) {
199 fprintf(stderr
, "%d: failed setting lxc.init.cmd\n", __LINE__
);
203 if (!c
->set_config_item(c
, "lxc.execute.cmd", "you-shall-fail")) {
204 fprintf(stderr
, "%d: failed setting lxc.init.cmd\n", __LINE__
);
208 /* Test whether we can start a really short-lived daemonized container. */
209 for (i
= 0; i
< 10; i
++) {
210 if (c
->startl(c
, 0, NULL
)) {
211 fprintf(stderr
, "%d: %s failed to start on %dth iteration\n", __LINE__
, c
->name
, i
);
215 if (!c
->wait(c
, "STOPPED", 30)) {
216 fprintf(stderr
, "%d: %s failed to wait on %dth iteration\n", __LINE__
, c
->name
, i
);
221 /* Test whether we can start a really short-lived daemonized container with lxc-init. */
222 for (i
= 0; i
< 10; i
++) {
223 /* An container started with lxc-init will always start
224 * succesfully unless lxc-init has a bug.
226 if (!c
->startl(c
, 1, NULL
)) {
227 fprintf(stderr
, "%d: %s failed to start on %dth iteration\n", __LINE__
, c
->name
, i
);
231 if (!c
->wait(c
, "STOPPED", 30)) {
232 fprintf(stderr
, "%d: %s failed to wait on %dth iteration\n", __LINE__
, c
->name
, i
);
239 fprintf(stderr
, "all lxc_container tests passed for %s\n", c
->name
);
247 lxc_container_put(c
);
250 fd
= open(template, O_RDONLY
);
254 while ((buflen
= read(fd
, buf
, 1024)) > 0) {
255 buflen
= write(STDERR_FILENO
, buf
, buflen
);