summaryrefslogtreecommitdiff
path: root/src/mem3/test/eunit/mem3_seeds_test.erl
blob: 1c1e2fed6127bdf820610e706989f52cb7ca4a5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
% 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(mem3_seeds_test).

-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}.

empty_seedlist_status_ok() ->
    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.

seedlist_misconfiguration() ->
    config:set("cluster", "seedlist", "couchdb@node1.example.com,couchdb@node2.example.com", false),
    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.

check_nodelist() ->
    config:set("cluster", "seedlist", "couchdb@node1.example.com,couchdb@node2.example.com", 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.

check_local_dbs() ->
    LocalDbs = mem3_sync:local_dbs(),
    {ok, _} = couch_server:create(<<"_users">>, []),
    ?assertEqual(lists:append(LocalDbs, [<<"_users">>]),
        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.

setup() ->
    test_util:start_couch([rexi]).

teardown(Ctx) ->
    test_util:stop_couch(Ctx).