]>
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")];
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
))
126 /* test a real container */
127 c
= lxc_container_new(MYNAME
, NULL
);
129 fprintf(stderr
, "%d: error creating lxc_container %s\n", __LINE__
, MYNAME
);
134 if (c
->is_defined(c
)) {
135 fprintf(stderr
, "%d: %s thought it was defined\n", __LINE__
, MYNAME
);
139 ret
= create_container();
141 fprintf(stderr
, "%d: failed to create a container\n", __LINE__
);
145 b
= c
->is_defined(c
);
147 fprintf(stderr
, "%d: %s thought it was not defined\n", __LINE__
, MYNAME
);
152 if (!s
|| strcmp(s
, "STOPPED")) {
153 fprintf(stderr
, "%d: %s is in state %s, not in STOPPED.\n", __LINE__
, c
->name
, s
? s
: "undefined");
157 b
= c
->load_config(c
, NULL
);
159 fprintf(stderr
, "%d: %s failed to read its config\n", __LINE__
, c
->name
);
163 if (!c
->set_config_item(c
, "lxc.init.cmd", "echo hello")) {
164 fprintf(stderr
, "%d: failed setting lxc.init.cmd\n", __LINE__
);
168 if (!c
->set_config_item(c
, "lxc.execute.cmd", "echo hello")) {
169 fprintf(stderr
, "%d: failed setting lxc.init.cmd\n", __LINE__
);
173 c
->want_daemonize(c
, true);
175 /* Test whether we can start a really short-lived daemonized container. */
176 for (i
= 0; i
< 10; i
++) {
177 if (!c
->startl(c
, 0, NULL
)) {
178 fprintf(stderr
, "%d: %s failed to start on %dth iteration\n", __LINE__
, c
->name
, i
);
182 /* The container needs to exit with a successful error code. */
183 if (c
->error_num
!= 0) {
184 fprintf(stderr
, "%d: %s exited successfully on %dth iteration\n", __LINE__
, c
->name
, i
);
187 fprintf(stderr
, "%d: %s exited correctly with error code %d on %dth iteration\n", __LINE__
, c
->name
, c
->error_num
, i
);
189 if (!c
->wait(c
, "STOPPED", 30)) {
190 fprintf(stderr
, "%d: %s failed to wait on %dth iteration\n", __LINE__
, c
->name
, i
);
195 /* Test whether we can start a really short-lived daemonized container with lxc-init. */
196 for (i
= 0; i
< 10; i
++) {
197 if (!c
->startl(c
, 1, NULL
)) {
198 fprintf(stderr
, "%d: %s failed to start on %dth iteration\n", __LINE__
, c
->name
, i
);
202 /* The container needs to exit with a successful error code. */
203 if (c
->error_num
!= 0) {
204 fprintf(stderr
, "%d: %s exited successfully on %dth iteration\n", __LINE__
, c
->name
, i
);
207 fprintf(stderr
, "%d: %s exited correctly with error code %d on %dth iteration\n", __LINE__
, c
->name
, c
->error_num
, 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 /* The container needs to exit with an error code.*/
233 if (c
->error_num
== 0) {
234 fprintf(stderr
, "%d: %s exited successfully on %dth iteration\n", __LINE__
, c
->name
, i
);
237 fprintf(stderr
, "%d: %s exited correctly with error code %d on %dth iteration\n", __LINE__
, c
->name
, c
->error_num
, i
);
239 if (!c
->wait(c
, "STOPPED", 30)) {
240 fprintf(stderr
, "%d: %s failed to wait on %dth iteration\n", __LINE__
, c
->name
, i
);
245 /* Test whether we can start a really short-lived daemonized container with lxc-init. */
246 for (i
= 0; i
< 10; i
++) {
247 /* An container started with lxc-init will always start
248 * succesfully unless lxc-init has a bug.
250 if (!c
->startl(c
, 1, NULL
)) {
251 fprintf(stderr
, "%d: %s failed to start on %dth iteration\n", __LINE__
, c
->name
, i
);
255 /* The container needs to exit with an error code.*/
256 if (c
->error_num
== 0) {
257 fprintf(stderr
, "%d: %s exited successfully on %dth iteration\n", __LINE__
, c
->name
, i
);
260 fprintf(stderr
, "%d: %s exited correctly with error code %d on %dth iteration\n", __LINE__
, c
->name
, c
->error_num
, i
);
262 if (!c
->wait(c
, "STOPPED", 30)) {
263 fprintf(stderr
, "%d: %s failed to wait on %dth iteration\n", __LINE__
, c
->name
, i
);
270 fprintf(stderr
, "all lxc_container tests passed for %s\n", c
->name
);
278 lxc_container_put(c
);