From: Christian Brauner Date: Wed, 22 Nov 2017 20:32:07 +0000 (+0100) Subject: test: add state server tests X-Git-Tag: lxc-2.0.10~516 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=069e60e4d8c914ccb5461ef0e84bbf8563ee52fd;p=thirdparty%2Flxc.git test: add state server tests This checks whether multiple concurrent waiters all get notified by the state server. Signed-off-by: Christian Brauner --- diff --git a/src/tests/Makefile.am b/src/tests/Makefile.am index 9bcb467ad..af9c490f2 100644 --- a/src/tests/Makefile.am +++ b/src/tests/Makefile.am @@ -27,6 +27,7 @@ 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 +lxc_test_state_server_SOURCES = state_server.c lxctest.h AM_CFLAGS=-DLXCROOTFSMOUNT=\"$(LXCROOTFSMOUNT)\" \ -DLXCPATH=\"$(LXCPATH)\" \ @@ -55,7 +56,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-shortlived + lxc-test-config-jump-table lxc-test-shortlived lxc-test-state-server bin_SCRIPTS = lxc-test-automount \ lxc-test-autostart \ @@ -111,7 +112,8 @@ EXTRA_DIST = \ shortlived.c \ shutdowntest.c \ snapshot.c \ - startone.c + startone.c \ + state_server.c clean-local: rm -f lxc-test-utils-* diff --git a/src/tests/state_server.c b/src/tests/state_server.c new file mode 100644 index 000000000..c59da12d6 --- /dev/null +++ b/src/tests/state_server.c @@ -0,0 +1,153 @@ +/* liblxcapi + * + * Copyright © 2017 Christian Brauner . + * + * 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 +#include + +#include "lxc/lxccontainer.h" +#include "lxctest.h" + +struct thread_args { + int thread_id; + int timeout; + bool success; + struct lxc_container *c; +}; + +void *state_wrapper(void *data) +{ + struct thread_args *args = data; + + lxc_debug("Starting state server thread %d\n", args->thread_id); + + args->success = args->c->shutdown(args->c, args->timeout); + + lxc_debug("State server thread %d with shutdown timeout %d returned \"%s\"\n", + args->thread_id, args->timeout, args->success ? "SUCCESS" : "FAILED"); + + pthread_exit(NULL); + return NULL; +} + +int main(int argc, char *argv[]) +{ + int i, j; + pthread_attr_t attr; + pthread_t threads[10]; + struct thread_args args[10]; + struct lxc_container *c; + int ret = EXIT_FAILURE; + + c = lxc_container_new("state-server", NULL); + if (!c) { + lxc_error("%s", "Failed to create container \"state-server\""); + exit(ret); + } + + if (c->is_defined(c)) { + lxc_error("%s\n", "Container \"state-server\" is defined"); + goto on_error_put; + } + + if (!c->createl(c, "busybox", NULL, NULL, 0, NULL)) { + lxc_error("%s\n", "Failed to create busybox container \"state-server\""); + goto on_error_put; + } + + if (!c->is_defined(c)) { + lxc_error("%s\n", "Container \"state-server\" is not defined"); + goto on_error_put; + } + + c->clear_config(c); + + if (!c->load_config(c, NULL)) { + lxc_error("%s\n", "Failed to load config for container \"state-server\""); + goto on_error_stop; + } + + if (!c->want_daemonize(c, true)) { + lxc_error("%s\n", "Failed to mark container \"state-server\" daemonized"); + goto on_error_stop; + } + + pthread_attr_init(&attr); + + for (j = 0; j < 10; j++) { + lxc_debug("Starting state server test iteration %d\n", j); + + if (!c->startl(c, 0, NULL)) { + lxc_error("%s\n", "Failed to start container \"state-server\" daemonized"); + goto on_error_stop; + } + + sleep(5); + + for (i = 0; i < 10; i++) { + int ret; + + args[i].thread_id = i; + args[i].c = c; + args[i].timeout = -1; + /* test non-blocking shutdown request */ + if (i == 0) + args[i].timeout = 0; + + ret = pthread_create(&threads[i], &attr, state_wrapper, (void *) &args[i]); + if (ret != 0) + goto on_error_stop; + } + + for (i = 0; i < 10; i++) { + int ret; + + ret = pthread_join(threads[i], NULL); + if (ret != 0) + goto on_error_stop; + + if (!args[i].success) { + lxc_error("State server thread %d failed\n", args[i].thread_id); + goto on_error_stop; + } + } + } + + ret = EXIT_SUCCESS; + +on_error_stop: + if (c->is_running(c) && !c->stop(c)) + lxc_error("%s\n", "Failed to stop container \"state-server\""); + + if (!c->destroy(c)) + lxc_error("%s\n", "Failed to destroy container \"state-server\""); + +on_error_put: + lxc_container_put(c); + if (ret == EXIT_SUCCESS) + lxc_debug("%s\n", "All state server tests passed"); + exit(ret); +}