File Coverage

lib/Consul/Simple/Test.pm
Criterion Covered Total %
statement 27 32 84.3
branch 9 16 56.2
condition 3 8 37.5
subroutine 3 3 100.0
pod 0 2 0.0
total 42 61 68.8


line stmt bran cond sub pod time code
1             package Consul::Simple::Test;
2             $Consul::Simple::Test::VERSION = '1.142430';
3             my $consul_data_dir = '/tmp/consul_data_' . $$;
4             our $consul_daemon_pid;
5              
6             sub init_tests {
7 10     10 0 9094 my $can_test = init_server();
8 5 50       176 if(not $can_test) {
9 0         0 print STDERR "#full tests can only be run on Linux or MacOS(Darwin) x86_64. Skipping all.\n";
10 0         0 exit 0;
11             }
12             }
13              
14             sub init_server {
15 10     10 0 92684 my $uname = `uname 2>&1`;
16 10         226 chomp $uname;
17 10 50 33     410 if($uname ne 'Linux' and $uname ne 'Darwin') {
18 0         0 return 0;
19             }
20 10         88220 my $arch = `uname -m 2>&1`;
21 10         324 chomp $arch;
22 10 50       1030 if($arch ne 'x86_64') {
23 0         0 return 0;
24             }
25 10   50     2442 mkdir $consul_data_dir || return 0;
26 10         38390 my $new_pid = fork;
27 10 50       1358 return 0 if not defined $new_pid; #fork failed. (!??)
28 10 100       723 if(not $new_pid) { #child
29 5         445 my $consul_bin = 'testbin/consul_' . $uname . '_' . $arch;
30 5 50 33     628 $consul_bin = '../' . $consul_bin if $ENV{PWD} and $ENV{PWD} =~ /\/t$/;
31 5         401 my @args = ('agent', '-server', '-bootstrap-expect', '1', '-data-dir', $consul_data_dir, '-log-level', 'err');
32 5         1853 print STDERR "##Ignore WARNING: Bootstrap* and [ERR] agent* output from consul daemon\n";
33 5         0 exec $consul_bin, @args;
34 0         0 exit;
35             }
36 5         70 $consul_daemon_pid = $new_pid; #parent
37 5         15002792 sleep 3;
38 5         71139 my $ret = `ps -p $consul_daemon_pid > /dev/null 2>&1;echo \$?`;
39 5         92 chomp $ret;
40 5 50       143 return 0 unless $ret eq '0';
41 5         166 return 1;
42             }
43              
44             END {
45 5 50   5   17726 kill 9, $consul_daemon_pid if $consul_daemon_pid;
46 5         5000692 sleep 1;
47 5         82855 system "rm -rf $consul_data_dir > /dev/null 2>&1";
48 5           system 'rm -rf /tmp/consul*';
49             };
50              
51             1;