Bug 19542: Add a check for ES configuration health
[koha.git] / t / Koha / SearchEngine / Elasticsearch.t
blobf7a7f27865051e392b9a4759dc20f1bea21064ac
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
18 use Modern::Perl;
20 use Test::More tests => 1;
21 use Test::Exception;
23 use t::lib::Mocks;
25 use Koha::SearchEngine::Elasticsearch;
27 subtest '_read_configuration() tests' => sub {
29 plan tests => 10;
31 my $configuration;
32 t::lib::Mocks::mock_config( 'elasticsearch', undef );
34 # 'elasticsearch' missing in configuration
35 throws_ok {
36 $configuration = Koha::SearchEngine::Elasticsearch::_read_configuration;
38 'Koha::Exceptions::Config::MissingEntry',
39 'Configuration problem, exception thrown';
40 is(
41 $@->message,
42 "Missing 'elasticsearch' block in config file",
43 'Exception message is correct'
46 # 'elasticsearch' present but no 'server' entry
47 t::lib::Mocks::mock_config( 'elasticsearch', {} );
48 throws_ok {
49 $configuration = Koha::SearchEngine::Elasticsearch::_read_configuration;
51 'Koha::Exceptions::Config::MissingEntry',
52 'Configuration problem, exception thrown';
53 is(
54 $@->message,
55 "Missing 'server' entry in config file for elasticsearch",
56 'Exception message is correct'
59 # 'elasticsearch' and 'server' entries present, but no 'index_name'
60 t::lib::Mocks::mock_config( 'elasticsearch', { server => 'a_server' } );
61 throws_ok {
62 $configuration = Koha::SearchEngine::Elasticsearch::_read_configuration;
64 'Koha::Exceptions::Config::MissingEntry',
65 'Configuration problem, exception thrown';
66 is(
67 $@->message,
68 "Missing 'index_name' entry in config file for elasticsearch",
69 'Exception message is correct'
72 # Correct configuration, only one server
73 t::lib::Mocks::mock_config( 'elasticsearch', { server => 'a_server', index_name => 'index' } );
75 $configuration = Koha::SearchEngine::Elasticsearch::_read_configuration;
76 is( $configuration->{index_name}, 'index', 'Index configuration parsed correctly' );
77 is_deeply( $configuration->{nodes}, ['a_server'], 'Server configuration parsed correctly' );
79 # Correct configuration, two servers
80 my @servers = ('a_server', 'another_server');
81 t::lib::Mocks::mock_config( 'elasticsearch', { server => \@servers, index_name => 'index' } );
83 $configuration = Koha::SearchEngine::Elasticsearch::_read_configuration;
84 is( $configuration->{index_name}, 'index', 'Index configuration parsed correctly' );
85 is_deeply( $configuration->{nodes}, \@servers , 'Server configuration parsed correctly' );