]>
Commit | Line | Data |
---|---|---|
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 | ||
3df9fa82 CB |
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) { | |
258611dd CB |
46 | execlp("lxc-destroy", "lxc-destroy", "-f", "-n", MYNAME, NULL); |
47 | exit(EXIT_FAILURE); | |
3df9fa82 CB |
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) { | |
258611dd CB |
76 | execlp("lxc-create", "lxc-create", "-t", "busybox", "-n", MYNAME, NULL); |
77 | exit(EXIT_FAILURE); | |
3df9fa82 CB |
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 | { | |
77f76f31 | 98 | int i; |
3df9fa82 CB |
99 | const char *s; |
100 | bool b; | |
77f76f31 CB |
101 | struct lxc_container *c; |
102 | struct lxc_log log; | |
103 | char template[sizeof(P_tmpdir"/shortlived_XXXXXX")]; | |
3df9fa82 CB |
104 | int ret = 0; |
105 | ||
77f76f31 CB |
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 | ||
3df9fa82 CB |
125 | ret = 1; |
126 | /* test a real container */ | |
127 | c = lxc_container_new(MYNAME, NULL); | |
128 | if (!c) { | |
129 | fprintf(stderr, "%d: error creating lxc_container %s\n", __LINE__, MYNAME); | |
130 | ret = 1; | |
131 | goto out; | |
132 | } | |
133 | ||
134 | if (c->is_defined(c)) { | |
135 | fprintf(stderr, "%d: %s thought it was defined\n", __LINE__, MYNAME); | |
136 | goto out; | |
137 | } | |
138 | ||
139 | ret = create_container(); | |
140 | if (ret) { | |
141 | fprintf(stderr, "%d: failed to create a container\n", __LINE__); | |
142 | goto out; | |
143 | } | |
144 | ||
145 | b = c->is_defined(c); | |
146 | if (!b) { | |
147 | fprintf(stderr, "%d: %s thought it was not defined\n", __LINE__, MYNAME); | |
148 | goto out; | |
149 | } | |
150 | ||
151 | s = c->state(c); | |
152 | if (!s || strcmp(s, "STOPPED")) { | |
153 | fprintf(stderr, "%d: %s is in state %s, not in STOPPED.\n", __LINE__, c->name, s ? s : "undefined"); | |
154 | goto out; | |
155 | } | |
156 | ||
157 | b = c->load_config(c, NULL); | |
158 | if (!b) { | |
159 | fprintf(stderr, "%d: %s failed to read its config\n", __LINE__, c->name); | |
160 | goto out; | |
161 | } | |
162 | ||
163 | if (!c->set_config_item(c, "lxc.init.cmd", "echo hello")) { | |
164 | fprintf(stderr, "%d: failed setting lxc.init.cmd\n", __LINE__); | |
165 | goto out; | |
166 | } | |
167 | ||
77f76f31 CB |
168 | if (!c->set_config_item(c, "lxc.execute.cmd", "echo hello")) { |
169 | fprintf(stderr, "%d: failed setting lxc.init.cmd\n", __LINE__); | |
170 | goto out; | |
171 | } | |
172 | ||
3df9fa82 CB |
173 | c->want_daemonize(c, true); |
174 | ||
77f76f31 | 175 | /* Test whether we can start a really short-lived daemonized container. */ |
3df9fa82 CB |
176 | for (i = 0; i < 10; i++) { |
177 | if (!c->startl(c, 0, NULL)) { | |
77f76f31 CB |
178 | fprintf(stderr, "%d: %s failed to start on %dth iteration\n", __LINE__, c->name, i); |
179 | goto out; | |
180 | } | |
181 | ||
182 | /* The container needs to exit with a successful error code. */ | |
183 | if (c->error_num != 0) { | |
184 | fprintf(stderr, "%d: %s exited successfully on %dth iteration\n", __LINE__, c->name, i); | |
185 | goto out; | |
186 | } | |
187 | fprintf(stderr, "%d: %s exited correctly with error code %d on %dth iteration\n", __LINE__, c->name, c->error_num, i); | |
188 | ||
189 | if (!c->wait(c, "STOPPED", 30)) { | |
190 | fprintf(stderr, "%d: %s failed to wait on %dth iteration\n", __LINE__, c->name, i); | |
191 | goto out; | |
192 | } | |
193 | } | |
194 | ||
195 | /* Test whether we can start a really short-lived daemonized container with lxc-init. */ | |
196 | for (i = 0; i < 10; i++) { | |
197 | if (!c->startl(c, 1, NULL)) { | |
198 | fprintf(stderr, "%d: %s failed to start on %dth iteration\n", __LINE__, c->name, i); | |
199 | goto out; | |
200 | } | |
201 | ||
202 | /* The container needs to exit with a successful error code. */ | |
203 | if (c->error_num != 0) { | |
204 | fprintf(stderr, "%d: %s exited successfully on %dth iteration\n", __LINE__, c->name, i); | |
205 | goto out; | |
206 | } | |
207 | fprintf(stderr, "%d: %s exited correctly with error code %d on %dth iteration\n", __LINE__, c->name, c->error_num, i); | |
208 | ||
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 | ||
232 | /* The container needs to exit with an error code.*/ | |
233 | if (c->error_num == 0) { | |
234 | fprintf(stderr, "%d: %s exited successfully on %dth iteration\n", __LINE__, c->name, i); | |
235 | goto out; | |
236 | } | |
237 | fprintf(stderr, "%d: %s exited correctly with error code %d on %dth iteration\n", __LINE__, c->name, c->error_num, i); | |
238 | ||
239 | if (!c->wait(c, "STOPPED", 30)) { | |
240 | fprintf(stderr, "%d: %s failed to wait on %dth iteration\n", __LINE__, c->name, i); | |
241 | goto out; | |
242 | } | |
243 | } | |
244 | ||
245 | /* Test whether we can start a really short-lived daemonized container with lxc-init. */ | |
246 | for (i = 0; i < 10; i++) { | |
247 | /* An container started with lxc-init will always start | |
248 | * succesfully unless lxc-init has a bug. | |
249 | */ | |
250 | if (!c->startl(c, 1, NULL)) { | |
251 | fprintf(stderr, "%d: %s failed to start on %dth iteration\n", __LINE__, c->name, i); | |
252 | goto out; | |
253 | } | |
254 | ||
255 | /* The container needs to exit with an error code.*/ | |
256 | if (c->error_num == 0) { | |
257 | fprintf(stderr, "%d: %s exited successfully on %dth iteration\n", __LINE__, c->name, i); | |
258 | goto out; | |
259 | } | |
260 | fprintf(stderr, "%d: %s exited correctly with error code %d on %dth iteration\n", __LINE__, c->name, c->error_num, i); | |
261 | ||
262 | if (!c->wait(c, "STOPPED", 30)) { | |
263 | fprintf(stderr, "%d: %s failed to wait on %dth iteration\n", __LINE__, c->name, i); | |
3df9fa82 CB |
264 | goto out; |
265 | } | |
3df9fa82 CB |
266 | } |
267 | ||
268 | c->stop(c); | |
269 | ||
270 | fprintf(stderr, "all lxc_container tests passed for %s\n", c->name); | |
271 | ret = 0; | |
272 | ||
273 | out: | |
274 | if (c) { | |
275 | c->stop(c); | |
276 | destroy_container(); | |
277 | } | |
278 | lxc_container_put(c); | |
77f76f31 | 279 | unlink(template); |
3df9fa82 CB |
280 | exit(ret); |
281 | } |