Blame SOURCES/0075-tcurl-test-refactor-so-new-options-can-be-added-more.patch

ecf709
From a886247bcdb1c551486c34a8d4eccd046a11382f Mon Sep 17 00:00:00 2001
ecf709
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
ecf709
Date: Fri, 24 Feb 2017 12:23:01 +0100
ecf709
Subject: [PATCH 75/90] tcurl test: refactor so new options can be added more
ecf709
 easily
ecf709
ecf709
Just to make the tool a little bit nicer and more flexible.
ecf709
ecf709
Reviewed-by: Simo Sorce <simo@redhat.com>
ecf709
Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
ecf709
(cherry picked from commit b800a6d09244359959404aca81c6796a58cafbcb)
ecf709
---
ecf709
 src/tests/tcurl_test_tool.c | 334 +++++++++++++++++++++++++++-----------------
ecf709
 1 file changed, 209 insertions(+), 125 deletions(-)
ecf709
ecf709
diff --git a/src/tests/tcurl_test_tool.c b/src/tests/tcurl_test_tool.c
ecf709
index 9a6266f89131ffd3a561e857af85df9854c44949..e5fc9705db415650d849b89c3d18e41574b7e28b 100644
ecf709
--- a/src/tests/tcurl_test_tool.c
ecf709
+++ b/src/tests/tcurl_test_tool.c
ecf709
@@ -28,26 +28,39 @@
ecf709
 
ecf709
 struct tool_ctx {
ecf709
     bool verbose;
ecf709
-
ecf709
-    errno_t error;
ecf709
     bool done;
ecf709
 
ecf709
     size_t nreqs;
ecf709
 };
ecf709
 
ecf709
+struct tool_options {
ecf709
+    int debug;
ecf709
+    int verbose;
ecf709
+
ecf709
+    enum tcurl_http_method method;
ecf709
+    const char *socket_path;
ecf709
+};
ecf709
+
ecf709
 static void request_done(struct tevent_req *req)
ecf709
 {
ecf709
-    int http_code;
ecf709
+    struct tool_ctx *tool_ctx;
ecf709
     struct sss_iobuf *outbuf;
ecf709
-    struct tool_ctx *tool_ctx = tevent_req_callback_data(req,
ecf709
-                                                         struct tool_ctx);
ecf709
+    int http_code;
ecf709
+    errno_t ret;
ecf709
 
ecf709
-    tool_ctx->error = tcurl_request_recv(tool_ctx, req, &outbuf, &http_code);
ecf709
+    tool_ctx = tevent_req_callback_data(req, struct tool_ctx);
ecf709
+
ecf709
+    ret = tcurl_request_recv(tool_ctx, req, &outbuf, &http_code);
ecf709
     talloc_zfree(req);
ecf709
 
ecf709
-    if (tool_ctx->error != EOK) {
ecf709
-        DEBUG(SSSDBG_FATAL_FAILURE, "HTTP request failed: %d\n", tool_ctx->error);
ecf709
+    tool_ctx->nreqs--;
ecf709
+    if (tool_ctx->nreqs == 0) {
ecf709
         tool_ctx->done = true;
ecf709
+    }
ecf709
+
ecf709
+    if (ret != EOK) {
ecf709
+        DEBUG(SSSDBG_FATAL_FAILURE, "HTTP request failed [%d]: %s\n",
ecf709
+              ret, sss_strerror(ret));
ecf709
         return;
ecf709
     } else if (tool_ctx->verbose) {
ecf709
         printf("Request HTTP code: %d\n", http_code);
ecf709
@@ -55,167 +68,171 @@ static void request_done(struct tevent_req *req)
ecf709
                (const char *) sss_iobuf_get_data(outbuf));
ecf709
         talloc_zfree(outbuf);
ecf709
     }
ecf709
-
ecf709
-    tool_ctx->nreqs--;
ecf709
-    if (tool_ctx->nreqs == 0) {
ecf709
-        tool_ctx->done = true;
ecf709
-    }
ecf709
 }
ecf709
 
ecf709
-int main(int argc, const char *argv[])
ecf709
+static errno_t
ecf709
+parse_options(poptContext pc, struct tool_options *opts)
ecf709
 {
ecf709
     int opt;
ecf709
-    poptContext pc;
ecf709
-
ecf709
-    int pc_debug = 0;
ecf709
-    int pc_verbose = 0;
ecf709
-    const char *socket_path = NULL;
ecf709
-    const char *extra_arg_ptr;
ecf709
-
ecf709
-    static const char *headers[] = {
ecf709
-        "Content-type: application/octet-stream",
ecf709
-        NULL,
ecf709
-    };
ecf709
-
ecf709
-    struct poptOption long_options[] = {
ecf709
-        POPT_AUTOHELP
ecf709
-        { "debug", '\0', POPT_ARG_INT, &pc_debug, 0,
ecf709
-          "The debug level to run with", NULL },
ecf709
-        { "socket-path", 's', POPT_ARG_STRING, &socket_path, 0,
ecf709
-          "The path to the HTTP server socket", NULL },
ecf709
-        { "get", 'g', POPT_ARG_NONE, NULL, 'g', "Perform a HTTP GET (default)", NULL },
ecf709
-        { "put", 'p', POPT_ARG_NONE, NULL, 'p', "Perform a HTTP PUT", NULL },
ecf709
-        { "post", 'o', POPT_ARG_NONE, NULL, 'o', "Perform a HTTP POST", NULL },
ecf709
-        { "del", 'd', POPT_ARG_NONE, NULL, 'd', "Perform a HTTP DELETE", NULL },
ecf709
-        { "verbose", 'v', POPT_ARG_NONE, NULL, 'v', "Print response code and body", NULL },
ecf709
-        POPT_TABLEEND
ecf709
-    };
ecf709
-
ecf709
-    struct tevent_req *req;
ecf709
-    struct tevent_context *ev;
ecf709
-    enum tcurl_http_method method = TCURL_HTTP_GET;
ecf709
-    struct tcurl_ctx *ctx;
ecf709
-    struct tcurl_request *tcurl_req;
ecf709
-    struct tool_ctx *tool_ctx;
ecf709
-
ecf709
-    const char *urls[MAXREQ] = { 0 };
ecf709
-    struct sss_iobuf **inbufs;
ecf709
-
ecf709
-    size_t n_reqs = 0;
ecf709
-
ecf709
-    debug_prg_name = argv[0];
ecf709
-    pc = poptGetContext(NULL, argc, argv, long_options, 0);
ecf709
-    poptSetOtherOptionHelp(pc, "HTTPDATA");
ecf709
 
ecf709
     while ((opt = poptGetNextOpt(pc)) > 0) {
ecf709
         switch (opt) {
ecf709
         case 'g':
ecf709
-            method = TCURL_HTTP_GET;
ecf709
+            opts->method = TCURL_HTTP_GET;
ecf709
             break;
ecf709
         case 'p':
ecf709
-            method = TCURL_HTTP_PUT;
ecf709
+            opts->method = TCURL_HTTP_PUT;
ecf709
             break;
ecf709
         case 'o':
ecf709
-            method = TCURL_HTTP_POST;
ecf709
+            opts->method = TCURL_HTTP_POST;
ecf709
             break;
ecf709
         case 'd':
ecf709
-            method = TCURL_HTTP_DELETE;
ecf709
-            break;
ecf709
-        case 'v':
ecf709
-            pc_verbose = 1;
ecf709
+            opts->method = TCURL_HTTP_DELETE;
ecf709
             break;
ecf709
         default:
ecf709
             DEBUG(SSSDBG_FATAL_FAILURE, "Unexpected option\n");
ecf709
-            return 1;
ecf709
+            return EINVAL;
ecf709
         }
ecf709
     }
ecf709
 
ecf709
-    DEBUG_CLI_INIT(pc_debug);
ecf709
-
ecf709
-    tool_ctx = talloc_zero(NULL, struct tool_ctx);
ecf709
-    if (tool_ctx == NULL) {
ecf709
-        DEBUG(SSSDBG_CRIT_FAILURE, "Could not init tool context\n");
ecf709
-        return 1;
ecf709
+    if (opt != -1) {
ecf709
+        poptPrintUsage(pc, stderr, 0);
ecf709
+        fprintf(stderr, "%s", poptStrerror(opt));
ecf709
+        return EINVAL;
ecf709
     }
ecf709
 
ecf709
-    inbufs = talloc_zero_array(tool_ctx, struct sss_iobuf *, MAXREQ);
ecf709
-    if (inbufs == NULL) {
ecf709
-        talloc_zfree(tool_ctx);
ecf709
-        return 1;
ecf709
+    return EOK;
ecf709
+}
ecf709
+
ecf709
+static errno_t
ecf709
+prepare_requests(TALLOC_CTX *mem_ctx,
ecf709
+                 poptContext pc,
ecf709
+                 struct tool_options *opts,
ecf709
+                 struct tcurl_request ***_requests,
ecf709
+                 size_t *_num_requests)
ecf709
+{
ecf709
+    struct tcurl_request **requests;
ecf709
+    const char *arg;
ecf709
+    const char *url;
ecf709
+    struct sss_iobuf *body;
ecf709
+    errno_t ret;
ecf709
+    size_t i;
ecf709
+
ecf709
+    static const char *headers[] = {
ecf709
+        "Content-type: application/octet-stream",
ecf709
+        NULL,
ecf709
+    };
ecf709
+
ecf709
+    requests = talloc_zero_array(mem_ctx, struct tcurl_request *, MAXREQ + 1);
ecf709
+    if (requests == NULL) {
ecf709
+        return ENOMEM;
ecf709
     }
ecf709
 
ecf709
-    while ((extra_arg_ptr = poptGetArg(pc)) != NULL) {
ecf709
-        switch(method) {
ecf709
+    i = 0;
ecf709
+    while ((arg = poptGetArg(pc)) != NULL) {
ecf709
+        if (i >= MAXREQ) {
ecf709
+            fprintf(stderr, _("Too many requests!\n"));
ecf709
+            ret = EINVAL;
ecf709
+            goto done;
ecf709
+        }
ecf709
+
ecf709
+        switch (opts->method) {
ecf709
         case TCURL_HTTP_GET:
ecf709
         case TCURL_HTTP_DELETE:
ecf709
-        case TCURL_HTTP_POST:
ecf709
-            urls[n_reqs++] = extra_arg_ptr;
ecf709
+            url = arg;
ecf709
+            body = NULL;
ecf709
             break;
ecf709
         case TCURL_HTTP_PUT:
ecf709
-            if (urls[n_reqs] == NULL) {
ecf709
-                urls[n_reqs] = extra_arg_ptr;
ecf709
-            } else {
ecf709
-                inbufs[n_reqs] = sss_iobuf_init_readonly(
ecf709
-                                              inbufs,
ecf709
-                                              (uint8_t *) discard_const(extra_arg_ptr),
ecf709
-                                              strlen(extra_arg_ptr));
ecf709
-                if (inbufs[n_reqs] == NULL) {
ecf709
-                    DEBUG(SSSDBG_CRIT_FAILURE, "Could not init input buffer\n");
ecf709
-                    talloc_zfree(tool_ctx);
ecf709
-                    return 1;
ecf709
-                }
ecf709
-                n_reqs++;
ecf709
+        case TCURL_HTTP_POST:
ecf709
+            url = arg;
ecf709
+
ecf709
+            arg = poptGetArg(pc);
ecf709
+            if (arg == NULL) {
ecf709
+                body = NULL;
ecf709
+                break;
ecf709
+            }
ecf709
+
ecf709
+            body = sss_iobuf_init_readonly(requests,
ecf709
+                                           discard_const_p(uint8_t, arg),
ecf709
+                                           strlen(arg));
ecf709
+            if (body == NULL) {
ecf709
+                ret = ENOMEM;
ecf709
+                goto done;
ecf709
             }
ecf709
             break;
ecf709
+        default:
ecf709
+            DEBUG(SSSDBG_CRIT_FAILURE, "Invalid method!\n");
ecf709
+            ret = EINVAL;
ecf709
+            goto done;
ecf709
         }
ecf709
+
ecf709
+        requests[i] = tcurl_http(requests, opts->method, opts->socket_path,
ecf709
+                                 url, headers, body);
ecf709
+        if (requests[i] == NULL) {
ecf709
+            ret = ENOMEM;
ecf709
+            goto done;
ecf709
+        }
ecf709
+
ecf709
+        i++;
ecf709
     }
ecf709
 
ecf709
-    if (opt != -1) {
ecf709
-        poptPrintUsage(pc, stderr, 0);
ecf709
-        fprintf(stderr, "%s", poptStrerror(opt));
ecf709
-        talloc_zfree(tool_ctx);
ecf709
-        return 1;
ecf709
+    *_requests = requests;
ecf709
+    *_num_requests = i;
ecf709
+
ecf709
+    ret = EOK;
ecf709
+
ecf709
+done:
ecf709
+    if (ret != EOK) {
ecf709
+        talloc_free(requests);
ecf709
     }
ecf709
 
ecf709
-    if (!socket_path) {
ecf709
-        DEBUG(SSSDBG_FATAL_FAILURE, "Please specify the socket path\n");
ecf709
-        poptPrintUsage(pc, stderr, 0);
ecf709
-        talloc_zfree(tool_ctx);
ecf709
-        return 1;
ecf709
+    return ret;
ecf709
+}
ecf709
+
ecf709
+static errno_t
ecf709
+run_requests(struct tool_ctx *tool_ctx,
ecf709
+             struct tcurl_request **requests)
ecf709
+{
ecf709
+    TALLOC_CTX *tmp_ctx;
ecf709
+    struct tcurl_ctx *tcurl_ctx;
ecf709
+    struct tevent_context *ev;
ecf709
+    struct tevent_req *req;
ecf709
+    errno_t ret;
ecf709
+    int i;
ecf709
+
ecf709
+    tmp_ctx = talloc_new(NULL);
ecf709
+    if (tmp_ctx == NULL) {
ecf709
+        DEBUG(SSSDBG_FATAL_FAILURE, "Out of memory!\n");
ecf709
+        return ENOMEM;
ecf709
     }
ecf709
 
ecf709
-    tool_ctx->nreqs = n_reqs;
ecf709
-    tool_ctx->verbose = !!pc_verbose;
ecf709
+    if (requests == NULL || requests[0] == NULL) {
ecf709
+        ret = EOK;
ecf709
+        goto done;
ecf709
+    }
ecf709
 
ecf709
-    ev = tevent_context_init(tool_ctx);
ecf709
+    ev = tevent_context_init(tmp_ctx);
ecf709
     if (ev == NULL) {
ecf709
         DEBUG(SSSDBG_CRIT_FAILURE, "Could not init tevent context\n");
ecf709
-        talloc_zfree(tool_ctx);
ecf709
-        return 1;
ecf709
+        ret = ENOMEM;
ecf709
+        goto done;
ecf709
     }
ecf709
 
ecf709
-    ctx = tcurl_init(tool_ctx, ev);
ecf709
-    if (ctx == NULL) {
ecf709
+    tcurl_ctx = tcurl_init(tmp_ctx, ev);
ecf709
+    if (tcurl_ctx == NULL) {
ecf709
         DEBUG(SSSDBG_FATAL_FAILURE, "Could not init tcurl context\n");
ecf709
-        talloc_zfree(tool_ctx);
ecf709
-        return 1;
ecf709
+        ret = ENOMEM;
ecf709
+        goto done;
ecf709
     }
ecf709
 
ecf709
-    for (size_t i = 0; i < n_reqs; i++) {
ecf709
-        tcurl_req = tcurl_http(tool_ctx, method, socket_path,
ecf709
-                               urls[i], headers, inbufs[i]);
ecf709
-        if (tcurl_req == NULL) {
ecf709
-            DEBUG(SSSDBG_FATAL_FAILURE, "Unable to create TCURL request\n");
ecf709
-            talloc_zfree(tool_ctx);
ecf709
-            return 1;
ecf709
+    for (i = 0; requests[i] != NULL; i++) {
ecf709
+        req = tcurl_request_send(tmp_ctx, ev, tcurl_ctx, requests[i], 5);
ecf709
+        if (req == NULL) {
ecf709
+            DEBUG(SSSDBG_FATAL_FAILURE, "Could not create tevent request\n");
ecf709
+            ret = ENOMEM;
ecf709
+            goto done;
ecf709
         }
ecf709
 
ecf709
-        req = tcurl_request_send(tool_ctx, ev, ctx, tcurl_req, 10);
ecf709
-        if (ctx == NULL) {
ecf709
-            DEBUG(SSSDBG_FATAL_FAILURE, "Could not create request\n");
ecf709
-            talloc_zfree(tool_ctx);
ecf709
-            return 1;
ecf709
-        }
ecf709
         tevent_req_set_callback(req, request_done, tool_ctx);
ecf709
     }
ecf709
 
ecf709
@@ -226,11 +243,78 @@ int main(int argc, const char *argv[])
ecf709
     if (tool_ctx->nreqs > 0) {
ecf709
         DEBUG(SSSDBG_FATAL_FAILURE,
ecf709
               "The tool finished with some pending requests, fail!\n");
ecf709
-        talloc_zfree(tool_ctx);
ecf709
-        return 1;
ecf709
+        ret = EEXIST;
ecf709
+        goto done;
ecf709
     }
ecf709
 
ecf709
+    ret = EOK;
ecf709
+
ecf709
+done:
ecf709
+    talloc_free(tmp_ctx);
ecf709
+    return ret;
ecf709
+}
ecf709
+
ecf709
+int main(int argc, const char *argv[])
ecf709
+{
ecf709
+    struct tool_options opts = { 0 };
ecf709
+    struct tool_ctx *tool_ctx;
ecf709
+    struct tcurl_request **requests;
ecf709
+    poptContext pc;
ecf709
+    errno_t ret;
ecf709
+
ecf709
+    struct poptOption long_options[] = {
ecf709
+        POPT_AUTOHELP
ecf709
+        { "debug", '\0', POPT_ARG_INT, &opts.debug, 0, "The debug level to run with", NULL },
ecf709
+        { "socket-path", 's', POPT_ARG_STRING, &opts.socket_path, 0, "The path to the HTTP server socket", NULL },
ecf709
+        { "get", 'g', POPT_ARG_NONE, NULL, 'g', "Perform a HTTP GET (default)", NULL },
ecf709
+        { "put", 'p', POPT_ARG_NONE, NULL, 'p', "Perform a HTTP PUT", NULL },
ecf709
+        { "post", 'o', POPT_ARG_NONE, NULL, 'o', "Perform a HTTP POST", NULL },
ecf709
+        { "del", 'd', POPT_ARG_NONE, NULL, 'd', "Perform a HTTP DELETE", NULL },
ecf709
+        { "verbose", 'v', POPT_ARG_NONE, &opts.verbose, '\0', "Print response code and body", NULL },
ecf709
+        POPT_TABLEEND
ecf709
+    };
ecf709
+
ecf709
+    pc = poptGetContext(NULL, argc, argv, long_options, 0);
ecf709
+    poptSetOtherOptionHelp(pc, "[URL HTTPDATA]*");
ecf709
+
ecf709
+    tool_ctx = talloc_zero(NULL, struct tool_ctx);
ecf709
+    if (tool_ctx == NULL) {
ecf709
+        DEBUG(SSSDBG_CRIT_FAILURE, "Could not init tool context\n");
ecf709
+        ret = ENOMEM;
ecf709
+        goto done;
ecf709
+    }
ecf709
+
ecf709
+    ret = parse_options(pc, &opts);
ecf709
+    if (ret != EOK) {
ecf709
+        DEBUG(SSSDBG_FATAL_FAILURE, "Unable to parse options [%d]: %s\n",
ecf709
+              ret, sss_strerror(ret));
ecf709
+        goto done;
ecf709
+    }
ecf709
+
ecf709
+    DEBUG_CLI_INIT(opts.debug);
ecf709
+    tool_ctx->verbose = opts.verbose;
ecf709
+
ecf709
+    ret = prepare_requests(tool_ctx, pc, &opts, &requests, &tool_ctx->nreqs);
ecf709
+    if (ret != EOK) {
ecf709
+        DEBUG(SSSDBG_FATAL_FAILURE, "Unable to prepare requests [%d]: %s\n",
ecf709
+              ret, sss_strerror(ret));
ecf709
+        goto done;
ecf709
+    }
ecf709
+
ecf709
+    ret = run_requests(tool_ctx, requests);
ecf709
+    if (ret != EOK) {
ecf709
+        DEBUG(SSSDBG_FATAL_FAILURE, "Unable to issue requests [%d]: %s\n",
ecf709
+              ret, sss_strerror(ret));
ecf709
+        goto done;
ecf709
+    }
ecf709
+
ecf709
+done:
ecf709
     talloc_free(tool_ctx);
ecf709
     poptFreeContext(pc);
ecf709
-    return 0;
ecf709
+
ecf709
+    if (ret != EOK) {
ecf709
+        return EXIT_FAILURE;
ecf709
+    }
ecf709
+
ecf709
+    return EXIT_SUCCESS;
ecf709
 }
ecf709
-- 
ecf709
2.9.3
ecf709