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