summaryrefslogtreecommitdiff
path: root/src/couch_replicator/test/eunit/couch_replicator_transient_jobs_tests.erl
blob: 222d13809ecb010b09e457a33bc2e1d5875d66ba (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
% 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(couch_replicator_transient_jobs_tests).

-include_lib("couch/include/couch_eunit.hrl").
-include_lib("couch/include/couch_db.hrl").
-include_lib("couch_replicator/src/couch_replicator.hrl").
-include_lib("fabric/test/fabric2_test.hrl").


transient_jobs_test_() ->
    {
        "Transient jobs tests",
        {
            setup,
            fun couch_replicator_test_helper:start_couch/0,
            fun couch_replicator_test_helper:stop_couch/1,
            {
                foreach,
                fun setup/0,
                fun teardown/1,
                [
                    ?TDEF_FE(transient_job_is_removed, 10),
                    ?TDEF_FE(posting_same_job_is_a_noop, 10),
                    ?TDEF_FE(transient_job_with_a_bad_filter, 10)
                ]
            }
        }
    }.


setup() ->
    Source = couch_replicator_test_helper:create_db(),
    couch_replicator_test_helper:create_docs(Source, [
        #{<<"_id">> => <<"doc1">>}
    ]),
    Target = couch_replicator_test_helper:create_db(),
    config:set("replicator", "stats_update_interval_sec", "0", false),
    config:set("replicator", "transient_job_max_age_sec", "9999", false),
    {Source, Target}.


teardown({Source, Target}) ->
    config:delete("replicator", "stats_update_interval_sec", false),
    config:delete("replicator", "transient_job_max_age_sec", false),
    couch_replicator_test_helper:delete_db(Source),
    couch_replicator_test_helper:delete_db(Target).


transient_job_is_removed({Source, Target}) ->
    {ok, #{}} = replicate(Source, Target),
    JobId = get_rep_id(Source, Target),

    couch_replicator_job_server:reschedule(),

    % Still there after clean up attempt ran
    ?assertMatch({200, #{}}, scheduler_jobs(JobId)),

    config:set("replicator", "transient_job_max_age_sec", "0", false),
    couch_replicator_job_server:reschedule(),

    % Should be gone now
    ?assertMatch({404, #{}}, scheduler_jobs(JobId)).


posting_same_job_is_a_noop({Source, Target}) ->
    {ok, Pid1, RepId1} = replicate_continuous(Source, Target),
    {ok, Pid2, RepId2} = replicate_continuous(Source, Target),
    ?assertEqual(RepId1, RepId2),
    ?assertEqual(Pid1, Pid2),
    couch_replicator_test_helper:cancel(RepId1).


transient_job_with_a_bad_filter({Source, Target}) ->
    DDoc = #{<<"_id">> => <<"_design/myddoc">>},
    couch_replicator_test_helper:create_docs(Source, [DDoc]),
    Result = couch_replicator:replicate(#{
        <<"source">> => couch_replicator_test_helper:db_url(Source),
        <<"target">> => couch_replicator_test_helper:db_url(Target),
        <<"continuous">> => true,
        <<"filter">> => <<"myddoc/myfilter">>
    }, ?ADMIN_USER),
    ?assertMatch({error, #{<<"error">> := <<"filter_fetch_error">>}}, Result).


get_rep_id(Source, Target) ->
    {ok, Id, _} = couch_replicator_parse:parse_transient_rep(#{
        <<"source">> => couch_replicator_test_helper:db_url(Source),
        <<"target">> => couch_replicator_test_helper:db_url(Target)
    }, null),
    Id.


replicate(Source, Target) ->
    couch_replicator:replicate(#{
        <<"source">> => couch_replicator_test_helper:db_url(Source),
        <<"target">> => couch_replicator_test_helper:db_url(Target)
    }, ?ADMIN_USER).


replicate_continuous(Source, Target) ->
    couch_replicator_test_helper:replicate_continuous(Source, Target).


scheduler_jobs(Id) ->
    SUrl = couch_replicator_test_helper:server_url(),
    Url = lists:flatten(io_lib:format("~s/_scheduler/jobs/~s", [SUrl, Id])),
    {ok, Code, _, Body} = test_request:get(Url, []),
    {Code, jiffy:decode(Body, [return_maps])}.