]> git.proxmox.com Git - mirror_lxc.git/blob - src/tests/shortlived.c
test: shortlived daemonized containers
[mirror_lxc.git] / src / tests / shortlived.c
1 /* liblxcapi
2 *
3 * Copyright © 2017 Christian Brauner <christian.brauner@ubuntu.com>.
4 * Copyright © 2017 Canonical Ltd.
5 *
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.
9 *
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.
14 *
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.
18 */
19 #include <lxc/lxccontainer.h>
20
21 #include <unistd.h>
22 #include <signal.h>
23 #include <stdio.h>
24 #include <sys/types.h>
25 #include <sys/wait.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <fcntl.h>
30
31 #define MYNAME "shortlived"
32
33 static int destroy_container(void)
34 {
35 int status, ret;
36 pid_t pid = fork();
37
38 if (pid < 0) {
39 perror("fork");
40 return -1;
41 }
42 if (pid == 0) {
43 ret = execlp("lxc-destroy", "lxc-destroy", "-f", "-n", MYNAME, NULL);
44 // Should not return
45 perror("execl");
46 exit(1);
47 }
48 again:
49 ret = waitpid(pid, &status, 0);
50 if (ret == -1) {
51 if (errno == EINTR)
52 goto again;
53 perror("waitpid");
54 return -1;
55 }
56 if (ret != pid)
57 goto again;
58 if (!WIFEXITED(status)) { // did not exit normally
59 fprintf(stderr, "%d: lxc-create exited abnormally\n", __LINE__);
60 return -1;
61 }
62 return WEXITSTATUS(status);
63 }
64
65 static int create_container(void)
66 {
67 int status, ret;
68 pid_t pid = fork();
69
70 if (pid < 0) {
71 perror("fork");
72 return -1;
73 }
74 if (pid == 0) {
75 ret = execlp("lxc-create", "lxc-create", "-t", "busybox", "-n", MYNAME, NULL);
76 // Should not return
77 perror("execl");
78 exit(1);
79 }
80 again:
81 ret = waitpid(pid, &status, 0);
82 if (ret == -1) {
83 if (errno == EINTR)
84 goto again;
85 perror("waitpid");
86 return -1;
87 }
88 if (ret != pid)
89 goto again;
90 if (!WIFEXITED(status)) { // did not exit normally
91 fprintf(stderr, "%d: lxc-create exited abnormally\n", __LINE__);
92 return -1;
93 }
94 return WEXITSTATUS(status);
95 }
96
97 int main(int argc, char *argv[])
98 {
99 struct lxc_container *c;
100 const char *s;
101 bool b;
102 int i;
103 int ret = 0;
104
105 ret = 1;
106 /* test a real container */
107 c = lxc_container_new(MYNAME, NULL);
108 if (!c) {
109 fprintf(stderr, "%d: error creating lxc_container %s\n", __LINE__, MYNAME);
110 ret = 1;
111 goto out;
112 }
113
114 if (c->is_defined(c)) {
115 fprintf(stderr, "%d: %s thought it was defined\n", __LINE__, MYNAME);
116 goto out;
117 }
118
119 ret = create_container();
120 if (ret) {
121 fprintf(stderr, "%d: failed to create a container\n", __LINE__);
122 goto out;
123 }
124
125 b = c->is_defined(c);
126 if (!b) {
127 fprintf(stderr, "%d: %s thought it was not defined\n", __LINE__, MYNAME);
128 goto out;
129 }
130
131 s = c->state(c);
132 if (!s || strcmp(s, "STOPPED")) {
133 fprintf(stderr, "%d: %s is in state %s, not in STOPPED.\n", __LINE__, c->name, s ? s : "undefined");
134 goto out;
135 }
136
137 b = c->load_config(c, NULL);
138 if (!b) {
139 fprintf(stderr, "%d: %s failed to read its config\n", __LINE__, c->name);
140 goto out;
141 }
142
143 if (!c->set_config_item(c, "lxc.init.cmd", "echo hello")) {
144 fprintf(stderr, "%d: failed setting lxc.init.cmd\n", __LINE__);
145 goto out;
146 }
147
148 c->want_daemonize(c, true);
149
150 /* Test whether we can start a really short-lived daemonized container.
151 */
152 for (i = 0; i < 10; i++) {
153 if (!c->startl(c, 0, NULL)) {
154 fprintf(stderr, "%d: %s failed to start\n", __LINE__, c->name);
155 goto out;
156 }
157 sleep(1);
158 }
159
160 if (!c->set_config_item(c, "lxc.init.cmd", "you-shall-fail")) {
161 fprintf(stderr, "%d: failed setting lxc.init.cmd\n", __LINE__);
162 goto out;
163 }
164
165 /* Test whether we catch the start failure of a really short-lived
166 * daemonized container.
167 */
168 for (i = 0; i < 10; i++) {
169 if (c->startl(c, 0, NULL)) {
170 fprintf(stderr, "%d: %s failed to start\n", __LINE__, c->name);
171 goto out;
172 }
173 sleep(1);
174 }
175
176 c->stop(c);
177
178 fprintf(stderr, "all lxc_container tests passed for %s\n", c->name);
179 ret = 0;
180
181 out:
182 if (c) {
183 c->stop(c);
184 destroy_container();
185 }
186 lxc_container_put(c);
187 exit(ret);
188 }