summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Shorin <kxepal@apache.org>2014-06-03 12:32:02 +0400
committerAlexander Shorin <kxepal@apache.org>2015-12-02 03:49:05 +0300
commitf3103f375e3c8215f0b79257f2e0dc40152e9da6 (patch)
treef4631d1f04b9901db8a3789c69b0c44838814f72
parent523599fb942f08f663669d1dc644d4c4c1ae741c (diff)
downloadcouchdb-f3103f375e3c8215f0b79257f2e0dc40152e9da6.tar.gz
Port 170-os-daemons.t etap test suite to eunit
-rw-r--r--test/couchdb/Makefile.am2
-rw-r--r--test/couchdb/couchdb_os_daemons_tests.erl145
-rwxr-xr-xtest/couchdb/fixtures/os_daemon_looper.escript (renamed from test/etap/170-os-daemons.es)0
-rwxr-xr-xtest/etap/170-os-daemons.t114
-rw-r--r--test/etap/Makefile.am2
5 files changed, 147 insertions, 116 deletions
diff --git a/test/couchdb/Makefile.am b/test/couchdb/Makefile.am
index c4a78e73c..b568335fa 100644
--- a/test/couchdb/Makefile.am
+++ b/test/couchdb/Makefile.am
@@ -39,6 +39,7 @@ eunit_files = \
couchdb_attachments_tests.erl \
couchdb_file_compression_tests.erl \
couchdb_modules_load_tests.erl \
+ couchdb_os_daemons_tests.erl \
couchdb_update_conflicts_tests.erl \
couchdb_vhosts_tests.erl \
couchdb_views_tests.erl \
@@ -50,6 +51,7 @@ fixture_files = \
fixtures/couch_config_tests_2.ini \
fixtures/couch_stats_aggregates.cfg \
fixtures/couch_stats_aggregates.ini \
+ fixtures/os_daemon_looper.escript \
fixtures/logo.png
EXTRA_DIST = \
diff --git a/test/couchdb/couchdb_os_daemons_tests.erl b/test/couchdb/couchdb_os_daemons_tests.erl
new file mode 100644
index 000000000..1f9f6be02
--- /dev/null
+++ b/test/couchdb/couchdb_os_daemons_tests.erl
@@ -0,0 +1,145 @@
+% 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).
+
+-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_LOOPER, "os_daemon_looper.escript").
+-define(DELAY, 100).
+-define(TIMEOUT, 1000).
+
+
+setup(DName) ->
+ {ok, CfgPid} = couch_config:start_link(?CONFIG_CHAIN),
+ {ok, OsDPid} = couch_os_daemons:start_link(),
+ couch_config:set("os_daemons", DName,
+ filename:join([?FIXTURESDIR, DName]), false),
+ timer:sleep(?DELAY), % sleep a bit to let daemon set kill flag
+ {CfgPid, OsDPid}.
+
+teardown(_, {CfgPid, OsDPid}) ->
+ erlang:monitor(process, CfgPid),
+ couch_config:stop(),
+ receive
+ {'DOWN', _, _, CfgPid, _} ->
+ ok
+ after ?TIMEOUT ->
+ throw({timeout, config_stop})
+ end,
+
+ erlang:monitor(process, OsDPid),
+ exit(OsDPid, normal),
+ receive
+ {'DOWN', _, _, OsDPid, _} ->
+ ok
+ after ?TIMEOUT ->
+ throw({timeout, os_daemon_stop})
+ end.
+
+
+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
+ ]]
+ }
+ }.
+
+
+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
+ couch_config:delete("os_daemons", DName, false),
+ {ok, Tab2} = couch_os_daemons:info(),
+ ?_assertEqual([], ets:tab2list(Tab2))
+ end).
+
+should_spawn_multiple_daemons(DName, _) ->
+ ?_test(begin
+ couch_config:set("os_daemons", "bar",
+ filename:join([?FIXTURESDIR, DName]), false),
+ couch_config:set("os_daemons", "baz",
+ filename:join([?FIXTURESDIR, DName]), false),
+ timer:sleep(?DELAY),
+ {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
+ couch_config:set("os_daemons", "bar",
+ filename:join([?FIXTURESDIR, DName]), false),
+ timer:sleep(?DELAY),
+ {ok, Daemons} = couch_os_daemons:info([table]),
+ lists:foreach(fun(D) ->
+ check_daemon(D)
+ end, Daemons),
+
+ couch_config:delete("os_daemons", "bar", false),
+ timer:sleep(?DELAY),
+ {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).
+
+
+check_daemon(D) ->
+ check_daemon(D, D#daemon.name).
+
+check_daemon(D, Name) ->
+ ?assert(is_port(D#daemon.port)),
+ ?assertEqual(Name, D#daemon.name),
+ ?assertNotEqual(undefined, D#daemon.kill),
+ ?assertEqual([], D#daemon.errors),
+ ?assertEqual([], D#daemon.buf).
diff --git a/test/etap/170-os-daemons.es b/test/couchdb/fixtures/os_daemon_looper.escript
index 73974e905..73974e905 100755
--- a/test/etap/170-os-daemons.es
+++ b/test/couchdb/fixtures/os_daemon_looper.escript
diff --git a/test/etap/170-os-daemons.t b/test/etap/170-os-daemons.t
deleted file mode 100755
index 6feaa1bf4..000000000
--- a/test/etap/170-os-daemons.t
+++ /dev/null
@@ -1,114 +0,0 @@
-#!/usr/bin/env escript
-%% -*- erlang -*-
-
-% 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.
-
--record(daemon, {
- port,
- name,
- cmd,
- kill,
- status=running,
- cfg_patterns=[],
- errors=[],
- buf=[]
-}).
-
-config_files() ->
- lists:map(fun test_util:build_file/1, [
- "etc/couchdb/default_dev.ini"
- ]).
-
-daemon_cmd() ->
- test_util:source_file("test/etap/170-os-daemons.es").
-
-main(_) ->
- test_util:init_code_path(),
-
- etap:plan(49),
- case (catch test()) of
- ok ->
- etap:end_tests();
- Other ->
- etap:diag(io_lib:format("Test died abnormally: ~p", [Other])),
- etap:bail(Other)
- end,
- ok.
-
-test() ->
- couch_config:start_link(config_files()),
- couch_os_daemons:start_link(),
-
- etap:diag("Daemons boot after configuration added."),
- couch_config:set("os_daemons", "foo", daemon_cmd(), false),
- timer:sleep(1000),
-
- {ok, [D1]} = couch_os_daemons:info([table]),
- check_daemon(D1, "foo"),
-
- % Check table form
- {ok, Tab1} = couch_os_daemons:info(),
- [T1] = ets:tab2list(Tab1),
- check_daemon(T1, "foo"),
-
- etap:diag("Daemons stop after configuration removed."),
- couch_config:delete("os_daemons", "foo", false),
- timer:sleep(500),
-
- {ok, []} = couch_os_daemons:info([table]),
- {ok, Tab2} = couch_os_daemons:info(),
- etap:is(ets:tab2list(Tab2), [], "As table returns empty table."),
-
- etap:diag("Adding multiple daemons causes both to boot."),
- couch_config:set("os_daemons", "bar", daemon_cmd(), false),
- couch_config:set("os_daemons", "baz", daemon_cmd(), false),
- timer:sleep(500),
- {ok, Daemons} = couch_os_daemons:info([table]),
- lists:foreach(fun(D) ->
- check_daemon(D)
- end, Daemons),
-
- {ok, Tab3} = couch_os_daemons:info(),
- lists:foreach(fun(D) ->
- check_daemon(D)
- end, ets:tab2list(Tab3)),
-
- etap:diag("Removing one daemon leaves the other alive."),
- couch_config:delete("os_daemons", "bar", false),
- timer:sleep(500),
-
- {ok, [D2]} = couch_os_daemons:info([table]),
- check_daemon(D2, "baz"),
-
- % Check table version
- {ok, Tab4} = couch_os_daemons:info(),
- [T4] = ets:tab2list(Tab4),
- check_daemon(T4, "baz"),
-
- ok.
-
-check_daemon(D) ->
- check_daemon(D, D#daemon.name).
-
-check_daemon(D, Name) ->
- BaseName = "170-os-daemons.es",
- BaseLen = length(BaseName),
- CmdLen = length(D#daemon.cmd),
- CmdName = lists:sublist(D#daemon.cmd, CmdLen-BaseLen+1, BaseLen),
-
- etap:is(is_port(D#daemon.port), true, "Daemon port is a port."),
- etap:is(D#daemon.name, Name, "Daemon name was set correctly."),
- etap:is(CmdName, BaseName, "Command name was set correctly."),
- etap:isnt(D#daemon.kill, undefined, "Kill command was set."),
- etap:is(D#daemon.errors, [], "No errors occurred while booting."),
- etap:is(D#daemon.buf, [], "No extra data left in the buffer.").
diff --git a/test/etap/Makefile.am b/test/etap/Makefile.am
index a19cab90d..13b5b4a58 100644
--- a/test/etap/Makefile.am
+++ b/test/etap/Makefile.am
@@ -36,8 +36,6 @@ fixture_files = \
fixtures/test.couch
tap_files = \
- 170-os-daemons.es \
- 170-os-daemons.t \
171-os-daemons-config.es \
171-os-daemons-config.t \
172-os-daemon-errors.1.sh \