]>
git.proxmox.com Git - mirror_lxc.git/blob - src/tests/containertests.c
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)
43 ret
= execlp("lxc-destroy", "lxc-destroy", "-f", "-n", MYNAME
, NULL
);
49 ret
= waitpid(pid
, &status
, 0);
58 if (!WIFEXITED(status
)) { // did not exit normally
59 fprintf(stderr
, "%d: lxc-create exited abnormally\n", __LINE__
);
62 return WEXITSTATUS(status
);
65 static int create_busybox(void)
75 ret
= execlp("lxc-create", "lxc-create", "-t", "busybox", "-n", MYNAME
, NULL
);
81 ret
= waitpid(pid
, &status
, 0);
90 if (!WIFEXITED(status
)) { // did not exit normally
91 fprintf(stderr
, "%d: lxc-create exited abnormally\n", __LINE__
);
94 return WEXITSTATUS(status
);
97 int main(int argc
, char *argv
[])
99 struct lxc_container
*c
;
106 /* test refcounting */
107 c
= lxc_container_new(MYNAME
, NULL
);
109 fprintf(stderr
, "%d: error creating lxc_container %s\n", __LINE__
, MYNAME
);
112 if (!lxc_container_get(c
)) {
113 fprintf(stderr
, "%d: error getting refcount\n", __LINE__
);
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);
121 if (strcmp(c
->name
, MYNAME
) != 0) {
122 fprintf(stderr
, "%d: container has wrong name (%s not %s)\n", __LINE__
, c
->name
, MYNAME
);
125 str
= c
->config_file_name(c
);
126 #define CONFIGFNAM LXCPATH "/" MYNAME "/config"
127 if (!str
|| strcmp(str
, CONFIGFNAM
)) {
128 fprintf(stderr
, "%d: got wrong config file name (%s, not %s)\n", __LINE__
, str
, CONFIGFNAM
);
133 c
->configfile
= NULL
;
134 str
= c
->config_file_name(c
);
136 fprintf(stderr
, "%d: config file name was not NULL as it should have been\n", __LINE__
);
139 if (lxc_container_put(c
) != 0) {
140 fprintf(stderr
, "%d: c was freed on non-final put\n", __LINE__
);
143 if (c
->numthreads
!= 1) {
144 fprintf(stderr
, "%d: refcount is %d, not %d\n", __LINE__
, c
->numthreads
, 1);
147 if (lxc_container_put(c
) != 1) {
148 fprintf(stderr
, "%d: c was not freed on final put\n", __LINE__
);
152 /* test a real container */
153 c
= lxc_container_new(MYNAME
, NULL
);
155 fprintf(stderr
, "%d: error creating lxc_container %s\n", __LINE__
, MYNAME
);
160 b
= c
->is_defined(c
);
162 fprintf(stderr
, "%d: %s thought it was defined\n", __LINE__
, MYNAME
);
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
);
175 // create a container
176 // the liblxc api does not support creation - it probably will eventually,
178 // So we just call out to lxc-create. We'll create a busybox container.
179 ret
= create_busybox();
181 fprintf(stderr
, "%d: failed to create a busybox container\n", __LINE__
);
185 b
= c
->is_defined(c
);
187 fprintf(stderr
, "%d: %s thought it was not defined\n", __LINE__
, MYNAME
);
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");
197 b
= c
->load_config(c
, NULL
);
199 fprintf(stderr
, "%d: %s failed to read its config\n", __LINE__
, c
->name
);
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
);
209 const char **sstr
= malloc(numstates
* sizeof(const char *));
210 numstates
= lxc_get_wait_states(sstr
);
212 for (i
=0; i
<numstates
; i
++) {
213 fprintf(stderr
, "got state %d %s\n", i
, sstr
[i
]);
217 /* non-daemonized is tested in 'startone' */
218 c
->want_daemonize(c
, true);
219 if (!c
->startl(c
, 0, NULL
, NULL
)) {
220 fprintf(stderr
, "%d: %s failed to start daemonized\n", __LINE__
, c
->name
);
224 if (!c
->wait(c
, "RUNNING", -1)) {
225 fprintf(stderr
, "%d: failed waiting for state RUNNING\n", __LINE__
);
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");
236 fprintf(stderr
, "all lxc_container tests passed for %s\n", c
->name
);
244 lxc_container_put(c
);