]>
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.
22 #include <lxc/lxccontainer.h>
27 #include <sys/types.h>
41 #define MYNAME "shortlived"
43 static int destroy_container(void)
54 execlp("lxc-destroy", "lxc-destroy", "-f", "-n", MYNAME
, NULL
);
59 ret
= waitpid(pid
, &status
, 0);
70 if (!WIFEXITED(status
)) { // did not exit normally
71 fprintf(stderr
, "%d: lxc-create exited abnormally\n", __LINE__
);
75 return WEXITSTATUS(status
);
78 static int create_container(void)
89 execlp("lxc-create", "lxc-create", "-t", "busybox", "-n", MYNAME
, NULL
);
94 ret
= waitpid(pid
, &status
, 0);
106 if (!WIFEXITED(status
)) { // did not exit normally
107 fprintf(stderr
, "%d: lxc-create exited abnormally\n", __LINE__
);
111 return WEXITSTATUS(status
);
114 int main(int argc
, char *argv
[])
119 struct lxc_container
*c
;
121 char template[sizeof(P_tmpdir
"/shortlived_XXXXXX")];
122 int ret
= EXIT_FAILURE
;
124 (void)strlcpy(template, P_tmpdir
"/shortlived_XXXXXX", sizeof(template));
126 i
= lxc_make_tmpfile(template, false);
128 lxc_error("Failed to create temporary log file for container %s\n", MYNAME
);
131 lxc_debug("Using \"%s\" as temporary log file for container %s\n", template, MYNAME
);
138 log
.prefix
= "shortlived";
142 if (lxc_log_init(&log
))
145 /* test a real container */
146 c
= lxc_container_new(MYNAME
, NULL
);
148 fprintf(stderr
, "%d: error creating lxc_container %s\n", __LINE__
, MYNAME
);
152 if (c
->is_defined(c
)) {
153 fprintf(stderr
, "%d: %s thought it was defined\n", __LINE__
, MYNAME
);
157 if (create_container() < 0) {
158 fprintf(stderr
, "%d: failed to create a container\n", __LINE__
);
162 b
= c
->is_defined(c
);
164 fprintf(stderr
, "%d: %s thought it was not defined\n", __LINE__
, MYNAME
);
169 if (!s
|| strcmp(s
, "STOPPED")) {
170 fprintf(stderr
, "%d: %s is in state %s, not in STOPPED.\n", __LINE__
, c
->name
, s
? s
: "undefined");
174 b
= c
->load_config(c
, NULL
);
176 fprintf(stderr
, "%d: %s failed to read its config\n", __LINE__
, c
->name
);
180 if (!c
->set_config_item(c
, "lxc.init.cmd", "echo hello")) {
181 fprintf(stderr
, "%d: failed setting lxc.init.cmd\n", __LINE__
);
185 if (!c
->set_config_item(c
, "lxc.execute.cmd", "echo hello")) {
186 fprintf(stderr
, "%d: failed setting lxc.execute.cmd\n", __LINE__
);
190 c
->want_daemonize(c
, true);
192 /* Test whether we can start a really short-lived daemonized container. */
193 for (i
= 0; i
< 10; i
++) {
194 if (!c
->startl(c
, 0, NULL
)) {
195 fprintf(stderr
, "%d: %s failed to start on %dth iteration\n", __LINE__
, c
->name
, i
);
199 if (!c
->wait(c
, "STOPPED", 30)) {
200 fprintf(stderr
, "%d: %s failed to wait on %dth iteration\n", __LINE__
, c
->name
, i
);
205 /* Test whether we can start a really short-lived daemonized container with lxc-init. */
206 for (i
= 0; i
< 10; i
++) {
207 if (!c
->startl(c
, 1, NULL
)) {
208 fprintf(stderr
, "%d: %s failed to start on %dth iteration\n", __LINE__
, c
->name
, i
);
212 if (!c
->wait(c
, "STOPPED", 30)) {
213 fprintf(stderr
, "%d: %s failed to wait on %dth iteration\n", __LINE__
, c
->name
, i
);
218 if (!c
->set_config_item(c
, "lxc.init.cmd", "you-shall-fail")) {
219 fprintf(stderr
, "%d: failed setting lxc.init.cmd\n", __LINE__
);
223 if (!c
->set_config_item(c
, "lxc.execute.cmd", "you-shall-fail")) {
224 fprintf(stderr
, "%d: failed setting lxc.init.cmd\n", __LINE__
);
228 /* Test whether we can start a really short-lived daemonized container. */
229 for (i
= 0; i
< 10; i
++) {
230 if (c
->startl(c
, 0, NULL
)) {
231 fprintf(stderr
, "%d: %s failed to start on %dth iteration\n", __LINE__
, c
->name
, i
);
235 if (!c
->wait(c
, "STOPPED", 30)) {
236 fprintf(stderr
, "%d: %s failed to wait on %dth iteration\n", __LINE__
, c
->name
, i
);
241 /* Test whether we can start a really short-lived daemonized container with lxc-init. */
242 for (i
= 0; i
< 10; i
++) {
243 /* An container started with lxc-init will always start
244 * successfully unless lxc-init has a bug.
246 if (!c
->startl(c
, 1, NULL
)) {
247 fprintf(stderr
, "%d: %s failed to start on %dth iteration\n", __LINE__
, c
->name
, i
);
251 if (!c
->wait(c
, "STOPPED", 30)) {
252 fprintf(stderr
, "%d: %s failed to wait on %dth iteration\n", __LINE__
, c
->name
, i
);
259 fprintf(stderr
, "all lxc_container tests passed for %s\n", c
->name
);
267 lxc_container_put(c
);
270 fd
= open(template, O_RDONLY
);
275 while ((buflen
= read(fd
, buf
, 1024)) > 0) {
276 buflen
= write(STDERR_FILENO
, buf
, buflen
);