]>
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>
35 #include "include/strlcpy.h"
38 #define MYNAME "shortlived"
40 static int destroy_container(void)
51 execlp("lxc-destroy", "lxc-destroy", "-f", "-n", MYNAME
, NULL
);
56 ret
= waitpid(pid
, &status
, 0);
67 if (!WIFEXITED(status
)) { // did not exit normally
68 fprintf(stderr
, "%d: lxc-create exited abnormally\n", __LINE__
);
72 return WEXITSTATUS(status
);
75 static int create_container(void)
86 execlp("lxc-create", "lxc-create", "-t", "busybox", "-n", MYNAME
, NULL
);
91 ret
= waitpid(pid
, &status
, 0);
103 if (!WIFEXITED(status
)) { // did not exit normally
104 fprintf(stderr
, "%d: lxc-create exited abnormally\n", __LINE__
);
108 return WEXITSTATUS(status
);
111 int main(int argc
, char *argv
[])
116 struct lxc_container
*c
;
118 char template[sizeof(P_tmpdir
"/shortlived_XXXXXX")];
119 int ret
= EXIT_FAILURE
;
121 (void)strlcpy(template, P_tmpdir
"/shortlived_XXXXXX", sizeof(template));
123 i
= lxc_make_tmpfile(template, false);
125 lxc_error("Failed to create temporary log file for container %s\n", MYNAME
);
128 lxc_debug("Using \"%s\" as temporary log file for container %s\n", template, MYNAME
);
135 log
.prefix
= "shortlived";
139 if (lxc_log_init(&log
))
142 /* test a real container */
143 c
= lxc_container_new(MYNAME
, NULL
);
145 fprintf(stderr
, "%d: error creating lxc_container %s\n", __LINE__
, MYNAME
);
149 if (c
->is_defined(c
)) {
150 fprintf(stderr
, "%d: %s thought it was defined\n", __LINE__
, MYNAME
);
154 if (create_container() < 0) {
155 fprintf(stderr
, "%d: failed to create a container\n", __LINE__
);
159 b
= c
->is_defined(c
);
161 fprintf(stderr
, "%d: %s thought it was not defined\n", __LINE__
, MYNAME
);
166 if (!s
|| strcmp(s
, "STOPPED")) {
167 fprintf(stderr
, "%d: %s is in state %s, not in STOPPED.\n", __LINE__
, c
->name
, s
? s
: "undefined");
171 b
= c
->load_config(c
, NULL
);
173 fprintf(stderr
, "%d: %s failed to read its config\n", __LINE__
, c
->name
);
177 if (!c
->set_config_item(c
, "lxc.init.cmd", "echo hello")) {
178 fprintf(stderr
, "%d: failed setting lxc.init.cmd\n", __LINE__
);
182 if (!c
->set_config_item(c
, "lxc.execute.cmd", "echo hello")) {
183 fprintf(stderr
, "%d: failed setting lxc.execute.cmd\n", __LINE__
);
187 c
->want_daemonize(c
, true);
189 /* Test whether we can start a really short-lived daemonized container. */
190 for (i
= 0; i
< 10; i
++) {
191 if (!c
->startl(c
, 0, NULL
)) {
192 fprintf(stderr
, "%d: %s failed to start on %dth iteration\n", __LINE__
, c
->name
, i
);
196 if (!c
->wait(c
, "STOPPED", 30)) {
197 fprintf(stderr
, "%d: %s failed to wait on %dth iteration\n", __LINE__
, c
->name
, i
);
202 /* Test whether we can start a really short-lived daemonized container with lxc-init. */
203 for (i
= 0; i
< 10; i
++) {
204 if (!c
->startl(c
, 1, NULL
)) {
205 fprintf(stderr
, "%d: %s failed to start on %dth iteration\n", __LINE__
, c
->name
, i
);
209 if (!c
->wait(c
, "STOPPED", 30)) {
210 fprintf(stderr
, "%d: %s failed to wait on %dth iteration\n", __LINE__
, c
->name
, i
);
215 if (!c
->set_config_item(c
, "lxc.init.cmd", "you-shall-fail")) {
216 fprintf(stderr
, "%d: failed setting lxc.init.cmd\n", __LINE__
);
220 if (!c
->set_config_item(c
, "lxc.execute.cmd", "you-shall-fail")) {
221 fprintf(stderr
, "%d: failed setting lxc.init.cmd\n", __LINE__
);
225 /* Test whether we can start a really short-lived daemonized container. */
226 for (i
= 0; i
< 10; i
++) {
227 if (c
->startl(c
, 0, NULL
)) {
228 fprintf(stderr
, "%d: %s failed to start on %dth iteration\n", __LINE__
, c
->name
, i
);
232 if (!c
->wait(c
, "STOPPED", 30)) {
233 fprintf(stderr
, "%d: %s failed to wait on %dth iteration\n", __LINE__
, c
->name
, i
);
238 /* Test whether we can start a really short-lived daemonized container with lxc-init. */
239 for (i
= 0; i
< 10; i
++) {
240 /* An container started with lxc-init will always start
241 * successfully unless lxc-init has a bug.
243 if (!c
->startl(c
, 1, NULL
)) {
244 fprintf(stderr
, "%d: %s failed to start on %dth iteration\n", __LINE__
, c
->name
, i
);
248 if (!c
->wait(c
, "STOPPED", 30)) {
249 fprintf(stderr
, "%d: %s failed to wait on %dth iteration\n", __LINE__
, c
->name
, i
);
256 fprintf(stderr
, "all lxc_container tests passed for %s\n", c
->name
);
264 lxc_container_put(c
);
267 fd
= open(template, O_RDONLY
);
272 while ((buflen
= read(fd
, buf
, 1024)) > 0) {
273 buflen
= write(STDERR_FILENO
, buf
, buflen
);