From: S.Çağlar Onur Date: Thu, 12 Sep 2013 21:13:15 +0000 (-0400) Subject: tests: Introduce lxc-test-concurrent for testing basic actions concurrently X-Git-Tag: lxc-1.0.0.alpha2~120 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f209d63a97a8a2df5324608fee7b0d7a494d69eb;p=thirdparty%2Flxc.git tests: Introduce lxc-test-concurrent for testing basic actions concurrently Signed-off-by: S.Çağlar Onur Signed-off-by: Serge Hallyn --- diff --git a/.gitignore b/.gitignore index 660756fe3..c005c4d96 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,7 @@ .deps .libs +.dirstamp Makefile.in Makefile @@ -75,6 +76,7 @@ src/python-lxc/lxc/__pycache__/ src/tests/lxc-test-cgpath src/tests/lxc-test-clonetest +src/tests/lxc-test-concurrent src/tests/lxc-test-console src/tests/lxc-test-containertests src/tests/lxc-test-createtest @@ -85,6 +87,7 @@ src/tests/lxc-test-locktests src/tests/lxc-test-lxcpath src/tests/lxc-test-saveconfig src/tests/lxc-test-shutdowntest +src/tests/lxc-test-snapshot src/tests/lxc-test-startone src/tests/lxc-usernic-test diff --git a/src/tests/Makefile.am b/src/tests/Makefile.am index a6dacab99..815740746 100644 --- a/src/tests/Makefile.am +++ b/src/tests/Makefile.am @@ -18,6 +18,7 @@ lxc_test_console_SOURCES = console.c lxc_usernic_test_SOURCES = ../lxc/lxc_user_nic.c ../lxc/nl.c lxc_usernic_test_CFLAGS = -DISTEST lxc_test_snapshot_SOURCES = snapshot.c +lxc_test_concurrent_SOURCES = concurrent.c AM_CFLAGS=-I$(top_srcdir)/src \ -DLXCROOTFSMOUNT=\"$(LXCROOTFSMOUNT)\" \ @@ -30,7 +31,7 @@ bin_PROGRAMS = lxc-test-containertests lxc-test-locktests lxc-test-startone \ lxc-test-destroytest lxc-test-saveconfig lxc-test-createtest \ lxc-test-shutdowntest lxc-test-get_item lxc-test-getkeys lxc-test-lxcpath \ lxc-test-cgpath lxc-test-clonetest lxc-test-console lxc-usernic-test \ - lxc-test-snapshot + lxc-test-snapshot lxc-test-concurrent bin_SCRIPTS = lxc-test-usernic @@ -51,4 +52,5 @@ EXTRA_DIST = \ startone.c \ console.c \ lxc-test-usernic \ - snapshot.c + snapshot.c \ + concurrent.c diff --git a/src/tests/concurrent.c b/src/tests/concurrent.c new file mode 100644 index 000000000..41c171b70 --- /dev/null +++ b/src/tests/concurrent.c @@ -0,0 +1,116 @@ +/* concurrent.c + * + * Copyright © 2013 S.Çağlar Onur + * + * 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 "../lxc/lxccontainer.h" + +#define NTHREADS 5 + +struct thread_args { + int thread_id; + int return_code; + char *mode; +}; + +void * concurrent(void *arguments) { + char name[4]; + struct thread_args *args = arguments; + struct lxc_container *c; + + sprintf(name, "%d", args->thread_id); + + c = lxc_container_new(name, NULL); + + args->return_code = 1; + if (strcmp(args->mode, "create") == 0) { + if (!c->is_defined(c)) { + if (!c->create(c, "ubuntu", NULL, NULL, 1, NULL)) { + fprintf(stderr, "Creating the container (%s) failed...\n", name); + goto out; + } + } + } else if(strcmp(args->mode, "start") == 0) { + if (c->is_defined(c) && !c->is_running(c)) { + c->want_daemonize(c); + if (!c->start(c, false, NULL)) { + fprintf(stderr, "Starting the container (%s) failed...\n", name); + goto out; + } + } + } else if(strcmp(args->mode, "stop") == 0) { + if (c->is_defined(c) && c->is_running(c)) { + if (!c->stop(c)) { + fprintf(stderr, "Stopping the container (%s) failed...\n", name); + goto out; + } + } + } else if(strcmp(args->mode, "destroy") == 0) { + if (c->is_defined(c) && !c->is_running(c)) { + if (!c->destroy(c)) { + fprintf(stderr, "Destroying the container (%s) failed...\n", name); + goto out; + } + } + } + args->return_code = 0; +out: + lxc_container_put(c); + pthread_exit(NULL); +} + + +int main(int argc, char *argv[]) { + int i, j; + pthread_attr_t attr; + pthread_t threads[NTHREADS]; + struct thread_args args[NTHREADS]; + + char *modes[] = {"create", "start", "stop", "destroy", NULL}; + + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); + + for (i = 0; modes[i];i++) { + printf("Executing (%s) for %d containers...\n", modes[i], NTHREADS); + for (j = 0; j < NTHREADS; j++) { + args[j].thread_id = j; + args[j].mode = modes[i]; + + if (pthread_create(&threads[j], &attr, concurrent, (void *) &args[j]) != 0) { + perror("pthread_create() error"); + exit(EXIT_FAILURE); + } + } + + for (j = 0; j < NTHREADS; j++) { + if ( pthread_join(threads[j], NULL) != 0) { + perror("pthread_join() error"); + exit(EXIT_FAILURE); + } + if (args[j].return_code) { + perror("thread returned an error"); + exit(EXIT_FAILURE); + } + } + printf("\n"); + } + + pthread_attr_destroy(&attr); + exit(EXIT_SUCCESS); +}