]> git.proxmox.com Git - mirror_lxc.git/commitdiff
test: add state server tests
authorChristian Brauner <christian.brauner@ubuntu.com>
Wed, 22 Nov 2017 20:32:07 +0000 (21:32 +0100)
committerChristian Brauner <christian.brauner@ubuntu.com>
Wed, 6 Dec 2017 15:01:23 +0000 (16:01 +0100)
This checks whether multiple concurrent waiters all get notified by the state
server.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/tests/Makefile.am
src/tests/state_server.c [new file with mode: 0644]

index 9a0a09ea3bca87776a360016a46a46783017db38..f223463d7726053513ed6a544f5342d703ba88be 100644 (file)
@@ -30,6 +30,7 @@ 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_livepatch_SOURCES = livepatch.c lxctest.h
+lxc_test_state_server_SOURCES = state_server.c lxctest.h
 
 AM_CFLAGS=-DLXCROOTFSMOUNT=\"$(LXCROOTFSMOUNT)\" \
        -DLXCPATH=\"$(LXCPATH)\" \
@@ -59,7 +60,7 @@ bin_PROGRAMS = lxc-test-containertests lxc-test-locktests lxc-test-startone \
        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-livepatch \
-       lxc-test-api-reboot
+       lxc-test-api-reboot lxc-test-state-server
 
 bin_SCRIPTS = lxc-test-automount \
              lxc-test-autostart \
@@ -119,7 +120,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 (file)
index 0000000..c59da12
--- /dev/null
@@ -0,0 +1,153 @@
+/* liblxcapi
+ *
+ * Copyright © 2017 Christian Brauner <christian.brauner@ubuntu.com>.
+ *
+ * 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 <alloca.h>
+#include <errno.h>
+#include <pthread.h>
+#include <sched.h>
+#include <signal.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/reboot.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#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);
+}