Csistck/Test/Pkg.pm

filter: Statementstmt 66.3% 71 / 107 Branchbran 45.8% 11 / 24 Conditioncond 37.5% 3 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 88.8% 16 / 18 Podpod 44.4% 4 / 9 totaltotal 61.4% 105 / 171 CC 16 SCAR 13.1
CC
16
Cov
61%
diff
12.0
new
6.3
pkg_name
4.2
CRAP 3.7
« Csistck/Test.pm ↑ Summary Csistck/Oper.pm »
1 package Csistck::Test::Pkg;
2  
3 17 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 17 ▶use base 'Csistck::Test';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 17 ▶use Csistck::Oper qw/debug/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 17 ▶use Csistck::Config qw/option/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 17 our @EXPORT_OK = qw/pkg/;
12  
13 17 ▶use Digest::MD5;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 17 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # Conditionally use linux-only modules
17 17 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.2
18 17 ▶ if ("$^O" eq "linux") {
Branch: 1 branch
BranchTrueFalseTotal
if ("$^O" eq "linux") 17 0 17
19 17 require Linux::Distribution;
20 }
21 }
22  
23 17 our $Cmds = {
24 dpkg => {
25 check => 'dpkg -L "%s"',
26 diff => 'apt-get -s install "%s"',
27 install => 'apt-get -qq -y install "%s"'
28 },
29 rpm => {
30 check => 'rpm -q "%s"',
31 install => 'yum -q -y --noplugins install "%s"'
32 },
33 emerge => {
34 check => 'equery -qC list "%s"',
35 diff => 'emerge --color n -pq "%s"',
36 install => 'emerge --color n -q "%s"'
37 },
38 pacman => {
39 check => 'pacman -Qe "%s"',
40 install => 'pacman -Sq --noconfirm "%s"'
41 },
42 pkg_info => {
43 check => 'pkg_info -Qq "%s>0"'
44 }
45 };
46  
47 =head1 NAME
48  
49 Csistck::Test::Pkg - Csistck package check
50  
51 =head1 METHODS
52  
53 =head2 pkg($package, $type, :\&on_repair)
54  
55 Test for existing package using forks to system package managers. Package can be
56 specified as a string, or as a hashref:
57  
58 pkg({
59 dpkg => 'test-server',
60 emerge => 'net-test',
61 default => 'test-server'
62 });
63  
64 The package manager will be automatically detected if none is explicitly
65 specified, and the hashref key matching the package manager decides the package
66 name to check. If a default key is provided, that package is used by default.
67  
68 In repair mode, install the package quietly, unless package manager doesn't
69 handle automating install.
70  
71 If a repair operation is run, the on_repair function is called
72  
73 Supported package managers:
74  
75 =over
76  
77 =item dpkg
78  
79 Debian package management utility
80  
81 =item pacman
82  
83 Arch linux package management utility
84  
85 =item More planned..
86  
87 =back
88  
89 =cut
90  
91 5 ▶sub pkg {
Subroutine pkg: called CC=2 SCAR=7.0
Pod documented
92 5 my $pkg = shift;
93 5 ▶ my $type = shift // undef;
Condition50%1/2
shift() // undef
l!l
50
MC/DC0%0/1
shift() // undef
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
94 5 my %args = (
95 type => $type,
96 @_
97 );
98 5 Csistck::Test::Pkg->new($pkg, %args);
99 }
100  
101 10 ▶sub new {
Subroutine new: called CC=5 SCAR=18.3
Pod undocumented
102 10 my $class = shift;
103 10 my $self = $class->SUPER::new(@_);
104 10 bless($self, $class);
105  
106 # We'll fix package name here, instead of redoing each call
107 # Priority: 'type' argument, 'pkg_type' option, detect_pkg_manager.
108 10 my $type = $self->{type};
109 10 ▶ if (! $type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type) 0 10 10
110 0 ▶ $type = option('pkg_type') // detect_pkg_manager();
Condition0%0/3
option("pkg_type") // detect_pkg_manager()
l!l&&r!l&&!r
000
MC/DC0%0/2
option("pkg_type") // detect_pkg_manager()
option("pkg_type") detect_pkg_manager()
Truth table0%0/3
A // B
A= option("pkg_type")B= detect_pkg_manager()
ABresult
1X1
011
000
111 }
112 10 ▶ return $self->fail("Unsupported package manager or OS: manager=<none>")
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 10 10
113 if (! $type);
114 return $self->fail("Package manager not supported: type=<$type>")
115 10 ▶ if (! $Cmds->{$type});
Branch: 1 branch
BranchTrueFalseTotal
unless $Csistck::Test::Pkg::Cmds->{$type} 0 10 10
116 10 $self->{type} = $type;
117  
118 10 return $self;
119 }
120  
121 8 ▶sub desc {
Subroutine desc: called CC=1 SCAR=0.0
Pod undocumented
122 8 return sprintf("Package test for %s, using %s",
123 $_[0]->pkg_name, $_[0]->pkg_type);
124 }
125  
126 4 ▶sub check {
Subroutine check: called CC=3 SCAR=11.0
Pod undocumented
127 4 my $self = shift;
128 4 my $pkg = $self->pkg_name;
129 4 my $type = $self->pkg_type;
130 4 ▶ my $cmd = sprintf($Cmds->{$type}->{check}, $pkg) or
Branch: 1 branch
BranchTrueFalseTotal
my $cmd = sprintf($Csistck::Test::Pkg::Cmds->{$type}{"check"}, $pkg) or return $self->fail("Package check command missing: type=<$type>") 4 0 4
131 return $self->fail("Package check command missing: type=<$type>");
132  
133 4 debug("Searching for package via command: cmd=<$cmd>");
134 4 my $ret = system("$cmd 1>/dev/null 2>/dev/null");
135  
136 4 ▶ return $self->fail("Package missing")
Branch: 1 branch
BranchTrueFalseTotal
unless $ret == 0 1 3 4
137 unless($ret == 0);
138  
139 3 return $self->pass("Package installed");
140 }
141  
142 1 ▶sub repair {
Subroutine repair: called CC=3 SCAR=11.2
Pod undocumented
143 1 my $self = shift;
144 1 my $pkg = $self->pkg_name;
145 1 my $type = $self->pkg_type;
146 1 my $cmd;
147  
148 1 ▶ if (defined $Cmds->{$type}->{install}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Csistck::Test::Pkg::Cmds->{$type}{'install'}) { } 1 0 1
149 1 $cmd = sprintf($Cmds->{$type}->{install}, $pkg);
150 }
151 else {
152 0 return $self->fail("Package install command missing: type=<$type>");
153 }
154  
155 1 $ENV{DEBIAN_FRONTEND} = "noninteractive";
156 1 debug("Installing package via command: cmd=<$cmd>");
157 1 my $ret = system("$cmd 1>/dev/null 2>/dev/null");
158  
159 1 ▶ return $self->fail("Package installation failed")
Branch: 1 branch
BranchTrueFalseTotal
unless $ret == 0 0 1 1
160 unless ($ret == 0);
161
162 1 return $self->pass("Package installation successful");
163 }
164  
165 # Package diff
166 0 ▶sub diff {
Subroutine diff: not called CC=3 SCAR=24.8
Pod undocumented
167 0 my $self = shift;
168 0 my $pkg = $self->pkg_name;
169 0 my $type = $self->pkg_type;
170 0 my $cmd;
171
172 0 ▶ if (defined $Cmds->{$type}->{diff}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Csistck::Test::Pkg::Cmds->{$type}{'diff'}) { } 0 0 0
173 0 $cmd = sprintf($Cmds->{$type}->{diff}, $pkg);
174 }
175 else {
176 0 return $self->fail("Package diff command missing: type=<$type>");
177 }
178
179 0 $ENV{DEBIAN_FRONTEND} = "noninteractive";
180 0 debug("Showing package differences via command: cmd=<$cmd>");
181 0 my $ret = system("$cmd 2>/dev/null");
182  
183 0 ▶ return $self->fail("Package differences query failed")
Branch: 1 branch
BranchTrueFalseTotal
unless $ret == 0 0 0 0
184 unless ($ret == 0);
185 }
186  
187 =head2 detect_pkg_manager()
188  
189 Detect package manager based on system OS and Linux distribution if
190 applicable. Return package manager as string. This is not exported, it is
191 used for the package test.
192  
193 =cut
194  
195 0 ▶sub detect_pkg_manager {
Subroutine detect_pkg_manager: not called CC=1 SCAR=6.9
Pod documented
196 0 my $self = shift;
197 0 given ("$^O") {
198 0 when (/^freebsd$/) { return 'pkg_info'; }
199 0 when (/^netbsd$/) { return 'pkg_info'; }
200 0 when (/^linux$/) {
201 0 given (Linux::Distribution::distribution_name()) {
202 0 when (/^(?:debian|ubuntu)$/) { return 'dpkg'; }
203 0 when (/^(?:fedora|redhat|centos)$/) { return 'rpm'; }
204 0 when (/^gentoo$/) { return 'emerge'; }
205 0 when (/^arch$/) { return 'pacman'; }
206 0 default { return undef; }
207 }
208 }
209 0 when (/^darwin$/) { return undef; }
210 0 default { return undef; }
211 }
212 }
213  
214 =head2 pkg_name($package, $type)
215  
216 Based on input package, return package name. With OS and distribution detection,
217 $package can be passed as a string or a hashref.
218  
219 See pkg() for information on passing hashrefs as package name
220  
221 =cut
222  
223 17 ▶sub pkg_name {
Subroutine pkg_name: called CC=4 SCAR=14.4
Pod documented
224 17 my $self = shift;
225 17 my $pkg = $self->{target};
226 17 my $type = $self->{type};
227  
228 17 given (ref $pkg) {
229 17 when ('') {
230 13 ▶ return $pkg if ($pkg =~ m/^[A-Za-z0-9\-\_\.\/]+$/);
Branch: 1 branch
BranchTrueFalseTotal
if $pkg =~ m[^[A-Za-z0-9\-\_\./]+$] 13 0 13
231 }
232 4 when ('HASH') {
233 4 ▶ my $pkg_name = $pkg->{$type} // $pkg->{default};
Condition66%2/3
$pkg->{$type} // $pkg->{"default"}
l!l&&r!l&&!r
310
MC/DC0%0/2
$pkg->{$type} // $pkg->{"default"}
$pkg->{$type} $pkg->{"default"}
Truth table66%2/3
A // B
A= $pkg->{$type}B= $pkg->{"default"}
ABresult
1X1
011
000
234 4 ▶ return $pkg_name if ($pkg_name =~ m/^[A-Za-z0-9\-\_\.\/]+$/);
Branch: 1 branch
BranchTrueFalseTotal
if $pkg_name =~ m[^[A-Za-z0-9\-\_\./]+$] 4 0 4
235 }
236 }
237
238 0 die('Invalid package');
239 }
240  
241 =head2 pkg_type()
242  
243 Return fixed package type
244  
245 =cut
246  
247 13 ▶sub pkg_type { $_[0]->{type}; }
Subroutine pkg_type: called CC=1 SCAR=0.0
Pod documented
248  
249 17 1;
250 __END__
« Csistck/Test.pm ↑ Summary Csistck/Oper.pm »