Port 081-config-override.t etap test suite to eunit Merged into couch_config_tests suite. Setup fixtures.
Project: http://git-wip-us.apache.org/repos/asf/couchdb/repo Commit: http://git-wip-us.apache.org/repos/asf/couchdb/commit/b817a9d7 Tree: http://git-wip-us.apache.org/repos/asf/couchdb/tree/b817a9d7 Diff: http://git-wip-us.apache.org/repos/asf/couchdb/diff/b817a9d7 Branch: refs/heads/1.x.x Commit: b817a9d7fa7e684e27c6a6d76abbc022b552adda Parents: 5c5bfce Author: Alexander Shorin <[email protected]> Authored: Sun May 25 22:02:19 2014 +0400 Committer: Alexander Shorin <[email protected]> Committed: Wed Dec 2 03:49:04 2015 +0300 ---------------------------------------------------------------------- test/couchdb/Makefile.am | 7 +- test/couchdb/couch_config_tests.erl | 130 +++++++++++- test/couchdb/fixtures/couch_config_tests_1.ini | 22 ++ test/couchdb/fixtures/couch_config_tests_2.ini | 22 ++ test/couchdb/include/couch_eunit.hrl.in | 2 + test/etap/081-config-override.1.ini | 22 -- test/etap/081-config-override.2.ini | 22 -- test/etap/081-config-override.t | 212 -------------------- test/etap/Makefile.am | 3 - 9 files changed, 176 insertions(+), 266 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/couchdb/blob/b817a9d7/test/couchdb/Makefile.am ---------------------------------------------------------------------- diff --git a/test/couchdb/Makefile.am b/test/couchdb/Makefile.am index 5070cc3..ea4e32c 100644 --- a/test/couchdb/Makefile.am +++ b/test/couchdb/Makefile.am @@ -40,10 +40,15 @@ eunit_files = \ test_request.erl \ include/couch_eunit.hrl +fixture_files = \ + fixtures/couch_config_tests_1.ini \ + fixtures/couch_config_tests_2.ini + EXTRA_DIST = \ run.in \ eunit.ini \ - $(eunit_files) + $(eunit_files) \ + $(fixture_files) clean-local: rm -rf ebin/ http://git-wip-us.apache.org/repos/asf/couchdb/blob/b817a9d7/test/couchdb/couch_config_tests.erl ---------------------------------------------------------------------- diff --git a/test/couchdb/couch_config_tests.erl b/test/couchdb/couch_config_tests.erl index ecff590..4bdc333 100644 --- a/test/couchdb/couch_config_tests.erl +++ b/test/couchdb/couch_config_tests.erl @@ -17,9 +17,30 @@ -define(TIMEOUT, 1000). +-define(CONFIG_DEFAULT, + filename:join([?BUILDDIR, "etc", "couchdb", "default_dev.ini"])). +-define(CONFIG_FIXTURE_1, + filename:join([?FIXTURESDIR, "couch_config_tests_1.ini"])). +-define(CONFIG_FIXTURE_2, + filename:join([?FIXTURESDIR, "couch_config_tests_2.ini"])). +-define(CONFIG_FIXTURE_TEMP, + begin + FileName = filename:join([?TEMPDIR, "couch_config_temp.ini"]), + {ok, Fd} = file:open(FileName, write), + ok = file:truncate(Fd), + ok = file:close(Fd), + FileName + end). + setup() -> - {ok, Pid} = couch_config:start_link(?CONFIG_CHAIN), + setup(?CONFIG_CHAIN). +setup({temporary, Chain}) -> + setup(Chain); +setup({persistent, Chain}) -> + setup(lists:append(Chain, [?CONFIG_FIXTURE_TEMP])); +setup(Chain) -> + {ok, Pid} = couch_config:start_link(Chain), Pid. teardown(Pid) -> @@ -31,6 +52,8 @@ teardown(Pid) -> after ?TIMEOUT -> throw({timeout_error, config_stop}) end. +teardown(_, Pid) -> + teardown(Pid). couch_config_test_() -> @@ -39,7 +62,9 @@ couch_config_test_() -> [ couch_config_get_tests(), couch_config_set_tests(), - couch_config_del_tests() + couch_config_del_tests(), + config_override_tests(), + config_persistent_changes_tests() ] }. @@ -90,6 +115,43 @@ couch_config_del_tests() -> } }. +config_override_tests() -> + { + "Configs overide tests", + { + foreachx, + fun setup/1, fun teardown/2, + [ + {{temporary, [?CONFIG_DEFAULT]}, + fun should_ensure_in_defaults/2}, + {{temporary, [?CONFIG_DEFAULT, ?CONFIG_FIXTURE_1]}, + fun should_override_options/2}, + {{temporary, [?CONFIG_DEFAULT, ?CONFIG_FIXTURE_2]}, + fun should_create_new_sections_on_override/2}, + {{temporary, [?CONFIG_DEFAULT, ?CONFIG_FIXTURE_1, + ?CONFIG_FIXTURE_2]}, + fun should_win_last_in_chain/2} + ] + } + }. + +config_persistent_changes_tests() -> + { + "Config persistent changes", + { + foreachx, + fun setup/1, fun teardown/2, + [ + {{persistent, [?CONFIG_DEFAULT]}, + fun should_write_changes/2}, + {{temporary, [?CONFIG_DEFAULT]}, + fun should_ensure_that_default_wasnt_modified/2}, + {{temporary, [?CONFIG_FIXTURE_TEMP]}, + fun should_ensure_that_written_to_last_config_in_chain/2} + ] + } + }. + should_load_all_configs() -> ?_assert(length(couch_config:all()) > 0). @@ -151,10 +213,7 @@ should_return_undefined_atom_after_option_deletion() -> end). should_be_ok_on_deleting_unknown_options() -> - ?_assertEqual(ok, - begin - couch_config:delete("zoo", "boo", false) - end). + ?_assertEqual(ok, couch_config:delete("zoo", "boo", false)). should_delete_binary_option() -> ?_assertEqual(undefined, @@ -163,3 +222,62 @@ should_delete_binary_option() -> ok = couch_config:delete(<<"foo">>, <<"bar">>, false), couch_config:get(<<"foo">>, <<"bar">>) end). + +should_ensure_in_defaults(_, _) -> + ?_test(begin + ?assertEqual("100", + couch_config:get("couchdb", "max_dbs_open")), + ?assertEqual("5984", + couch_config:get("httpd", "port")), + ?assertEqual(undefined, + couch_config:get("fizbang", "unicode")) + end). + +should_override_options(_, _) -> + ?_test(begin + ?assertEqual("10", + couch_config:get("couchdb", "max_dbs_open")), + ?assertEqual("4895", + couch_config:get("httpd", "port")) + end). + +should_create_new_sections_on_override(_, _) -> + ?_test(begin + ?assertEqual("80", + couch_config:get("httpd", "port")), + ?assertEqual("normalized", + couch_config:get("fizbang", "unicode")) + end). + +should_win_last_in_chain(_, _) -> + ?_assertEqual("80", couch_config:get("httpd", "port")). + +should_write_changes(_, _) -> + ?_test(begin + ?assertEqual("5984", + couch_config:get("httpd", "port")), + ?assertEqual(ok, + couch_config:set("httpd", "port", "8080")), + ?assertEqual("8080", + couch_config:get("httpd", "port")), + ?assertEqual(ok, + couch_config:delete("httpd", "bind_address", "8080")), + ?assertEqual(undefined, + couch_config:get("httpd", "bind_address")) + end). + +should_ensure_that_default_wasnt_modified(_, _) -> + ?_test(begin + ?assertEqual("5984", + couch_config:get("httpd", "port")), + ?assertEqual("127.0.0.1", + couch_config:get("httpd", "bind_address")) + end). + +should_ensure_that_written_to_last_config_in_chain(_, _) -> + ?_test(begin + ?assertEqual("8080", + couch_config:get("httpd", "port")), + ?assertEqual(undefined, + couch_config:get("httpd", "bind_address")) + end). http://git-wip-us.apache.org/repos/asf/couchdb/blob/b817a9d7/test/couchdb/fixtures/couch_config_tests_1.ini ---------------------------------------------------------------------- diff --git a/test/couchdb/fixtures/couch_config_tests_1.ini b/test/couchdb/fixtures/couch_config_tests_1.ini new file mode 100644 index 0000000..55451da --- /dev/null +++ b/test/couchdb/fixtures/couch_config_tests_1.ini @@ -0,0 +1,22 @@ +; Licensed to the Apache Software Foundation (ASF) under one +; or more contributor license agreements. See the NOTICE file +; distributed with this work for additional information +; regarding copyright ownership. The ASF licenses this file +; to you 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. + +[couchdb] +max_dbs_open=10 + +[httpd] +port=4895 http://git-wip-us.apache.org/repos/asf/couchdb/blob/b817a9d7/test/couchdb/fixtures/couch_config_tests_2.ini ---------------------------------------------------------------------- diff --git a/test/couchdb/fixtures/couch_config_tests_2.ini b/test/couchdb/fixtures/couch_config_tests_2.ini new file mode 100644 index 0000000..5f46357 --- /dev/null +++ b/test/couchdb/fixtures/couch_config_tests_2.ini @@ -0,0 +1,22 @@ +; Licensed to the Apache Software Foundation (ASF) under one +; or more contributor license agreements. See the NOTICE file +; distributed with this work for additional information +; regarding copyright ownership. The ASF licenses this file +; to you 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. + +[httpd] +port = 80 + +[fizbang] +unicode = normalized http://git-wip-us.apache.org/repos/asf/couchdb/blob/b817a9d7/test/couchdb/include/couch_eunit.hrl.in ---------------------------------------------------------------------- diff --git a/test/couchdb/include/couch_eunit.hrl.in b/test/couchdb/include/couch_eunit.hrl.in index 6f5cf61..6a46d2f 100644 --- a/test/couchdb/include/couch_eunit.hrl.in +++ b/test/couchdb/include/couch_eunit.hrl.in @@ -18,6 +18,8 @@ filename:join([?BUILDDIR, "etc", "couchdb", "default_dev.ini"]), filename:join([?BUILDDIR, "etc", "couchdb", "local_dev.ini"]), filename:join([?SOURCEDIR, "test", "couchdb", "eunit.ini"])]). +-define(FIXTURESDIR, + filename:join([?SOURCEDIR, "test", "couchdb", "fixtures"])). -define(TEMPDIR, filename:join([?BUILDDIR, "test", "couchdb", "temp"])). http://git-wip-us.apache.org/repos/asf/couchdb/blob/b817a9d7/test/etap/081-config-override.1.ini ---------------------------------------------------------------------- diff --git a/test/etap/081-config-override.1.ini b/test/etap/081-config-override.1.ini deleted file mode 100644 index 55451da..0000000 --- a/test/etap/081-config-override.1.ini +++ /dev/null @@ -1,22 +0,0 @@ -; Licensed to the Apache Software Foundation (ASF) under one -; or more contributor license agreements. See the NOTICE file -; distributed with this work for additional information -; regarding copyright ownership. The ASF licenses this file -; to you 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. - -[couchdb] -max_dbs_open=10 - -[httpd] -port=4895 http://git-wip-us.apache.org/repos/asf/couchdb/blob/b817a9d7/test/etap/081-config-override.2.ini ---------------------------------------------------------------------- diff --git a/test/etap/081-config-override.2.ini b/test/etap/081-config-override.2.ini deleted file mode 100644 index 5f46357..0000000 --- a/test/etap/081-config-override.2.ini +++ /dev/null @@ -1,22 +0,0 @@ -; Licensed to the Apache Software Foundation (ASF) under one -; or more contributor license agreements. See the NOTICE file -; distributed with this work for additional information -; regarding copyright ownership. The ASF licenses this file -; to you 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. - -[httpd] -port = 80 - -[fizbang] -unicode = normalized http://git-wip-us.apache.org/repos/asf/couchdb/blob/b817a9d7/test/etap/081-config-override.t ---------------------------------------------------------------------- diff --git a/test/etap/081-config-override.t b/test/etap/081-config-override.t deleted file mode 100755 index 01f8b4c..0000000 --- a/test/etap/081-config-override.t +++ /dev/null @@ -1,212 +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. - -default_config() -> - test_util:build_file("etc/couchdb/default_dev.ini"). - -local_config_1() -> - test_util:source_file("test/etap/081-config-override.1.ini"). - -local_config_2() -> - test_util:source_file("test/etap/081-config-override.2.ini"). - -local_config_write() -> - test_util:build_file("test/etap/temp.081"). - -% Run tests and wait for the config gen_server to shutdown. -run_tests(IniFiles, Tests) -> - {ok, Pid} = couch_config:start_link(IniFiles), - erlang:monitor(process, Pid), - Tests(), - couch_config:stop(), - receive - {'DOWN', _, _, Pid, _} -> ok; - _Other -> etap:diag("OTHER: ~p~n", [_Other]) - after - 1000 -> throw({timeout_error, config_stop}) - end. - -main(_) -> - test_util:init_code_path(), - etap:plan(17), - - 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() -> - - CheckStartStop = fun() -> ok end, - run_tests([default_config()], CheckStartStop), - - CheckDefaults = fun() -> - etap:is( - couch_config:get("couchdb", "max_dbs_open"), - "100", - "{couchdb, max_dbs_open} is 100 by defualt." - ), - - etap:is( - couch_config:get("httpd","port"), - "5984", - "{httpd, port} is 5984 by default" - ), - - etap:is( - couch_config:get("fizbang", "unicode"), - undefined, - "{fizbang, unicode} is undefined by default" - ) - end, - - run_tests([default_config()], CheckDefaults), - - - % Check that subsequent files override values appropriately - - CheckOverride = fun() -> - etap:is( - couch_config:get("couchdb", "max_dbs_open"), - "10", - "{couchdb, max_dbs_open} was overriden with the value 10" - ), - - etap:is( - couch_config:get("httpd", "port"), - "4895", - "{httpd, port} was overriden with the value 4895" - ) - end, - - run_tests([default_config(), local_config_1()], CheckOverride), - - - % Check that overrides can create new sections - - CheckOverride2 = fun() -> - etap:is( - couch_config:get("httpd", "port"), - "80", - "{httpd, port} is overriden with the value 80" - ), - - etap:is( - couch_config:get("fizbang", "unicode"), - "normalized", - "{fizbang, unicode} was created by override INI file" - ) - end, - - run_tests([default_config(), local_config_2()], CheckOverride2), - - - % Check that values can be overriden multiple times - - CheckOverride3 = fun() -> - etap:is( - couch_config:get("httpd", "port"), - "80", - "{httpd, port} value was taken from the last specified INI file." - ) - end, - - run_tests( - [default_config(), local_config_1(), local_config_2()], - CheckOverride3 - ), - - % Check persistence to last file. - - % Empty the file in case it exists. - {ok, Fd} = file:open(local_config_write(), write), - ok = file:truncate(Fd), - ok = file:close(Fd), - - % Open and write a value - CheckCanWrite = fun() -> - etap:is( - couch_config:get("httpd", "port"), - "5984", - "{httpd, port} is still 5984 by default" - ), - - etap:is( - couch_config:set("httpd", "port", "8080"), - ok, - "Writing {httpd, port} is kosher." - ), - - etap:is( - couch_config:get("httpd", "port"), - "8080", - "{httpd, port} was updated to 8080 successfully." - ), - - etap:is( - couch_config:delete("httpd", "bind_address"), - ok, - "Deleting {httpd, bind_address} succeeds" - ), - - etap:is( - couch_config:get("httpd", "bind_address"), - undefined, - "{httpd, bind_address} was actually deleted." - ) - end, - - run_tests([default_config(), local_config_write()], CheckCanWrite), - - % Open and check where we don't expect persistence. - - CheckDidntWrite = fun() -> - etap:is( - couch_config:get("httpd", "port"), - "5984", - "{httpd, port} was not persisted to the primary INI file." - ), - - etap:is( - couch_config:get("httpd", "bind_address"), - "127.0.0.1", - "{httpd, bind_address} was not deleted form the primary INI file." - ) - end, - - run_tests([default_config()], CheckDidntWrite), - - % Open and check we have only the persistence we expect. - CheckDidWrite = fun() -> - etap:is( - couch_config:get("httpd", "port"), - "8080", - "{httpd, port} is still 8080 after reopening the config." - ), - - etap:is( - couch_config:get("httpd", "bind_address"), - undefined, - "{httpd, bind_address} is still \"\" after reopening." - ) - end, - - run_tests([local_config_write()], CheckDidWrite), - - ok. http://git-wip-us.apache.org/repos/asf/couchdb/blob/b817a9d7/test/etap/Makefile.am ---------------------------------------------------------------------- diff --git a/test/etap/Makefile.am b/test/etap/Makefile.am index 436a27b..07583ac 100644 --- a/test/etap/Makefile.am +++ b/test/etap/Makefile.am @@ -36,9 +36,6 @@ fixture_files = \ fixtures/test.couch tap_files = \ - 081-config-override.1.ini \ - 081-config-override.2.ini \ - 081-config-override.t \ 082-config-register.t \ 083-config-no-files.t \ 090-task-status.t \
