test: add state server tests

This checks whether multiple concurrent waiters all get notified by the state
server.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
This commit is contained in:
Christian Brauner 2017-11-22 21:32:07 +01:00
parent c02c49ee3d
commit fc788340f7
No known key found for this signature in database
GPG Key ID: 8EB056D53EECB12D
2 changed files with 157 additions and 2 deletions

View 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-*

153
src/tests/state_server.c Normal file
View File

@ -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);
}