Skip to content

Commit

Permalink
test: add state server tests
Browse files Browse the repository at this point in the history
This checks whether multiple concurrent waiters all get notified by the state
server.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
  • Loading branch information
Christian Brauner committed Dec 15, 2017
1 parent 859bea7 commit 0d927ed
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/tests/Makefile.am
Expand Up @@ -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)\" \
Expand Down Expand Up @@ -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 \
Expand Down Expand Up @@ -113,7 +114,8 @@ EXTRA_DIST = \
shortlived.c \
shutdowntest.c \
snapshot.c \
startone.c
startone.c \
state_server.c

clean-local:
rm -f lxc-test-utils-*
Expand Down
153 changes: 153 additions & 0 deletions src/tests/state_server.c
@@ -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);
}

0 comments on commit 0d927ed

Please sign in to comment.