]> git.proxmox.com Git - mirror_lxc.git/blob - src/tests/containertests.c
0fb6fbdfbfa9690c5572b95edb7f68d5e3c0c0d6
[mirror_lxc.git] / src / tests / containertests.c
1 /* liblxcapi
2 *
3 * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>.
4 * Copyright © 2012 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 <errno.h>
28 #include <string.h>
29 #include "lxc/state.h"
30
31 #define MYNAME "lxctest1"
32
33 static int destroy_busybox(void)
34 {
35 int status, ret;
36 pid_t pid = fork();
37
38 if (pid < 0) {
39 perror("fork");
40 return -1;
41 }
42
43 if (pid == 0) {
44 execlp("lxc-destroy", "lxc-destroy", "-f", "-n", MYNAME, NULL);
45 exit(EXIT_FAILURE);
46 }
47
48 again:
49 ret = waitpid(pid, &status, 0);
50 if (ret == -1) {
51 if (errno == EINTR)
52 goto again;
53 perror("waitpid");
54 return -1;
55 }
56
57 if (ret != pid)
58 goto again;
59
60 if (!WIFEXITED(status)) { // did not exit normally
61 fprintf(stderr, "%d: lxc-create exited abnormally\n", __LINE__);
62 return -1;
63 }
64
65 return WEXITSTATUS(status);
66 }
67
68 static int create_busybox(void)
69 {
70 int status, ret;
71 pid_t pid = fork();
72
73 if (pid < 0) {
74 perror("fork");
75 return -1;
76 }
77
78 if (pid == 0) {
79 execlp("lxc-create", "lxc-create", "-t", "busybox", "-n", MYNAME, NULL);
80 exit(EXIT_FAILURE);
81 }
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
92 if (ret != pid)
93 goto again;
94
95 if (!WIFEXITED(status)) { // did not exit normally
96 fprintf(stderr, "%d: lxc-create exited abnormally\n", __LINE__);
97 return -1;
98 }
99
100 return WEXITSTATUS(status);
101 }
102
103 int main(int argc, char *argv[])
104 {
105 struct lxc_container *c;
106 int ret = 0;
107 const char *s;
108 bool b;
109 char *str;
110
111 ret = 1;
112
113 /* test refcounting */
114 c = lxc_container_new(MYNAME, NULL);
115 if (!c) {
116 fprintf(stderr, "%d: error creating lxc_container %s\n", __LINE__, MYNAME);
117 goto out;
118 }
119
120 if (!lxc_container_get(c)) {
121 fprintf(stderr, "%d: error getting refcount\n", __LINE__);
122 goto out;
123 }
124
125 /* peek in, inappropriately, make sure refcount is a we'd like */
126 if (c->numthreads != 2) {
127 fprintf(stderr, "%d: refcount is %d, not %d\n", __LINE__, c->numthreads, 2);
128 goto out;
129 }
130
131 if (strcmp(c->name, MYNAME) != 0) {
132 fprintf(stderr, "%d: container has wrong name (%s not %s)\n", __LINE__, c->name, MYNAME);
133 goto out;
134 }
135
136 str = c->config_file_name(c);
137 #define CONFIGFNAM LXCPATH "/" MYNAME "/config"
138 if (str && strcmp(str, CONFIGFNAM)) {
139 fprintf(stderr, "%d: got wrong config file name (%s, not %s)\n", __LINE__, str, CONFIGFNAM);
140 goto out;
141 }
142 free(str);
143 free(c->configfile);
144 c->configfile = NULL;
145
146 str = c->config_file_name(c);
147 if (str) {
148 fprintf(stderr, "%d: config file name was not NULL as it should have been\n", __LINE__);
149 goto out;
150 }
151
152 ret = lxc_container_put(c);
153 if (ret < 0) {
154 fprintf(stderr, "%d: c is invalid pointer\n", __LINE__);
155 ret = 1;
156 goto out;
157 }
158 else if (ret == 1) {
159 fprintf(stderr, "%d: c was freed on non-final put\n", __LINE__);
160 c = NULL;
161 goto out;
162 }
163
164 if (c->numthreads != 1) {
165 fprintf(stderr, "%d: refcount is %d, not %d\n", __LINE__, c->numthreads, 1);
166 goto out;
167 }
168
169 if (lxc_container_put(c) != 1) {
170 fprintf(stderr, "%d: c was not freed on final put\n", __LINE__);
171 goto out;
172 }
173
174 /* test a real container */
175 c = lxc_container_new(MYNAME, NULL);
176 if (!c) {
177 fprintf(stderr, "%d: error creating lxc_container %s\n", __LINE__, MYNAME);
178 ret = 1;
179 goto out;
180 }
181
182 b = c->is_defined(c);
183 if (b) {
184 fprintf(stderr, "%d: %s thought it was defined\n", __LINE__, MYNAME);
185 goto out;
186 }
187
188 s = c->state(c);
189 if (s && strcmp(s, "STOPPED") != 0) {
190 // liblxc says a container is STOPPED if it doesn't exist. That's because
191 // the container may be an application container - it's not wrong, just
192 // sometimes unintuitive.
193 fprintf(stderr, "%d: %s thinks it is in state %s\n", __LINE__, c->name, s);
194 goto out;
195 }
196
197 // create a container
198 // the liblxc api does not support creation - it probably will eventually,
199 // but not yet.
200 // So we just call out to lxc-create. We'll create a busybox container.
201 ret = create_busybox();
202 if (ret) {
203 fprintf(stderr, "%d: failed to create a busybox container\n", __LINE__);
204 goto out;
205 }
206
207 b = c->is_defined(c);
208 if (!b) {
209 fprintf(stderr, "%d: %s thought it was not defined\n", __LINE__, MYNAME);
210 goto out;
211 }
212
213 s = c->state(c);
214 if (!s || strcmp(s, "STOPPED")) {
215 fprintf(stderr, "%d: %s is in state %s, not in STOPPED.\n", __LINE__, c->name, s ? s : "undefined");
216 goto out;
217 }
218
219 b = c->load_config(c, NULL);
220 if (!b) {
221 fprintf(stderr, "%d: %s failed to read its config\n", __LINE__, c->name);
222 goto out;
223 }
224
225 // test wait states
226 int numstates = lxc_get_wait_states(NULL);
227 if (numstates != MAX_STATE) {
228 fprintf(stderr, "%d: lxc_get_wait_states gave %d not %d\n", __LINE__, numstates, MAX_STATE);
229 goto out;
230 }
231
232 const char **sstr = malloc(numstates * sizeof(const char *));
233 numstates = lxc_get_wait_states(sstr);
234 int i;
235
236 for (i=0; i<numstates; i++) {
237 fprintf(stderr, "got state %d %s\n", i, sstr[i]);
238 }
239 free(sstr);
240
241 /* non-daemonized is tested in 'startone' */
242 c->want_daemonize(c, true);
243 if (!c->startl(c, 0, NULL, NULL)) {
244 fprintf(stderr, "%d: %s failed to start daemonized\n", __LINE__, c->name);
245 goto out;
246 }
247
248 if (!c->wait(c, "RUNNING", -1)) {
249 fprintf(stderr, "%d: failed waiting for state RUNNING\n", __LINE__);
250 goto out;
251 }
252
253 sleep(3);
254 s = c->state(c);
255 if (!s || strcmp(s, "RUNNING")) {
256 fprintf(stderr, "%d: %s is in state %s, not in RUNNING.\n", __LINE__, c->name, s ? s : "undefined");
257 goto out;
258 }
259
260 fprintf(stderr, "all lxc_container tests passed for %s\n", c->name);
261 ret = 0;
262
263 out:
264 if (c) {
265 c->stop(c);
266 destroy_busybox();
267 lxc_container_put(c);
268 }
269
270 exit(ret);
271 }