Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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.
  • Loading branch information...
commit d20e792617db738dd5ad0e046ae847cd740f586f 1 parent 257eb52
@tilgovi tilgovi authored
View
2  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";
View
60 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;
View
3  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
View
8 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);
View
8 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);
View
8 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);
View
3  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;
}
-
View
2  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[]);
View
1  test/Makefile.am
@@ -11,4 +11,5 @@
## the License.
SUBDIRS = bench etap javascript view_server
+EXTRA_DIST = random_port.ini
View
1  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 \
View
2  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")
].
View
1  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 $@
View
9 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;
}
}
View
9 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 \
View
2  test/etap/random_port.ini → 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,
Please sign in to comment.
Something went wrong with that request. Please try again.