containertests.c revision f2363e38d07db2ad4796b5580b95e3665ed34a88
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen/* liblxcapi
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen *
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>.
bdd36cfdba3ff66d25570a9ff568d69e1eb543cfTimo Sirainen * Copyright © 2012 Canonical Ltd.
1299f2c3723ca9ccf8f9e563ec23ee1a1721fe4cTimo Sirainen *
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen * This program is free software; you can redistribute it and/or modify
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen * it under the terms of the GNU General Public License version 2, as
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen * published by the Free Software Foundation.
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen *
73bfdbe28c2ce6d143eadf0bab8ccfbe4cab0faeTimo Sirainen * This program is distributed in the hope that it will be useful,
73bfdbe28c2ce6d143eadf0bab8ccfbe4cab0faeTimo Sirainen * but WITHOUT ANY WARRANTY; without even the implied warranty of
d8702d15ee7721ed1fcfc8f00a589970bd6b3598Timo Sirainen * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d8702d15ee7721ed1fcfc8f00a589970bd6b3598Timo Sirainen * GNU General Public License for more details.
38505846b6d083e19f0a7d1373761bdda5d9a5a9Timo Sirainen *
38505846b6d083e19f0a7d1373761bdda5d9a5a9Timo Sirainen * You should have received a copy of the GNU General Public License along
2087543679f0b35a8f70e1277b234e66ffdf96edTimo Sirainen * with this program; if not, write to the Free Software Foundation, Inc.,
2087543679f0b35a8f70e1277b234e66ffdf96edTimo Sirainen * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2087543679f0b35a8f70e1277b234e66ffdf96edTimo Sirainen */
73bfdbe28c2ce6d143eadf0bab8ccfbe4cab0faeTimo Sirainen#include <lxc/lxccontainer.h>
73bfdbe28c2ce6d143eadf0bab8ccfbe4cab0faeTimo Sirainen
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen#include <unistd.h>
7c849dbc7be089175c1a83a84ee7249ed695810dTimo Sirainen#include <signal.h>
7c849dbc7be089175c1a83a84ee7249ed695810dTimo Sirainen#include <stdio.h>
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen#include <sys/types.h>
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen#include <sys/wait.h>
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen#include <stdlib.h>
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen#include <errno.h>
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen#include <string.h>
d1414c09cf0d58ac983054e2f4e1a1f329272dcfTimo Sirainen#include "lxc/state.h"
d1414c09cf0d58ac983054e2f4e1a1f329272dcfTimo Sirainen
73bfdbe28c2ce6d143eadf0bab8ccfbe4cab0faeTimo Sirainen#define MYNAME "lxctest1"
d1414c09cf0d58ac983054e2f4e1a1f329272dcfTimo Sirainen
d1414c09cf0d58ac983054e2f4e1a1f329272dcfTimo Sirainenstatic int destroy_busybox(void)
8eea67470c1bd8562a62e7445d930bb2079b1a43Timo Sirainen{
8eea67470c1bd8562a62e7445d930bb2079b1a43Timo Sirainen int status, ret;
8eea67470c1bd8562a62e7445d930bb2079b1a43Timo Sirainen pid_t pid = fork();
8eea67470c1bd8562a62e7445d930bb2079b1a43Timo Sirainen
8eea67470c1bd8562a62e7445d930bb2079b1a43Timo Sirainen if (pid < 0) {
64e244defe74f513ce94f33d000a048ddbe2ea23Timo Sirainen perror("fork");
64e244defe74f513ce94f33d000a048ddbe2ea23Timo Sirainen return -1;
87cc5e9025e7fb6408f0de64c48d2d2897773ba5Timo Sirainen }
f016dec9837e6a41867708e4b89ca5308dedab05Timo Sirainen if (pid == 0) {
939451389b8e0ad529277b84fe51dab38a8cf77cTimo Sirainen ret = execlp("lxc-destroy", "lxc-destroy", "-f", "-n", MYNAME, NULL);
73bfdbe28c2ce6d143eadf0bab8ccfbe4cab0faeTimo Sirainen // Should not return
64e244defe74f513ce94f33d000a048ddbe2ea23Timo Sirainen perror("execl");
325d17cdbb7a338f7c413788f5e8e42d2e80a7f8Timo Sirainen exit(1);
009217abb57a24a4076092e8e4e165545747839eStephan Bosch }
64e244defe74f513ce94f33d000a048ddbe2ea23Timo Sirainenagain:
73bfdbe28c2ce6d143eadf0bab8ccfbe4cab0faeTimo Sirainen ret = waitpid(pid, &status, 0);
64e244defe74f513ce94f33d000a048ddbe2ea23Timo Sirainen if (ret == -1) {
64e244defe74f513ce94f33d000a048ddbe2ea23Timo Sirainen if (errno == -EINTR)
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen goto again;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen perror("waitpid");
73bfdbe28c2ce6d143eadf0bab8ccfbe4cab0faeTimo Sirainen return -1;
73bfdbe28c2ce6d143eadf0bab8ccfbe4cab0faeTimo Sirainen }
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen if (ret != pid)
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen goto again;
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen if (!WIFEXITED(status)) { // did not exit normally
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen fprintf(stderr, "%d: lxc-create exited abnormally\n", __LINE__);
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen return -1;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen }
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen return WEXITSTATUS(status);
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen}
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen
18ddd4fba186b1b407cae98bb388fa8add7db48dTimo Sirainenstatic int create_busybox(void)
90ed03ab289947f5576d2c616ada27724f50e9cdTimo Sirainen{
9ddd3d7d8651985e373a6c48e0ddc76b8a4ef1c7Timo Sirainen int status, ret;
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen pid_t pid = fork();
05817ffe09295892e1aa5c4a7f91d060e249563cTimo Sirainen
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen if (pid < 0) {
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen perror("fork");
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen return -1;
d1414c09cf0d58ac983054e2f4e1a1f329272dcfTimo Sirainen }
d1414c09cf0d58ac983054e2f4e1a1f329272dcfTimo Sirainen if (pid == 0) {
d1414c09cf0d58ac983054e2f4e1a1f329272dcfTimo Sirainen ret = execlp("lxc-create", "lxc-create", "-t", "busybox", "-f", LXC_DEFAULT_CONFIG, "-n", MYNAME, NULL);
d1414c09cf0d58ac983054e2f4e1a1f329272dcfTimo Sirainen // Should not return
d1414c09cf0d58ac983054e2f4e1a1f329272dcfTimo Sirainen perror("execl");
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen exit(1);
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen }
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainenagain:
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen ret = waitpid(pid, &status, 0);
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen if (ret == -1) {
8eea67470c1bd8562a62e7445d930bb2079b1a43Timo Sirainen if (errno == -EINTR)
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen goto again;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen perror("waitpid");
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen return -1;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen }
64e244defe74f513ce94f33d000a048ddbe2ea23Timo Sirainen if (ret != pid)
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen goto again;
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen if (!WIFEXITED(status)) { // did not exit normally
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen fprintf(stderr, "%d: lxc-create exited abnormally\n", __LINE__);
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen return -1;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen }
73bfdbe28c2ce6d143eadf0bab8ccfbe4cab0faeTimo Sirainen return WEXITSTATUS(status);
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen}
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainenint main(int argc, char *argv[])
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen{
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen struct lxc_container *c;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen int ret = 0;
9137c55411aa39d41c1e705ddc34d5bd26c65021Timo Sirainen const char *s;
66c3f635f2f33905af527d49b27f95322aa7dfa7Timo Sirainen bool b;
66c3f635f2f33905af527d49b27f95322aa7dfa7Timo Sirainen char *str;
acf3b7bf3a8891b118a71c45e6c48d17bc90b259Timo Sirainen
3b8d05391336c0e4d24c8ddcc962f350409ffbd3Timo Sirainen ret = 1;
3b8d05391336c0e4d24c8ddcc962f350409ffbd3Timo Sirainen /* test refcounting */
3b8d05391336c0e4d24c8ddcc962f350409ffbd3Timo Sirainen c = lxc_container_new(MYNAME, NULL);
2a90d8a14b0e7cc1508814bc87d3dfa598ef46a8Timo Sirainen if (!c) {
fprintf(stderr, "%d: error creating lxc_container %s\n", __LINE__, MYNAME);
goto out;
}
if (!lxc_container_get(c)) {
fprintf(stderr, "%d: error getting refcount\n", __LINE__);
goto out;
}
/* peek in, inappropriately, make sure refcount is a we'd like */
if (c->numthreads != 2) {
fprintf(stderr, "%d: refcount is %d, not %d\n", __LINE__, c->numthreads, 2);
goto out;
}
if (strcmp(c->name, MYNAME) != 0) {
fprintf(stderr, "%d: container has wrong name (%s not %s)\n", __LINE__, c->name, MYNAME);
goto out;
}
str = c->config_file_name(c);
#define CONFIGFNAM LXCPATH "/" MYNAME "/config"
if (!str || strcmp(str, CONFIGFNAM)) {
fprintf(stderr, "%d: got wrong config file name (%s, not %s)\n", __LINE__, str, CONFIGFNAM);
goto out;
}
free(str);
free(c->configfile);
c->configfile = NULL;
str = c->config_file_name(c);
if (str) {
fprintf(stderr, "%d: config file name was not NULL as it should have been\n", __LINE__);
goto out;
}
if (lxc_container_put(c) != 0) {
fprintf(stderr, "%d: c was freed on non-final put\n", __LINE__);
goto out;
}
if (c->numthreads != 1) {
fprintf(stderr, "%d: refcount is %d, not %d\n", __LINE__, c->numthreads, 1);
goto out;
}
if (lxc_container_put(c) != 1) {
fprintf(stderr, "%d: c was not freed on final put\n", __LINE__);
goto out;
}
/* test a real container */
c = lxc_container_new(MYNAME, NULL);
if (!c) {
fprintf(stderr, "%d: error creating lxc_container %s\n", __LINE__, MYNAME);
ret = 1;
goto out;
}
if (c->lxc_conf != NULL) {
fprintf(stderr, "%d: lxc_conf is not NULL as it should be\n", __LINE__);
ret = 1;
goto out;
}
b = c->is_defined(c);
if (b) {
fprintf(stderr, "%d: %s thought it was defined\n", __LINE__, MYNAME);
goto out;
}
s = c->state(c);
if (s && strcmp(s, "STOPPED") != 0) {
// liblxc says a container is STOPPED if it doesn't exist. That's because
// the container may be an application container - it's not wrong, just
// sometimes unintuitive.
fprintf(stderr, "%d: %s thinks it is in state %s\n", __LINE__, c->name, s);
goto out;
}
// create a container
// the liblxc api does not support creation - it probably will eventually,
// but not yet.
// So we just call out to lxc-create. We'll create a busybox container.
ret = create_busybox();
if (ret) {
fprintf(stderr, "%d: failed to create a busybox container\n", __LINE__);
goto out;
}
b = c->is_defined(c);
if (!b) {
fprintf(stderr, "%d: %s thought it was not defined\n", __LINE__, MYNAME);
goto out;
}
s = c->state(c);
if (!s || strcmp(s, "STOPPED")) {
fprintf(stderr, "%d: %s is in state %s, not in STOPPED.\n", __LINE__, c->name, s ? s : "undefined");
goto out;
}
b = c->load_config(c, NULL);
if (!b) {
fprintf(stderr, "%d: %s failed to read its config\n", __LINE__, c->name);
goto out;
}
// test wait states
int numstates = lxc_get_wait_states(NULL);
if (numstates != MAX_STATE) {
fprintf(stderr, "%d: lxc_get_wait_states gave %d not %d\n", __LINE__, numstates, MAX_STATE);
goto out;
}
const char **sstr = malloc(numstates * sizeof(const char *));
numstates = lxc_get_wait_states(sstr);
int i;
for (i=0; i<numstates; i++) {
fprintf(stderr, "got state %d %s\n", i, sstr[i]);
}
free(sstr);
printf("hit return to start container");
char mychar;
ret = scanf("%c", &mychar);
if (ret < 0)
goto out;
/* non-daemonized is tested in 'startone' */
c->want_daemonize(c, true);
if (!c->startl(c, 0, NULL, NULL)) {
fprintf(stderr, "%d: %s failed to start daemonized\n", __LINE__, c->name);
goto out;
}
if (!c->wait(c, "RUNNING", -1)) {
fprintf(stderr, "%d: failed waiting for state RUNNING\n", __LINE__);
goto out;
}
sleep(3);
s = c->state(c);
if (!s || strcmp(s, "RUNNING")) {
fprintf(stderr, "%d: %s is in state %s, not in RUNNING.\n", __LINE__, c->name, s ? s : "undefined");
goto out;
}
printf("hit return to finish");
ret = scanf("%c", &mychar);
if (ret < 0)
goto out;
fprintf(stderr, "all lxc_container tests passed for %s\n", c->name);
ret = 0;
out:
if (c) {
c->stop(c);
destroy_busybox();
}
lxc_container_put(c);
exit(ret);
}