blib/lib/Gerrit/Client/Test.pm

filter: Statementstmt 27.9% 97 / 347 Branchbran 10.4% 10 / 96 Conditioncond 6.9% 3 / 43 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 40.0% 24 / 60 Podpod 100% 19 / 19 totaltotal 25.8% 153 / 591 CC 67 SCAR 39.3
CC
67
Cov
23%
ensure_gerrit_installed
306.0
_setup_peer_key
42.0
git_test_commit
42.0
CRAP 51.0
↑ Summary blib/lib/Gerrit/Client.pm »
1 #############################################################################
2 ##
3 ## Copyright (C) 2012-2014 Rohan McGovern <rohan@mcgovern.id.au>
4 ##
5 ## This library is free software; you can redistribute it and/or
6 ## modify it under the terms of the GNU Lesser General Public
7 ## License as published by the Free Software Foundation; either
8 ## version 2.1 of the License, or (at your option) any later version.
9 ##
10 ## This library is distributed in the hope that it will be useful,
11 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## Lesser General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU Lesser General Public
16 ## License along with this library; if not, write to the Free Software
17 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 ##
19 ##
20 #############################################################################
21  
22 =head1 NAME
23  
24 Gerrit::Client::Test - test helpers for Gerrit::Client
25  
26 =head1 DESCRIPTION
27  
28 This package provides various utilities written for testing of the
29 Gerrit::Client package. It is not intended for general use and the interface is
30 subject to change.
31  
32 Gerrit::Client::Test may be used to install and manage a local Gerrit instance
33 for the purpose of running system tests.
34  
35 =cut
36  
37 package Gerrit::Client::Test;
38  
39 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 12 ▶use AnyEvent::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 12 ▶use Archive::Zip qw(:ERROR_CODES);
Subroutine BEGIN: called CC=1 SCAR=0.0
44 12 ▶use Capture::Tiny qw(capture_merged capture);
Subroutine BEGIN: called CC=1 SCAR=0.0
45 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 12 ▶use English;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 12 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 12 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 12 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 12 ▶use File::chdir;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 12 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 12 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 12 ▶use List::MoreUtils qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
54 12 ▶use Params::Validate qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
55 12 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 12 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 12 my $HAVE_GIT;
59  
60 # like system(), but fail test and diag() the output if the command fails
61 0 ▶sub _system_or_fail {
Subroutine _system_or_fail: not called CC=2 SCAR=17.9
62 0 my (@cmd) = @_;
63 0 local $Test::Builder::Level = $Test::Builder::Level + 1;
64 0 my $status;
65 0 ▶ my ($out) = capture_merged { $status = system(@cmd) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
66 0 ▶ if ( $status != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status != 0) 0 0 0
67 0 diag($out);
68 }
69 0 return is( $status, 0 );
70 }
71  
72 # Returns a TCP port which, at the time of the call, can be bound on 127.0.0.1
73 0 ▶sub _find_available_tcp_port {
Subroutine _find_available_tcp_port: not called CC=1 SCAR=6.9
74 0 my $port;
75 0 ▶ my $guard = tcp_server '127.0.0.1', 0, sub { }, sub {
Subroutine __ANON__: not called CC=1 SCAR=0.0
Subroutine __ANON__: not called CC=1 SCAR=6.9
76 0 $port = $_[2];
77 0 };
78 0 return $port;
79 }
80  
81 # Fetches the given $url to a local File::Temp, which is returned.
82 0 ▶sub _fetch_remote {
Subroutine _fetch_remote: not called CC=2 SCAR=17.9
83 0 my ($url) = @_;
84 0 local $Test::Builder::Level = $Test::Builder::Level + 1;
85 0 my $file = File::Temp->new(
86 TEMPLATE => 'Gerrit-Client-war.XXXXXX',
87 TMPDIR => 1,
88 CLEANUP => 1
89 );
90  
91 0 my $ua = LWP::UserAgent->new();
92 0 diag("Downloading $url ...");
93 0 my $response = $ua->get( $url, ':content_file' => "$file" );
94 0 ▶ if ( !ok( $response->is_success(), "can fetch $url" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&ok(scalar $response->is_success, "can fetch $url")) 0 0 0
95 0 diag( $response->status_line() . "\n" . $response->decoded_content() );
96 0 return;
97 }
98  
99 0 return $file;
100 }
101  
102 # Get or set some gerrit config
103 0 ▶sub _gerrit_config {
Subroutine _gerrit_config: not called CC=3 SCAR=24.8
104 0 my ( $key, $value ) = @_;
105  
106 0 local $Test::Builder::Level = $Test::Builder::Level + 1;
107  
108 0 my @cmd = ( 'git', 'config', '-f', 'etc/gerrit.config', $key );
109 0 ▶ if ( defined($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
110 0 push @cmd, $value;
111 0 return _system_or_fail(@cmd);
112 }
113 0 my $status;
114 0 ▶ my ( $out, $err ) = capture {
Subroutine __ANON__: not called CC=1 SCAR=6.9
115 0 $status = system(@cmd);
116 0 };
117 0 ▶ if ( !is( $status, 0, "retrieve gerrit config $key OK" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&is($status, 0, "retrieve gerrit config $key OK")) 0 0 0
118 0 diag($err);
119 0 return;
120 }
121 0 chomp $out;
122 0 return $out;
123 }
124  
125 # Returns a string suitable for usage as an Authorization header
126 # for the given $username and $password, using HTTP Basic authentication
127 0 ▶sub _http_basic_auth {
Subroutine _http_basic_auth: not called CC=1 SCAR=0.6
128 0 my ( $username, $password ) = @_;
129  
130 12 ▶ use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
131 0 return 'Basic ' . encode_base64("$username:$password");
132 }
133  
134 # Creates a gerrit user account with the given $username and $password;
135 # this relies on gerrit being set up such that logging in automatically
136 # creates an account, which is true for HTTP authentication.
137 0 ▶sub _create_gerrit_user {
Subroutine _create_gerrit_user: not called CC=4 SCAR=30.0
138 0 my ( $self, $username, $password ) = @_;
139  
140 0 local $Test::Builder::Level = $Test::Builder::Level + 1;
141  
142 0 ▶ $password ||= $username;
Condition0%0/3
$password ||= $username
l!l&&r!l&&!r
000
MC/DC0%0/2
$password ||= $username
$password $username
Truth table0%0/3
A ||= B
A= $passwordB= $username
ABresult
1X1
011
000
143  
144 0 ▶ return unless $self->ensure_gerrit_running();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ensure_gerrit_running 0 0 0
145  
146 0 diag("Creating gerrit user $username ...");
147  
148 0 my $auth = _http_basic_auth( $username, $password );
149 my $response = LWP::UserAgent->new()
150 0 ->get( $self->{http_url} . '/login/mine', Authorization => $auth );
151  
152 0 ▶ ok( $response->is_success(), "user $username created" )
Branch: 1 branch
BranchTrueFalseTotal
&ok(scalar $response->is_success, "user $username created") or diag($response->status_line . "\n" . $response->decoded_content) 0 0 0
153 || diag( $response->status_line() . "\n" . $response->decoded_content() );
154  
155 0 return;
156 }
157  
158 # Generates an ssh key pair, passphraseless, and sets it as a peer key of
159 # gerrit; this allows it to be used as a key for the 'Gerrit Code Review' user.
160 # Sets the following:
161 # $self->{ sshkey_public_key }: ssh public key, unadorned (i.e. no 'ssh-rsa'
162 # prefix or comment suffix)
163 # $self->{ sshkey_file }: ssh private key filename; public key filename
164 # is identical with .pub appended
165 # Note: it may be necessary to flush-cache after this.
166 0 ▶sub _setup_peer_key {
Subroutine _setup_peer_key: not called CC=6 SCAR=37.4
167 0 my ($self) = @_;
168  
169 0 local $Test::Builder::Level = $Test::Builder::Level + 1;
170  
171 # create an ssh key set as a peer key, making it usable as the
172 # 'Gerrit Code Review' superuser.
173 0 my $sshkey_file = "$CWD/gerrit-client-test-id_rsa";
174 0 ▶ if ( !-f $sshkey_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $sshkey_file) 0 0 0
175 return
176 0 ▶ unless _system_or_fail( 'ssh-keygen', '-f', $sshkey_file, '-N', q{} );
Branch: 1 branch
BranchTrueFalseTotal
unless _system_or_fail("ssh-keygen", "-f", $sshkey_file, "-N", "") 0 0 0
177 }
178  
179 {
180 0 my $fh;
181 0 ▶ if ( !ok( open( $fh, '<', $sshkey_file . '.pub' ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&ok(scalar &Gerrit::Client::Test::open($fh, "<", $sshkey_file . '.pub'))) 0 0 0
182 0 diag "open $sshkey_file.pub: $!";
183 0 return;
184 }
185 0 my $line = <$fh>;
186 0 close($fh);
187 0 my ( undef, $key, undef ) = split( / /, $line, 3 );
188 0 ▶ if ( !ok( open( $fh, '>', 'etc/peer_keys' ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&ok(scalar &Gerrit::Client::Test::open($fh, ">", "etc/peer_keys"))) 0 0 0
189 0 diag "open peer_keys for write: $!";
190 0 return;
191 }
192 0 print $fh "$key\n";
193 0 ▶ if ( !ok( close($fh) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&ok(scalar &Gerrit::Client::Test::close($fh))) 0 0 0
194 0 diag "close peer_keys after write: $!";
195 0 return;
196 }
197 0 $self->{sshkey_public_key} = $key;
198 }
199  
200 0 $self->{sshkey_file} = $sshkey_file;
201 0 return;
202 }
203  
204 # Creates wrapper scripts for git and ssh to ensure git invokes ssh
205 # with the needed options for interacting with this gerrit.
206 0 ▶sub _setup_git_ssh {
Subroutine _setup_git_ssh: not called CC=2 SCAR=17.9
207 0 my ($self) = @_;
208 0 my (@ssh) = $self->ssh_base();
209  
210 0 my $ssh_cmd;
211 {
212 0 local $LIST_SEPARATOR = '" "';
213 0 $ssh_cmd = "\"@ssh\"";
214 }
215  
216 0 local $CWD = $self->{dir};
217 0 my $fh;
218  
219 0 eval {
220 0 open( $fh, '>', 'git_ssh_helper' );
221 0 print $fh <<"END_GIT_SSH";
222 #!/bin/sh
223 exec $ssh_cmd "\$\@\"
224 END_GIT_SSH
225 0 close($fh);
226  
227 0 open( $fh, '>', 'git_wrapper' );
228 0 print $fh <<'END_GIT';
229 #!/bin/sh
230 GIT_SSH=$(readlink -f $(dirname $0)/git_ssh_helper)
231 export GIT_SSH
232 exec git "$@"
233 END_GIT
234 0 close($fh);
235  
236 0 chmod( 0755, 'git_ssh_helper', 'git_wrapper' );
237 };
238 0 ▶ if ( my $error = $EVAL_ERROR ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $Gerrit::Client::Test::EVAL_ERROR) 0 0 0
239 0 fail("setting up git ssh: $error");
240 0 return;
241 }
242  
243 0 return 1;
244 }
245  
246 # Perform a $query via h2 Shell.
247 # Gerrit should not be running when the query is executed.
248 # NOTE: this method currently doesn't report errors (because the h2 shell exits
249 # with a zero exit code even if queries fail)
250 0 ▶sub _do_h2_query {
Subroutine _do_h2_query: not called CC=1 SCAR=6.9
251 0 my ( $self, $query ) = @_;
252  
253 0 my (@cmd) = (
254 'java',
255 '-cp', $self->_h2_jar(),
256 'org.h2.tools.Shell',
257 '-url', $self->_h2_jdbc_url(),
258 '-sql', $query,
259 );
260  
261 0 return _system_or_fail(@cmd);
262 }
263  
264 # Returns path to the h2 jar for this gerrit installation, extracting it
265 # from gerrit.war if needed.
266 0 ▶sub _h2_jar {
Subroutine _h2_jar: not called CC=5 SCAR=34.0
267 0 my ( $self ) = @_;
268  
269 0 my $h2_jar = "$self->{dir}/h2.jar";
270 0 ▶ if (-e $h2_jar) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $h2_jar) 0 0 0
271 0 return $h2_jar;
272 }
273  
274 0 my $zip = Archive::Zip->new( $self->_installed_gerrit_war() );
275 0 ▶ ok( $zip, 'open gerrit.war' ) || return;
Branch: 1 branch
BranchTrueFalseTotal
&ok($zip, "open gerrit.war") or return 0 0 0
276  
277 0 my (@possible) = $zip->membersMatching( '^WEB-INF/lib/h2-.*\.jar$' );
278 0 ▶ is( scalar(@possible), 1, 'find h2.jar in gerrit.war' ) || return;
Branch: 1 branch
BranchTrueFalseTotal
&is(scalar @possible, 1, "find h2.jar in gerrit.war") or return 0 0 0
279  
280 0 ▶ is( $zip->extractMember( $possible[0], $h2_jar ), AZ_OK, 'extract h2.jar' )
Branch: 1 branch
BranchTrueFalseTotal
&is(scalar $zip->extractMember($possible[0], $h2_jar), 0, "extract h2.jar") or return 0 0 0
281 || return;
282  
283 0 return $h2_jar;
284 }
285  
286 sub _installed_gerrit_war
287 0 ▶{
Subroutine _installed_gerrit_war: not called CC=1 SCAR=6.9
288 0 my ( $self ) = @_;
289 0 return "$self->{dir}/bin/gerrit.war";
290 }
291  
292 sub _h2_jdbc_url
293 0 ▶{
Subroutine _h2_jdbc_url: not called CC=1 SCAR=6.9
294 0 my ( $self ) = @_;
295 0 return "jdbc:h2:$self->{dir}/db/ReviewDB";
296 }
297  
298 # Like _system_or_fail, but operates on a $subref instead of
299 # a command via system()
300 0 ▶sub _cmd_ok {
Subroutine _cmd_ok: not called CC=2 SCAR=17.9
301 0 my ( $self, $name, $subref, @cmd ) = @_;
302  
303 0 local $Test::Builder::Level = $Test::Builder::Level + 1;
304  
305 0 my $cmdstr;
306 {
307 0 local $LIST_SEPARATOR = '] [';
308 0 $cmdstr = "[$name] [@cmd]";
309 }
310  
311 0 my $status;
312 0 ▶ my $captured = capture_merged {
Subroutine __ANON__: not called CC=1 SCAR=6.9
313 0 $status = $subref->( $self, @cmd );
314 0 };
315 0 ▶ if ( $status != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status != 0) 0 0 0
316 0 diag($captured);
317 }
318 0 return is( $status, 0, "cmd ok: $cmdstr" );
319 }
320  
321 0 ▶sub _load_gerrit {
Subroutine _load_gerrit: not called CC=4 SCAR=30.0
322 0 my ( $package, %args ) = @_;
323  
324 0 local $CWD = $args{dir};
325  
326 0 my $http_url = URI->new( _gerrit_config('httpd.listenUrl') );
327 0 ▶ if ( !$http_url ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($http_url) 0 0 0
328 0 warn "Can't load httpd.listenUrl from gerrit in $CWD";
329 0 return;
330 }
331  
332 0 my $ssh_address = _gerrit_config('sshd.listenAddress');
333 0 ▶ if ( !$ssh_address ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ssh_address) 0 0 0
334 0 warn "Can't load sshd.listenAddress from gerrit in $CWD";
335 0 return;
336 }
337  
338 0 diag("Found gerrit in $CWD, listening on $http_url and $ssh_address");
339  
340 0 $args{http_url} = $http_url->as_string();
341 0 $args{http_port} = $http_url->port();
342 0 ( undef, $args{ssh_port} ) = split( /:/, $ssh_address, 2 );
343 0 ▶ $args{war} ||= "$CWD/bin/gerrit.war";
Condition0%0/2
$args{"war"} ||= "$Gerrit::Client::Test::CWD/bin/gerrit.war"
l!l
00
MC/DC0%0/1
$args{"war"} ||= "$Gerrit::Client::Test::CWD/bin/gerrit.war"
$args{"war"}
Truth table0%0/2
A
A= $args{"war"}
Aresult
11
00
344 0 $args{user} = _gerrit_config('gerrit-client-test.user');
345  
346 0 my $self = bless \%args, $package;
347 0 $self->_setup_peer_key();
348 0 return $self;
349 }
350  
351 400 ▶sub _mock_command_step_filename {
Subroutine _mock_command_step_filename: called CC=1 SCAR=0.0
352 400 my ( $script, $i ) = @_;
353 400 return sprintf( '%s.step-%02d', $script, $i );
354 }
355  
356 14 ▶sub _mock_command_write_step_file {
Subroutine _mock_command_write_step_file: called CC=3 SCAR=11.2
357 14 my ( $filename, $data ) = @_;
358  
359 # $data is something like:
360 #
361 # { stdout => 'something', stderr => 'something', exitcode => 43 }
362 #
363 # We want to write literally a string like the above to the step file.
364 #
365 14 my $data_code = Data::Dumper->new( [$data] )->Terse(1)->Dump();
366  
367 14 ▶ my $fh = IO::File->new( $filename, '>' )
Condition50%1/2
'IO::File'->new($filename, '>') || croak("open $filename for write: $!")
l!l
140
MC/DC0%0/1
'IO::File'->new($filename, '>') || croak("open $filename for write: $!")
'IO::File'->new($filename, '>')
Truth table50%1/2
A
A= 'IO::File'->new($filename, '>')
Aresult
11
00
368 || croak "open $filename for write: $!";
369  
370 14 $fh->print("$data_code;\n");
371  
372 14 ▶ $fh->close()
Branch: 1 branch
BranchTrueFalseTotal
$fh->close or croak("close $filename after write: $!") 14 0 14
373 || croak "close $filename after write: $!";
374  
375 14 return;
376 }
377  
378 4 ▶sub _mock_command_write_command {
Subroutine _mock_command_write_command: called CC=7 SCAR=25.1
379 4 my ( $command_file, @step_files ) = @_;
380  
381 4 my $step_files_code = Data::Dumper->new( [ \@step_files ] )->Terse(1)->Dump();
382  
383 4 ▶ my $fh = IO::File->new( $command_file, '>' )
Condition50%1/2
'IO::File'->new($command_file, '>') || croak("open $command_file for write: $!")
l!l
40
MC/DC0%0/1
'IO::File'->new($command_file, '>') || croak("open $command_file for write: $!")
'IO::File'->new($command_file, '>')
Truth table50%1/2
A
A= 'IO::File'->new($command_file, '>')
Aresult
11
00
384 || croak "open $command_file for write: $!";
385  
386 4 $fh->print(
387 q|#!/usr/bin/env perl
388 use strict;
389 use warnings;
390 use utf8;
391 use Data::Dumper;
392 use English qw(-no_match_vars);
393  
394 binmode( STDOUT, ':utf8' );
395 binmode( STDERR, ':utf8' );
396  
397 my $step_files = | . $step_files_code . q|;
398 foreach my $file (@{$step_files}) {
399 next if (! -e $file);
400 my $data = do $file;
401  
402 die "couldn't parse $file: $@" if $@;
403 die "couldn't do $file: $!" if (! defined $data);
404 die "$file did not give a hashref" if (ref($data) ne 'HASH');
405 die "couldn't unlink $file: $!" if (! unlink( $file ));
406  
407 local $OUTPUT_AUTOFLUSH = 1;
408 print STDOUT $data->{stdout};
409 print STDERR $data->{stderr};
410 sleep( $data->{delay} ) if $data->{delay};
411 exit $data->{exitcode};
412 }
413  
414 die "no more test steps!\n"
415 .'A mocked command created by Gerrit::Client::Test::create_mock_command was '
416 ."run more times than expected.\n"
417 .'I expected to be run at most '.scalar(@{$step_files}).' time(s), reading '
418 ."instructions from these files:\n".Dumper($step_files)
419 .'...but the files do not exist!';|
420 );
421  
422 4 ▶ $fh->close() || croak "close $command_file after write: $!";
Branch: 1 branch
BranchTrueFalseTotal
$fh->close or croak("close $command_file after write: $!") 4 0 4
423  
424 # On most OS, we simply need to make the script have executable permission
425 4 ▶ if ( $OSNAME !~ m{win32}i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $Gerrit::Client::Test::OSNAME =~ /win32/i) { } 4 0 4
426 4 ▶ chmod( 0755, $command_file ) || croak "chmod $command_file: $!";
Branch: 1 branch
BranchTrueFalseTotal
&Gerrit::Client::Test::chmod(493, $command_file) or croak("chmod ${command_file}: $!") 4 0 4
427 }
428  
429 # On Windows, we cannot directly execute the above script as a command.
430 # Make a .bat file which executes it.
431 else {
432 0 ▶ $fh = IO::File->new( "$command_file.bat", '>' )
Condition0%0/2
'IO::File'->new("$command_file.bat", '>') || croak("open $command_file.bat for write: $!")
l!l
00
MC/DC0%0/1
'IO::File'->new("$command_file.bat", '>') || croak("open $command_file.bat for write: $!")
'IO::File'->new("$command_file.bat", '>')
Truth table0%0/2
A
A= 'IO::File'->new("$command_file.bat", '>')
Aresult
11
00
433 || croak "open $command_file.bat for write: $!";
434  
435 # %~dp0 == the full path to the directory containing the .bat
436 # %* == all arguments
437  
438 0 $fh->print( '@perl.exe %~dp0' . basename($command_file) . " %*\n" );
439 0 ▶ $fh->close() || croak "close $command_file.bat after write: $!";
Branch: 1 branch
BranchTrueFalseTotal
$fh->close or croak("close $command_file.bat after write: $!") 0 0 0
440 }
441  
442 4 return;
443 }
444  
445 ################################## public ######################################
446  
447 =head1 FUNCTIONS
448  
449 =over
450  
451 =item B<create_mock_command>( OPTIONS )
452  
453 Creates a mock command whose behavior is defined by the content of OPTIONS.
454  
455 The purpose of this function is to facilitate the testing of code
456 which interacts with possibly failing external processes. This is
457 made clear with an example: to test how a script handles temporary
458 network failures from git, the following code could be used:
459  
460 create_mock_command(
461 name => 'git',
462 directory => $tempdir,
463 sequence => [
464 # first two times, simulate the server hanging up for unknown
465 # reasons after a few seconds
466 { stdout => q{}, stderr => "fatal: The remote end hung up unexpectedly\n",
467 exitcode => 2, delay => 3 },
468 { stdout => q{}, stderr => "fatal: The remote end hung up unexpectedly\n",
469 exitcode => 2, delay => 3 },
470 # on the third try, complete successfully
471 { stdout => q{}, stderr => q{}, exitcode => 0 },
472 ],
473 );
474  
475 # make the above mocked git first in PATH...
476 local $ENV{PATH} = $tempdir . ':' . $ENV{PATH};
477  
478 # and verify that some code can robustly handle the above errors
479 # (but warned about them)
480 my $result;
481 my ($stdout, $stderr) = capture {
482 $result = $git->clone('git://example.com/repo')
483 };
484 ok( $result );
485 is( $stderr,
486 "Warning: 3 attempt(s) required to successfully complete git operation\n" );
487  
488 OPTIONS is a hash or hashref with the following keys:
489  
490 =over
491  
492 =item name
493  
494 The basename of the command, e.g. `git'.
495  
496 =item directory
497  
498 The directory in which the command should be created, e.g. `/tmp/command-test'.
499  
500 This should be a temporary directory, because B<create_mock_command> will write
501 some otherwise useless data files to this directory. The caller is responsible
502 for creating and deleting this directory (and prepending it to $ENV{PATH}, if
503 that is appropriate).
504  
505 =item sequence
506  
507 The test sequence which should be simulated by the command.
508  
509 This is a reference to an array of hashrefs, each of which has these keys:
510  
511 =over
512  
513 =item stdout
514  
515 Standard output to be written by the command.
516  
517 =item stderr
518  
519 Standard error to be written by the command.
520  
521 =item exitcode
522  
523 The exit code for the command.
524  
525 =item delay
526  
527 Delay, in seconds, to wait after the command has printed its output and before
528 the command exits.
529  
530 =back
531  
532 Each time the mock command is executed, the next element in the array is used
533 to determine the command's behavior. For example, with this sequence:
534  
535 sequence => [
536 { stdout => q{}, stderr => "example.com: host not found\n",
537 exitcode => 2 },
538 { stdout => "OK\n", stderr => q{}, exitcode => 0 },
539 ]
540  
541 ... the first time the command is run, it will print "example.com:
542 host not found" to standard error, and exit with exit code 2
543 (failure). The second time the command is run, it will print "OK" to
544 standard output, and exit with exit code 0 (success). (It is an error
545 to run the command a third time - if this is done, it will die,
546 noisily).
547  
548 =back
549  
550 =cut
551 4 ▶sub create_mock_command {
Subroutine create_mock_command: called CC=8 SCAR=21.2
Pod documented
552 4 my %options = validate(
553 @_,
554 { name => 1,
555 directory => 1,
556 sequence => { type => ARRAYREF },
557 }
558 );
559  
560 my ( $name, $directory, $sequence_ref ) =
561 4 @options{qw(name directory sequence)};
562  
563 4 ▶ croak "`$directory' is not an existing directory" if ( !-d $directory );
Branch: 1 branch
BranchTrueFalseTotal
unless -d $directory 0 4 4
564 4 ▶ croak 'name is empty' if ( !$name );
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 4 4
565  
566 4 my $script = File::Spec->catfile( $directory, $name );
567 4 ▶ croak "`$script' already exists" if ( -e $script );
Branch: 1 branch
BranchTrueFalseTotal
if -e $script 0 4 4
568  
569 4 my @sequence = @{$sequence_ref};
570  
571 # We use data files like:
572 #
573 # command.step-NN
574 #
575 # ... to instruct the command on what to do.
576 #
577 # Each time the command is run, it will read and delete the
578 # lowest-numbered step file.
579 #
580 # We arbitrarily choose 2 digits, meaning a maximum of 100 steps.
581 #
582 # Note that we intentionally support having 0 steps. This means that
583 # we create a command which simply dies immediately if it is called.
584 # This may be used to test that a command is _not_ called, or to
585 # satisfy code which requires some command to be in PATH but does not
586 # actually invoke it.
587 4 my $MAX = 100;
588 4 ▶ croak "test sequence is too large! Maximum of $MAX steps permitted"
Branch: 1 branch
BranchTrueFalseTotal
if @sequence > $MAX 0 4 4
589 if ( @sequence > $MAX );
590  
591 # Verify that none of the step files exist
592 4 my @FILENAMES = map { _mock_command_step_filename( $script, $_ ) }
593 ( 0 .. ( $MAX - 1 ) );
594  
595 croak
596 "step file(s) still exist in $directory - did you forget to clean this up "
597 . 'since an earlier test?'
598 4 ▶ if ( any { -e $_ } @FILENAMES );
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { -e $_; } , @FILENAMES) 0 4 4
599  
600 4 my $step_number = 0;
601 4 foreach my $step (@sequence) {
602 14 my $validated_step = eval {
603 14 validate_with(
604 params => [$step],
605 spec => {
606 stdout => { default => q{} },
607 stderr => { default => q{} },
608 exitcode => { default => 0 },
609 delay => { default => 0 },
610 },
611 );
612 };
613  
614 14 ▶ croak "at step $step_number of test sequence: $EVAL_ERROR" if ($EVAL_ERROR);
Branch: 1 branch
BranchTrueFalseTotal
if $Gerrit::Client::Test::EVAL_ERROR 0 14 14
615  
616 14 my $filename = $FILENAMES[ $step_number++ ];
617 14 _mock_command_write_step_file( $filename, $validated_step );
618 }
619  
620 4 _mock_command_write_command( $script,
621 @FILENAMES[ 0 .. ( $step_number - 1 ) ] );
622  
623 4 return;
624 }
625  
626 =back
627  
628 =head1 METHODS
629  
630 In typical usage, B<ensure_gerrit_installed> would first be called in order to
631 obtain a handle to a local Gerrit instance; afterwards, other methods act in
632 the context of that Gerrit. This means that git and ssh commands are adjusted so
633 that passwordless superuser access is available to the local Gerrit.
634  
635 =over
636  
637 =item Gerrit::Client::Test->B<ensure_gerrit_installed>
638  
639 =item Gerrit::Client::Test->B<ensure_gerrit_installed>( OPTIONS )
640  
641 Installs Gerrit, or checks an existing Gerrit installation, and returns an
642 object representing the Gerrit site.
643  
644 If no options are provided, an arbitrary version of Gerrit is downloaded and
645 installed to a temporary directory.
646  
647 OPTIONS is a hashref with the following permitted keys:
648  
649 =over
650  
651 =item dir
652  
653 Directory in which gerrit should be installed.
654  
655 Defaults to a new temporary directory, which will be removed when the returned
656 object is destroyed.
657  
658 =item war
659  
660 URL or path to a gerrit.war to use for installation.
661  
662 Defaults to http://gerrit-releases.storage.googleapis.com/gerrit-2.8.5.war .
663  
664 =item user
665  
666 Username for the initial gerrit user account (account 1000000).
667 This account has administrative privileges.
668  
669 Defaults to "perl-gerrit-client-test".
670  
671 =item ssh_port
672  
673 =item http_port
674  
675 TCP ports for the ssh and http interfaces to this Gerrit site.
676  
677 Defaults to any unused ports chosen by the operating system.
678  
679 =back
680  
681 All of the above described options may also be directly extracted from the
682 returned object, which is a blessed hashref.
683  
684 =cut
685  
686 0 ▶sub ensure_gerrit_installed {
Subroutine ensure_gerrit_installed: not called CC=17 SCAR=57.2
Pod documented
687 0 my ( $package, %args ) = @_;
688  
689 # We consider that gerrit is installed if gerrit.war exists in the destination
690 # directory
691 0 ▶ if ( $args{dir} && -f "$args{ dir }/bin/gerrit.war" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"dir"} and -f qq[$args{"dir"}/bin/gerrit.war]) 0 0 0
Condition0%0/3
$args{"dir"} and -f qq[$args{"dir"}/bin/gerrit.war]
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"dir"} and -f qq[$args{"dir"}/bin/gerrit.war]
$args{"dir"} -f qq[$args{"dir"}/bin/gerrit.war]
Truth table0%0/3
A and B
A= $args{"dir"}B= -f qq[$args{"dir"}/bin/gerrit.war]
ABresult
0X0
100
111
692 0 return $package->_load_gerrit(%args);
693 }
694  
695 0 ▶ $args{ssh_port} ||= _find_available_tcp_port();
Condition0%0/3
$args{"ssh_port"} ||= _find_available_tcp_port()
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"ssh_port"} ||= _find_available_tcp_port()
$args{"ssh_port"} _find_available_tcp_port()
Truth table0%0/3
A ||= B
A= $args{"ssh_port"}B= _find_available_tcp_port()
ABresult
1X1
011
000
696 0 ▶ $args{http_port} ||= _find_available_tcp_port();
Condition0%0/3
$args{"http_port"} ||= _find_available_tcp_port()
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"http_port"} ||= _find_available_tcp_port()
$args{"http_port"} _find_available_tcp_port()
Truth table0%0/3
A ||= B
A= $args{"http_port"}B= _find_available_tcp_port()
ABresult
1X1
011
000
697 0 ▶ $args{war} ||= 'http://gerrit-releases.storage.googleapis.com/gerrit-2.8.5.war';
Condition0%0/2
$args{"war"} ||= "http://gerrit-releases.storage.googleapis.com/gerrit-2.8.5.war"
l!l
00
MC/DC0%0/1
$args{"war"} ||= "http://gerrit-releases.storage.googleapis.com/gerrit-2.8.5.war"
$args{"war"}
Truth table0%0/2
A
A= $args{"war"}
Aresult
11
00
698 0 ▶ $args{dir} ||= File::Temp->newdir( 'Gerrit-Client-Test.XXXXXX', TMPDIR => 1 );
Condition0%0/3
$args{"dir"} ||= "File::Temp"->newdir("Gerrit-Client-Test.XXXXXX", "TMPDIR", 1)
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"dir"} ||= "File::Temp"->newdir("Gerrit-Client-Test.XXXXXX", "TMPDIR", 1)
$args{"dir"} "File::Temp"->newdir("Gerrit-Client-Test.XXXXXX", "TMPDIR", 1)
Truth table0%0/3
A ||= B
A= $args{"dir"}B= "File::Temp"->newdir("Gerrit-Client-Test.XXXXXX", "TMPDIR", 1)
ABresult
1X1
011
000
699 0 ▶ $args{user} ||= 'perl-gerrit-client-test';
Condition0%0/2
$args{"user"} ||= "perl-gerrit-client-test"
l!l
00
MC/DC0%0/1
$args{"user"} ||= "perl-gerrit-client-test"
$args{"user"}
Truth table0%0/2
A
A= $args{"user"}
Aresult
11
00
700  
701 0 my $local_war;
702 0 my $uri = URI->new( $args{war} );
703 0 ▶ if ( !$uri->scheme() ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $uri->scheme) { } 0 0 0
elsif ($uri->scheme eq 'file') { } 0 0 0
704 0 $local_war = $args{war};
705 }
706 elsif ( $uri->scheme() eq 'file' ) {
707 0 $local_war = $uri->path();
708 }
709 else {
710 0 $local_war = _fetch_remote( $args{war} );
711 }
712  
713 0 ▶ if ( !-d $args{dir} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $args{"dir"}) 0 0 0
714 0 mkpath( $args{dir} );
715 }
716 0 local $CWD = $args{dir};
717 0 my @installcmd =
718 ( 'java', '-jar', $local_war, 'init', '--batch', '--no-auto-start' );
719 0 ▶ return unless _system_or_fail(@installcmd);
Branch: 1 branch
BranchTrueFalseTotal
unless _system_or_fail(@installcmd) 0 0 0
720  
721 0 $args{http_url} = "http://127.0.0.1:$args{http_port}";
722  
723 0 ▶ return unless _gerrit_config( 'auth.type', 'HTTP' );
Branch: 1 branch
BranchTrueFalseTotal
unless _gerrit_config("auth.type", "HTTP") 0 0 0
724 return
725 0 ▶ unless _gerrit_config( 'sshd.listenAddress', "127.0.0.1:$args{ssh_port}" );
Branch: 1 branch
BranchTrueFalseTotal
unless _gerrit_config("sshd.listenAddress", qq[127.0.0.1:$args{"ssh_port"}]) 0 0 0
726 0 ▶ return unless _gerrit_config( 'httpd.listenUrl', $args{http_url} );
Branch: 1 branch
BranchTrueFalseTotal
unless _gerrit_config("httpd.listenUrl", $args{"http_url"}) 0 0 0
727 0 ▶ return unless _gerrit_config( 'gerrit.canonicalWebUrl', $args{http_url} );
Branch: 1 branch
BranchTrueFalseTotal
unless _gerrit_config("gerrit.canonicalWebUrl", $args{"http_url"}) 0 0 0
728 0 ▶ return unless _gerrit_config( 'gerrit-client-test.user', $args{user} );
Branch: 1 branch
BranchTrueFalseTotal
unless _gerrit_config("gerrit-client-test.user", $args{"user"}) 0 0 0
729  
730 0 my $self = bless \%args, $package;
731 0 $self->_setup_peer_key();
732 0 $self->_setup_git_ssh();
733 0 $self->_create_gerrit_user( $args{user} );
734 0 $self->ensure_gerrit_stopped();
735 0 $self->_do_h2_query( "insert into account_ssh_keys("
736 . "ssh_public_key, valid, account_id, seq"
737 . ") values("
738 . "'ssh-rsa $args{ sshkey_public_key } test','Y',1000000,0"
739 . ")" );
740 0 $self->_do_h2_query( "insert into account_external_ids("
741 . "account_id,email_address,external_id"
742 . ") values("
743 . "1000000, 'perl-gerrit-client-test\@127.0.0.1', "
744 . "'mailto:perl-gerrit-client-test\@127.0.0.1'"
745 . ")" );
746 0 $self->_do_h2_query( "UPDATE account_external_ids "
747 . "SET password='abcdefghijkl' "
748 . "WHERE account_id=1000000;"
749 );
750  
751 0 return $self;
752 }
753  
754 =item B<ssh_base>
755  
756 =item B<ssh_base>( USERNAME )
757  
758 Returns the initial part of the ssh command which should be used when
759 interacting with this Gerrit installation. The command includes options for
760 setting the port number and identity file to allow passwordless access to this
761 Gerrit site.
762  
763 If USERNAME is given, the command will also contain the USER@HOST argument;
764 otherwise, it must be specified manually. The HOST is always "127.0.0.1".
765  
766 =cut
767  
768 0 ▶sub ssh_base {
Subroutine ssh_base: not called CC=2 SCAR=17.9
Pod documented
769 0 my ( $self, $user ) = @_;
770  
771 my @out = (
772 'ssh',
773 "-oUserKnownHostsFile=$self->{ dir }/ssh_known_hosts",
774 '-oStrictHostKeyChecking=no',
775 '-oBatchMode=yes',
776 '-i',
777 $self->{sshkey_file},
778 '-p',
779 $self->{ssh_port},
780 0 );
781  
782 0 ▶ if ($user) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user) 0 0 0
783 0 push @out, "$user\@127.0.0.1";
784 }
785 0 return @out;
786 }
787  
788 =item B<git>( COMMAND )
789  
790 Runs the given git COMMAND in the context of this gerrit.
791 COMMAND should be a git command with arguments, excluding the leading 'git', as
792 in the following example:
793  
794 $gerrit->git( 'fetch', 'origin', 'refs/heads/*:refs/remotes/origin/*' );
795  
796 Returns the exit status of the git command.
797  
798 =cut
799  
800 0 ▶sub git {
Subroutine git: not called CC=1 SCAR=6.9
Pod documented
801 0 my ( $self, @cmd ) = @_;
802 0 return system( "$self->{ dir }/git_wrapper", @cmd );
803 }
804  
805 =item B<git_ok>( COMMAND )
806  
807 Like B<git>, but the command is treated as a test.
808 If the command fails, the test fails and the command's output is printed to the
809 test log.
810  
811 =cut
812  
813 0 ▶sub git_ok {
Subroutine git_ok: not called CC=1 SCAR=6.9
Pod documented
814 0 my ( $self, @cmd ) = @_;
815 0 local $Test::Builder::Level = $Test::Builder::Level + 1;
816 0 return $self->_cmd_ok( 'git', \&git, @cmd );
817 }
818  
819 =item B<gerrit>( COMMAND )
820  
821 =item B<gerrit>( OPTIONS, COMMAND )
822  
823 Runs the given gerrit COMMAND, via ssh, in the context of this gerrit.
824 COMMAND should be a gerrit command with arguments, excluding the leading
825 'gerrit', as in the following example:
826  
827 $gerrit->gerrit( 'create-project', 'testproject' );
828  
829 OPTIONS may be passed as a hashref with the following keys:
830  
831 =over
832  
833 =item user
834  
835 Username for the gerrit connection.
836  
837 Defaults to $gerrit->{user}, which is the first created user and has
838 administrative privileges.
839  
840 =back
841  
842 Returns the exit status of the ssh command.
843  
844 =cut
845  
846 0 ▶sub gerrit {
Subroutine gerrit: not called CC=3 SCAR=24.8
Pod documented
847 0 my ( $self, @cmd ) = @_;
848 0 $self->ensure_gerrit_running();
849 0 my $opts;
850 0 ▶ if ( ref( $cmd[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cmd[0]) 0 0 0
851 0 $opts = shift @cmd;
852 }
853 0 ▶ $opts->{user} ||= $self->{user};
Condition0%0/3
$opts->{"user"} ||= $self->{"user"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{"user"} ||= $self->{"user"}
$opts->{"user"} $self->{"user"}
Truth table0%0/3
A ||= B
A= $opts->{"user"}B= $self->{"user"}
ABresult
1X1
011
000
854  
855 0 my (@base) = $self->ssh_base( $opts->{user} );
856 0 return system( @base, 'gerrit', @cmd );
857 }
858  
859 =item B<gerrit_ok>( COMMAND )
860  
861 =item B<gerrit_ok>( OPTIONS, COMMAND )
862  
863 Like B<gerrit>, but the command is treated as a test.
864 If the command fails, the test fails and the command's output is printed to the
865 test log.
866  
867 =cut
868  
869 0 ▶sub gerrit_ok {
Subroutine gerrit_ok: not called CC=1 SCAR=6.9
Pod documented
870 0 my ( $self, @cmd ) = @_;
871 0 local $Test::Builder::Level = $Test::Builder::Level + 1;
872 0 return $self->_cmd_ok( 'gerrit', \&gerrit, @cmd );
873 }
874  
875 =item B<git_test_commit>
876  
877 =item B<git_test_commit>( MESSAGE )
878  
879 Create a test commit (an arbitrary, non-empty commit) in the local git
880 repository.
881  
882 If MESSAGE is given, it is used as the commit message; otherwise, a reasonable
883 default is used.
884  
885 =cut
886  
887 0 ▶sub git_test_commit {
Subroutine git_test_commit: not called CC=6 SCAR=37.4
Pod documented
888 0 my ( $self, $message ) = @_;
889  
890 0 my $opts;
891 0 ▶ if ( $message && ref($message) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($message and ref $message) 0 0 0
Condition0%0/3
$message and ref $message
!ll&&!rl&&r
000
MC/DC0%0/2
$message and ref $message
$message ref $message
Truth table0%0/3
A and B
A= $messageB= ref $message
ABresult
0X0
100
111
892 0 $opts = $message;
893 0 $message = shift;
894 }
895  
896 0 ▶ $message ||= 'test commit';
Condition0%0/2
$message ||= "test commit"
l!l
00
MC/DC0%0/1
$message ||= "test commit"
$message
Truth table0%0/2
A
A= $message
Aresult
11
00
897  
898 0 my $fh;
899 0 open( $fh, '>>', 'testfile' );
900 0 print $fh "===\n$message\n";
901 0 close($fh);
902  
903 0 my @commit_cmd = ( 'commit', '-m', $message );
904 0 ▶ if ( $opts->{amend} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"amend"}) 0 0 0
905 0 @commit_cmd = ( 'commit', '--amend', '--reuse-message', 'HEAD' );
906 }
907  
908 0 local $Test::Builder::Level = $Test::Builder::Level + 1;
909  
910 0 ▶ return $self->git_ok( 'add', 'testfile' )
Condition0%0/3
$self->git_ok('add', 'testfile') && $self->git_ok(@commit_cmd)
!ll&&!rl&&r
000
MC/DC0%0/2
$self->git_ok('add', 'testfile') && $self->git_ok(@commit_cmd)
$self->git_ok('add', 'testfile') $self->git_ok(@commit_cmd)
Truth table0%0/3
A && B
A= $self->git_ok('add', 'testfile')B= $self->git_ok(@commit_cmd)
ABresult
0X0
100
111
911 && $self->git_ok(@commit_cmd);
912 }
913  
914 =item B<giturl_base>
915  
916 =item B<giturl_base>( USER )
917  
918 Returns the base git URL for this gerrit site.
919  
920 The URL contains scheme, user, host and port components.
921 By default, $gerrit->{user} is used as the username;
922 this may be overriden by the USER parameter.
923  
924 The URL has no path component, and hence the full git URL for a given
925 project may be constructed as in the following example:
926  
927 my $giturl = $gerrit->giturl_base() . '/some/project';
928 $gerrit->git( 'clone', $giturl );
929 ...
930  
931 =cut
932  
933 0 ▶sub giturl_base {
Subroutine giturl_base: not called CC=2 SCAR=17.9
Pod documented
934 0 my ( $self, $user ) = @_;
935 0 ▶ $user ||= $self->{user};
Condition0%0/3
$user ||= $self->{"user"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$user ||= $self->{"user"}
$user $self->{"user"}
Truth table0%0/3
A ||= B
A= $userB= $self->{"user"}
ABresult
1X1
011
000
936 0 return "ssh://$user\@127.0.0.1:$self->{ ssh_port }";
937 }
938  
939 =item B<http_url>
940  
941 Returns the base HTTP URL for this gerrit site.
942  
943 =cut
944  
945 0 ▶sub http_url {
Subroutine http_url: not called CC=1 SCAR=6.9
Pod documented
946 0 my ( $self ) = @_;
947 0 return $self->{http_url};
948 }
949  
950 =item B<git_ssh_wrapper>
951  
952 Returns the path to a wrapper script for the ssh command. The wrapper
953 script may be used in place of 'ssh' to ensure that the correct setup
954 is used for passwordless access to this gerrit site.
955  
956 Useful in conjunction with @Gerrit::Client::SSH to allow Gerrit::Client
957 passwordless access to this gerrit:
958  
959 local @Gerrit::Client::SSH = ( $gerrit->git_ssh_wrapper() );
960 my $stream = Gerrit::Client::stream_events(
961 url => $gerrit->giturl_base(),
962 ...
963 );
964  
965 =cut
966  
967 0 ▶sub git_ssh_wrapper {
Subroutine git_ssh_wrapper: not called CC=1 SCAR=6.9
Pod documented
968 0 my ($self) = @_;
969 0 return "$self->{ dir }/git_ssh_helper";
970 }
971  
972 =item B<git_wrapper>
973  
974 Returns the path to a wrapper script for the git command. The wrapper
975 script may be used in place of 'git' to ensure that the correct setup
976 is used for passwordless access to this gerrit site.
977  
978 Useful in conjunction with @Gerrit::Client::GIT to allow Gerrit::Client
979 passwordless access to this gerrit:
980  
981 local @Gerrit::Client::GIT = ( $gerrit->git_wrapper() );
982 my $stream = Gerrit::Client::stream_events(
983 url => $gerrit->giturl_base(),
984 ...
985 );
986  
987 =cut
988  
989 0 ▶sub git_wrapper {
Subroutine git_wrapper: not called CC=1 SCAR=6.9
Pod documented
990 0 my ($self) = @_;
991 0 return "$self->{ dir }/git_wrapper";
992 }
993  
994  
995 =item B<start_gerrit>
996  
997 Start the gerrit daemon or add a failure to the test log.
998  
999 =cut
1000  
1001 0 ▶sub start_gerrit {
Subroutine start_gerrit: not called CC=1 SCAR=6.9
Pod documented
1002 0 my ($self) = @_;
1003  
1004 0 local $CWD = $self->{dir};
1005 0 diag("Starting gerrit in $CWD...");
1006 0 return _system_or_fail( 'bin/gerrit.sh', 'start' );
1007 }
1008  
1009 =item B<stop_gerrit>
1010  
1011 Stop the gerrit daemon or add a failure to the test log.
1012  
1013 =cut
1014  
1015 0 ▶sub stop_gerrit {
Subroutine stop_gerrit: not called CC=1 SCAR=6.9
Pod documented
1016 0 my ($self) = @_;
1017  
1018 0 local $CWD = $self->{dir};
1019 0 diag("Stopping gerrit in $CWD...");
1020 0 return _system_or_fail( 'bin/gerrit.sh', 'stop' );
1021 }
1022  
1023 =item B<gerrit_pid>
1024  
1025 Returns the PID of the main Gerrit process, if available.
1026  
1027 This may return a stale value if Gerrit was terminated unexpectedly.
1028  
1029 =cut
1030  
1031 0 ▶sub gerrit_pid {
Subroutine gerrit_pid: not called CC=2 SCAR=17.9
Pod documented
1032 0 my ($self) = @_;
1033  
1034 0 my $pidfile = "$self->{ dir }/logs/gerrit.pid";
1035 0 ▶ if ( !-f $pidfile ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $pidfile) 0 0 0
1036 0 return;
1037 }
1038 0 open( my $fh, '<', $pidfile );
1039 0 my $pid = <$fh>;
1040 0 chomp $pid;
1041 0 close($fh);
1042  
1043 0 return 0 + $pid;
1044 }
1045  
1046 =item B<gerrit_running>
1047  
1048 Returns 1 if and only if this Gerrit instance appears to be running.
1049  
1050 =cut
1051  
1052 0 ▶sub gerrit_running {
Subroutine gerrit_running: not called CC=2 SCAR=17.9
Pod documented
1053 0 my ($self) = @_;
1054  
1055 0 my $pid = $self->gerrit_pid();
1056 0 ▶ if ( !$pid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 0 0 0
1057 0 return;
1058 }
1059 0 return kill( 0, $pid );
1060 }
1061  
1062 =item B<ensure_gerrit_running>
1063  
1064 Start gerrit only if it is not already running, or add a failure to the test
1065 log.
1066  
1067 =cut
1068  
1069 0 ▶sub ensure_gerrit_running {
Subroutine ensure_gerrit_running: not called CC=2 SCAR=17.9
Pod documented
1070 0 my ($self) = @_;
1071  
1072 0 ▶ if ( $self->gerrit_running() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->gerrit_running) 0 0 0
1073 0 return 1;
1074 }
1075 0 return $self->start_gerrit();
1076 }
1077  
1078 =item B<ensure_gerrit_stopped>
1079  
1080 Stop gerrit only if it is running, or add a failure to the test log.
1081  
1082 =cut
1083  
1084 0 ▶sub ensure_gerrit_stopped {
Subroutine ensure_gerrit_stopped: not called CC=2 SCAR=17.9
Pod documented
1085 0 my ($self) = @_;
1086  
1087 0 ▶ if ( !$self->gerrit_running() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->gerrit_running) 0 0 0
1088 0 return 1;
1089 }
1090 0 return $self->stop_gerrit();
1091 }
1092  
1093 =item B<have_git>
1094  
1095 Returns true if and only if a functional git command is in PATH.
1096  
1097 =cut
1098  
1099 2 ▶sub have_git {
Subroutine have_git: called CC=2 SCAR=7.2
Pod documented
1100 2 ▶ $HAVE_GIT //= (0 == system('git', '--version'));
Condition50%1/2
$HAVE_GIT //= 0 == system("git", "--version")
l!l
02
MC/DC0%0/1
$HAVE_GIT //= 0 == system("git", "--version")
$HAVE_GIT
Truth table50%1/2
A
A= $HAVE_GIT
Aresult
11
00
1101  
1102 2 return $HAVE_GIT;
1103 }
1104  
1105 =back
1106  
1107 =cut
1108  
1109 12 1;
↑ Summary blib/lib/Gerrit/Client.pm »