]>
Commit | Line | Data |
---|---|---|
72d0e1cb SG |
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 | */ | |
948955a2 | 19 | #include <lxc/lxccontainer.h> |
72d0e1cb SG |
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> | |
95ee490b | 28 | #include <string.h> |
f2363e38 | 29 | #include "lxc/state.h" |
72d0e1cb SG |
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 | } | |
00e196f2 | 42 | |
72d0e1cb | 43 | if (pid == 0) { |
258611dd CB |
44 | execlp("lxc-destroy", "lxc-destroy", "-f", "-n", MYNAME, NULL); |
45 | exit(EXIT_FAILURE); | |
72d0e1cb | 46 | } |
00e196f2 | 47 | |
72d0e1cb SG |
48 | again: |
49 | ret = waitpid(pid, &status, 0); | |
50 | if (ret == -1) { | |
21e624d9 | 51 | if (errno == EINTR) |
72d0e1cb SG |
52 | goto again; |
53 | perror("waitpid"); | |
54 | return -1; | |
55 | } | |
00e196f2 | 56 | |
72d0e1cb SG |
57 | if (ret != pid) |
58 | goto again; | |
00e196f2 | 59 | |
72d0e1cb SG |
60 | if (!WIFEXITED(status)) { // did not exit normally |
61 | fprintf(stderr, "%d: lxc-create exited abnormally\n", __LINE__); | |
62 | return -1; | |
63 | } | |
00e196f2 | 64 | |
72d0e1cb SG |
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 | } | |
00e196f2 | 77 | |
72d0e1cb | 78 | if (pid == 0) { |
258611dd CB |
79 | execlp("lxc-create", "lxc-create", "-t", "busybox", "-n", MYNAME, NULL); |
80 | exit(EXIT_FAILURE); | |
72d0e1cb | 81 | } |
00e196f2 | 82 | |
72d0e1cb SG |
83 | again: |
84 | ret = waitpid(pid, &status, 0); | |
85 | if (ret == -1) { | |
21e624d9 | 86 | if (errno == EINTR) |
72d0e1cb SG |
87 | goto again; |
88 | perror("waitpid"); | |
89 | return -1; | |
90 | } | |
00e196f2 | 91 | |
72d0e1cb SG |
92 | if (ret != pid) |
93 | goto again; | |
00e196f2 | 94 | |
72d0e1cb SG |
95 | if (!WIFEXITED(status)) { // did not exit normally |
96 | fprintf(stderr, "%d: lxc-create exited abnormally\n", __LINE__); | |
97 | return -1; | |
98 | } | |
00e196f2 | 99 | |
72d0e1cb SG |
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; | |
00e196f2 | 112 | |
72d0e1cb | 113 | /* test refcounting */ |
afeecbba | 114 | c = lxc_container_new(MYNAME, NULL); |
72d0e1cb SG |
115 | if (!c) { |
116 | fprintf(stderr, "%d: error creating lxc_container %s\n", __LINE__, MYNAME); | |
117 | goto out; | |
118 | } | |
00e196f2 | 119 | |
72d0e1cb SG |
120 | if (!lxc_container_get(c)) { |
121 | fprintf(stderr, "%d: error getting refcount\n", __LINE__); | |
122 | goto out; | |
123 | } | |
00e196f2 | 124 | |
72d0e1cb SG |
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 | } | |
00e196f2 | 130 | |
72d0e1cb SG |
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 | } | |
00e196f2 | 135 | |
72d0e1cb | 136 | str = c->config_file_name(c); |
e29bf450 | 137 | #define CONFIGFNAM LXCPATH "/" MYNAME "/config" |
72d0e1cb SG |
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; | |
00e196f2 | 145 | |
72d0e1cb SG |
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 | } | |
00e196f2 | 151 | |
12a1a872 | 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) { | |
72d0e1cb | 159 | fprintf(stderr, "%d: c was freed on non-final put\n", __LINE__); |
12a1a872 | 160 | c = NULL; |
72d0e1cb SG |
161 | goto out; |
162 | } | |
00e196f2 | 163 | |
72d0e1cb SG |
164 | if (c->numthreads != 1) { |
165 | fprintf(stderr, "%d: refcount is %d, not %d\n", __LINE__, c->numthreads, 1); | |
166 | goto out; | |
167 | } | |
00e196f2 | 168 | |
72d0e1cb SG |
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 */ | |
afeecbba | 175 | c = lxc_container_new(MYNAME, NULL); |
72d0e1cb SG |
176 | if (!c) { |
177 | fprintf(stderr, "%d: error creating lxc_container %s\n", __LINE__, MYNAME); | |
178 | ret = 1; | |
179 | goto out; | |
180 | } | |
181 | ||
72d0e1cb SG |
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 | } | |
00e196f2 | 231 | |
4a7c7daa | 232 | const char **sstr = malloc(numstates * sizeof(const char *)); |
72d0e1cb SG |
233 | numstates = lxc_get_wait_states(sstr); |
234 | int i; | |
00e196f2 | 235 | |
72d0e1cb SG |
236 | for (i=0; i<numstates; i++) { |
237 | fprintf(stderr, "got state %d %s\n", i, sstr[i]); | |
238 | } | |
239 | free(sstr); | |
240 | ||
72d0e1cb | 241 | /* non-daemonized is tested in 'startone' */ |
540f932a | 242 | c->want_daemonize(c, true); |
72d0e1cb SG |
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 | ||
72d0e1cb SG |
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(); | |
12a1a872 | 267 | lxc_container_put(c); |
72d0e1cb | 268 | } |
00e196f2 | 269 | |
72d0e1cb SG |
270 | exit(ret); |
271 | } |