From 3df9fa8211af2263ff77d235797e10167641e8e4 Mon Sep 17 00:00:00 2001 From: Christian Brauner Date: Thu, 29 Jun 2017 15:01:11 +0200 Subject: [PATCH] test: shortlived daemonized containers Add a test to see if we can start daemonized containers that have a very short-lived init process. The point of this is to see whether we can correctly retrieve the state. Signed-off-by: Christian Brauner --- .gitignore | 1 + src/tests/Makefile.am | 4 +- src/tests/shortlived.c | 188 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 src/tests/shortlived.c diff --git a/.gitignore b/.gitignore index 510bef25e..c88aea06f 100644 --- a/.gitignore +++ b/.gitignore @@ -96,6 +96,7 @@ src/tests/lxc-test-utils* src/tests/lxc-usernic-test src/tests/lxc-test-config-jump-table src/tests/lxc-test-parse-config-file +src/tests/lxc-test-shortlived config/compile config/config.guess diff --git a/src/tests/Makefile.am b/src/tests/Makefile.am index fbd6631b8..be6735e6e 100644 --- a/src/tests/Makefile.am +++ b/src/tests/Makefile.am @@ -26,6 +26,7 @@ lxc_test_apparmor_SOURCES = aa.c lxc_test_utils_SOURCES = lxc-test-utils.c lxctest.h lxc_test_parse_config_file_SOURCES = parse_config_file.c lxctest.h lxc_test_config_jump_table_SOURCES = config_jump_table.c lxctest.h +lxc_test_shortlived_SOURCES = shortlived.c AM_CFLAGS=-DLXCROOTFSMOUNT=\"$(LXCROOTFSMOUNT)\" \ -DLXCPATH=\"$(LXCPATH)\" \ @@ -54,7 +55,7 @@ bin_PROGRAMS = lxc-test-containertests lxc-test-locktests lxc-test-startone \ lxc-test-snapshot lxc-test-concurrent lxc-test-may-control \ lxc-test-reboot lxc-test-list lxc-test-attach lxc-test-device-add-remove \ lxc-test-apparmor lxc-test-utils lxc-test-parse-config-file \ - lxc-test-config-jump-table + lxc-test-config-jump-table lxc-test-shortlived bin_SCRIPTS = lxc-test-automount \ lxc-test-autostart \ @@ -107,6 +108,7 @@ EXTRA_DIST = \ may_control.c \ parse_config_file.c \ saveconfig.c \ + shortlived.c \ shutdowntest.c \ snapshot.c \ startone.c diff --git a/src/tests/shortlived.c b/src/tests/shortlived.c new file mode 100644 index 000000000..aff0e7d4f --- /dev/null +++ b/src/tests/shortlived.c @@ -0,0 +1,188 @@ +/* liblxcapi + * + * Copyright © 2017 Christian Brauner . + * Copyright © 2017 Canonical Ltd. + * + * This program is free software; you can redistribute it and/or modify + * 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 + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define MYNAME "shortlived" + +static int destroy_container(void) +{ + int status, ret; + pid_t pid = fork(); + + if (pid < 0) { + perror("fork"); + return -1; + } + if (pid == 0) { + ret = execlp("lxc-destroy", "lxc-destroy", "-f", "-n", MYNAME, NULL); + // Should not return + perror("execl"); + exit(1); + } +again: + ret = waitpid(pid, &status, 0); + if (ret == -1) { + if (errno == EINTR) + goto again; + perror("waitpid"); + return -1; + } + if (ret != pid) + goto again; + if (!WIFEXITED(status)) { // did not exit normally + fprintf(stderr, "%d: lxc-create exited abnormally\n", __LINE__); + return -1; + } + return WEXITSTATUS(status); +} + +static int create_container(void) +{ + int status, ret; + pid_t pid = fork(); + + if (pid < 0) { + perror("fork"); + return -1; + } + if (pid == 0) { + ret = execlp("lxc-create", "lxc-create", "-t", "busybox", "-n", MYNAME, NULL); + // Should not return + perror("execl"); + exit(1); + } +again: + ret = waitpid(pid, &status, 0); + if (ret == -1) { + if (errno == EINTR) + goto again; + perror("waitpid"); + return -1; + } + if (ret != pid) + goto again; + if (!WIFEXITED(status)) { // did not exit normally + fprintf(stderr, "%d: lxc-create exited abnormally\n", __LINE__); + return -1; + } + return WEXITSTATUS(status); +} + +int main(int argc, char *argv[]) +{ + struct lxc_container *c; + const char *s; + bool b; + int i; + int ret = 0; + + ret = 1; + /* 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->is_defined(c)) { + fprintf(stderr, "%d: %s thought it was defined\n", __LINE__, MYNAME); + goto out; + } + + ret = create_container(); + if (ret) { + fprintf(stderr, "%d: failed to create a 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; + } + + if (!c->set_config_item(c, "lxc.init.cmd", "echo hello")) { + fprintf(stderr, "%d: failed setting lxc.init.cmd\n", __LINE__); + goto out; + } + + c->want_daemonize(c, true); + + /* Test whether we can start a really short-lived daemonized container. + */ + for (i = 0; i < 10; i++) { + if (!c->startl(c, 0, NULL)) { + fprintf(stderr, "%d: %s failed to start\n", __LINE__, c->name); + goto out; + } + sleep(1); + } + + if (!c->set_config_item(c, "lxc.init.cmd", "you-shall-fail")) { + fprintf(stderr, "%d: failed setting lxc.init.cmd\n", __LINE__); + goto out; + } + + /* Test whether we catch the start failure of a really short-lived + * daemonized container. + */ + for (i = 0; i < 10; i++) { + if (c->startl(c, 0, NULL)) { + fprintf(stderr, "%d: %s failed to start\n", __LINE__, c->name); + goto out; + } + sleep(1); + } + + c->stop(c); + + fprintf(stderr, "all lxc_container tests passed for %s\n", c->name); + ret = 0; + +out: + if (c) { + c->stop(c); + destroy_container(); + } + lxc_container_put(c); + exit(ret); +}