[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 2/9] pkd: run with SOCKET_WRAPPER_LIBRARY


Use the socket_wrapper preload shim when running the `pkd_hello`
test with `make test`.  The end goal here is to get this test
running alongside normal tests in regular CI.  Changes to do
this:

 * Configure PKD_ENVIRONMENT for the `pkd_hello_i1` test in the
   CMakeLists.txt file.

 * Add a `--socket-wrapper-dir|-w` flag that is used to opt-in to
   initializing a SOCKET_WRAPPER_DIR as expected by the socket_wrapper
   library.

   A runtime flag is used here to make it easy to run `pkd_hello`
   with the socket_wrapper library while avoiding a hard dependency.

Testing done: observed socker_wrapper in effect with `strace`;
running `make test` uses the wrapper correctly on my local
machine.

Signed-off-by: Jon Simons <jon@xxxxxxxxxxxxx>
---
 tests/pkd/CMakeLists.txt | 14 +++++++-
 tests/pkd/pkd_daemon.h   |  4 +++
 tests/pkd/pkd_hello.c    | 89 +++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 105 insertions(+), 2 deletions(-)

diff --git a/tests/pkd/CMakeLists.txt b/tests/pkd/CMakeLists.txt
index ff23ba95..a07021ec 100644
--- a/tests/pkd/CMakeLists.txt
+++ b/tests/pkd/CMakeLists.txt
@@ -38,6 +38,18 @@ target_link_libraries(pkd_hello ${pkd_libs})
 # specified with `-i` and may be helpful for chasing down bugs that
 # are not 100% reproducible.
 #
-add_test(pkd_hello_i1 ${CMAKE_CURRENT_BINARY_DIR}/pkd_hello -i1)
+add_test(pkd_hello_i1 ${CMAKE_CURRENT_BINARY_DIR}/pkd_hello -i1 -w /tmp/pkd_socket_wrapper_XXXXXX)
+
+#
+# Configure environment for cwrap socket wrapper.
+#
+find_package(socket_wrapper 1.1.5 REQUIRED)
+if (OSX)
+    set(PKD_ENVIRONMENT "DYLD_FORCE_FLAT_NAMESPACE=1;DYLD_INSERT_LIBRARIES=${SOCKET_WRAPPER_LIBRARY}")
+else ()
+    set(PKD_ENVIRONMENT "LD_PRELOAD=${SOCKET_WRAPPER_LIBRARY}")
+endif ()
+message(STATUS "PKD_ENVIRONMENT=${PKD_ENVIRONMENT}")
+set_property(TEST pkd_hello_i1 PROPERTY ENVIRONMENT ${PKD_ENVIRONMENT})
 
 endif (WITH_SERVER AND UNIX AND NOT WIN32)
diff --git a/tests/pkd/pkd_daemon.h b/tests/pkd/pkd_daemon.h
index 7bdf894d..abb07acd 100644
--- a/tests/pkd/pkd_daemon.h
+++ b/tests/pkd/pkd_daemon.h
@@ -32,6 +32,10 @@ struct pkd_daemon_args {
         const char *testname;
         const char *testmatch;
         unsigned int iterations;
+
+        struct {
+            const char *mkdtemp_str;
+        } socket_wrapper;
     } opts;
 };
 
diff --git a/tests/pkd/pkd_hello.c b/tests/pkd/pkd_hello.c
index e0c0cbf6..951bd7b1 100644
--- a/tests/pkd/pkd_hello.c
+++ b/tests/pkd/pkd_hello.c
@@ -1,13 +1,14 @@
 /*
  * pkd_hello.c --
  *
- * (c) 2014, 2017 Jon Simons <jon@xxxxxxxxxxxxx>
+ * (c) 2014, 2017-2018 Jon Simons <jon@xxxxxxxxxxxxx>
  */
 #include "config.h"
 
 #include <setjmp.h> // for cmocka
 #include <stdarg.h> // for cmocka
 #include <stdio.h>
+#include <stdlib.h>
 #include <unistd.h> // for cmocka
 #include <cmocka.h>
 
@@ -51,6 +52,8 @@ static struct argp_option options[] = {
       "Run each test for the given number of iterations (default is 10)", 0 },
     { "match", 'm', "testmatch", 0,
       "Run all tests with the given string", 0 },
