containertests.c revision 95ee490bbdb97ab2b4f1dfa63a0a26e0dd1c2f17
/* liblxcapi
*
* Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>.
* Copyright © 2012 Canonical Ltd.
*
* it under the terms of the GNU General Public License version 2, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <lxc/lxccontainer.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#define MYNAME "lxctest1"
static int destroy_busybox(void)
{
if (pid < 0) {
perror("fork");
return -1;
}
if (pid == 0) {
// Should not return
perror("execl");
exit(1);
}
if (ret == -1) {
goto again;
perror("waitpid");
return -1;
}
goto again;
return -1;
}
return WEXITSTATUS(status);
}
static int create_busybox(void)
{
if (pid < 0) {
perror("fork");
return -1;
}
if (pid == 0) {
ret = execlp("lxc-create", "lxc-create", "-t", "busybox", "-f", LXC_DEFAULT_CONFIG, "-n", MYNAME, NULL);
// Should not return
perror("execl");
exit(1);
}
if (ret == -1) {
goto again;
perror("waitpid");
return -1;
}
goto again;
return -1;
}
return WEXITSTATUS(status);
}
{
struct lxc_container *c;
int ret = 0;
const char *s;
bool b;
char *str;
ret = 1;
/* test refcounting */
if (!c) {
goto out;
}
if (!lxc_container_get(c)) {
goto out;
}
/* peek in, inappropriately, make sure refcount is a we'd like */
if (c->numthreads != 2) {
goto out;
}
goto out;
}
str = c->config_file_name(c);
goto out;
}
free(c->configfile);
c->configfile = NULL;
str = c->config_file_name(c);
if (str) {
goto out;
}
if (lxc_container_put(c) != 0) {
goto out;
}
if (c->numthreads != 1) {
goto out;
}
if (lxc_container_put(c) != 1) {
goto out;
}
/* test a real container */
if (!c) {
ret = 1;
goto out;
}
ret = 1;
goto out;
}
b = c->is_defined(c);
if (b) {
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.
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) {
goto out;
}
b = c->is_defined(c);
if (!b) {
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) {
goto out;
}
// test wait states
goto out;
}
int i;
for (i=0; i<numstates; i++) {
}
printf("hit return to start container");
char mychar;
if (ret < 0)
goto out;
/* non-daemonized is tested in 'startone' */
c->want_daemonize(c, true);
goto out;
}
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");
if (ret < 0)
goto out;
ret = 0;
out:
if (c) {
c->stop(c);
}
}