]> git.proxmox.com Git - mirror_lxc.git/commitdiff
tests: Introduce lxc-test-concurrent for testing basic actions concurrently
authorS.Çağlar Onur <caglar@10ur.org>
Thu, 12 Sep 2013 21:13:15 +0000 (17:13 -0400)
committerSerge Hallyn <serge.hallyn@ubuntu.com>
Thu, 12 Sep 2013 22:47:10 +0000 (17:47 -0500)
Signed-off-by: S.Çağlar Onur <caglar@10ur.org>
Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
.gitignore
src/tests/Makefile.am
src/tests/concurrent.c [new file with mode: 0644]

index 660756fe3731324101113421d3bcea5f069a8b3a..c005c4d9601a329afd3a823d1928b7fe598fac12 100644 (file)
@@ -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
 
index a6dacab997ffdf04f478ff6a3f677eb748766886..815740746afa9479d752657e8dbc2c4c0f4cf1a2 100644 (file)
@@ -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 (file)
index 0000000..41c171b
--- /dev/null
@@ -0,0 +1,116 @@
+/* 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);
+}