lib/Tapper/Installer/Precondition.pm

filter: Statementstmt 81.0% 107 / 132 Branchbran 54.5% 48 / 88 Conditioncond 46.6% 7 / 15 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 94.4% 17 / 18 Podpod 100% 6 / 6 totaltotal 70.4% 188 / 267 CC 51 SCAR 32.0
CC
51
Cov
69%
get_file_type
44.4
precondition_install
29.1
file_save
20.0
CRAP 24.4
« lib/Tapper/Installer/Precondition/Repository.pm ↑ Summary lib/Tapper/Installer/Precondition/Package.pm »
1 package Tapper::Installer::Precondition;
2 23 our $AUTHORITY = 'cpan:TAPPER';
3 23 $Tapper::Installer::Precondition::VERSION = '5.0.3';
4 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 23 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 23 ▶use Hash::Merge::Simple 'merge';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 23 ▶use File::Type;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 23 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 23 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 23 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 23 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 23 ▶use YAML;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 23 ▶use File::Temp qw/tempdir/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 23 extends 'Tapper::Installer';
18  
19  
20  
21  
22 sub get_file_type
23 11 ▶{
Subroutine get_file_type: called CC=19 SCAR=37.9
Pod documented
24 11 my ($self, $file) = @_;
25 11 my @file_split=split(/\./,$file);
26 11 my $type=$file_split[-1];
27 11 ▶ if ($type eq "iso") {
Branch: 6 branches
BranchTrueFalseTotal
if ($type eq 'iso') { } 1 10 11
elsif ($type eq 'gz' or $type eq 'tgz') { } 4 6 10
elsif ($type eq 'tar') { } 1 5 6
elsif ($type eq 'bz' or $type eq 'bz2') { } 0 5 5
elsif ($type eq 'rpm') { } 0 5 5
elsif ($type eq 'deb') { } 2 3 5
28 1 return (0,"iso");
29 ▶ } elsif ($type eq "gz" or $type eq "tgz") {
Condition100%3/3
$type eq "gz" or $type eq "tgz"
l!l&&r!l&&!r
136
MC/DC100%2/2
$type eq "gz" or $type eq "tgz"
$type eq "gz" $type eq "tgz"
Truth table100%3/3
A or B
A= $type eq "gz"B= $type eq "tgz"
ABresult
1X1
011
000
30 4 return (0,"gzip");
31 } elsif ($type eq "tar") {
32 1 return (0,"tar");
33 ▶ } elsif ($type eq "bz" or $type eq "bz2") {
Condition33%1/3
$type eq "bz" or $type eq "bz2"
l!l&&r!l&&!r
005
MC/DC0%0/2
$type eq "bz" or $type eq "bz2"
$type eq "bz" $type eq "bz2"
Truth table33%1/3
A or B
A= $type eq "bz"B= $type eq "bz2"
ABresult
1X1
011
000
34 0 return (0,"bz2");
35 } elsif ($type eq "rpm") {
36 0 return(0,"rpm");
37 } elsif ($type eq "deb") {
38 2 return(0,"deb");
39 }
40  
41 3 ▶ if (not -e $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file) 0 3 3
42 0 return (0,"$file does not exist. Can't check file type");
43 }
44 3 my $ft = File::Type->new();
45 3 $type = $ft->mime_type("$file");
46 3 ▶ if ($type eq "application/octet-stream") {
Branch: 5 branches
BranchTrueFalseTotal
if ($type eq 'application/octet-stream') { } 0 3 3
elsif ($type eq 'application/x-dpkg') { } 0 3 3
elsif ($type eq 'application/x-gzip') { } 1 2 3
elsif ($type eq 'application/x-gtar') { } 1 1 2
elsif ($type eq 'application/x-bzip2') { } 1 0 1
47 0 my ($error, $output)=$self->log_and_exec("file $file");
48 0 ▶ return (0, "Getting file type of $file failed: $output") if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 0 0
49 0 ▶ return (0,"iso") if $output =~m/ISO 9660/i;
Branch: 1 branch
BranchTrueFalseTotal
if $output =~ /ISO 9660/i 0 0 0
50 0 ▶ return (0,"rpm") if $output =~m/$file: RPM/i;
Branch: 1 branch
BranchTrueFalseTotal
if $output =~ /$file: RPM/i 0 0 0
51 0 ▶ return (0,"deb") if $output =~m/$file: Debian/i;
Branch: 1 branch
BranchTrueFalseTotal
if $output =~ /$file: Debian/i 0 0 0
52 } elsif ($type eq "application/x-dpkg") {
53 0 return (0,"deb");
54 } elsif ($type eq "application/x-gzip") {
55 1 return (0,"gzip");
56 } elsif ($type eq "application/x-gtar") {
57 1 return (0,"tar");
58 } elsif ($type eq "application/x-bzip2") {
59 1 return (0,"bz2");
60 } else {
61 0 return(1, "$file is of unrecognised file type \"$type\"");
62 }
63 }
64  
65  
66  
67  
68  
69 sub gethostname
70 1 ▶{
Subroutine gethostname: called CC=4 SCAR=22.2
Pod documented
71 1 my ($self) = @_;
72 1 my $hostname = Sys::Hostname::hostname();
73 1 ▶ if ($hostname =~ m/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hostname =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) 0 1 1
74 0 ▶ ($hostname) = gethostbyaddr(inet_aton($hostname), AF_INET) or ( print("Can't get hostname: $!") and exit 1);
Branch: 1 branch
BranchTrueFalseTotal
($hostname) = gethostbyaddr(inet_aton($hostname), 2) or print "Can't get hostname: $!" and exit 1 0 0 0
Condition0%0/5
(($hostname) = gethostbyaddr(inet_aton($hostname), 2)) || print("Can't get hostname: $!") && exit 1
l!l&&r!l&&!r
000
print "Can't get hostname: $!" and exit 1
!ll
00
MC/DC0%0/2
(($hostname) = gethostbyaddr(inet_aton($hostname), 2)) || print("Can't get hostname: $!") && exit 1
(($hostname) = gethostbyaddr(inet_aton($hostname), 2)) print "Can't get hostname: $!"
Truth table0%0/3
A || B
A= (($hostname) = gethostbyaddr(inet_aton($hostname), 2))B= print "Can't get hostname: $!"
ABresult
1X1
011
000
75 0 $hostname =~ s/^(\w+?)\..+$/$1/;
76 0 system("hostname", "$hostname");
77 }
78 1 return $hostname;
79 }
80  
81  
82 sub cleanup
83 6 ▶{
Subroutine cleanup: called CC=4 SCAR=13.9
Pod documented
84 6 my ($self) = @_;
85 6 ▶ foreach my $order (@{$self->cfg->{cleanup} || []}) {
Condition100%2/2
$self->cfg->{'cleanup'} || []
l!l
15
MC/DC100%1/1
$self->cfg->{'cleanup'} || []
$self->cfg->{'cleanup'}
Truth table100%2/2
A
A= $self->cfg->{'cleanup'}
Aresult
11
00
86 1 ▶ $order->{call}->(@{$order->{options} || []});
Condition50%1/2
$order->{'options'} || []
l!l
10
MC/DC0%0/1
$order->{'options'} || []
$order->{'options'}
Truth table50%1/2
A
A= $order->{'options'}
Aresult
11
00
87 }
88 6 return 0;
89 }
90  
91  
92  
93 sub handle_source_url
94 1 ▶{
Subroutine handle_source_url: called CC=2 SCAR=6.9
Pod documented
95 1 my ($self, $precondition) = @_;
96  
97 1 ▶ if ( $precondition->{source_url} =~ m{^nfs://(.+/)([^/]+)(/?)$} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($precondition->{"source_url"} =~ m[^nfs://(.+/)([^/]+)(/?)$]) 1 0 1
98 1 my $nfs_dir = tempdir (CLEANUP => 1); # allow to have multiple nfs mount
99 1 $self->log_and_exec("mount","-t nfs","$1", $nfs_dir);
100 1 $precondition->{name} = $precondition->{filename} = "$nfs_dir/$2";
101 1 ▶ push @{$self->cfg->{cleanup}}, {call => sub {$self->log_and_exec(@_)},
Subroutine __ANON__: called CC=1 SCAR=0.0
102 1 options => ['umount',$nfs_dir]};
103 }
104 1 return $precondition;
105 }
106  
107  
108  
109 sub precondition_install
110 6 ▶{
Subroutine precondition_install: called CC=23 SCAR=33.7
Pod documented
111 6 my ($self, $precondition) = @_;
112  
113 6 ▶ $precondition = $self->handle_source_url($precondition) if $precondition->{source_url};
Branch: 1 branch
BranchTrueFalseTotal
if $precondition->{"source_url"} 1 5 6
114  
115 6 my $retval;
116 6 my ($error, $loop);
117 6 ▶ $self->makedir($self->cfg->{paths}{guest_mount_dir}) if not -d $self->cfg->{paths}{guest_mount_dir};
Branch: 1 branch
BranchTrueFalseTotal
unless -d $self->cfg->{"paths"}{"guest_mount_dir"} 0 6 6
118  
119 6 my $image;
120 6 my $partition = $precondition->{mountpartition};
121 6 my $new_base_dir = $self->cfg->{paths}{base_dir};
122  
123 6 ▶ if ($precondition->{mountfile}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($precondition->{'mountfile'}) { } 2 4 6
elsif ($precondition->{'mountpartition'}) { } 1 3 4
elsif ($precondition->{'mountdir'}) { } 1 2 3
124 2 $image = $self->cfg->{paths}{base_dir}.$precondition->{mountfile};
125 2 $new_base_dir = $self->cfg->{paths}{guest_mount_dir};
126 2 ▶ if ( $precondition->{mountpartition} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($precondition->{'mountpartition'}) { } 1 1 2
127 # make sure loop device is free
128 # don't use losetup -f, until it is available on installer NFS root
129 1 $self->log_and_exec("losetup -d /dev/loop0"); # ignore error since most of the time device won't be already bound
130 1 ▶ return $retval if $retval = $self->log_and_exec("losetup /dev/loop0 $image");
Branch: 1 branch
BranchTrueFalseTotal
if $retval = $self->log_and_exec("losetup /dev/loop0 $image") 0 1 1
131 1 ▶ return $retval if $retval = $self->log_and_exec("kpartx -a /dev/loop0");
Branch: 1 branch
BranchTrueFalseTotal
if $retval = $self->log_and_exec("kpartx -a /dev/loop0") 0 1 1
132 1 ▶ return $retval if $retval = $self->log_and_exec("mount /dev/mapper/loop0$partition ".$new_base_dir);
Branch: 1 branch
BranchTrueFalseTotal
if $retval = $self->log_and_exec("mount /dev/mapper/loop0$partition " . $new_base_dir) 0 1 1
133 } else {
134 1 ▶ return $retval if $retval = $self->log_and_exec("mount -o loop $image ".$new_base_dir);
Branch: 1 branch
BranchTrueFalseTotal
if $retval = $self->log_and_exec("mount -o loop $image " . $new_base_dir) 0 1 1
135 }
136 }
137 elsif ($precondition->{mountpartition}) {
138 1 $new_base_dir = $self->cfg->{paths}{guest_mount_dir};
139 1 ▶ return $retval if $retval = $self->log_and_exec("mount $partition ".$new_base_dir);
Branch: 1 branch
BranchTrueFalseTotal
if $retval = $self->log_and_exec("mount $partition " . $new_base_dir) 0 1 1
140 }
141 elsif ($precondition->{mountdir}) {
142 1 $new_base_dir .= $precondition->{mountdir};
143 }
144  
145 # call
146 6 my $old_basedir = $self->cfg->{paths}{base_dir};
147 6 $self->cfg->{paths}{base_dir} = $new_base_dir;
148 6 ▶ return $retval if $retval=$self->install($precondition);
Branch: 1 branch
BranchTrueFalseTotal
if $retval = $self->install($precondition) 0 6 6
149  
150  
151 6 ▶ if ($precondition->{mountfile}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($precondition->{'mountfile'}) { } 2 4 6
elsif ($precondition->{'mountpartition'}) { } 1 3 4
152 2 ▶ if ( $precondition->{mountpartition} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($precondition->{'mountpartition'}) { } 1 1 2
153 1 ▶ return $retval if $retval = $self->log_and_exec("umount /dev/mapper/loop0$partition");
Branch: 1 branch
BranchTrueFalseTotal
if $retval = $self->log_and_exec("umount /dev/mapper/loop0$partition") 0 1 1
154 1 ▶ return $retval if $retval = $self->log_and_exec("kpartx -d /dev/loop0");
Branch: 1 branch
BranchTrueFalseTotal
if $retval = $self->log_and_exec("kpartx -d /dev/loop0") 0 1 1
155 1 ▶ if ($retval = $self->log_and_exec("losetup -d /dev/loop0")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($retval = $self->log_and_exec("losetup -d /dev/loop0")) 0 1 1
156 0 sleep (2);
157 0 ▶ return $retval if $retval = $self->log_and_exec("kpartx -d /dev/loop0");
Branch: 1 branch
BranchTrueFalseTotal
if $retval = $self->log_and_exec("kpartx -d /dev/loop0") 0 0 0
158 0 ▶ return $retval if $retval = $self->log_and_exec("losetup -d /dev/loop0");
Branch: 1 branch
BranchTrueFalseTotal
if $retval = $self->log_and_exec("losetup -d /dev/loop0") 0 0 0
159 }
160 } else {
161 1 $retval = $self->log_and_exec("umount $new_base_dir");
162 1 ▶ $self->log->error("Can not unmount $new_base_dir: $retval") if $retval;
Branch: 1 branch
BranchTrueFalseTotal
if $retval 0 1 1
163  
164 # seems like mount -o loop uses a loop device that is not freed at umount
165 1 $self->log_and_exec("kpartx -d /dev/loop0");
166 1 $self->log_and_exec("losetup -d /dev/loop0");
167 }
168 }
169 elsif ($precondition->{mountpartition}) {
170 1 $retval = $self->log_and_exec("umount $new_base_dir");
171 1 ▶ $self->log->error("Can not unmount $new_base_dir: $retval") if $retval;
Branch: 1 branch
BranchTrueFalseTotal
if $retval 0 1 1
172 }
173 6 $self->cleanup();
174 6 $self->cfg->{paths}{base_dir} = $old_basedir;
175 6 return 0;
176  
177 }
178  
179  
180  
181 sub file_save
182 0 ▶{
Subroutine file_save: not called CC=4 SCAR=30.0
Pod documented
183 0 my ($self, $output, $filename) = @_;
184 0 my $testrun_id = $self->cfg->{test_run};
185 0 my $destdir = $self->cfg->{paths}{output_dir}."/$testrun_id/install/";
186 0 my $destfile = $destdir."/$filename";
187 0 ▶ if (not -d $destdir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $destdir) 0 0 0
188 0 ▶ system("mkdir","-p",$destdir) == 0 or return ("Can't create $destdir:$!");
Branch: 1 branch
BranchTrueFalseTotal
system("mkdir", "-p", $destdir) == 0 or return "Can't create ${destdir}:$!" 0 0 0
189 }
190 0 ▶ open(my $FH,">",$destfile)
Branch: 1 branch
BranchTrueFalseTotal
open my $FH, ">", $destfile or return "Can't open ${destfile}:$!" 0 0 0
191 or return ("Can't open $destfile:$!");
192 0 print $FH $output;
193 0 close $FH;
194 }
195  
196  
197  
198 23 1;
199  
200 __END__
« lib/Tapper/Installer/Precondition/Repository.pm ↑ Summary lib/Tapper/Installer/Precondition/Package.pm »