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 get_elastic_pools() of
   48:         [] ->
   49:             {skip, elasticsearch_unavailable};
   50:         [_] = Pools ->
   51:             ok = rpc(mim(), mongoose_wpool, stop, [elastic, global, default]),
   52:             [{elastic_pools, Pools} | Config]
   53:     end.
   54: 
   55: end_per_suite(Config) ->
   56:     Pools = ?config(elastic_pools, Config),
   57:     rpc(mim(), mongoose_wpool, start_configured_pools, [Pools]).
   58: 
   59: %%--------------------------------------------------------------------
   60: %% Test cases
   61: %%--------------------------------------------------------------------
   62: 
   63: start_and_stop_sequence(Config) ->
   64:     ElasticPools = ?config(elastic_pools, Config),
   65:     rpc(mim(), mongoose_wpool, start_configured_pools, [ElasticPools]),
   66:     ?assertMatch({ok, _}, rpc(mim(), mongoose_elasticsearch, health, [])),
   67: 
   68:     rpc(mim(), mongoose_wpool, stop, [elastic, global, default]),
   69:     ?assertMatch({error, _}, rpc(mim(), mongoose_elasticsearch, health, [])),
   70: 
   71:     rpc(mim(), mongoose_wpool, start_configured_pools, [ElasticPools]),
   72:     ?assertMatch({ok, _}, rpc(mim(), mongoose_elasticsearch, health, [])).
   73: 
   74: %%--------------------------------------------------------------------
   75: %% Helpers
   76: %%--------------------------------------------------------------------
   77: 
   78: -spec get_elastic_pools() -> list().
   79: get_elastic_pools() ->
   80:     Pools = rpc(mim(), mongoose_config, get_opt, [outgoing_pools]),
   81:     [Pool || Pool = #{type := elastic, scope := global, tag := default} <- Pools].