1: %% Copyright © 2018 Erlang Solutions Ltd
    2: %%
    3: %% Licensed under the Apache License, Version 2.0 (the "License");
    4: %% you may not use this file except in compliance with the License.
    5: %% You may obtain a copy of the License at
    6: %%
    7: %% http://www.apache.org/licenses/LICENSE-2.0
    8: %%
    9: %% Unless required by applicable law or agreed to in writing, software
   10: %% distributed under the License is distributed on an "AS IS" BASIS,
   11: %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   12: %% See the License for the specific language governing permissions and
   13: %% limitations under the License.
   14: 
   15: -module(mongoose_elasticsearch_SUITE).
   16: 
   17: -compile([export_all, nowarn_export_all]).
   18: 
   19: -include_lib("common_test/include/ct.hrl").
   20: -include_lib("eunit/include/eunit.hrl").
   21: 
   22: -import(distributed_helper, [mim/0, rpc/4]).
   23: 
   24: %%--------------------------------------------------------------------
   25: %% Suite configuration
   26: %%--------------------------------------------------------------------
   27: 
   28: suite() ->
   29:     [{require, ejabberd_node},
   30:      {require, ejabberd_cookie} |
   31:      distributed_helper:require_rpc_nodes([mim])].
   32: 
   33: all() ->
   34:     [{group, all}].
   35: 
   36: groups() ->
   37:     [{all, [], all_test_cases()}].
   38: 
   39: all_test_cases() ->
   40:     [start_and_stop_sequence].
   41: 
   42: %%--------------------------------------------------------------------
   43: %% Init & teardown
   44: %%--------------------------------------------------------------------
   45: 
   46: init_per_suite(Config) ->
   47:     case is_elasticsearch_enabled() of
   48:         true ->
   49:             ok = rpc(mim(), mongoose_wpool, stop, [elastic, global, default]),
   50:             Config;
   51:         false ->
   52:             {skip, elasticsearch_unavailable}
   53:     end.
   54: 
   55: end_per_suite(_Config) ->
   56:     case is_elasticsearch_enabled() of
   57:         true ->
   58:             ok;
   59:         false ->
   60:             ok = rpc(mim(), mongoose_elasticsearch, start, [])
   61:     end.
   62: 
   63: 
   64: %%--------------------------------------------------------------------
   65: %% Test cases
   66: %%--------------------------------------------------------------------
   67: 
   68: start_and_stop_sequence(_Config) ->
   69:     rpc(mim(), mongoose_wpool, start, [elastic, global, default, [], []]),
   70:     ?assertMatch({ok, _}, rpc(mim(), mongoose_elasticsearch, health, [])),
   71: 
   72:     rpc(mim(), mongoose_wpool, stop, [elastic, global, default]),
   73:     ?assertMatch({error, _}, rpc(mim(), mongoose_elasticsearch, health, [])),
   74: 
   75:     rpc(mim(), mongoose_wpool, start, [elastic, global, default, [], []]),
   76:     ?assertMatch({ok, _}, rpc(mim(), mongoose_elasticsearch, health, [])).
   77: 
   78: %%--------------------------------------------------------------------
   79: %% Helpers
   80: %%--------------------------------------------------------------------
   81: 
   82: -spec is_elasticsearch_enabled() -> boolean().
   83: is_elasticsearch_enabled() ->
   84:     rpc(mim(), mongoose_wpool, is_configured, [elastic]).
   85: