diff options
author | Randall Leeds <randall@apache.org> | 2012-01-07 14:21:29 -0800 |
---|---|---|
committer | Randall Leeds <randall@apache.org> | 2012-01-26 17:03:10 -0800 |
commit | d20e792617db738dd5ad0e046ae847cd740f586f (patch) | |
tree | b0f6b97c0911b7c0da161a683ab22256ff16441c | |
parent | 257eb522c753ae39a5333938ec1856acd8451ec2 (diff) | |
download | couchdb-d20e792617db738dd5ad0e046ae847cd740f586f.tar.gz |
COUCHDB-1338 - run js tests with port=0
When the JS tests POST to /_restart, the server comes back up on a
different port. To work around this, add a getter property for the
CouchHTTP.prototype.base_url property, using a reserved slot on the
object to store the value.
-rw-r--r-- | src/couchdb/priv/couch_js/help.h | 2 | ||||
-rw-r--r-- | src/couchdb/priv/couch_js/http.c | 60 | ||||
-rw-r--r-- | src/couchdb/priv/couch_js/http.h | 3 | ||||
-rw-r--r-- | src/couchdb/priv/couch_js/sm170.c | 8 | ||||
-rw-r--r-- | src/couchdb/priv/couch_js/sm180.c | 8 | ||||
-rw-r--r-- | src/couchdb/priv/couch_js/sm185.c | 8 | ||||
-rw-r--r-- | src/couchdb/priv/couch_js/util.c | 3 | ||||
-rw-r--r-- | src/couchdb/priv/couch_js/util.h | 2 | ||||
-rw-r--r-- | test/Makefile.am | 1 | ||||
-rw-r--r-- | test/etap/Makefile.am | 1 | ||||
-rw-r--r-- | test/etap/test_util.erl.in | 2 | ||||
-rw-r--r-- | test/javascript/Makefile.am | 1 | ||||
-rw-r--r-- | test/javascript/couch_http.js | 9 | ||||
-rw-r--r-- | test/javascript/run.tpl | 9 | ||||
-rw-r--r-- | test/random_port.ini (renamed from test/etap/random_port.ini) | 2 |
15 files changed, 107 insertions, 12 deletions
diff --git a/src/couchdb/priv/couch_js/help.h b/src/couchdb/priv/couch_js/help.h index 410259422..b31bb8c0a 100644 --- a/src/couchdb/priv/couch_js/help.h +++ b/src/couchdb/priv/couch_js/help.h @@ -50,6 +50,8 @@ static const char USAGE_TEMPLATE[] = " if package was built with cURL available)\n" " -S SIZE specify that the interpreter should set the\n" " stack quota for JS contexts to SIZE bytes\n" + " -u FILE path to a .uri file containing the address\n" + " (or addresses) of one or more servers\n" "\n" "Report bugs at <%s>.\n"; diff --git a/src/couchdb/priv/couch_js/http.c b/src/couchdb/priv/couch_js/http.c index fb5486983..e8cd789f2 100644 --- a/src/couchdb/priv/couch_js/http.c +++ b/src/couchdb/priv/couch_js/http.c @@ -13,9 +13,13 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> #include <jsapi.h> #include "config.h" #include "utf8.h" +#include "util.h" // Soft dependency on cURL bindings because they're // only used when running the JS tests from the @@ -71,6 +75,12 @@ http_status(JSContext* cx, JSObject* req, jsval body) return -1; } +JSBool +http_uri(JSContext* cx, JSObject* req, couch_args* args, jsval* uri_val) +{ + return JS_FALSE; +} + #else #include <curl/curl.h> @@ -342,6 +352,42 @@ http_status(JSContext* cx, JSObject* req) return http->last_status; } +JSBool +http_uri(JSContext* cx, JSObject* req, couch_args* args, jsval* uri_val) +{ + FILE* uri_fp = NULL; + JSString* uri_str; + + // Default is http://localhost:5984/ when no uri file is specified + if (!args->uri_file) { + uri_str = JS_InternString(cx, "http://localhost:5984/"); + *uri_val = STRING_TO_JSVAL(uri_str); + return JS_TRUE; + } + + // Else check to see if the base url is cached in a reserved slot + if (JS_GetReservedSlot(cx, req, 0, uri_val) && !JSVAL_IS_VOID(*uri_val)) { + return JS_TRUE; + } + + // Read the first line of the couch.uri file. + if(!((uri_fp = fopen(args->uri_file, "r")) && + (uri_str = couch_readline(cx, uri_fp)))) { + JS_ReportError(cx, "Failed to read couch.uri file."); + goto error; + } + + fclose(uri_fp); + *uri_val = STRING_TO_JSVAL(uri_str); + JS_SetReservedSlot(cx, req, 0, *uri_val); + return JS_TRUE; + +error: + if(uri_fp) fclose(uri_fp); + return JS_FALSE; +} + + // Curl Helpers typedef struct { @@ -373,6 +419,7 @@ static JSBool go(JSContext* cx, JSObject* obj, HTTPData* http, char* body, size_t bodylen) { CurlState state; + char* referer; JSString* jsbody; JSBool ret = JS_FALSE; jsval tmp; @@ -400,7 +447,6 @@ go(JSContext* cx, JSObject* obj, HTTPData* http, char* body, size_t bodylen) curl_easy_setopt(HTTP_HANDLE, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); curl_easy_setopt(HTTP_HANDLE, CURLOPT_ERRORBUFFER, ERRBUF); curl_easy_setopt(HTTP_HANDLE, CURLOPT_COOKIEFILE, ""); - curl_easy_setopt(HTTP_HANDLE, CURLOPT_REFERER, "http://127.0.0.1:5984/"); curl_easy_setopt(HTTP_HANDLE, CURLOPT_USERAGENT, "CouchHTTP Client - Relax"); } @@ -410,6 +456,18 @@ go(JSContext* cx, JSObject* obj, HTTPData* http, char* body, size_t bodylen) goto done; } + if(!JS_GetReservedSlot(cx, obj, 0, &tmp)) { + JS_ReportError(cx, "Failed to readreserved slot."); + goto done; + } + + if(!(referer = enc_string(cx, tmp, NULL))) { + JS_ReportError(cx, "Failed to encode referer."); + goto done; + } + curl_easy_setopt(HTTP_HANDLE, CURLOPT_REFERER, referer); + free(referer); + if(http->method < 0 || http->method > OPTIONS) { JS_ReportError(cx, "INTERNAL: Unknown method."); goto done; diff --git a/src/couchdb/priv/couch_js/http.h b/src/couchdb/priv/couch_js/http.h index 373d1e483..63d45bd06 100644 --- a/src/couchdb/priv/couch_js/http.h +++ b/src/couchdb/priv/couch_js/http.h @@ -13,6 +13,8 @@ #ifndef COUCH_JS_HTTP_H #define COUCH_JS_HTTP_H +#include "util.h" + void http_check_enabled(); JSBool http_ctor(JSContext* cx, JSObject* req); void http_dtor(JSContext* cx, JSObject* req); @@ -20,5 +22,6 @@ JSBool http_open(JSContext* cx, JSObject* req, jsval mth, jsval url, jsval snc); JSBool http_set_hdr(JSContext* cx, JSObject* req, jsval name, jsval val); JSBool http_send(JSContext* cx, JSObject* req, jsval body); int http_status(JSContext* cx, JSObject* req); +JSBool http_uri(JSContext* cx, JSObject *req, couch_args* args, jsval* uri); #endif diff --git a/src/couchdb/priv/couch_js/sm170.c b/src/couchdb/priv/couch_js/sm170.c index 5e136dec9..4a18d8fee 100644 --- a/src/couchdb/priv/couch_js/sm170.c +++ b/src/couchdb/priv/couch_js/sm170.c @@ -114,6 +114,13 @@ req_status(JSContext* cx, JSObject* obj, jsval idval, jsval* rval) static JSBool base_url(JSContext *cx, JSObject* obj, jsval idval, jsval* rval) +{ + couch_args *args = (couch_args*)JS_GetContextPrivate(cx); + return http_uri(cx, obj, args, rval); +} + + +static JSBool evalcx(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { JSString *str; @@ -317,6 +324,7 @@ main(int argc, const char* argv[]) JS_SetErrorReporter(cx, couch_error); JS_ToggleOptions(cx, JSOPTION_XML); + JS_SetContextPrivate(cx, args); SETUP_REQUEST(cx); diff --git a/src/couchdb/priv/couch_js/sm180.c b/src/couchdb/priv/couch_js/sm180.c index 5b6984a1f..9ffd1df64 100644 --- a/src/couchdb/priv/couch_js/sm180.c +++ b/src/couchdb/priv/couch_js/sm180.c @@ -117,6 +117,13 @@ req_status(JSContext* cx, JSObject* obj, jsval idval, jsval* vp) static JSBool base_url(JSContext *cx, JSObject* obj, jsid pid, jsval* vp) +{ + couch_args *args = (couch_args*)JS_GetContextPrivate(cx); + return http_uri(cx, obj, args, &JS_RVAL(cx, vp)); +} + + +static JSBool evalcx(JSContext *cx, uintN argc, jsval* vp) { jsval* argv = JS_ARGV(cx, vp); @@ -326,6 +333,7 @@ main(int argc, const char* argv[]) JS_SetErrorReporter(cx, couch_error); JS_ToggleOptions(cx, JSOPTION_XML); + JS_SetContextPrivate(cx, args); SETUP_REQUEST(cx); diff --git a/src/couchdb/priv/couch_js/sm185.c b/src/couchdb/priv/couch_js/sm185.c index 52a81a40f..c11e885d9 100644 --- a/src/couchdb/priv/couch_js/sm185.c +++ b/src/couchdb/priv/couch_js/sm185.c @@ -135,6 +135,13 @@ req_status(JSContext* cx, JSObject* obj, jsid pid, jsval* vp) static JSBool base_url(JSContext *cx, JSObject* obj, jsid pid, jsval* vp) +{ + couch_args *args = (couch_args*)JS_GetContextPrivate(cx); + return http_uri(cx, obj, args, &JS_RVAL(cx, vp)); +} + + +static JSBool evalcx(JSContext *cx, uintN argc, jsval* vp) { jsval* argv = JS_ARGV(cx, vp); @@ -338,6 +345,7 @@ main(int argc, const char* argv[]) JS_SetErrorReporter(cx, couch_error); JS_ToggleOptions(cx, JSOPTION_XML); + JS_SetContextPrivate(cx, args); SETUP_REQUEST(cx); diff --git a/src/couchdb/priv/couch_js/util.c b/src/couchdb/priv/couch_js/util.c index d41d9e994..5c88402e2 100644 --- a/src/couchdb/priv/couch_js/util.c +++ b/src/couchdb/priv/couch_js/util.c @@ -94,6 +94,8 @@ couch_parse_args(int argc, const char* argv[]) fprintf(stderr, "Invalid stack size.\n"); exit(2); } + } else if(strcmp("-u", argv[i]) == 0) { + args->uri_file = argv[++i]; } else if(strcmp("--", argv[i]) == 0) { i++; break; @@ -290,4 +292,3 @@ couch_load_funcs(JSContext* cx, JSObject* obj, JSFunctionSpec* funcs) } return JS_TRUE; } - diff --git a/src/couchdb/priv/couch_js/util.h b/src/couchdb/priv/couch_js/util.h index 1319e62b7..65a2a0664 100644 --- a/src/couchdb/priv/couch_js/util.h +++ b/src/couchdb/priv/couch_js/util.h @@ -19,6 +19,8 @@ typedef struct { int use_http; int stack_size; const char** scripts; + const char* uri_file; + JSString* uri; } couch_args; couch_args* couch_parse_args(int argc, const char* argv[]); diff --git a/test/Makefile.am b/test/Makefile.am index 45130a64e..7c70a5a28 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -11,4 +11,5 @@ ## the License. SUBDIRS = bench etap javascript view_server +EXTRA_DIST = random_port.ini diff --git a/test/etap/Makefile.am b/test/etap/Makefile.am index be85c49e4..b72b982bf 100644 --- a/test/etap/Makefile.am +++ b/test/etap/Makefile.am @@ -32,7 +32,6 @@ DISTCLEANFILES = temp.* EXTRA_DIST = \ run.tpl \ test_web.erl \ - random_port.ini \ 001-load.t \ 002-icu-driver.t \ 010-file-basics.t \ diff --git a/test/etap/test_util.erl.in b/test/etap/test_util.erl.in index 92b9208fd..352714e35 100644 --- a/test/etap/test_util.erl.in +++ b/test/etap/test_util.erl.in @@ -46,7 +46,7 @@ build_file(Name) -> config_files() -> [ build_file("etc/couchdb/default_dev.ini"), - source_file("test/etap/random_port.ini"), + source_file("test/random_port.ini"), build_file("etc/couchdb/local_dev.ini") ]. diff --git a/test/javascript/Makefile.am b/test/javascript/Makefile.am index 71f9ae62f..d7367e0d2 100644 --- a/test/javascript/Makefile.am +++ b/test/javascript/Makefile.am @@ -21,5 +21,6 @@ CLEANFILES = run run: run.tpl sed -e "s|%abs_top_srcdir%|$(abs_top_srcdir)|" \ -e "s|%abs_top_builddir%|$(abs_top_builddir)|" \ + -e "s|%localstaterundir%|$(abs_top_builddir)/tmp/run|g" \ < $< > $@ chmod +x $@ diff --git a/test/javascript/couch_http.js b/test/javascript/couch_http.js index 141993eff..c44ce2823 100644 --- a/test/javascript/couch_http.js +++ b/test/javascript/couch_http.js @@ -11,15 +11,14 @@ // the License. (function() { - CouchHTTP.prototype.base_url = "http://127.0.0.1:5984" - if(typeof(CouchHTTP) != "undefined") { CouchHTTP.prototype.open = function(method, url, async) { if(!/^\s*http:\/\//.test(url)) { - if(/^[^\/]/.test(url)) { - url = this.base_url + "/" + url; + if(/^\//.test(url)) { + // The couch.uri file (base_url) has a trailing slash + url = this.base_url + url.slice(1); } else { - url = this.base_url + url; + url = this.base_url + url; } } diff --git a/test/javascript/run.tpl b/test/javascript/run.tpl index 47d2f6e64..ac78b5005 100644 --- a/test/javascript/run.tpl +++ b/test/javascript/run.tpl @@ -17,6 +17,7 @@ SCRIPT_DIR=$SRC_DIR/share/www/script JS_TEST_DIR=$SRC_DIR/test/javascript COUCHJS=%abs_top_builddir%/src/couchdb/priv/couchjs +COUCH_URI_FILE=%localstaterundir%/couch.uri if [ "$#" -eq 0 ]; then @@ -48,11 +49,15 @@ abort() { if [ -z $COUCHDB_NO_START ]; then make dev trap 'abort' 0 1 2 3 4 6 8 15 - ./utils/run -b -r 1 + ./utils/run -b -r 1 -n \ + -a $SRC_DIR/etc/couchdb/default_dev.ini \ + -a $SRC_DIR/test/random_port.ini \ + -a $SRC_DIR/etc/couchdb/local_dev.ini sleep 1 # give it a sec fi -$COUCHJS -H \ +# start the tests +$COUCHJS -H -u $COUCH_URI_FILE \ $SCRIPT_DIR/json2.js \ $SCRIPT_DIR/sha1.js \ $SCRIPT_DIR/oauth.js \ diff --git a/test/etap/random_port.ini b/test/random_port.ini index ada3c13d8..2b2d13027 100644 --- a/test/etap/random_port.ini +++ b/test/random_port.ini @@ -5,7 +5,7 @@ ; to you under the Apache License, Version 2.0 (the ; "License"); you may not use this file except in compliance ; with the License. You may obtain a copy of the License at -; +; ; http://www.apache.org/licenses/LICENSE-2.0 ; ; Unless required by applicable law or agreed to in writing, |