You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ch...@apache.org on 2014/07/22 01:57:43 UTC

[30/43] couchdb commit: updated refs/heads/1963-eunit-bigcouch to 424dca5

Port 170-os-daemons.t etap test suite to eunit


Project: http://git-wip-us.apache.org/repos/asf/couchdb/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb/commit/9d135efc
Tree: http://git-wip-us.apache.org/repos/asf/couchdb/tree/9d135efc
Diff: http://git-wip-us.apache.org/repos/asf/couchdb/diff/9d135efc

Branch: refs/heads/1963-eunit-bigcouch
Commit: 9d135efccc45176cc03cecf2c98d40461408655d
Parents: da4e82b
Author: Alexander Shorin <kx...@apache.org>
Authored: Tue Jun 3 12:32:02 2014 +0400
Committer: Russell Branca <ch...@apache.org>
Committed: Mon Jul 21 16:45:08 2014 -0700

----------------------------------------------------------------------
 test/couchdb/couchdb_os_daemons_tests.erl      | 145 ++++++++++++++++++++
 test/couchdb/fixtures/os_daemon_looper.escript |  26 ++++
 test/etap/170-os-daemons.es                    |  26 ----
 test/etap/170-os-daemons.t                     | 114 ---------------
 4 files changed, 171 insertions(+), 140 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb/blob/9d135efc/test/couchdb/couchdb_os_daemons_tests.erl
----------------------------------------------------------------------
diff --git a/test/couchdb/couchdb_os_daemons_tests.erl b/test/couchdb/couchdb_os_daemons_tests.erl
new file mode 100644
index 0000000..1f9f6be
--- /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).

http://git-wip-us.apache.org/repos/asf/couchdb/blob/9d135efc/test/couchdb/fixtures/os_daemon_looper.escript
----------------------------------------------------------------------
diff --git a/test/couchdb/fixtures/os_daemon_looper.escript b/test/couchdb/fixtures/os_daemon_looper.escript
new file mode 100755
index 0000000..73974e9
--- /dev/null
+++ b/test/couchdb/fixtures/os_daemon_looper.escript
@@ -0,0 +1,26 @@
+#! /usr/bin/env escript
+
+% 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.
+
+loop() ->
+    loop(io:read("")).
+
+loop({ok, _}) ->
+    loop(io:read(""));
+loop(eof) ->
+    stop;
+loop({error, Reason}) ->
+    throw({error, Reason}).
+
+main([]) ->
+    loop().

http://git-wip-us.apache.org/repos/asf/couchdb/blob/9d135efc/test/etap/170-os-daemons.es
----------------------------------------------------------------------
diff --git a/test/etap/170-os-daemons.es b/test/etap/170-os-daemons.es
deleted file mode 100755
index 73974e9..0000000
--- a/test/etap/170-os-daemons.es
+++ /dev/null
@@ -1,26 +0,0 @@
-#! /usr/bin/env escript
-
-% 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.
-
-loop() ->
-    loop(io:read("")).
-
-loop({ok, _}) ->
-    loop(io:read(""));
-loop(eof) ->
-    stop;
-loop({error, Reason}) ->
-    throw({error, Reason}).
-
-main([]) ->
-    loop().

http://git-wip-us.apache.org/repos/asf/couchdb/blob/9d135efc/test/etap/170-os-daemons.t
----------------------------------------------------------------------
diff --git a/test/etap/170-os-daemons.t b/test/etap/170-os-daemons.t
deleted file mode 100755
index f1961a9..0000000
--- 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() ->
-    application:start(config),
-    couch_os_daemons:start_link(),
-
-    etap:diag("Daemons boot after configuration added."),
-    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."),
-    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."),
-    config:set("os_daemons", "bar", daemon_cmd(), false),
-    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."),
-    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.").