]> git.proxmox.com Git - mirror_lxc.git/blob - src/tests/shortlived.c
spelling: successfully
[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 #include "lxctest.h"
32 #include "utils.h"
33
34 #ifndef HAVE_STRLCPY
35 #include "include/strlcpy.h"
36 #endif
37
38 #define MYNAME "shortlived"
39
40 static 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 }
49
50 if (pid == 0) {
51 execlp("lxc-destroy", "lxc-destroy", "-f", "-n", MYNAME, NULL);
52 exit(EXIT_FAILURE);
53 }
54
55 again:
56 ret = waitpid(pid, &status, 0);
57 if (ret == -1) {
58 if (errno == EINTR)
59 goto again;
60 perror("waitpid");
61 return -1;
62 }
63
64 if (ret != pid)
65 goto again;
66
67 if (!WIFEXITED(status)) { // did not exit normally
68 fprintf(stderr, "%d: lxc-create exited abnormally\n", __LINE__);
69 return -1;
70 }
71
72 return WEXITSTATUS(status);
73 }
74
75 static 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 }
84
85 if (pid == 0) {
86 execlp("lxc-create", "lxc-create", "-t", "busybox", "-n", MYNAME, NULL);
87 exit(EXIT_FAILURE);
88 }
89
90 again:
91 ret = waitpid(pid, &status, 0);
92 if (ret == -1) {
93 if (errno == EINTR)
94 goto again;
95
96 perror("waitpid");
97 return -1;
98 }
99
100 if (ret != pid)
101 goto again;
102
103 if (!WIFEXITED(status)) { // did not exit normally
104 fprintf(stderr, "%d: lxc-create exited abnormally\n", __LINE__);
105 return -1;
106 }
107
108 return WEXITSTATUS(status);
109 }
110
111 int main(int argc, char *argv[])
112 {
113 int fd, i;
114 const char *s;
115 bool b;
116 struct lxc_container *c;
117 struct lxc_log log;
118 char template[sizeof(P_tmpdir"/shortlived_XXXXXX")];
119 int ret = EXIT_FAILURE;
120
121 (void)strlcpy(template, P_tmpdir"/shortlived_XXXXXX", sizeof(template));
122
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;
138
139 if (lxc_log_init(&log))
140 exit(EXIT_FAILURE);
141
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);
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
154 if (create_container() < 0) {
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
182 if (!c->set_config_item(c, "lxc.execute.cmd", "echo hello")) {
183 fprintf(stderr, "%d: failed setting lxc.execute.cmd\n", __LINE__);
184 goto out;
185 }
186
187 c->want_daemonize(c, true);
188
189 /* Test whether we can start a really short-lived daemonized container. */
190 for (i = 0; i < 10; i++) {
191 if (!c->startl(c, 0, NULL)) {
192 fprintf(stderr, "%d: %s failed to start on %dth iteration\n", __LINE__, c->name, i);
193 goto out;
194 }
195
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
209 if (!c->wait(c, "STOPPED", 30)) {
210 fprintf(stderr, "%d: %s failed to wait on %dth iteration\n", __LINE__, c->name, i);
211 goto out;
212 }
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
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. */
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);
229 goto out;
230 }
231
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
241 * successfully unless lxc-init has a bug.
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
248 if (!c->wait(c, "STOPPED", 30)) {
249 fprintf(stderr, "%d: %s failed to wait on %dth iteration\n", __LINE__, c->name, i);
250 goto out;
251 }
252 }
253
254 c->stop(c);
255
256 fprintf(stderr, "all lxc_container tests passed for %s\n", c->name);
257 ret = 0;
258
259 out:
260 if (c) {
261 c->stop(c);
262 destroy_container();
263 }
264 lxc_container_put(c);
265
266 if (ret != 0) {
267 fd = open(template, O_RDONLY);
268 if (fd >= 0) {
269 char buf[4096];
270 ssize_t buflen;
271
272 while ((buflen = read(fd, buf, 1024)) > 0) {
273 buflen = write(STDERR_FILENO, buf, buflen);
274 if (buflen <= 0)
275 break;
276 }
277
278 close(fd);
279 }
280 }
281
282 unlink(template);
283 exit(ret);
284 }