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