]>
git.proxmox.com Git - mirror_lxc.git/blob - src/tests/containertests.c
0fb6fbdfbfa9690c5572b95edb7f68d5e3c0c0d6
3 * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>.
4 * Copyright © 2012 Canonical Ltd.
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.
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.
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.
19 #include <lxc/lxccontainer.h>
24 #include <sys/types.h>
29 #include "lxc/state.h"
31 #define MYNAME "lxctest1"
33 static int destroy_busybox(void)
44 execlp("lxc-destroy", "lxc-destroy", "-f", "-n", MYNAME
, NULL
);
49 ret
= waitpid(pid
, &status
, 0);
60 if (!WIFEXITED(status
)) { // did not exit normally
61 fprintf(stderr
, "%d: lxc-create exited abnormally\n", __LINE__
);
65 return WEXITSTATUS(status
);
68 static int create_busybox(void)
79 execlp("lxc-create", "lxc-create", "-t", "busybox", "-n", MYNAME
, NULL
);
84 ret
= waitpid(pid
, &status
, 0);
95 if (!WIFEXITED(status
)) { // did not exit normally
96 fprintf(stderr
, "%d: lxc-create exited abnormally\n", __LINE__
);
100 return WEXITSTATUS(status
);
103 int main(int argc
, char *argv
[])
105 struct lxc_container
*c
;
113 /* test refcounting */
114 c
= lxc_container_new(MYNAME
, NULL
);
116 fprintf(stderr
, "%d: error creating lxc_container %s\n", __LINE__
, MYNAME
);
120 if (!lxc_container_get(c
)) {
121 fprintf(stderr
, "%d: error getting refcount\n", __LINE__
);
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);
131 if (strcmp(c
->name
, MYNAME
) != 0) {
132 fprintf(stderr
, "%d: container has wrong name (%s not %s)\n", __LINE__
, c
->name
, MYNAME
);
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
);
144 c
->configfile
= NULL
;
146 str
= c
->config_file_name(c
);
148 fprintf(stderr
, "%d: config file name was not NULL as it should have been\n", __LINE__
);
152 ret
= lxc_container_put(c
);
154 fprintf(stderr
, "%d: c is invalid pointer\n", __LINE__
);
159 fprintf(stderr
, "%d: c was freed on non-final put\n", __LINE__
);
164 if (c
->numthreads
!= 1) {
165 fprintf(stderr
, "%d: refcount is %d, not %d\n", __LINE__
, c
->numthreads
, 1);
169 if (lxc_container_put(c
) != 1) {
170 fprintf(stderr
, "%d: c was not freed on final put\n", __LINE__
);
174 /* test a real container */
175 c
= lxc_container_new(MYNAME
, NULL
);
177 fprintf(stderr
, "%d: error creating lxc_container %s\n", __LINE__
, MYNAME
);
182 b
= c
->is_defined(c
);
184 fprintf(stderr
, "%d: %s thought it was defined\n", __LINE__
, MYNAME
);
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
);
197 // create a container
198 // the liblxc api does not support creation - it probably will eventually,
200 // So we just call out to lxc-create. We'll create a busybox container.
201 ret
= create_busybox();
203 fprintf(stderr
, "%d: failed to create a busybox container\n", __LINE__
);
207 b
= c
->is_defined(c
);
209 fprintf(stderr
, "%d: %s thought it was not defined\n", __LINE__
, MYNAME
);
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");
219 b
= c
->load_config(c
, NULL
);
221 fprintf(stderr
, "%d: %s failed to read its config\n", __LINE__
, c
->name
);
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
);
232 const char **sstr
= malloc(numstates
* sizeof(const char *));
233 numstates
= lxc_get_wait_states(sstr
);
236 for (i
=0; i
<numstates
; i
++) {
237 fprintf(stderr
, "got state %d %s\n", i
, sstr
[i
]);
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
);
248 if (!c
->wait(c
, "RUNNING", -1)) {
249 fprintf(stderr
, "%d: failed waiting for state RUNNING\n", __LINE__
);
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");
260 fprintf(stderr
, "all lxc_container tests passed for %s\n", c
->name
);
267 lxc_container_put(c
);