+    { "socket-wrapper-dir", 'w', "<mkdtemp-template>", 0,
+      "Run in socket-wrapper mode using the given mkdtemp directory template", 0 },
     { "stdout", 'o', NULL, 0,
       "Emit pkd stdout messages", 0 },
     { "test", 't', "testname", 0,
@@ -87,6 +90,9 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state) {
     case 'v':
         pkd_dargs.opts.libssh_log_level += 1;
         break;
+    case 'w':
+        pkd_dargs.opts.socket_wrapper.mkdtemp_str = arg;
+        break;
     default:
         return ARGP_ERR_UNKNOWN;
     }
@@ -651,6 +657,75 @@ static int pkd_run_tests(void) {
     return rc;
 }
 
+static int pkd_init_socket_wrapper(void) {
+    int rc = 0;
+    char *mkdtemp_str = NULL;
+
+    if (pkd_dargs.opts.socket_wrapper.mkdtemp_str == NULL) {
+        goto out;
+    }
+
+    mkdtemp_str = strdup(pkd_dargs.opts.socket_wrapper.mkdtemp_str);
+    if (mkdtemp_str == NULL) {
+        fprintf(stderr, "pkd_init_socket_wrapper strdup failed\n");
+        goto errstrdup;
+    }
+    pkd_dargs.opts.socket_wrapper.mkdtemp_str = mkdtemp_str;
+
+    if (mkdtemp(mkdtemp_str) == NULL) {
+        fprintf(stderr, "pkd_init_socket_wrapper mkdtemp '%s' failed\n", mkdtemp_str);
+        goto errmkdtemp;
+    }
+
+    if (setenv("SOCKET_WRAPPER_DIR", mkdtemp_str, 1) != 0) {
+        fprintf(stderr, "pkd_init_socket_wrapper setenv failed\n");
+        goto errsetenv;
+    }
+
+    goto out;
+errsetenv:
+errmkdtemp:
+    free(mkdtemp_str);
+errstrdup:
+    rc = -1;
+out:
+    return rc;
+}
+
+static int pkd_rmfiles(const char *path) {
+    char bin[1024] = { 0 };
+    snprintf(&bin[0], sizeof(bin), "rm -f %s/*", path);
+    return system_checked(bin);
+}
+
+static int pkd_cleanup_socket_wrapper(void) {
+    int rc = 0;
+
+    if (pkd_dargs.opts.socket_wrapper.mkdtemp_str == NULL) {
+        goto out;
+    }
+
+    /* clean up socket-wrapper unix domain sockets */
+    if (pkd_rmfiles(pkd_dargs.opts.socket_wrapper.mkdtemp_str) != 0) {
+        fprintf(stderr, "pkd_cleanup_socket_wrapper pkd_rmfiles '%s' failed\n",
+                        pkd_dargs.opts.socket_wrapper.mkdtemp_str);
+        goto errrmfiles;
+    }
+
+    if (rmdir(pkd_dargs.opts.socket_wrapper.mkdtemp_str) != 0) {
+        fprintf(stderr, "pkd_cleanup_socket_wrapper rmdir '%s' failed\n",
+                        pkd_dargs.opts.socket_wrapper.mkdtemp_str);
+        goto errrmdir;
+    }
+
+    goto out;
+errrmdir:
+errrmfiles:
+    rc = -1;
+out:
+    return rc;
+}
+
 int main(int argc, char **argv) {
     int i = 0;
     int rc = 0;
@@ -670,6 +745,12 @@ int main(int argc, char **argv) {
     (void) argc;  (void) argv;
 #endif /* HAVE_ARGP_H */
 
+    rc = pkd_init_socket_wrapper();
+    if (rc != 0) {
+        fprintf(stderr, "pkd_init_socket_wrapper failed: %d\n", rc);
+        goto out_finalize;
+    }
+
     if (pkd_dargs.opts.list != 0) {
         while (testmap[i].testname != NULL) {
             printf("%s\n", testmap[i++].testname);
@@ -681,6 +762,12 @@ int main(int argc, char **argv) {
         }
     }
 
+    rc = pkd_cleanup_socket_wrapper();
+    if (rc != 0) {
+        fprintf(stderr, "pkd_cleanup_socket_wrapper failed: %d\n", rc);
+    }
+
+out_finalize:
     rc = ssh_finalize();
     if (rc != 0) {
         fprintf(stderr, "ssh_finalize: %d\n", rc);
-- 
2.14.1


References:
[PATCH 0/9] pkd: fixups for the 0.8 releaseJon Simons <jon@xxxxxxxxxxxxx>
Archive administrator: postmaster@lists.cynapses.org