Commit f209d63a by S.Çağlar Onur Committed by Serge Hallyn

tests: Introduce lxc-test-concurrent for testing basic actions concurrently

parent 813a4837
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
.deps .deps
.libs .libs
.dirstamp
Makefile.in Makefile.in
Makefile Makefile
...@@ -75,6 +76,7 @@ src/python-lxc/lxc/__pycache__/ ...@@ -75,6 +76,7 @@ src/python-lxc/lxc/__pycache__/
src/tests/lxc-test-cgpath src/tests/lxc-test-cgpath
src/tests/lxc-test-clonetest src/tests/lxc-test-clonetest
src/tests/lxc-test-concurrent
src/tests/lxc-test-console src/tests/lxc-test-console
src/tests/lxc-test-containertests src/tests/lxc-test-containertests
src/tests/lxc-test-createtest src/tests/lxc-test-createtest
...@@ -85,6 +87,7 @@ src/tests/lxc-test-locktests ...@@ -85,6 +87,7 @@ src/tests/lxc-test-locktests
src/tests/lxc-test-lxcpath src/tests/lxc-test-lxcpath
src/tests/lxc-test-saveconfig src/tests/lxc-test-saveconfig
src/tests/lxc-test-shutdowntest src/tests/lxc-test-shutdowntest
src/tests/lxc-test-snapshot
src/tests/lxc-test-startone src/tests/lxc-test-startone
src/tests/lxc-usernic-test src/tests/lxc-usernic-test
......
...@@ -18,6 +18,7 @@ lxc_test_console_SOURCES = console.c ...@@ -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_SOURCES = ../lxc/lxc_user_nic.c ../lxc/nl.c
lxc_usernic_test_CFLAGS = -DISTEST lxc_usernic_test_CFLAGS = -DISTEST
lxc_test_snapshot_SOURCES = snapshot.c lxc_test_snapshot_SOURCES = snapshot.c
lxc_test_concurrent_SOURCES = concurrent.c
AM_CFLAGS=-I$(top_srcdir)/src \ AM_CFLAGS=-I$(top_srcdir)/src \
-DLXCROOTFSMOUNT=\"$(LXCROOTFSMOUNT)\" \ -DLXCROOTFSMOUNT=\"$(LXCROOTFSMOUNT)\" \
...@@ -30,7 +31,7 @@ bin_PROGRAMS = lxc-test-containertests lxc-test-locktests lxc-test-startone \ ...@@ -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-destroytest lxc-test-saveconfig lxc-test-createtest \
lxc-test-shutdowntest lxc-test-get_item lxc-test-getkeys lxc-test-lxcpath \ 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-cgpath lxc-test-clonetest lxc-test-console lxc-usernic-test \
lxc-test-snapshot lxc-test-snapshot lxc-test-concurrent
bin_SCRIPTS = lxc-test-usernic bin_SCRIPTS = lxc-test-usernic
...@@ -51,4 +52,5 @@ EXTRA_DIST = \ ...@@ -51,4 +52,5 @@ EXTRA_DIST = \
startone.c \ startone.c \
console.c \ console.c \
lxc-test-usernic \ lxc-test-usernic \
snapshot.c snapshot.c \
concurrent.c
/* concurrent.c
*
* Copyright © 2013 S.Çağlar Onur <caglar@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);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment