1#!/usr/bin/env escript
2%% -*- erlang -*-
3%%! -smp enable
4
5% Licensed under the Apache License, Version 2.0 (the "License"); you may not
6% use this file except in compliance with the License. You may obtain a copy of
7% the License at
8%
9%   http://www.apache.org/licenses/LICENSE-2.0
10%
11% Unless required by applicable law or agreed to in writing, software
12% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14% License for the specific language governing permissions and limitations under
15% the License.
16
17default_config() ->
18    test_util:build_file("etc/couchdb/default_dev.ini").
19
20main(_) ->
21    test_util:init_code_path(),
22    etap:plan(6),
23    case (catch test()) of
24        ok ->
25            etap:end_tests();
26        Other ->
27            etap:diag(io_lib:format("Test died abnormally: ~p", [Other])),
28            etap:bail(Other)
29    end,
30    ok.
31
32test() ->
33    couch_config:start_link([default_config()]),
34
35    etap:is(
36        couch_config:get("httpd", "port"),
37        "5984",
38        "{httpd, port} is 5984 by default."
39    ),
40
41    ok = couch_config:set("httpd", "port", "4895", false),
42
43    etap:is(
44        couch_config:get("httpd", "port"),
45        "4895",
46        "{httpd, port} changed to 4895"
47    ),
48
49    SentinelFunc = fun() ->
50        % Ping/Pong to make sure we wait for this
51        % process to die
52        receive {ping, From} -> From ! pong end
53    end,
54    SentinelPid = spawn(SentinelFunc),
55
56    couch_config:register(
57        fun("httpd", "port", Value) ->
58            etap:is(Value, "8080", "Registered function got notification.")
59        end,
60        SentinelPid
61    ),
62
63    ok = couch_config:set("httpd", "port", "8080", false),
64
65    % Implicitly checking that we *don't* call the function
66    etap:is(
67        couch_config:get("httpd", "ip4_bind_address"),
68        "127.0.0.1",
69        "{httpd, ip4_bind_address} is not '0.0.0.0'"
70    ),
71    ok = couch_config:set("httpd", "ip4_bind_address", "0.0.0.0", false),
72
73    etap:is(
74        couch_config:get("httpd", "ip6_bind_address"),
75        "::1",
76        "{httpd, ip6_bind_address} is not ::"
77    ),
78    ok = couch_config:set("httpd", "ip6_bind_address", "::", false),
79
80
81    % Ping-Pong kill process
82    SentinelPid ! {ping, self()},
83    receive
84        _Any -> ok
85    after 1000 ->
86        throw({timeout_error, registered_pid})
87    end,
88
89    ok = couch_config:set("httpd", "port", "80", false),
90    etap:is(
91        couch_config:get("httpd", "port"),
92        "80",
93        "Implicitly test that the function got de-registered"
94    ),
95
96    % test passing of Persist flag
97    couch_config:register(
98        fun("httpd", _, _, Persist) ->
99            etap:is(Persist, false)
100        end),
101    ok = couch_config:set("httpd", "port", "80", false),
102
103    ok.
104