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