| 1 | # Licensed to Elasticsearch B.V under one or more agreements. | ||||||||||||||||||||||||||||
| 2 | # Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||||||||||||||||||||||||||||
| 3 | # See the LICENSE file in the project root for more information | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | package Search::Elasticsearch::TestServer; | ||||||||||||||||||||||||||||
| 6 | 2 | $Search::Elasticsearch::TestServer::VERSION = '7.30_1'; # TRIAL | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | $Search::Elasticsearch::TestServer::VERSION = '7.301'; | |||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Search::Elasticsearch(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use POSIX 'setsid'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use File::Temp(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use IO::Socket(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use HTTP::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use Search::Elasticsearch::Util qw(parse_params throw); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 2 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 2 | has 'es_home' => ( is => 'ro', default => $ENV{ES_HOME} ); | |||||||||||||||||||||||||||
| 20 | 2 | has 'es_version' => ( is => 'ro', default => $ENV{ES_VERSION} ); | |||||||||||||||||||||||||||
| 21 | 2 | has 'instances' => ( is => 'ro', default => 1 ); | |||||||||||||||||||||||||||
| 22 | 2 | has 'http_port' => ( is => 'ro', default => 9600 ); | |||||||||||||||||||||||||||
| 23 | 2 | has 'es_port' => ( is => 'ro', default => 9700 ); | |||||||||||||||||||||||||||
| 24 | has 'pids' => ( | ||||||||||||||||||||||||||||
| 25 | is => 'ro', | ||||||||||||||||||||||||||||
| 26 | 0 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 27 | 2 | clearer => 1, | |||||||||||||||||||||||||||
| 28 | predicate => 1 | ||||||||||||||||||||||||||||
| 29 | ); | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | has 'dirs' => ( is => 'ro', default => sub { [] } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 32 | 0 | ▶ | has 'conf' => ( is => 'ro', default => sub { [] } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 33 | 2 | has '_starter_pid' => ( is => 'rw', required => 0, predicate => 1 ); | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | #=================================== | ||||||||||||||||||||||||||||
| 36 | sub start { | ||||||||||||||||||||||||||||
| 37 | #=================================== | ||||||||||||||||||||||||||||
| 38 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
start: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | my $home = $self->es_home | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | or throw( 'Param', "Missing required param <es_home>" ); | ||||||||||||||||||||||||||||
| 42 | 0 | ▶ | $self->es_version | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | or throw( 'Param', "Missing required param <es_version>" ); | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | my $instances = $self->instances; | |||||||||||||||||||||||||||
| 46 | 0 | my $port = $self->http_port; | |||||||||||||||||||||||||||
| 47 | 0 | my $es_port = $self->es_port; | |||||||||||||||||||||||||||
| 48 | 0 | my @http = map { $port++ } ( 1 .. $instances ); | |||||||||||||||||||||||||||
| 49 | 0 | my @transport = map { $es_port++ } ( 1 .. $instances ); | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | $self->_check_ports( @http, @transport ); | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | my $old_SIGINT = $SIG{INT}; | |||||||||||||||||||||||||||
| 54 | $SIG{INT} = sub { | ||||||||||||||||||||||||||||
| 55 | 0 | ▶ | $self->shutdown; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | if ( ref $old_SIGINT eq 'CODE' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 0 | return $old_SIGINT->(); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | 0 | exit(1); | |||||||||||||||||||||||||||
| 60 | 0 | }; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | for ( 0 .. $instances - 1 ) { | |||||||||||||||||||||||||||
| 63 | 0 | my $dir = File::Temp->newdir(); | |||||||||||||||||||||||||||
| 64 | 0 | push @{ $self->dirs }, $dir; | |||||||||||||||||||||||||||
| 65 | 0 | print "Starting node: http://127.0.0.1:$http[$_]\n"; | |||||||||||||||||||||||||||
| 66 | 0 | $self->_start_node( $dir, $transport[$_], $http[$_] ); | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | $self->_check_nodes(@http); | |||||||||||||||||||||||||||
| 70 | 0 | return [ map {"http://127.0.0.1:$_"} @http ]; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | #=================================== | ||||||||||||||||||||||||||||
| 74 | sub _check_ports { | ||||||||||||||||||||||||||||
| 75 | #=================================== | ||||||||||||||||||||||||||||
| 76 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_check_ports: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 77 | 0 | for my $port (@_) { | |||||||||||||||||||||||||||
| 78 | 0 | ▶ | next unless IO::Socket::INET->new("127.0.0.1:$port"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | throw( 'Param', | |||||||||||||||||||||||||||
| 80 | "There is already a service running on 127.0.0.1:$port. " | ||||||||||||||||||||||||||||
| 81 | . "Please shut it down before starting the test server" ); | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | #=================================== | ||||||||||||||||||||||||||||
| 86 | sub _check_nodes { | ||||||||||||||||||||||||||||
| 87 | #=================================== | ||||||||||||||||||||||||||||
| 88 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_check_nodes: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 89 | 0 | my $http = HTTP::Tiny->new; | |||||||||||||||||||||||||||
| 90 | 0 | for my $node (@_) { | |||||||||||||||||||||||||||
| 91 | 0 | print "Checking node: http://127.0.0.1:$node\n"; | |||||||||||||||||||||||||||
| 92 | 0 | my $i = 20; | |||||||||||||||||||||||||||
| 93 | 0 | while (1) { | |||||||||||||||||||||||||||
| 94 | last | ||||||||||||||||||||||||||||
| 95 | 0 | ▶ | if $http->head("http://127.0.0.1:$node/")->{status} == 200; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 0 | ▶ | throw( 'Cxn', "Couldn't connect to http://127.0.0.1:$node" ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | unless $i--; | ||||||||||||||||||||||||||||
| 98 | 0 | sleep 1; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | #=================================== | ||||||||||||||||||||||||||||
| 105 | sub _start_node { | ||||||||||||||||||||||||||||
| 106 | #=================================== | ||||||||||||||||||||||||||||
| 107 | 0 | ▶ | my ( $self, $dir, $transport, $http ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_start_node: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | my $pid_file = File::Temp->new; | |||||||||||||||||||||||||||
| 110 | 0 | my @config = $self->_command_line( $pid_file, $dir, $transport, $http ); | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | my $int_caught = 0; | |||||||||||||||||||||||||||
| 113 | { | ||||||||||||||||||||||||||||
| 114 | 0 | ▶ | local $SIG{INT} = sub { $int_caught++; }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 115 | 0 | ▶ | defined( my $pid = fork ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | or throw( 'Internal', "Couldn't fork a new process: $!" ); | ||||||||||||||||||||||||||||
| 117 | 0 | ▶ | if ( $pid == 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 0 | ▶ | throw( 'Internal', "Can't start a new session: $!" ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | if setsid == -1; | ||||||||||||||||||||||||||||
| 120 | 0 | ▶ | exec(@config) or die "Couldn't execute @config: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | else { | ||||||||||||||||||||||||||||
| 123 | 0 | for ( 1 .. 5 ) { | |||||||||||||||||||||||||||
| 124 | 0 | ▶ | last if -s $pid_file->filename(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 0 | sleep 1; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | 0 | open my $pid_fh, '<', $pid_file->filename; | |||||||||||||||||||||||||||
| 128 | 0 | my $pid = <$pid_fh>; | |||||||||||||||||||||||||||
| 129 | 0 | ▶ | throw( 'Internal', "No PID file found for Elasticsearch" ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | unless $pid; | ||||||||||||||||||||||||||||
| 131 | 0 | chomp $pid; | |||||||||||||||||||||||||||
| 132 | 0 | push @{ $self->{pids} }, $pid; | |||||||||||||||||||||||||||
| 133 | 0 | $self->_starter_pid($$); | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | 0 | ▶ | $SIG{INT}->('INT') if $int_caught; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | #=================================== | ||||||||||||||||||||||||||||
| 140 | sub guarded_shutdown { | ||||||||||||||||||||||||||||
| 141 | #=================================== | ||||||||||||||||||||||||||||
| 142 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
guarded_shutdown: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 143 | 0 | ▶ | if ( $self->_has_starter_pid && $$ == $self->_starter_pid ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->_has_starter_pid and $$ == $self->_starter_pid
MC/DC0%0/2
$self->_has_starter_pid and $$ == $self->_starter_pid
$self->_has_starter_pid
$$ == $self->_starter_pid
Truth table0%0/3
A and B
A= $self->_has_starter_pidB= $$ == $self->_starter_pid
| |||||||||||||||||||||||||||||
| 144 | 0 | $self->shutdown(); | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | #=================================== | ||||||||||||||||||||||||||||
| 149 | sub shutdown { | ||||||||||||||||||||||||||||
| 150 | #=================================== | ||||||||||||||||||||||||||||
| 151 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
shutdown: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 152 | 0 | local $?; | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 0 | ▶ | return unless $self->has_pids; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 0 | my $pids = $self->pids; | |||||||||||||||||||||||||||
| 157 | 0 | $self->clear_pids; | |||||||||||||||||||||||||||
| 158 | 0 | ▶ | return unless @$pids; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 0 | kill 9, @$pids; | |||||||||||||||||||||||||||
| 161 | 0 | $self->clear_dirs; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | #=================================== | ||||||||||||||||||||||||||||
| 165 | sub _command_line { | ||||||||||||||||||||||||||||
| 166 | #=================================== | ||||||||||||||||||||||||||||
| 167 | 0 | ▶ | my ( $self, $pid_file, $dir, $transport, $http ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_command_line: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 0 | my $version = $self->es_version; | |||||||||||||||||||||||||||
| 170 | 0 | my $class = "Search::Elasticsearch::Client::${version}::TestServer"; | |||||||||||||||||||||||||||
| 171 | 0 | ▶ | eval "require $class" || die $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 0 | return $class->command_line(@_); | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | #=================================== | ||||||||||||||||||||||||||||
| 177 | sub clear_dirs { | ||||||||||||||||||||||||||||
| 178 | #=================================== | ||||||||||||||||||||||||||||
| 179 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
clear_dirs: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 180 | 0 | @{ $self->dirs() } = (); | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | #=================================== | ||||||||||||||||||||||||||||
| 184 | 0 | ▶ | sub DEMOLISH { shift->guarded_shutdown } | ||||||||||||||||||||||||||
|
Subroutine
DEMOLISH: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 185 | #=================================== | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 2 | 1; | |||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | # ABSTRACT: A helper class to launch Elasticsearch nodes | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | __END__ | ||||||||||||||||||||||||||||