summaryrefslogtreecommitdiff
path: root/src/couch/test/couchdb_os_daemons_tests.erl
blob: 9e8be5fc3e4e855d5f1ccd4cb25c0b6b1f8010f7 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
% 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(couchdb_os_daemons_tests).

%% tests are UNIX-specific, will not function under Windows
-ifdef(WINDOWS).
-undef(TEST).
-define(NOTEST, 1).
-endif.

-include_lib("couch/include/couch_eunit.hrl").

%% keep in sync with couchdb/couch_os_daemons.erl
-record(daemon, {
    port,
    name,
    cmd,
    kill,
    status=running,
    cfg_patterns=[],
    errors=[],
    buf=[]
}).

-define(DAEMON_CONFIGER, "os_daemon_configer.escript").
-define(DAEMON_LOOPER, "os_daemon_looper.escript").
-define(DAEMON_BAD_PERM, "os_daemon_bad_perm.sh").
-define(DAEMON_CAN_REBOOT, "os_daemon_can_reboot.sh").
-define(DAEMON_DIE_ON_BOOT, "os_daemon_die_on_boot.sh").
-define(DAEMON_DIE_QUICKLY, "os_daemon_die_quickly.sh").
-define(TRIES, 20).
-define(TRY_DELAY_MS, 100).
-define(TIMEOUT, 10000).
-define(CONFIG_TIMEOUT, 1000).


setup(DName) ->
    Ctx = test_util:start(?MODULE, [couch_log], [{dont_mock, [config]}]),
    {ok, OsDPid} = couch_os_daemons:start_link(),
    config:set("os_daemons", DName,
                     filename:join([?FIXTURESDIR, DName]), false),
    % Set configuration option to be used by configuration_reader_test_
    % This will be used in os_daemon_configer.escript:test_get_cfg2
    config:set("uuids", "algorithm","sequential", false),
    ensure_n_daemons_are_alive(1),
    {Ctx, OsDPid}.

teardown(_, {Ctx, OsDPid}) ->
    test_util:stop_sync_throw(OsDPid, fun() ->
        exit(OsDPid, shutdown)
    end, {timeout, os_daemon_stop}, ?TIMEOUT),
    test_util:stop(Ctx).


os_daemons_test_() ->
    {
        "OS Daemons tests",
        {
            foreachx,
            fun setup/1, fun teardown/2,
            [{?DAEMON_LOOPER, Fun} || Fun <- [
                fun should_check_daemon/2,
                fun should_check_daemon_table_form/2,
                fun should_clean_tables_on_daemon_remove/2,
                fun should_spawn_multiple_daemons/2,
                fun should_keep_alive_one_daemon_on_killing_other/2
            ]]
        }
    }.

configuration_reader_test_() ->
    {
        "OS Daemon requests CouchDB configuration",
        {
            foreachx,
            fun setup/1, fun teardown/2,
            [{?DAEMON_CONFIGER,
              fun should_read_write_config_settings_by_daemon/2}]

        }
    }.

error_test_() ->
    {
        "OS Daemon process error tests",
        {
            foreachx,
            fun setup/1, fun teardown/2,
            [{?DAEMON_BAD_PERM, fun should_fail_due_to_lack_of_permissions/2},
             {?DAEMON_DIE_ON_BOOT, fun should_die_on_boot/2},
             {?DAEMON_DIE_QUICKLY, fun should_die_quickly/2},
             {?DAEMON_CAN_REBOOT, fun should_not_being_halted/2}]
        }
    }.


should_check_daemon(DName, _) ->
    ?_test(begin
        {ok, [D]} = couch_os_daemons:info([table]),
        check_daemon(D, DName)
    end).

should_check_daemon_table_form(DName, _) ->
    ?_test(begin
        {ok, Tab} = couch_os_daemons:info(),
        [D] = ets:tab2list(Tab),
        check_daemon(D, DName)
    end).

should_clean_tables_on_daemon_remove(DName, _) ->
    ?_test(begin
        config:delete("os_daemons", DName, false),
        {ok, Tab2} = couch_os_daemons:info(),
        ?_assertEqual([], ets:tab2list(Tab2))
    end).

