Elasticsearch/TestServer.pm

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