createtest.c revision 72d0e1cb2facaa4b8ba2f15e311d6bb9491badb7
89a126810703c666309310d0f3189e9834d70b5bTimo Sirainen/* liblxcapi
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen *
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>.
16f816d3f3c32ae3351834253f52ddd0212bcbf3Timo Sirainen * Copyright © 2012 Canonical Ltd.
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen *
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen * This program is free software; you can redistribute it and/or modify
58be9d6bcc3800f5b3d76a064ee767fbe31a5a8aTimo Sirainen * it under the terms of the GNU General Public License version 2, as
1098fc409a45e7603701dc94635927a673bee0c1Timo Sirainen * published by the Free Software Foundation.
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen *
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen * This program is distributed in the hope that it will be useful,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen * but WITHOUT ANY WARRANTY; without even the implied warranty of
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen * GNU General Public License for more details.
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen *
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen * You should have received a copy of the GNU General Public License along
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen * with this program; if not, write to the Free Software Foundation, Inc.,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "../lxc/lxccontainer.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include <unistd.h>
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include <signal.h>
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include <stdio.h>
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include <sys/types.h>
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include <sys/wait.h>
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include <stdlib.h>
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include <errno.h>
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#define MYNAME "lxctest1"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenint main(int argc, char *argv[])
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct lxc_container *c;
fd2f5fbc1f07aa93e2214a28cdf02437fb7d06c8Timo Sirainen int ret = 1;
ad850190d946d34966a56838cfdb216e021b5b5fTimo Sirainen
ad850190d946d34966a56838cfdb216e021b5b5fTimo Sirainen if ((c = lxc_container_new(MYNAME)) == NULL) {
ad850190d946d34966a56838cfdb216e021b5b5fTimo Sirainen fprintf(stderr, "%d: error opening lxc_container %s\n", __LINE__, MYNAME);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ret = 1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen goto out;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (c->is_defined(c)) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen fprintf(stderr, "%d: %s thought it was defined\n", __LINE__, MYNAME);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen goto out;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
8aacc9e7c84f8376822823ec98c2f551d4919b2eTimo Sirainen if (!c->set_config_item(c, "lxc.network.type", "veth")) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen fprintf(stderr, "%d: failed to set network type\n", __LINE__);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen goto out;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen c->set_config_item(c, "lxc.network.link", "lxcbr0");
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen c->set_config_item(c, "lxc.network.flags", "up");
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (!c->createl(c, "ubuntu", "-r", "lucid", NULL)) {
1098fc409a45e7603701dc94635927a673bee0c1Timo Sirainen fprintf(stderr, "%d: failed to create a lucid container\n", __LINE__);
1098fc409a45e7603701dc94635927a673bee0c1Timo Sirainen goto out;
1098fc409a45e7603701dc94635927a673bee0c1Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
1098fc409a45e7603701dc94635927a673bee0c1Timo Sirainen if (!c->is_defined(c)) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen fprintf(stderr, "%d: %s thought it was not defined\n", __LINE__, MYNAME);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen goto out;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen c->load_config(c, NULL);
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen c->want_daemonize(c);
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen if (!c->startl(c, 0, NULL)) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen fprintf(stderr, "%d: failed to start %s\n", __LINE__, MYNAME);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen goto out;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen fprintf(stderr, "%d: %s started, you have 60 seconds to test a console\n", __LINE__, MYNAME);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen sleep(60); // wait a minute to let user connect to console
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (!c->stop(c)) {
abe7afb8f1766fbcef1b9df513109e43d7d16e49Timo Sirainen fprintf(stderr, "%d: failed to stop %s\n", __LINE__, MYNAME);
abe7afb8f1766fbcef1b9df513109e43d7d16e49Timo Sirainen goto out;
abe7afb8f1766fbcef1b9df513109e43d7d16e49Timo Sirainen }
abe7afb8f1766fbcef1b9df513109e43d7d16e49Timo Sirainen
abe7afb8f1766fbcef1b9df513109e43d7d16e49Timo Sirainen if (!c->destroy(c)) {
abe7afb8f1766fbcef1b9df513109e43d7d16e49Timo Sirainen fprintf(stderr, "%d: error deleting %s\n", __LINE__, MYNAME);
abe7afb8f1766fbcef1b9df513109e43d7d16e49Timo Sirainen goto out;
3b20a37cb65c70f55e48dbdc912313fdacdab630Timo Sirainen }
3b20a37cb65c70f55e48dbdc912313fdacdab630Timo Sirainen
3b20a37cb65c70f55e48dbdc912313fdacdab630Timo Sirainen if (c->is_defined(c)) {
3b20a37cb65c70f55e48dbdc912313fdacdab630Timo Sirainen fprintf(stderr, "%d: %s thought it was defined\n", __LINE__, MYNAME);
3b20a37cb65c70f55e48dbdc912313fdacdab630Timo Sirainen goto out;
51b979b6414b940f04677a7e2d064be119345954Timo Sirainen }
abe7afb8f1766fbcef1b9df513109e43d7d16e49Timo Sirainen
abe7afb8f1766fbcef1b9df513109e43d7d16e49Timo Sirainen fprintf(stderr, "all lxc_container tests passed for %s\n", c->name);
abe7afb8f1766fbcef1b9df513109e43d7d16e49Timo Sirainen ret = 0;
abe7afb8f1766fbcef1b9df513109e43d7d16e49Timo Sirainenout:
abe7afb8f1766fbcef1b9df513109e43d7d16e49Timo Sirainen lxc_container_put(c);
abe7afb8f1766fbcef1b9df513109e43d7d16e49Timo Sirainen exit(ret);
abe7afb8f1766fbcef1b9df513109e43d7d16e49Timo Sirainen}
abe7afb8f1766fbcef1b9df513109e43d7d16e49Timo Sirainen