should_spawn_multiple_daemons(DName, _) ->
    ?_test(begin
        config:set("os_daemons", "bar",
                         filename:join([?FIXTURESDIR, DName]), false),
        config:set("os_daemons", "baz",
                         filename:join([?FIXTURESDIR, DName]), false),
        ensure_n_daemons_are_alive(3), % DName, "bar" and "baz"
        {ok, Daemons} = couch_os_daemons:info([table]),
        lists:foreach(fun(D) ->
            check_daemon(D)
        end, Daemons),
        {ok, Tab} = couch_os_daemons:info(),
        lists:foreach(fun(D) ->
            check_daemon(D)
        end, ets:tab2list(Tab))
    end).

should_keep_alive_one_daemon_on_killing_other(DName, _) ->
    ?_test(begin
        config:set("os_daemons", "bar",
                         filename:join([?FIXTURESDIR, DName]), false),
        ensure_n_daemons_are_alive(2), % DName and "bar"
        {ok, Daemons} = couch_os_daemons:info([table]),
        lists:foreach(fun(D) ->
            check_daemon(D)
        end, Daemons),

        config:delete("os_daemons", "bar", false),
        ensure_n_daemons_are_alive(1), % Dname only, "bar" should be dead
        {ok, [D2]} = couch_os_daemons:info([table]),
        check_daemon(D2, DName),

        {ok, Tab} = couch_os_daemons:info(),
        [T] = ets:tab2list(Tab),
        check_daemon(T, DName)
    end).

should_read_write_config_settings_by_daemon(DName, _) ->
    ?_test(begin
        % have to wait till daemon run all his tests
        % see daemon's script for more info
        timer:sleep(?CONFIG_TIMEOUT),
        {ok, [D]} = couch_os_daemons:info([table]),
        check_daemon(D, DName)
    end).

should_fail_due_to_lack_of_permissions(DName, _) ->
    ?_test(should_halts(DName, 1000)).

should_die_on_boot(DName, _) ->
    ?_test(should_halts(DName, 1000)).

should_die_quickly(DName, _) ->
    ?_test(should_halts(DName, 4000)).

should_not_being_halted(DName, _) ->
    ?_test(begin
        timer:sleep(1000),
        {ok, [D1]} = couch_os_daemons:info([table]),
        check_daemon(D1, DName, 0),

        % Should reboot every two seconds. We're at 1s, so wait
        % until 3s to be in the middle of the next invocation's
        % life span.

        timer:sleep(2000),
        {ok, [D2]} = couch_os_daemons:info([table]),
        check_daemon(D2, DName, 1),

        % If the kill command changed, that means we rebooted the process.
        ?assertNotEqual(D1#daemon.kill, D2#daemon.kill)
    end).

should_halts(DName, Time) ->
    timer:sleep(Time),
    {ok, [D]} = couch_os_daemons:info([table]),
    check_dead(D, DName),
    config:delete("os_daemons", DName, false).

check_daemon(D) ->
    check_daemon(D, D#daemon.name).

check_daemon(D, Name) ->
    check_daemon(D, Name, 0).

check_daemon(D, Name, Errs) ->
    ?assert(is_port(D#daemon.port)),
    ?assertEqual(Name, D#daemon.name),
    ?assertNotEqual(undefined, D#daemon.kill),
    ?assertEqual(running, D#daemon.status),
    ?assertEqual(Errs, length(D#daemon.errors)),
    ?assertEqual([], D#daemon.buf).

check_dead(D, Name) ->
    ?assert(is_port(D#daemon.port)),
    ?assertEqual(Name, D#daemon.name),
    ?assertNotEqual(undefined, D#daemon.kill),
    ?assertEqual(halted, D#daemon.status),
    ?assertEqual(nil, D#daemon.errors),
    ?assertEqual(nil, D#daemon.buf).

daemons() ->
    {ok, Daemons} = couch_os_daemons:info([table]),
    Daemons.

ensure_n_daemons_are_alive(NumDaemons) ->
    retry(fun() -> length(daemons()) == NumDaemons end, "spawning"),
    retry(fun() ->
                  lists:all(fun(D) -> D#daemon.kill =/= undefined end, daemons())
          end, "waiting for kill flag").

retry(Pred, FailReason) ->
    retry(Pred, ?TRIES, FailReason).

retry(_Pred, 0, FailReason) ->
    erlang:error({assertion_failed,[{module, ?MODULE}, {line, ?LINE},
                                    {reason, "Timed out: " ++ FailReason}]});
retry(Pred, N, FailReason) ->
    case Pred() of
        true ->
            ok;
        false ->
            timer:sleep(?TRY_DELAY_MS),
            retry(Pred, N - 1, FailReason)
    end.