summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/couch/test/couch_db_tests.erl223
-rwxr-xr-xsrc/couch/test/couchdb_cookie_domain_tests.erl78
-rw-r--r--src/couch/test/global_changes_tests.erl2
3 files changed, 169 insertions, 134 deletions
diff --git a/src/couch/test/couch_db_tests.erl b/src/couch/test/couch_db_tests.erl
index c57a0d497..62e059070 100644
--- a/src/couch/test/couch_db_tests.erl
+++ b/src/couch/test/couch_db_tests.erl
@@ -17,25 +17,55 @@
-define(TIMEOUT, 120).
-setup() ->
- Ctx = test_util:start_couch(),
- config:set("log", "include_sasl", "false", false),
- Ctx.
-
create_delete_db_test_()->
{
"Database create/delete tests",
{
setup,
- fun setup/0, fun test_util:stop_couch/1,
- fun(_) ->
- [should_create_db(),
- should_delete_db(),
- should_create_multiple_dbs(),
- should_delete_multiple_dbs(),
- should_create_delete_database_continuously()]
- end
+ fun test_util:start_couch/0, fun test_util:stop_couch/1,
+ {
+ foreach,
+ fun() -> ?tempdb() end,
+ [
+ fun should_create_db/1,
+ fun should_delete_db/1
+ ]
+ }
+ }
+ }.
+
+create_delete_multiple_dbs_test_()->
+ {
+ "Multiple database create/delete tests",
+ {
+ setup,
+ fun test_util:start_couch/0, fun test_util:stop_couch/1,
+ {
+ foreach,
+ fun() -> [?tempdb() || _ <- lists:seq(1, 6)] end,
+ [
+ fun should_create_multiple_dbs/1,
+ fun should_delete_multiple_dbs/1
+ ]
+ }
+ }
+ }.
+
+create_delete_database_continuously_test_() ->
+ {
+ "Continious database create/delete tests",
+ {
+ setup,
+ fun test_util:start_couch/0, fun test_util:stop_couch/1,
+ {
+ foreachx,
+ fun(_) -> ?tempdb() end,
+ [
+ {10, fun should_create_delete_database_continuously/2},
+ {100, fun should_create_delete_database_continuously/2}
+ ]
+ }
}
}.
@@ -44,87 +74,100 @@ open_db_test_()->
"Database open tests",
{
setup,
- fun setup/0, fun test_util:stop_couch/1,
- fun(_) ->
- [should_create_db_if_missing()]
- end
+ fun test_util:start_couch/0, fun test_util:stop_couch/1,
+ {
+ foreach,
+ fun() -> ?tempdb() end,
+ [
+ fun should_create_db_if_missing/1,
+ fun should_open_db_if_exists/1
+ ]
+ }
}
}.
-should_create_db() ->
- DbName = ?tempdb(),
- {ok, Db} = couch_db:create(DbName, []),
- ok = couch_db:close(Db),
- {ok, AllDbs} = couch_server:all_databases(),
- ?_assert(lists:member(DbName, AllDbs)).
-
-should_delete_db() ->
- DbName = ?tempdb(),
- couch_db:create(DbName, []),
- couch_server:delete(DbName, []),
- {ok, AllDbs} = couch_server:all_databases(),
- ?_assertNot(lists:member(DbName, AllDbs)).
-
-should_create_multiple_dbs() ->
- gen_server:call(couch_server, {set_max_dbs_open, 3}),
-
- DbNames = [?tempdb() || _ <- lists:seq(1, 6)],
- lists:foreach(fun(DbName) ->
- {ok, Db} = couch_db:create(DbName, []),
- ok = couch_db:close(Db)
- end, DbNames),
-
- {ok, AllDbs} = couch_server:all_databases(),
- NumCreated = lists:foldl(fun(DbName, Acc) ->
- ?assert(lists:member(DbName, AllDbs)),
- Acc+1
- end, 0, DbNames),
-
- ?_assertEqual(NumCreated, 6).
-
-should_delete_multiple_dbs() ->
- DbNames = [?tempdb() || _ <- lists:seq(1, 6)],
- lists:foreach(fun(DbName) ->
- {ok, Db} = couch_db:create(DbName, []),
- ok = couch_db:close(Db)
- end, DbNames),
-
- lists:foreach(fun(DbName) ->
- ok = couch_server:delete(DbName, [])
- end, DbNames),
-
- {ok, AllDbs} = couch_server:all_databases(),
- NumDeleted = lists:foldl(fun(DbName, Acc) ->
- ?assertNot(lists:member(DbName, AllDbs)),
- Acc + 1
- end, 0, DbNames),
-
- ?_assertEqual(NumDeleted, 6).
-
-should_create_delete_database_continuously() ->
- DbName = ?tempdb(),
- {ok, Db} = couch_db:create(DbName, []),
- couch_db:close(Db),
- [{timeout, ?TIMEOUT, {integer_to_list(N) ++ " times",
- ?_assert(loop(DbName, N))}}
- || N <- [10, 100]].
-
-should_create_db_if_missing() ->
- DbName = ?tempdb(),
- {ok, Db} = couch_db:open(DbName, [{create_if_missing, true}]),
+should_create_db(DbName) ->
+ ?_test(begin
+ {ok, Before} = couch_server:all_databases(),
+ ?assertNot(lists:member(DbName, Before)),
+ ?assert(create_db(DbName)),
+ {ok, After} = couch_server:all_databases(),
+ ?assert(lists:member(DbName, After))
+ end).
+
+should_delete_db(DbName) ->
+ ?_test(begin
+ ?assert(create_db(DbName)),
+ {ok, Before} = couch_server:all_databases(),
+ ?assert(lists:member(DbName, Before)),
+ couch_server:delete(DbName, []),
+ {ok, After} = couch_server:all_databases(),
+ ?assertNot(lists:member(DbName, After))
+ end).
+
+should_create_multiple_dbs(DbNames) ->
+ ?_test(begin
+ gen_server:call(couch_server, {set_max_dbs_open, 3}),
+ {ok, Before} = couch_server:all_databases(),
+ [?assertNot(lists:member(DbName, Before)) || DbName <- DbNames],
+ [?assert(create_db(DbName)) || DbName <- DbNames],
+ {ok, After} = couch_server:all_databases(),
+ [?assert(lists:member(DbName, After)) || DbName <- DbNames]
+ end).
+
+should_delete_multiple_dbs(DbNames) ->
+ ?_test(begin
+ [?assert(create_db(DbName)) || DbName <- DbNames],
+ {ok, Before} = couch_server:all_databases(),
+ [?assert(lists:member(DbName, Before)) || DbName <- DbNames],
+ [?assert(delete_db(DbName)) || DbName <- DbNames],
+ {ok, After} = couch_server:all_databases(),
+ [?assertNot(lists:member(DbName, After)) || DbName <- DbNames]
+ end).
+
+should_create_delete_database_continuously(Times, DbName) ->
+ {lists:flatten(io_lib:format("~b times", [Times])),
+ ?_test(begin
+ ?assert(create_db(DbName)),
+ lists:foreach(fun(_) ->
+ {timeout, ?TIMEOUT, [
+ ?assert(delete_db(DbName)),
+ ?assert(create_db(DbName))
+ ]}
+ end, lists:seq(1, Times))
+ end)}.
+
+should_create_db_if_missing(DbName) ->
+ ?_test(begin
+ {ok, Before} = couch_server:all_databases(),
+ ?assertNot(lists:member(DbName, Before)),
+ {ok, Db} = couch_db:open(DbName, [{create_if_missing, true}]),
+ ok = couch_db:close(Db),
+ {ok, After} = couch_server:all_databases(),
+ ?assert(lists:member(DbName, After))
+ end).
+
+should_open_db_if_exists(DbName) ->
+ ?_test(begin
+ ?assert(create_db(DbName)),
+ {ok, Before} = couch_server:all_databases(),
+ ?assert(lists:member(DbName, Before)),
+ {ok, Db} = couch_db:open(DbName, [{create_if_missing, true}]),
+ ok = couch_db:close(Db),
+ {ok, After} = couch_server:all_databases(),
+ ?assert(lists:member(DbName, After))
+ end).
+
+
+create_db(DbName) ->
+ create_db(DbName, []).
+
+create_db(DbName, Opts) ->
+ {ok, Db} = couch_db:create(DbName, Opts),
ok = couch_db:close(Db),
- {ok, AllDbs} = couch_server:all_databases(),
- ?_assert(lists:member(DbName, AllDbs)).
-
-loop(_, 0) ->
- true;
-loop(DbName, N) ->
- ok = cycle(DbName),
- loop(DbName, N - 1).
+ true.
-cycle(DbName) ->
+delete_db(DbName) ->
ok = couch_server:delete(DbName, []),
- {ok, Db} = couch_db:create(DbName, []),
- couch_db:close(Db),
- ok.
+ true.
diff --git a/src/couch/test/couchdb_cookie_domain_tests.erl b/src/couch/test/couchdb_cookie_domain_tests.erl
index 1a9aedb93..e66ab31e6 100755
--- a/src/couch/test/couchdb_cookie_domain_tests.erl
+++ b/src/couch/test/couchdb_cookie_domain_tests.erl
@@ -18,60 +18,52 @@
-define(USER, "cookie_domain_test_admin").
-define(PASS, "pass").
-setup(PortType) ->
+setup() ->
+ Ctx = test_util:start_couch([chttpd]),
Hashed = couch_passwords:hash_admin_password(?PASS),
ok = config:set("admins", ?USER, ?b2l(Hashed), _Persist=false),
Addr = config:get("httpd", "bind_address", "127.0.0.1"),
- lists:concat(["http://", Addr, ":", port(PortType), "/_session"]).
+ Port = mochiweb_socket_server:get(chttpd, port),
+ Url = ?l2b(io_lib:format("http://~s:~b/_session", [Addr, Port])),
+ ContentType = [{"Content-Type", "application/json"}],
+ Payload = jiffy:encode({[{name, ?l2b(?USER)}, {password, ?l2b(?PASS)}]}),
+ {ok, ?b2l(Url), ContentType, ?b2l(Payload), Ctx}.
-teardown(_,_) ->
- ok = config:delete("admins", ?USER, _Persist=false).
+teardown({ok, _, _, _, Ctx}) ->
+ ok = config:delete("admins", ?USER, _Persist=false),
+ test_util:stop_couch(Ctx).
cookie_test_() ->
- Tests = [
- fun should_set_cookie_domain/2,
- fun should_not_set_cookie_domain/2
- ],
{
"Cookie domain tests",
{
setup,
- fun() -> test_util:start_couch([chttpd]) end, fun test_util:stop_couch/1,
- [
- make_test_case(clustered, Tests)
- ]
+ fun setup/0,
+ fun teardown/1,
+ fun({ok, Url, ContentType, Payload, _}) ->
+ [
+ should_set_cookie_domain(Url, ContentType, Payload),
+ should_not_set_cookie_domain(Url, ContentType, Payload)
+ ]
+ end
}
}.
-make_test_case(Mod, Funs) ->
-{
- lists:flatten(io_lib:format("~s", [Mod])),
- {foreachx, fun setup/1, fun teardown/2, [{Mod, Fun} || Fun <- Funs]}
-}.
+should_set_cookie_domain(Url, ContentType, Payload) ->
+ ?_test(begin
+ ok = config:set("couch_httpd_auth", "cookie_domain",
+ "example.com", false),
+ {ok, Code, Headers, _} = test_request:post(Url, ContentType, Payload),
+ ?assertEqual(200, Code),
+ Cookie = proplists:get_value("Set-Cookie", Headers),
+ ?assert(string:str(Cookie, "; Domain=example.com") > 0)
+ end).
-should_set_cookie_domain(_PortType, Url) ->
- ?_assertEqual(true,
- begin
- ok = config:set("couch_httpd_auth", "cookie_domain", "example.com", false),
- {ok, Code, Headers, _} = test_request:post(Url, [{"Content-Type", "application/json"}],
- "{\"name\":\"" ++ ?USER ++ "\", \"password\": \"" ++ ?PASS ++ "\"}"),
- ?_assert(Code =:= 200),
- Cookie = proplists:get_value("Set-Cookie", Headers),
- string:str(Cookie, "; Domain=example.com") > 0
- end).
-
-should_not_set_cookie_domain(_PortType, Url) ->
- ?_assertEqual(0,
- begin
- ok = config:set("couch_httpd_auth", "cookie_domain", "", false),
- {ok, Code, Headers, _} = test_request:post(Url, [{"Content-Type", "application/json"}],
- "{\"name\":\"" ++ ?USER ++ "\", \"password\": \"" ++ ?PASS ++ "\"}"),
- ?_assert(Code =:= 200),
- Cookie = proplists:get_value("Set-Cookie", Headers),
- string:str(Cookie, "; Domain=")
- end).
-
-port(clustered) ->
- integer_to_list(mochiweb_socket_server:get(chttpd, port));
-port(backdoor) ->
- integer_to_list(mochiweb_socket_server:get(couch_httpd, port)).
+should_not_set_cookie_domain(Url, ContentType, Payload) ->
+ ?_test(begin
+ ok = config:set("couch_httpd_auth", "cookie_domain", "", false),
+ {ok, Code, Headers, _} = test_request:post(Url, ContentType, Payload),
+ ?assertEqual(200, Code),
+ Cookie = proplists:get_value("Set-Cookie", Headers),
+ ?assertEqual(0, string:str(Cookie, "; Domain="))
+ end).
diff --git a/src/couch/test/global_changes_tests.erl b/src/couch/test/global_changes_tests.erl
index 864a6a0ec..4392aafac 100644
--- a/src/couch/test/global_changes_tests.erl
+++ b/src/couch/test/global_changes_tests.erl
@@ -32,7 +32,7 @@ teardown({_, DbName}) ->
ok.
http_create_db(Name) ->
- Resp = {ok, Status, _, _} = test_request:put(db_url(Name), [?AUTH], ""),
+ {ok, Status, _, _} = test_request:put(db_url(Name), [?AUTH], ""),
true = lists:member(Status, [201, 202]),
ok.