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