]>
git.proxmox.com Git - mirror_lxc.git/blob - src/tests/shortlived.c
2a039a2f392b9db952cd7b4a71bcb4adfc6f96af
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>
35 #include "include/strlcpy.h"
38 #define MYNAME "shortlived"
40 static int destroy_container(void)
50 execlp("lxc-destroy", "lxc-destroy", "-f", "-n", MYNAME
, NULL
);
54 ret
= waitpid(pid
, &status
, 0);
63 if (!WIFEXITED(status
)) { // did not exit normally
64 fprintf(stderr
, "%d: lxc-create exited abnormally\n", __LINE__
);
67 return WEXITSTATUS(status
);
70 static int create_container(void)
80 execlp("lxc-create", "lxc-create", "-t", "busybox", "-n", MYNAME
, NULL
);
84 ret
= waitpid(pid
, &status
, 0);
93 if (!WIFEXITED(status
)) { // did not exit normally
94 fprintf(stderr
, "%d: lxc-create exited abnormally\n", __LINE__
);
97 return WEXITSTATUS(status
);
100 int main(int argc
, char *argv
[])
105 struct lxc_container
*c
;
107 char template[sizeof(P_tmpdir
"/shortlived_XXXXXX")];
108 int ret
= EXIT_FAILURE
;
110 (void)strlcpy(template, P_tmpdir
"/shortlived_XXXXXX", sizeof(template));
112 i
= lxc_make_tmpfile(template, false);
114 lxc_error("Failed to create temporary log file for container %s\n", MYNAME
);
117 lxc_debug("Using \"%s\" as temporary log file for container %s\n", template, MYNAME
);
124 log
.prefix
= "shortlived";
127 if (lxc_log_init(&log
))
130 /* test a real container */
131 c
= lxc_container_new(MYNAME
, NULL
);
133 fprintf(stderr
, "%d: error creating lxc_container %s\n", __LINE__
, MYNAME
);
137 if (c
->is_defined(c
)) {
138 fprintf(stderr
, "%d: %s thought it was defined\n", __LINE__
, MYNAME
);
142 if (create_container() < 0) {
143 fprintf(stderr
, "%d: failed to create a container\n", __LINE__
);
147 b
= c
->is_defined(c
);
149 fprintf(stderr
, "%d: %s thought it was not defined\n", __LINE__
, MYNAME
);
154 if (!s
|| strcmp(s
, "STOPPED")) {
155 fprintf(stderr
, "%d: %s is in state %s, not in STOPPED.\n", __LINE__
, c
->name
, s
? s
: "undefined");
159 b
= c
->load_config(c
, NULL
);
161 fprintf(stderr
, "%d: %s failed to read its config\n", __LINE__
, c
->name
);
165 if (!c
->set_config_item(c
, "lxc.init.cmd", "echo hello")) {
166 fprintf(stderr
, "%d: failed setting lxc.init.cmd\n", __LINE__
);
170 if (!c
->set_config_item(c
, "lxc.execute.cmd", "echo hello")) {
171 fprintf(stderr
, "%d: failed setting lxc.execute.cmd\n", __LINE__
);
175 c
->want_daemonize(c
, true);
177 /* Test whether we can start a really short-lived daemonized container. */
178 for (i
= 0; i
< 10; i
++) {
179 if (!c
->startl(c
, 0, NULL
)) {
180 fprintf(stderr
, "%d: %s failed to start on %dth iteration\n", __LINE__
, c
->name
, i
);
184 if (!c
->wait(c
, "STOPPED", 30)) {
185 fprintf(stderr
, "%d: %s failed to wait on %dth iteration\n", __LINE__
, c
->name
, i
);
190 /* Test whether we can start a really short-lived daemonized container with lxc-init. */
191 for (i
= 0; i
< 10; i
++) {
192 if (!c
->startl(c
, 1, NULL
)) {
193 fprintf(stderr
, "%d: %s failed to start on %dth iteration\n", __LINE__
, c
->name
, i
);
197 if (!c
->wait(c
, "STOPPED", 30)) {
198 fprintf(stderr
, "%d: %s failed to wait on %dth iteration\n", __LINE__
, c
->name
, i
);
203 if (!c
->set_config_item(c
, "lxc.init.cmd", "you-shall-fail")) {
204 fprintf(stderr
, "%d: failed setting lxc.init.cmd\n", __LINE__
);
208 if (!c
->set_config_item(c
, "lxc.execute.cmd", "you-shall-fail")) {
209 fprintf(stderr
, "%d: failed setting lxc.init.cmd\n", __LINE__
);
213 /* Test whether we can start a really short-lived daemonized container. */
214 for (i
= 0; i
< 10; i
++) {
215 if (c
->startl(c
, 0, NULL
)) {
216 fprintf(stderr
, "%d: %s failed to start on %dth iteration\n", __LINE__
, c
->name
, i
);
220 if (!c
->wait(c
, "STOPPED", 30)) {
221 fprintf(stderr
, "%d: %s failed to wait on %dth iteration\n", __LINE__
, c
->name
, i
);
226 /* Test whether we can start a really short-lived daemonized container with lxc-init. */
227 for (i
= 0; i
< 10; i
++) {
228 /* An container started with lxc-init will always start
229 * succesfully unless lxc-init has a bug.
231 if (!c
->startl(c
, 1, NULL
)) {
232 fprintf(stderr
, "%d: %s failed to start on %dth iteration\n", __LINE__
, c
->name
, i
);
236 if (!c
->wait(c
, "STOPPED", 30)) {
237 fprintf(stderr
, "%d: %s failed to wait on %dth iteration\n", __LINE__
, c
->name
, i
);
244 fprintf(stderr
, "all lxc_container tests passed for %s\n", c
->name
);
252 lxc_container_put(c
);
255 fd
= open(template, O_RDONLY
);
259 while ((buflen
= read(fd
, buf
, 1024)) > 0) {
260 buflen
= write(STDERR_FILENO
, buf
, buflen
);