You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by va...@apache.org on 2024/04/04 02:34:17 UTC
(couchdb) branch main updated: Fix seedlist to not return duplicate json keys
This is an automated email from the ASF dual-hosted git repository.
vatamane pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/couchdb.git
The following commit(s) were added to refs/heads/main by this push:
new 8bbf77654 Fix seedlist to not return duplicate json keys
8bbf77654 is described below
commit 8bbf77654319c4106974d8e1ac90bd5fd429b8cf
Author: Nick Vatamaniuc <va...@gmail.com>
AuthorDate: Tue Apr 2 14:43:28 2024 -0400
Fix seedlist to not return duplicate json keys
Previously, we used `lists:ukeymerge` which only works if the entries in the
list are kept in a sorted order, but they weren't. Since we now have maps
available in Erlang, use those instead of proplists which would avoid the
sorting issue.
While at it, make sure to test the `_up` and `_uuids` "misc" handler endpoints
and modernize the seedlist test suite as well.
---
src/chttpd/src/chttpd_misc.erl | 14 +-
src/chttpd/test/eunit/chttpd_misc_test.erl | 186 ++++++++++++++++++++++++++
src/chttpd/test/eunit/chttpd_welcome_test.erl | 101 --------------
src/mem3/src/mem3_seeds.erl | 105 +++++++--------
src/mem3/test/eunit/mem3_seeds_test.erl | 125 ++++++++++-------
5 files changed, 316 insertions(+), 215 deletions(-)
diff --git a/src/chttpd/src/chttpd_misc.erl b/src/chttpd/src/chttpd_misc.erl
index ddab5a1fc..4b7c73b35 100644
--- a/src/chttpd/src/chttpd_misc.erl
+++ b/src/chttpd/src/chttpd_misc.erl
@@ -304,16 +304,14 @@ handle_uuids_req(Req) ->
handle_up_req(#httpd{method = 'GET'} = Req) ->
case config:get("couchdb", "maintenance_mode") of
"true" ->
- send_json(Req, 404, {[{status, maintenance_mode}]});
+ send_json(Req, 404, #{status => maintenance_mode});
"nolb" ->
- send_json(Req, 404, {[{status, nolb}]});
+ send_json(Req, 404, #{status => nolb});
_ ->
- {ok, {Status}} = mem3_seeds:get_status(),
- case couch_util:get_value(status, Status) of
- ok ->
- send_json(Req, 200, {Status});
- seeding ->
- send_json(Req, 404, {Status})
+ {ok, #{} = Status} = mem3_seeds:get_status(),
+ case Status of
+ #{status := ok} -> send_json(Req, 200, Status);
+ #{status := seeding} -> send_json(Req, 404, Status)
end
end;
handle_up_req(Req) ->
diff --git a/src/chttpd/test/eunit/chttpd_misc_test.erl b/src/chttpd/test/eunit/chttpd_misc_test.erl
new file mode 100644
index 000000000..030ab1760
--- /dev/null
+++ b/src/chttpd/test/eunit/chttpd_misc_test.erl
@@ -0,0 +1,186 @@
+% Licensed 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, software
+% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+% License for the specific language governing permissions and limitations under
+% the License.
+
+-module(chttpd_misc_test).
+
+-include_lib("couch/include/couch_eunit.hrl").
+-include_lib("couch/include/couch_db.hrl").
+
+-define(USER, "chttpd_misc_test_admin").
+-define(PASS, "pass").
+-define(AUTH, {basic_auth, {?USER, ?PASS}}).
+-define(CONTENT_JSON, {"Content-Type", "application/json"}).
+
+setup() ->
+ Hashed = couch_passwords:hash_admin_password(?PASS),
+ ok = config:set("admins", ?USER, ?b2l(Hashed), _Persist = false),
+ Addr = config:get("chttpd", "bind_address", "127.0.0.1"),
+ Port = mochiweb_socket_server:get(chttpd, port),
+ Url = lists:concat(["http://", Addr, ":", Port, "/"]),
+ Url.
+
+teardown(_Url) ->
+ Persist = false,
+ ok = config:delete("couchdb", "maintenance_mode", Persist = false),
+ ok = config:delete("cluster", "seedlist", Persist = false),
+ ok = config:delete("admins", ?USER, Persist = false).
+
+welcome_test_() ->
+ {
+ "chttpd welcome endpoint tests",
+ {
+ setup,
+ fun chttpd_test_util:start_couch/0,
+ fun chttpd_test_util:stop_couch/1,
+ {
+ foreach,
+ fun setup/0,
+ fun teardown/1,
+ [
+ ?TDEF_FE(should_have_version),
+ ?TDEF_FE(should_have_features),
+ ?TDEF_FE(should_have_uuid)
+ ]
+ }
+ }
+ }.
+
+should_have_uuid(Url) ->
+ {ok, Status, _, Body} = req_get(Url),
+ ?assertEqual(200, Status),
+ #{
+ <<"couchdb">> := CouchDB,
+ <<"uuid">> := Uuid
+ } = Body,
+ ?assertEqual(<<"Welcome">>, CouchDB),
+ RealUuid = couch_server:get_uuid(),
+ ?assertEqual(RealUuid, Uuid).
+
+should_have_version(Url) ->
+ {ok, Status, _, Body} = req_get(Url),
+ ?assertEqual(200, Status),
+ #{
+ <<"couchdb">> := CouchDB,
+ <<"version">> := Version,
+ <<"git_sha">> := Sha
+ } = Body,
+ ?assertNotEqual(Sha, undefined),
+ ?assertEqual(<<"Welcome">>, CouchDB),
+ RealVersion = list_to_binary(couch_server:get_version()),
+ ?assertEqual(RealVersion, Version).
+
+should_have_features(Url) ->
+ config:enable_feature(snek),
+ {ok, 200, _, Body1} = req_get(Url),
+ #{<<"features">> := Features1} = Body1,
+ ?assert(is_list(Features1)),
+ ?assert(lists:member(<<"snek">>, Features1)),
+ config:disable_feature(snek),
+ {ok, 200, _, Body2} = req_get(Url),
+ #{<<"features">> := Features2} = Body2,
+ ?assert(is_list(Features2)),
+ ?assertNot(lists:member(<<"snek">>, Features2)).
+
+up_test_() ->
+ {
+ "chttpd _up endpoint tests",
+ {
+ setup,
+ fun chttpd_test_util:start_couch/0,
+ fun chttpd_test_util:stop_couch/1,
+ {
+ foreach,
+ fun setup/0,
+ fun teardown/1,
+ [
+ ?TDEF_FE(t_basic),
+ ?TDEF_FE(t_maintenance_mode),
+ ?TDEF_FE(t_nolb_mode),
+ ?TDEF_FE(t_with_seeds)
+ ]
+ }
+ }
+ }.
+
+t_basic(Url) ->
+ {ok, Code, _, Body} = req_get(Url ++ "/_up"),
+ ?assertEqual(200, Code),
+ ?assertMatch(#{<<"status">> := <<"ok">>, <<"seeds">> := #{}}, Body).
+
+t_maintenance_mode(Url) ->
+ config:set("couchdb", "maintenance_mode", "true", _Persist = false),
+ {ok, Code, _, Body} = req_get(Url ++ "/_up"),
+ ?assertEqual(404, Code),
+ ?assertMatch(#{<<"status">> := <<"maintenance_mode">>}, Body).
+
+t_nolb_mode(Url) ->
+ config:set("couchdb", "maintenance_mode", "nolb", _Persist = false),
+ {ok, Code, _, Body} = req_get(Url ++ "/_up"),
+ ?assertEqual(404, Code),
+ ?assertMatch(#{<<"status">> := <<"nolb">>}, Body).
+
+t_with_seeds(Url) ->
+ CfgSeeds = "couchdb@node1.example.com,couchdb@node2.example.com",
+ config:set("cluster", "seedlist", CfgSeeds, false),
+ ok = gen_server:stop(mem3_seeds),
+ WaitFun = fun() ->
+ case catch mem3_seeds:get_status() of
+ {ok, #{}} -> ok;
+ _ -> wait
+ end
+ end,
+ test_util:wait(WaitFun),
+ {ok, Code, _, Body} = req_get(Url ++ "/_up"),
+ ?assertEqual(404, Code),
+ ?assertMatch(#{<<"seeds">> := #{}}, Body),
+ ?assertMatch(#{<<"status">> := <<"seeding">>}, Body),
+ #{<<"seeds">> := Seeds} = Body,
+ ?assertMatch(#{<<"couchdb@node1.example.com">> := #{}}, Seeds),
+ ?assertMatch(#{<<"couchdb@node2.example.com">> := #{}}, Seeds).
+
+uuid_test_() ->
+ {
+ "chttpd _uuid endpoint tests",
+ {
+ setup,
+ fun chttpd_test_util:start_couch/0,
+ fun chttpd_test_util:stop_couch/1,
+ {
+ foreach,
+ fun setup/0,
+ fun teardown/1,
+ [
+ ?TDEF_FE(t_one_uuid),
+ ?TDEF_FE(t_multiple_uuids)
+ ]
+ }
+ }
+ }.
+
+t_one_uuid(Url) ->
+ {ok, Code, _, Body} = req_get(Url ++ "/_uuids"),
+ ?assertEqual(200, Code),
+ ?assertMatch(#{<<"uuids">> := [_]}, Body),
+ #{<<"uuids">> := [Uuid]} = Body,
+ ?assertEqual(32, byte_size(Uuid)).
+
+t_multiple_uuids(Url) ->
+ {ok, Code, _, Body} = req_get(Url ++ "/_uuids?count=3"),
+ ?assertEqual(200, Code),
+ ?assertMatch(#{<<"uuids">> := [_, _, _]}, Body),
+ #{<<"uuids">> := [Uuid1, Uuid2, Uuid3]} = Body,
+ ?assert(Uuid1 /= Uuid2),
+ ?assert(Uuid1 /= Uuid3).
+
+req_get(Url) ->
+ {ok, Code, Headers, Body} = test_request:get(Url, [?CONTENT_JSON, ?AUTH]),
+ {ok, Code, Headers, jiffy:decode(Body, [return_maps])}.
diff --git a/src/chttpd/test/eunit/chttpd_welcome_test.erl b/src/chttpd/test/eunit/chttpd_welcome_test.erl
deleted file mode 100644
index 7a24efb71..000000000
--- a/src/chttpd/test/eunit/chttpd_welcome_test.erl
+++ /dev/null
@@ -1,101 +0,0 @@
-% Licensed 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, software
-% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
-% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
-% License for the specific language governing permissions and limitations under
-% the License.
-
--module(chttpd_welcome_test).
-
--include_lib("couch/include/couch_eunit.hrl").
--include_lib("couch/include/couch_db.hrl").
-
--define(USER, "chttpd_db_test_admin").
--define(PASS, "pass").
--define(AUTH, {basic_auth, {?USER, ?PASS}}).
--define(CONTENT_JSON, {"Content-Type", "application/json"}).
-
-setup() ->
- Hashed = couch_passwords:hash_admin_password(?PASS),
- ok = config:set("admins", ?USER, ?b2l(Hashed), _Persist = false),
- Addr = config:get("chttpd", "bind_address", "127.0.0.1"),
- Port = mochiweb_socket_server:get(chttpd, port),
- Url = lists:concat(["http://", Addr, ":", Port, "/"]),
- Url.
-
-teardown(_Url) ->
- ok = config:delete("admins", ?USER, _Persist = false).
-
-welcome_test_() ->
- {
- "chttpd welcome endpoint tests",
- {
- setup,
- fun chttpd_test_util:start_couch/0,
- fun chttpd_test_util:stop_couch/1,
- {
- foreach,
- fun setup/0,
- fun teardown/1,
- [
- fun should_have_version/1,
- fun should_have_features/1,
- fun should_have_uuid/1
- ]
- }
- }
- }.
-
-should_have_uuid(Url) ->
- ?_test(begin
- {ok, Status, _, Body} = test_request:get(Url, [?CONTENT_JSON, ?AUTH]),
- ?assertEqual(200, Status),
- {Json} = ?JSON_DECODE(Body),
- CouchDB = couch_util:get_value(<<"couchdb">>, Json, undefined),
- Uuid = couch_util:get_value(<<"uuid">>, Json, undefined),
- Features = couch_util:get_value(<<"features">>, Json, undefined),
- Sha = couch_util:get_value(<<"git_sha">>, Json, undefined),
- ?assertNotEqual(Sha, undefined),
- ?assertEqual(<<"Welcome">>, CouchDB),
- RealUuid = couch_server:get_uuid(),
-
- ?assertEqual(RealUuid, Uuid),
- ?assert(is_list(Features))
- end).
-
-should_have_version(Url) ->
- ?_test(begin
- {ok, Status, _, Body} = test_request:get(Url, [?CONTENT_JSON, ?AUTH]),
- ?assertEqual(200, Status),
- {Json} = ?JSON_DECODE(Body),
- Version = couch_util:get_value(<<"version">>, Json, undefined),
- CouchDB = couch_util:get_value(<<"couchdb">>, Json, undefined),
- Features = couch_util:get_value(<<"features">>, Json, undefined),
- Sha = couch_util:get_value(<<"git_sha">>, Json, undefined),
- ?assertNotEqual(Sha, undefined),
- ?assertEqual(<<"Welcome">>, CouchDB),
- RealVersion = list_to_binary(couch_server:get_version()),
- ?assertEqual(RealVersion, Version),
- ?assert(is_list(Features))
- end).
-
-should_have_features(Url) ->
- ?_test(begin
- config:enable_feature(snek),
- {ok, 200, _, Body1} = test_request:get(Url, [?CONTENT_JSON, ?AUTH]),
- {Json1} = ?JSON_DECODE(Body1),
- Features1 = couch_util:get_value(<<"features">>, Json1, undefined),
- ?assert(is_list(Features1)),
- ?assert(lists:member(<<"snek">>, Features1)),
- config:disable_feature(snek),
- {ok, 200, _, Body2} = test_request:get(Url, [?CONTENT_JSON, ?AUTH]),
- {Json2} = ?JSON_DECODE(Body2),
- Features2 = couch_util:get_value(<<"features">>, Json2, undefined),
- ?assert(is_list(Features2)),
- ?assertNot(lists:member(<<"snek">>, Features2))
- end).
diff --git a/src/mem3/src/mem3_seeds.erl b/src/mem3/src/mem3_seeds.erl
index b007bdeec..874bd8196 100644
--- a/src/mem3/src/mem3_seeds.erl
+++ b/src/mem3/src/mem3_seeds.erl
@@ -30,12 +30,10 @@
ready = false,
seeds = [],
jobref = nil,
- % nested proplist keyed on node name
- status = []
+ % map keyed on node name
+ status = #{}
}).
--define(REPLICATION_INTERVAL, 60000).
-
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
@@ -54,29 +52,26 @@ get_status() ->
init([]) ->
Seeds = get_seeds(),
- InitStatus = [{Seed, {[]}} || Seed <- Seeds],
- State = #st{
+ Ready =
+ case Seeds of
+ [] -> true;
+ _ -> false
+ end,
+ St = #st{
seeds = Seeds,
- ready =
- case Seeds of
- [] -> true;
- _ -> false
- end,
+ ready = Ready,
jobref = start_replication(Seeds),
- status = InitStatus
+ status = maps:from_keys(Seeds, #{})
},
- {ok, State}.
+ {ok, St}.
handle_call(get_status, _From, St) ->
- Status =
- {[
- {status,
- case St#st.ready of
- true -> ok;
- false -> seeding
- end},
- {seeds, {St#st.status}}
- ]},
+ Ready =
+ case St#st.ready of
+ true -> ok;
+ false -> seeding
+ end,
+ Status = #{status => Ready, seeds => St#st.status},
{reply, {ok, Status}, St}.
handle_cast(_Msg, St) ->
@@ -100,16 +95,14 @@ start_replication([Seed | _]) ->
exit({ok, Reply})
end).
-update_state(State, {ok, Data}) ->
- #st{seeds = [Current | Tail], status = Status} = State,
- Report =
- {[
- {timestamp, list_to_binary(mem3_util:iso8601_timestamp())},
- {last_replication_status, ok},
- format_data(Data)
- ]},
- NewStatus = lists:ukeymerge(1, [{Current, Report}], Status),
- Ready = is_ready(State#st.ready, Data),
+update_state(#st{} = St, {ok, Data}) ->
+ #st{seeds = [Current | Tail], status = #{} = Status} = St,
+ Report = #{
+ timestamp => list_to_binary(mem3_util:iso8601_timestamp()),
+ pending_updates => lists:foldl(fun pending_updates/2, #{}, Data),
+ last_replication_status => ok
+ },
+ Ready = is_ready(St#st.ready, Data),
case Ready of
true ->
Seeds = Tail ++ [Current],
@@ -119,31 +112,29 @@ update_state(State, {ok, Data}) ->
Seeds = [Current | Tail],
Job = start_replication([Current | Tail])
end,
- State#st{
+ St#st{
seeds = Seeds,
jobref = Job,
ready = Ready,
- status = NewStatus
+ status = Status#{Current => Report}
};
-update_state(State, {_Error, _Stack}) ->
- #st{seeds = [Current | Tail], status = Status} = State,
- Report =
- {[
- {timestamp, list_to_binary(mem3_util:iso8601_timestamp())},
- {last_replication_status, error}
- ]},
- NewStatus = lists:ukeymerge(1, [{Current, Report}], Status),
+update_state(#st{} = St, {_Error, _Stack}) ->
+ #st{seeds = [Current | Tail], status = #{} = Status} = St,
+ Report = #{
+ timestamp => list_to_binary(mem3_util:iso8601_timestamp()),
+ last_replication_status => error
+ },
Seeds = Tail ++ [Current],
if
- not State#st.ready ->
+ not St#st.ready ->
erlang:send_after(1000, self(), start_replication);
true ->
ok
end,
- State#st{
+ St#st{
seeds = Seeds,
jobref = nil,
- status = NewStatus
+ status = Status#{Current => Report}
}.
is_ready(true, _) ->
@@ -151,18 +142,12 @@ is_ready(true, _) ->
is_ready(false, Data) ->
lists:all(fun({_DbName, Pending}) -> Pending =:= {ok, 0} end, Data).
-format_data(Data) ->
- Formatted = lists:map(
- fun({DbName, Status}) ->
- case Status of
- {ok, Pending} when is_number(Pending) ->
- {DbName, Pending};
- {error, Tag} ->
- {DbName, list_to_binary(io_lib:format("~p", [Tag]))};
- _Else ->
- {DbName, unknown_error}
- end
- end,
- Data
- ),
- {pending_updates, {Formatted}}.
+pending_updates({DbName, Status}, #{} = Acc) ->
+ case Status of
+ {ok, Pending} when is_number(Pending) ->
+ Acc#{DbName => Pending};
+ {error, Tag} ->
+ Acc#{DbName => list_to_binary(io_lib:format("~p", [Tag]))};
+ _Else ->
+ Acc#{DbName => unknown_error}
+ end.
diff --git a/src/mem3/test/eunit/mem3_seeds_test.erl b/src/mem3/test/eunit/mem3_seeds_test.erl
index 2c9c1d383..4ccae0d07 100644
--- a/src/mem3/test/eunit/mem3_seeds_test.erl
+++ b/src/mem3/test/eunit/mem3_seeds_test.erl
@@ -14,51 +14,73 @@
-include_lib("couch/include/couch_eunit.hrl").
-a_test_() ->
- Tests = [
- {"empty seedlist should set status ok", fun empty_seedlist_status_ok/0},
- {"all seedlist nodes unreachable keeps status seeding", fun seedlist_misconfiguration/0},
- {"seedlist entries should be present in _nodes", fun check_nodelist/0},
- {"optional local _users db in mem3_sync:local_dbs()", fun check_local_dbs/0}
- ],
- {setup, fun setup/0, fun teardown/1, Tests}.
+-define(FAKE_NODES, ['couchdb@node1.example.com', 'couchdb@node2.example.com']).
-empty_seedlist_status_ok() ->
+mem3_seeds_test_() ->
+ {
+ foreach,
+ fun setup/0,
+ fun teardown/1,
+ [
+ ?TDEF_FE(t_empty_seedlist_status_ok),
+ ?TDEF_FE(t_seedlist_misconfiguration),
+ ?TDEF_FE(t_seedlist_replicate_all),
+ ?TDEF_FE(t_seedlist_replicate_partial),
+ ?TDEF_FE(t_check_nodelist),
+ ?TDEF_FE(t_check_local_dbs)
+ ]
+ }.
+
+t_empty_seedlist_status_ok(_) ->
+ ok = application:start(mem3),
+ {ok, Result = #{}} = mem3_seeds:get_status(),
+ ?assertMatch(#{seeds := _, status := ok}, Result).
+
+t_seedlist_misconfiguration(_) ->
+ CfgSeeds = string:join([atom_to_list(N) || N <- ?FAKE_NODES], ","),
+ config:set("cluster", "seedlist", CfgSeeds, false),
+ ok = application:start(mem3),
+ {ok, Result = #{seeds := Seeds}} = mem3_seeds:get_status(),
+ ?assertEqual(2, map_size(Seeds)),
+ ?assertMatch(#{'couchdb@node1.example.com' := _}, Seeds),
+ ?assertMatch(#{'couchdb@node2.example.com' := _}, Seeds),
+ ?assertMatch(#{status := seeding}, Result).
+
+t_seedlist_replicate_all(_) ->
+ CfgSeeds = string:join([atom_to_list(N) || N <- ?FAKE_NODES], ","),
+ config:set("cluster", "seedlist", CfgSeeds, false),
+ mock_rep(?FAKE_NODES, {ok, 0}),
+ meck:reset(mem3_seeds),
ok = application:start(mem3),
- try
- {ok, {Result}} = mem3_seeds:get_status(),
- ?assertEqual({[]}, couch_util:get_value(seeds, Result)),
- ?assertEqual(ok, couch_util:get_value(status, Result))
- after
- cleanup()
- end.
+ meck:wait(mem3_seeds, handle_info, [{'DOWN', '_', '_', '_', '_'}, '_'], 4000),
+ {ok, Result = #{seeds := Seeds}} = mem3_seeds:get_status(),
+ ?assertEqual(2, map_size(Seeds)),
+ ?assertMatch(#{'couchdb@node1.example.com' := _}, Seeds),
+ ?assertMatch(#{'couchdb@node2.example.com' := _}, Seeds),
+ ?assertMatch(#{status := ok}, Result).
-seedlist_misconfiguration() ->
- config:set("cluster", "seedlist", "couchdb@node1.example.com,couchdb@node2.example.com", false),
+t_seedlist_replicate_partial(_) ->
+ CfgSeeds = string:join([atom_to_list(N) || N <- ?FAKE_NODES], ","),
+ config:set("cluster", "seedlist", CfgSeeds, false),
+ mock_rep(?FAKE_NODES, {ok, 42}),
+ meck:reset(mem3_seeds),
ok = application:start(mem3),
- try
- {ok, {Result}} = mem3_seeds:get_status(),
- {Seeds} = couch_util:get_value(seeds, Result),
- ?assertEqual(2, length(Seeds)),
- ?assertMatch({_}, couch_util:get_value('couchdb@node1.example.com', Seeds)),
- ?assertMatch({_}, couch_util:get_value('couchdb@node2.example.com', Seeds)),
- ?assertEqual(seeding, couch_util:get_value(status, Result))
- after
- cleanup()
- end.
+ meck:wait(mem3_seeds, handle_info, [{'DOWN', '_', '_', '_', '_'}, '_'], 4000),
+ {ok, Result = #{seeds := Seeds}} = mem3_seeds:get_status(),
+ ?assertEqual(2, map_size(Seeds)),
+ ?assertMatch(#{'couchdb@node1.example.com' := _}, Seeds),
+ ?assertMatch(#{'couchdb@node2.example.com' := _}, Seeds),
+ ?assertMatch(#{status := seeding}, Result).
-check_nodelist() ->
- config:set("cluster", "seedlist", "couchdb@node1.example.com,couchdb@node2.example.com", false),
+t_check_nodelist(_) ->
+ CfgSeeds = string:join([atom_to_list(N) || N <- ?FAKE_NODES], ","),
+ config:set("cluster", "seedlist", CfgSeeds, false),
ok = application:start(mem3),
- try
- Nodes = mem3:nodes(),
- ?assert(lists:member('couchdb@node1.example.com', Nodes)),
- ?assert(lists:member('couchdb@node2.example.com', Nodes))
- after
- cleanup()
- end.
+ Nodes = mem3:nodes(),
+ ?assert(lists:member('couchdb@node1.example.com', Nodes)),
+ ?assert(lists:member('couchdb@node2.example.com', Nodes)).
-check_local_dbs() ->
+t_check_local_dbs(_) ->
LocalDbs = mem3_sync:local_dbs(),
{ok, _} = couch_server:create(<<"_users">>, []),
?assertEqual(
@@ -66,17 +88,28 @@ check_local_dbs() ->
mem3_sync:local_dbs()
).
-cleanup() ->
- application:stop(mem3),
- Filename = config:get("mem3", "nodes_db", "_nodes") ++ ".couch",
- file:delete(filename:join([?BUILDDIR(), "tmp", "data", Filename])),
- case config:get("couch_httpd_auth", "authentication_db") of
- undefined -> ok;
- DbName -> couch_server:delete(list_to_binary(DbName), [])
- end.
+mock_rep(Nodes, Result) ->
+ ResultDbs = [{DbName, Result} || DbName <- mem3_sync:local_dbs()],
+ meck:expect(mem3_rpc, pull_replication, fun(Node) ->
+ case lists:member(Node, Nodes) of
+ true -> ResultDbs;
+ false -> meck:passthrough()
+ end
+ end).
setup() ->
+ meck:new(mem3_seeds, [passthrough]),
+ meck:new(mem3_rpc, [passthrough]),
test_util:start_couch([rexi]).
teardown(Ctx) ->
+ catch application:stop(mem3),
+ config:delete("cluster", "seedlist", false),
+ Filename = config:get("mem3", "nodes_db", "_nodes") ++ ".couch",
+ file:delete(filename:join([?BUILDDIR(), "tmp", "data", Filename])),
+ case config:get("couch_httpd_auth", "authentication_db") of
+ undefined -> ok;
+ DbName -> couch_server:delete(list_to_binary(DbName), [])
+ end,
+ meck:unload(),
test_util:stop_couch(Ctx).