[lxc-devel] [PATCH] tests: Introduce lxc-test-concurrent for testing basic actions concurrently
Serge Hallyn
serge.hallyn at ubuntu.com
Thu Sep 12 21:57:38 UTC 2013
Quoting S.Çağlar Onur (caglar at 10ur.org):
> Signed-off-by: S.Çağlar Onur <caglar at 10ur.org>
Oh, great, thanks :)
Acked-by: Serge E. Hallyn <serge.hallyn at ubuntu.com>
> ---
> .gitignore | 3 ++
> src/tests/Makefile.am | 6 ++-
> src/tests/concurrent.c | 116 +++++++++++++++++++++++++++++++++++++++++++++++++
> 3 files changed, 123 insertions(+), 2 deletions(-)
> create mode 100644 src/tests/concurrent.c
>
> diff --git a/.gitignore b/.gitignore
> index 660756f..c005c4d 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 a6dacab..8157407 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 0000000..41c171b
> --- /dev/null
> +++ b/src/tests/concurrent.c
> @@ -0,0 +1,116 @@
> +/* concurrent.c
> + *
> + * Copyright © 2013 S.Çağlar Onur <caglar at 10ur.org>
> + *
> + * 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 <stdio.h>
> +#include <pthread.h>
> +
> +#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);
> +}
> --
> 1.8.1.2
>
>
> ------------------------------------------------------------------------------
> How ServiceNow helps IT people transform IT departments:
> 1. Consolidate legacy IT systems to a single system of record for IT
> 2. Standardize and globalize service processes across IT
> 3. Implement zero-touch automation to replace manual, redundant tasks
> http://pubads.g.doubleclick.net/gampad/clk?id=51271111&iu=/4140/ostg.clktrk
> _______________________________________________
> Lxc-devel mailing list
> Lxc-devel at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/lxc-devel
More information about the lxc-devel
mailing list