]> git.proxmox.com Git - mirror_lxc.git/blob - src/tests/shortlived.c
tests: include config.h
[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
20 #include "config.h"
21
22 #include <lxc/lxccontainer.h>
23
24 #include <unistd.h>
25 #include <signal.h>
26 #include <stdio.h>
27 #include <sys/types.h>
28 #include <sys/wait.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <fcntl.h>
33
34 #include "lxctest.h"
35 #include "utils.h"
36
37 #if !HAVE_STRLCPY
38 #include "strlcpy.h"
39 #endif
40
41 #define MYNAME "shortlived"
42
43 static int destroy_container(void)
44 {
45 int status, ret;
46 pid_t pid = fork();
47
48 if (pid < 0) {
49 perror("fork");
50 return -1;
51 }
52
53 if (pid == 0) {
54 execlp("lxc-destroy", "lxc-destroy", "-f", "-n", MYNAME, NULL);
55 exit(EXIT_FAILURE);
56 }
57
58 again:
59 ret = waitpid(pid, &status, 0);
60 if (ret == -1) {
61 if (errno == EINTR)
62 goto again;
63 perror("waitpid");
64 return -1;
65 }
66
67 if (ret != pid)
68 goto again;
69
70 if (!WIFEXITED(status)) { // did not exit normally
71 fprintf(stderr, "%d: lxc-create exited abnormally\n", __LINE__);
72 return -1;
73 }
74
75 return WEXITSTATUS(status);
76 }
77
78 static int create_container(void)
79 {
80 int status, ret;
81 pid_t pid = fork();
82
83 if (pid < 0) {
84 perror("fork");
85 return -1;
86 }
87
88 if (pid == 0) {
89 execlp("lxc-create", "lxc-create", "-t", "busybox", "-n", MYNAME, NULL);
90 exit(EXIT_FAILURE);
91 }
92
93 again:
94 ret = waitpid(pid, &status, 0);
95 if (ret == -1) {
96 if (errno == EINTR)
97 goto again;
98
99 perror("waitpid");
100 return -1;
101 }
102
103 if (ret != pid)
104 goto again;
105
106 if (!WIFEXITED(status)) { // did not exit normally
107 fprintf(stderr, "%d: lxc-create exited abnormally\n", __LINE__);
108 return -1;
109 }
110
111 return WEXITSTATUS(status);
112 }
113
114 int main(int argc, char *argv[])
115 {
116 int fd, i;
117 const char *s;
118 bool b;
119 struct lxc_container *c;
120 struct lxc_log log;
121 char template[sizeof(P_tmpdir"/shortlived_XXXXXX")];
122 int ret = EXIT_FAILURE;
123
124 (void)strlcpy(template, P_tmpdir"/shortlived_XXXXXX", sizeof(template));
125
126 i = lxc_make_tmpfile(template, false);
127 if (i < 0) {
128 lxc_error("Failed to create temporary log file for container %s\n", MYNAME);
129 exit(EXIT_FAILURE);
130 } else {
131 lxc_debug("Using \"%s\" as temporary log file for container %s\n", template, MYNAME);
132 close(i);
133 }
134
135 log.name = MYNAME;
136 log.file = template;
137 log.level = "TRACE";
138 log.prefix = "shortlived";
139 log.quiet = false;
140 log.lxcpath = NULL;
141
142 if (lxc_log_init(&log))
143 exit(EXIT_FAILURE);
144
145 /* test a real container */
146 c = lxc_container_new(MYNAME, NULL);
147 if (!c) {
148 fprintf(stderr, "%d: error creating lxc_container %s\n", __LINE__, MYNAME);
149 goto out;
150 }
151
152 if (c->is_defined(c)) {
153 fprintf(stderr, "%d: %s thought it was defined\n", __LINE__, MYNAME);
154 goto out;
155 }
156
157 if (create_container() < 0) {
158 fprintf(stderr, "%d: failed to create a container\n", __LINE__);
159 goto out;
160 }
161
162 b = c->is_defined(c);
163 if (!b) {
164 fprintf(stderr, "%d: %s thought it was not defined\n", __LINE__, MYNAME);
165 goto out;
166 }
167
168 s = c->state(c);
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");
171 goto out;
172 }
173
174 b = c->load_config(c, NULL);
175 if (!b) {
176 fprintf(stderr, "%d: %s failed to read its config\n", __LINE__, c->name);
177 goto out;
178 }
179
180 if (!c->set_config_item(c, "lxc.init.cmd", "echo hello")) {
181 fprintf(stderr, "%d: failed setting lxc.init.cmd\n", __LINE__);
182 goto out;
183 }
184
185 if (!c->set_config_item(c, "lxc.execute.cmd", "echo hello")) {
186 fprintf(stderr, "%d: failed setting lxc.execute.cmd\n", __LINE__);
187 goto out;
188 }
189
190 c->want_daemonize(c, true);
191
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);
196 goto out;
197 }
198
199 if (!c->wait(c, "STOPPED", 30)) {
200 fprintf(stderr, "%d: %s failed to wait on %dth iteration\n", __LINE__, c->name, i);
201 goto out;
202 }
203 }
204
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);
209 goto out;
210 }
211
212 if (!c->wait(c, "STOPPED", 30)) {
213 fprintf(stderr, "%d: %s failed to wait on %dth iteration\n", __LINE__, c->name, i);
214 goto out;
215 }
216 }
217
218 if (!c->set_config_item(c, "lxc.init.cmd", "you-shall-fail")) {
219 fprintf(stderr, "%d: failed setting lxc.init.cmd\n", __LINE__);
220 goto out;
221 }
222
223 if (!c->set_config_item(c, "lxc.execute.cmd", "you-shall-fail")) {
224 fprintf(stderr, "%d: failed setting lxc.init.cmd\n", __LINE__);
225 goto out;
226 }
227
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);
232 goto out;
233 }
234
235 if (!c->wait(c, "STOPPED", 30)) {
236 fprintf(stderr, "%d: %s failed to wait on %dth iteration\n", __LINE__, c->name, i);
237 goto out;
238 }
239 }
240
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.
245 */
246 if (!c->startl(c, 1, NULL)) {
247 fprintf(stderr, "%d: %s failed to start on %dth iteration\n", __LINE__, c->name, i);
248 goto out;
249 }
250
251 if (!c->wait(c, "STOPPED", 30)) {
252 fprintf(stderr, "%d: %s failed to wait on %dth iteration\n", __LINE__, c->name, i);
253 goto out;
254 }
255 }
256
257 c->stop(c);
258
259 fprintf(stderr, "all lxc_container tests passed for %s\n", c->name);
260 ret = 0;
261
262 out:
263 if (c) {
264 c->stop(c);
265 destroy_container();
266 }
267 lxc_container_put(c);
268
269 if (ret != 0) {
270 fd = open(template, O_RDONLY);
271 if (fd >= 0) {
272 char buf[4096];
273 ssize_t buflen;
274
275 while ((buflen = read(fd, buf, 1024)) > 0) {
276 buflen = write(STDERR_FILENO, buf, buflen);
277 if (buflen <= 0)
278 break;
279 }
280
281 close(fd);
282 }
283 }
284
285 unlink(template);
286 exit(ret);
287 }