lib/Tapper/Installer/Precondition/PRC.pm

filter: Statementstmt 72.6% 109 / 150 Branchbran 36.2% 29 / 80 Conditioncond 25.0% 3 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 93.7% 15 / 16 Podpod 100% 8 / 8 totaltotal 59.8% 164 / 274 CC 46 SCAR 41.6
CC
46
Cov
58%
install_startscript
240.0
install
12.1
get_distro
10.9
CRAP 64.4
« lib/Tapper/Installer/Precondition/Rawimage.pm ↑ Summary lib/Tapper/Installer/Precondition/Image.pm »
1 package Tapper::Installer::Precondition::PRC;
2 8 our $AUTHORITY = 'cpan:TAPPER';
3 8 $Tapper::Installer::Precondition::PRC::VERSION = '5.0.3';
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 8 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Hash::Merge::Simple 'merge';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use File::ShareDir 'module_file';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use YAML;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 extends 'Tapper::Installer::Precondition';
13  
14  
15  
16  
17 sub create_common_config
18 6 ▶{
Subroutine create_common_config: called CC=7 SCAR=19.8
Pod documented
19 6 my ($self, $config) = @_;
20 6 $config->{report_server} = $self->{cfg}->{report_server};
21 6 $config->{report_port} = $self->{cfg}->{report_port};
22 6 $config->{report_api_port} = $self->{cfg}->{report_api_port};
23 6 $config->{hostname} = $self->{cfg}->{hostname}; # allows guest systems to know their host system name
24 6 $config->{test_run} = $self->{cfg}->{test_run};
25 6 ▶ $config->{mcp_port} = $self->{cfg}->{mcp_port} if $self->{cfg}->{mcp_port};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cfg"}{"mcp_port"} 6 0 6
26 6 $config->{mcp_server} = $self->{cfg}->{mcp_server};
27 6 ▶ $config->{sync_port} = $self->{cfg}->{sync_port} if $self->{cfg}->{sync_port};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cfg"}{"sync_port"} 6 0 6
28 6 ▶ $config->{prc_nfs_server} = $self->{cfg}->{prc_nfs_server} if $self->{cfg}->{prc_nfs_server}; # prc_nfs_path is set by merging paths above
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cfg"}{"prc_nfs_server"} 6 0 6
29 6 ▶ $config->{scenario_id} = $self->{cfg}->{scenario_id} if $self->{cfg}->{scenario_id};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cfg"}{"scenario_id"} 0 6 6
30 6 $config->{paths} = $self->{cfg}->{paths};
31 6 ▶ $config->{files} = $self->{cfg}->{files} if $self->{cfg}->{files} ;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cfg"}{"files"} 1 5 6
32 6 ▶ $config->{testplan} = $self->{cfg}->{testplan} if $self->{cfg}->{testplan};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cfg"}{"testplan"} 0 6 6
33 6 $config->{log_to_file} = $self->{cfg}->{log_to_file};
34  
35  
36 6 return $config;
37 }
38  
39  
40 sub create_unix_config
41 2 ▶{
Subroutine create_unix_config: called CC=2 SCAR=7.3
Pod documented
42 2 my ($self, $prc) = @_;
43  
44 2 my $config = $self->create_common_config($prc->{config});
45 2 $config = merge($config, {times=>$self->{cfg}->{times}});
46 2 my @timeouts;
47  
48 2 ▶ if ($prc->{config}->{guest_count})
Branch: 1 branch
BranchTrueFalseTotal
if ($prc->{'config'}{'guest_count'}) { } 0 2 2
49 {
50 0 $config->{guest_count} = $prc->{config}->{guest_count};
51 0 $config->{timeouts} = $prc->{config}->{timeouts};
52 }
53 else
54 {
55 2 $config->{mcp_server} = $self->{cfg}->{mcp_server};
56 }
57  
58 2 return $config;
59 }
60  
61  
62 sub write_unix_config
63 1 ▶{
Subroutine write_unix_config: called CC=3 SCAR=11.1
Pod documented
64 1 my ($self, $prc) = @_;
65 1 my $basedir = $self->cfg->{paths}{base_dir};
66 1 my $config = $self->create_unix_config($prc);
67  
68 1 ▶ $self->makedir("$basedir/etc") if not -d "$basedir/etc";
Branch: 1 branch
BranchTrueFalseTotal
unless -d "$basedir/etc" 1 0 1
69 1 ▶ open my $file, '>',"$basedir/etc/tapper" or return "Can not open /etc/tapper in $basedir:$!";
Branch: 1 branch
BranchTrueFalseTotal
open my $file, ">", "$basedir/etc/tapper" or return "Can not open /etc/tapper in ${basedir}:$!" 1 0 1
70 1 print $file YAML::Dump($config);
71 1 close $file;
72 1 return 0;
73 }
74  
75  
76  
77 sub install_startscript
78 0 ▶{
Subroutine install_startscript: not called CC=15 SCAR=54.8
Pod documented
79 0 my ($self, $distro) = @_;
80 0 my $basedir = $self->cfg->{paths}{base_dir};
81 0 my ($error, $retval);
82 0 ▶ if (not -d "$basedir/etc/init.d" ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d "$basedir/etc/init.d") 0 0 0
83 0 ▶ mkdir("$basedir/etc/init.d") or return "Can't create /etc/init.d/ in $basedir";
Branch: 1 branch
BranchTrueFalseTotal
mkdir "$basedir/etc/init.d" or return "Can't create /etc/init.d/ in $basedir" 0 0 0
84 }
85 0 ($error, $retval) = $self->log_and_exec("cp",module_file('Tapper::Installer', "startfiles/$distro/etc/init.d/tapper"),"$basedir/etc/init.d/tapper");
86 0 ▶ return $retval if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 0 0
87 0 ▶ if ($distro!~/tapper/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $distro =~ /tapper/) 0 0 0
88  
89 0 pipe (my $read, my $write);
90 0 ▶ return ("Can't open pipe:$!") if not (defined $read and defined $write);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $read and defined $write 0 0 0
Condition0%0/3
defined $read and defined $write
!ll&&!rl&&r
000
MC/DC0%0/2
defined $read and defined $write
defined $read defined $write
Truth table0%0/3
A and B
A= defined $readB= defined $write
ABresult
0X0
100
111
91  
92 # fork for the stuff inside chroot
93 0 my $pid = fork();
94 0 ▶ return "fork failed: $!" if not defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 0 0
95  
96 # child
97 0 ▶ if ($pid == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == 0) { } 0 0 0
98 0 close $read;
99 0 chroot $basedir;
100 0 chdir ("/");
101  
102 0 my $ret = 0;
103 0 my ($error, $retval);
104 0 ▶ if ($distro=~m/suse|debian/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($distro =~ /suse|debian/) { } 0 0 0
elsif ($distro =~ /(redhat)|(fedora)/) { } 0 0 0
elsif ($distro =~ /(ubuntu)/) { } 0 0 0
elsif ($distro =~ /gentoo/) { } 0 0 0
105 0 ($error, $retval)=$self->log_and_exec("insserv","/etc/init.d/tapper");
106 } elsif ($distro=~m/(redhat)|(fedora)/) {
107 0 ($error, $retval)=$self->log_and_exec("chkconfig","--add","tapper");
108 } elsif ($distro=~m/(ubuntu)/) {
109 0 ($error, $retval)=$self->log_and_exec("update-rc.d","-f", "tapper", "defaults");
110 } elsif ($distro=~/gentoo/) {
111 0 ($error, $retval)=$self->log_and_exec("rc-update", "add", "tapper_gentoo", "default");
112 } else {
113 0 ($error, $retval)=(1,"No supported distribution detected.");
114 }
115 0 ▶ print($write "$retval") if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 0 0
116 0 close $write;
117 0 exit $error;
118 } else { # parent
119 0 close $write;
120 0 waitpid($pid,0);
121 0 ▶ if ($?) {
Branch: 1 branch
BranchTrueFalseTotal
if ($?) 0 0 0
122 0 my $output = <$read>;
123 0 return($output);
124 }
125 }
126 }
127 }
128  
129  
130 sub create_windows_config
131 3 ▶{
Subroutine create_windows_config: called CC=8 SCAR=21.2
Pod documented
132 3 my ($self, $prc) = @_;
133 3 my $basedir = $self->cfg->{paths}{base_dir};
134  
135 3 my $config = $self->create_common_config();
136 3 ▶ $config->{guest_number} = $prc->{config}->{guest_number} if $prc->{config}->{guest_number};
Branch: 1 branch
BranchTrueFalseTotal
if $prc->{"config"}{"guest_number"} 2 1 3
137  
138 3 ▶ if ($prc->{config}->{guest_count})
Branch: 1 branch
BranchTrueFalseTotal
if ($prc->{"config"}{"guest_count"}) 0 3 3
139 {
140 0 $config->{guest_count} = $prc->{config}->{guest_count};
141 }
142 3 ▶ if ($prc->{config}->{testprogram_list}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($prc->{'config'}{'testprogram_list'}) { } 1 2 3
elsif ($prc->{'config'}{'test_program'}) { } 1 1 2
143 1 ▶ for (my $i=0; $i< int @{$prc->{config}->{testprogram_list}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < int @{$prc->{"config"}{"testprogram_list"};}) 3 1 4
144 # string concatenation for hash keys, otherwise perl can't tell whether
145 # $i ot $i_prog is the name of the variable
146 3 my $list_element = $prc->{config}->{testprogram_list}->[$i];
147 3 $config->{"test".$i."_prog"} = $list_element->{program};
148 3 ▶ $config->{"test".$i."_prog"} ||= $list_element->{test_program};
Condition33%1/3
$$config{'test' . $i . '_prog'} ||= $list_element->{"test_program"}
l!l&&r!l&&!r
030
MC/DC0%0/2
$$config{'test' . $i . '_prog'} ||= $list_element->{"test_program"}
$$config{'test' . $i . '_prog'} $list_element->{"test_program"}
Truth table33%1/3
A ||= B
A= $$config{'test' . $i . '_prog'}B= $list_element->{"test_program"}
ABresult
1X1
011
000
149 3 $config->{"test".$i."_runtime_default"} = $list_element->{runtime};
150 3 $config->{"test".$i."_timeout"} = $list_element->{timeout};
151 3 ▶ $config->{"test".$i."_timeout"} ||= $list_element->{timeout_testprogram};
Condition33%1/3
$$config{'test' . $i . '_timeout'} ||= $list_element->{"timeout_testprogram"}
l!l&&r!l&&!r
030
MC/DC0%0/2
$$config{'test' . $i . '_timeout'} ||= $list_element->{"timeout_testprogram"}
$$config{'test' . $i . '_timeout'} $list_element->{"timeout_testprogram"}
Truth table33%1/3
A ||= B
A= $$config{'test' . $i . '_timeout'}B= $list_element->{"timeout_testprogram"}
ABresult
1X1
011
000
152 }
153 } elsif ($prc->{config}->{test_program}) {
154 1 $config->{test0_prog} = $prc->{config}->{test_program};
155 1 $config->{test0_runtime_default} = $prc->{config}->{runtime};
156 $config->{test0_timeout} = $prc->{config}->{timeout_testprogram}
157 1 }
158 3 return $config;
159  
160 }
161  
162  
163 sub write_windows_config
164 1 ▶{
Subroutine write_windows_config: called CC=2 SCAR=7.0
Pod documented
165 1 my ($self, $prc) = @_;
166  
167 1 my $config = $self->create_windows_config($prc);
168 1 my $basedir = $self->cfg->{paths}{base_dir};
169 1 ▶ open my $file, '>', $basedir.'/test.config' or return "Can not open /test.config in $basedir:$!";
Branch: 1 branch
BranchTrueFalseTotal
open my $file, ">", $basedir . '/test.config' or return "Can not open /test.config in ${basedir}:$!" 1 0 1
170 1 print $file YAML::Dump($config);
171 1 close $file;
172  
173 1 return 0
174 }
175  
176  
177  
178  
179 sub install
180 1 ▶{
Subroutine install: called CC=8 SCAR=24.9
Pod documented
181 1 my ($self, $prc) = @_;
182  
183 1 my $basedir = $self->cfg->{paths}{base_dir};
184 1 my ($error, $retval);
185 1 my $distro = $self->get_distro($basedir);
186 1 ▶ $retval = $self->install_startscript($distro) if $distro and not $prc->{skip_startscript};
Branch: 1 branch
BranchTrueFalseTotal
if $distro and not $prc->{"skip_startscript"} 0 1 1
Condition33%1/3
$distro and not $prc->{"skip_startscript"}
!ll&&!rl&&r
100
MC/DC0%0/2
$distro and not $prc->{"skip_startscript"}
$distro not $prc->{"skip_startscript"}
Truth table33%1/3
A and not(B)
A= $distroB= not $prc->{"skip_startscript"}
ABresult
0X0
100
111
187 1 ▶ return $retval if $retval;
Branch: 1 branch
BranchTrueFalseTotal
if $retval 0 1 1
188  
189 1 $error = $self->write_unix_config($prc);
190 1 ▶ return $error if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 1 1
191  
192  
193 1 $error = $self->write_windows_config($prc);
194 1 ▶ return $error if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 1 1
195  
196 1 ▶ if ($prc->{tapper_package}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prc->{"tapper_package"}) 0 1 1
197 0 my $pkg_object=Tapper::Installer::Precondition::Package->new($self->cfg);
198 0 my $package={filename => $prc->{tapper_package}};
199 0 ▶ $self->logdie($retval) if $retval = $pkg_object->install($package);
Branch: 1 branch
BranchTrueFalseTotal
if $retval = $pkg_object->install($package) 0 0 0
200 }
201  
202 1 return 0;
203 }
204  
205  
206  
207  
208 sub get_distro
209 4 ▶{
Subroutine get_distro: called CC=8 SCAR=23.9
Pod documented
210 4 my ($self, $dir) = @_;
211 4 ▶ my @files=glob("$dir/etc/*-release");
Subroutine BEGIN: called CC=1 SCAR=0.0
212 4 for my $file(@files){
213 1 ▶ return "suse" if $file =~ /suse/i;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /suse/i 1 0 1
214 0 ▶ return "redhat" if $file =~ /redhat/i;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /redhat/i 0 0 0
215 0 ▶ return "gentoo" if $file =~ /gentoo/i;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /gentoo/i 0 0 0
216 0 ▶ return "tapper" if $file =~ /tapper/i;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /tapper/i 0 0 0
217 }
218 {
219 3 ▶ open my $fh, '<',"$dir/etc/issue" or next;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", "$dir/etc/issue" or next 1 2 3
220 1 local $\='';
221 1 my $issue = <$fh>;
222 1 close $fh;
223 1 my $distro;
224 1 ($distro) = $issue =~ m/(Debian|Ubuntu)/i;
225 1 ▶ return lc($distro) if $distro;
Branch: 1 branch
BranchTrueFalseTotal
if $distro 1 0 1
226 }
227 2 return "";
228 }
229  
230  
231 8 1;
232  
233 __END__
« lib/Tapper/Installer/Precondition/Rawimage.pm ↑ Summary lib/Tapper/Installer/Precondition/Image.pm »