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