1: %%============================================================================== 2: %% Copyright 2018 Erlang Solutions Ltd. 3: %% 4: %% Licensed under the Apache License, Version 2.0 (the "License"); 5: %% you may not use this file except in compliance with the License. 6: %% You may obtain a copy of the License at 7: %% 8: %% http://www.apache.org/licenses/LICENSE-2.0 9: %% 10: %% Unless required by applicable law or agreed to in writing, software 11: %% distributed under the License is distributed on an "AS IS" BASIS, 12: %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13: %% See the License for the specific language governing permissions and 14: %% limitations under the License. 15: %%============================================================================== 16: 17: -module(gen_mod_SUITE). 18: -compile([export_all, nowarn_export_all]). 19: -author('bartlomiej.gorny@erlang-solutions.com'). 20: 21: -include_lib("eunit/include/eunit.hrl"). 22: 23: %%-------------------------------------------------------------------- 24: %% Suite configuration 25: %%-------------------------------------------------------------------- 26: 27: all() -> 28: [start_and_stop, 29: start_error, 30: stop_error, 31: loaded_modules, 32: loaded_modules_with_opts, 33: hosts_with_module, 34: hosts_and_opts_with_module]. 35: 36: init_per_testcase(_, Config) -> 37: [mongoose_config:set_opt(Opt, Val) || {Opt, Val} <- opts()], 38: [setup_meck(Module) || Module <- [a_module, b_module]], 39: Config. 40: 41: end_per_testcase(_, Config) -> 42: [mongoose_config:unset_opt(Opt) || Opt <- opts()], 43: [meck:unload(Module) || Module <- [a_module, b_module]], 44: Config. 45: 46: start_and_stop(_Config) -> 47: ?assertEqual({ok, ok}, gen_mod:start_module(host(a), a_module, [])), 48: ?assertError(#{what := module_not_loaded}, gen_mod:start_module(host(a), b_module, [{k, v}])), 49: ?assertError(#{what := module_not_loaded}, gen_mod:start_module(host(b), a_module, [])), 50: ?assertEqual({ok, ok}, gen_mod:start_module(host(b), b_module, [{k, v}])), 51: ?assertEqual(ok, gen_mod:stop_module(host(a), a_module)), 52: ?assertError(#{what := module_not_loaded}, gen_mod:stop_module(host(a), b_module)), 53: ?assertError(#{what := module_not_loaded}, gen_mod:stop_module(host(b), a_module)), 54: ?assertEqual(ok, gen_mod:stop_module(host(b), b_module)). 55: 56: start_error(_Config) -> 57: meck:expect(a_module, start, fun(_, _) -> error(bad_weather) end), 58: ?assertError(bad_weather, gen_mod:start_module(host(a), a_module, [])). 59: 60: stop_error(_Config) -> 61: meck:expect(a_module, stop, fun(_) -> error(bad_mood) end), 62: ?assertError(bad_mood, gen_mod:stop_module(host(a), a_module)). 63: 64: loaded_modules(_Config) -> 65: ?assertEqual([a_module], gen_mod:loaded_modules(host(a))), 66: ?assertEqual([b_module], gen_mod:loaded_modules(host(b))), 67: ?assertEqual([a_module, b_module], gen_mod:loaded_modules()). 68: 69: loaded_modules_with_opts(_Config) -> 70: MA = #{a_module => []}, 71: MB = #{b_module => [{k, v}]}, 72: ?assertEqual(MA, gen_mod:loaded_modules_with_opts(host(a))), 73: ?assertEqual(MB, gen_mod:loaded_modules_with_opts(host(b))), 74: ?assertEqual(#{host(a) => MA, host(b) => MB}, gen_mod:loaded_modules_with_opts()). 75: 76: hosts_with_module(_Config) -> 77: ?assertEqual([host(a)], gen_mod:hosts_with_module(a_module)), 78: ?assertEqual([host(b)], gen_mod:hosts_with_module(b_module)). 79: 80: hosts_and_opts_with_module(_Config) -> 81: ?assertEqual(#{host(a) => []}, gen_mod:hosts_and_opts_with_module(a_module)), 82: ?assertEqual(#{host(b) => [{k, v}]}, gen_mod:hosts_and_opts_with_module(b_module)). 83: 84: host(a) -> 85: <<"localhost">>; 86: host(b) -> 87: <<"localhost.bis">>. 88: 89: setup_meck(Module) -> 90: meck:new(Module, [non_strict]), 91: meck:expect(Module, start, fun(_, _) -> ok end), 92: meck:expect(Module, stop, fun(_) -> ok end). 93: 94: opts() -> 95: [{hosts, [host(a), host(b)]}, 96: {host_types, []}, 97: {{modules, host(a)}, #{a_module => []}}, 98: {{modules, host(b)}, #{b_module => [{k, v}]}}].