| 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 | BEGIN { | ||||||||||||||||||||
| 18 | 17 | ▶ | if ("$^O" eq "linux") { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.2
Branch: 1 branch
| |||||||||||||||||||||
| 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]) | ||||||||||||||||||||
| 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 | Supported package managers: | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | =over | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | =item dpkg | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | Debian package management utility | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | =item pacman | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | Arch linux package management utility | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | =item More planned.. | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | =back | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | =cut | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | sub pkg { | ||||||||||||||||||||
| 90 | 5 | ▶ | my $pkg = shift; | ||||||||||||||||||
|
Subroutine
pkg: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 91 | 5 | ▶ | my $type = shift // undef; | ||||||||||||||||||
|
Condition50%1/2
shift() // undef
MC/DC0%0/1
shift() // undef
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||
| 92 | 5 | my %args = ( | |||||||||||||||||||
| 93 | type => $type, | ||||||||||||||||||||
| 94 | @_ | ||||||||||||||||||||
| 95 | ); | ||||||||||||||||||||
| 96 | 5 | Csistck::Test::Pkg->new($pkg, %args); | |||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | sub new { | ||||||||||||||||||||
| 100 | 10 | ▶ | my $class = shift; | ||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=18.3
Pod
undocumented
| |||||||||||||||||||||
| 101 | 10 | my $self = $class->SUPER::new(@_); | |||||||||||||||||||
| 102 | 10 | bless($self, $class); | |||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | # We'll fix package name here, instead of redoing each call | ||||||||||||||||||||
| 105 | # Priority: 'type' argument, 'pkg_type' option, detect_pkg_manager. | ||||||||||||||||||||
| 106 | 10 | my $type = $self->{type}; | |||||||||||||||||||
| 107 | 10 | ▶ | if (! $type) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 108 | 0 | ▶ | $type = option('pkg_type') // detect_pkg_manager(); | ||||||||||||||||||
|
Condition0%0/3
option("pkg_type") // detect_pkg_manager()
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()
| |||||||||||||||||||||
| 109 | } | ||||||||||||||||||||
| 110 | 10 | ▶ | return $self->fail("Unsupported package manager or OS: manager=<none>") | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 111 | if (! $type); | ||||||||||||||||||||
| 112 | return $self->fail("Package manager not supported: type=<$type>") | ||||||||||||||||||||
| 113 | 10 | ▶ | if (! $Cmds->{$type}); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 114 | 10 | $self->{type} = $type; | |||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | 10 | return $self; | |||||||||||||||||||
| 117 | } | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | sub desc { | ||||||||||||||||||||
| 120 | 8 | ▶ | return sprintf("Package test for %s, using %s", | ||||||||||||||||||
|
Subroutine
desc: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 121 | $_[0]->pkg_name, $_[0]->pkg_type); | ||||||||||||||||||||
| 122 | } | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | sub check { | ||||||||||||||||||||
| 125 | 4 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
check: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 126 | 4 | my $pkg = $self->pkg_name; | |||||||||||||||||||
| 127 | 4 | my $type = $self->pkg_type; | |||||||||||||||||||
| 128 | 4 | ▶ | my $cmd = sprintf($Cmds->{$type}->{check}, $pkg) or | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 129 | return $self->fail("Package check command missing: type=<$type>"); | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | 4 | debug("Searching for package via command: cmd=<$cmd>"); | |||||||||||||||||||
| 132 | 4 | my $ret = system("$cmd 1>/dev/null 2>/dev/null"); | |||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | 4 | ▶ | return $self->fail("Package missing") | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 135 | unless($ret == 0); | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 3 | return $self->pass("Package installed"); | |||||||||||||||||||
| 138 | } | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | sub repair { | ||||||||||||||||||||
| 141 | 1 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
repair: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||||||||
| 142 | 1 | my $pkg = $self->pkg_name; | |||||||||||||||||||
| 143 | 1 | my $type = $self->pkg_type; | |||||||||||||||||||
| 144 | 1 | my $cmd; | |||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | 1 | ▶ | if (defined $Cmds->{$type}->{install}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 147 | 1 | $cmd = sprintf($Cmds->{$type}->{install}, $pkg); | |||||||||||||||||||
| 148 | } | ||||||||||||||||||||
| 149 | else { | ||||||||||||||||||||
| 150 | 0 | return $self->fail("Package install command missing: type=<$type>"); | |||||||||||||||||||
| 151 | } | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | 1 | $ENV{DEBIAN_FRONTEND} = "noninteractive"; | |||||||||||||||||||
| 154 | 1 | debug("Installing package via command: cmd=<$cmd>"); | |||||||||||||||||||
| 155 | 1 | my $ret = system("$cmd 1>/dev/null 2>/dev/null"); | |||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | 1 | ▶ | return $self->fail("Package installation failed") | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 158 | unless ($ret == 0); | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | 1 | return $self->pass("Package installation successful"); | |||||||||||||||||||
| 161 | } | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | # Package diff | ||||||||||||||||||||
| 164 | sub diff { | ||||||||||||||||||||
| 165 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
diff: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 166 | 0 | my $pkg = $self->pkg_name; | |||||||||||||||||||
| 167 | 0 | my $type = $self->pkg_type; | |||||||||||||||||||
| 168 | 0 | my $cmd; | |||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | 0 | ▶ | if (defined $Cmds->{$type}->{diff}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 171 | 0 | $cmd = sprintf($Cmds->{$type}->{diff}, $pkg); | |||||||||||||||||||
| 172 | } | ||||||||||||||||||||
| 173 | else { | ||||||||||||||||||||
| 174 | 0 | return $self->fail("Package diff command missing: type=<$type>"); | |||||||||||||||||||
| 175 | } | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | 0 | $ENV{DEBIAN_FRONTEND} = "noninteractive"; | |||||||||||||||||||
| 178 | 0 | debug("Showing package differences via command: cmd=<$cmd>"); | |||||||||||||||||||
| 179 | 0 | my $ret = system("$cmd 2>/dev/null"); | |||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | 0 | ▶ | return $self->fail("Package differences query failed") | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 182 | unless ($ret == 0); | ||||||||||||||||||||
| 183 | } | ||||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | =head2 detect_pkg_manager() | ||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | Detect package manager based on system OS and Linux distribution if | ||||||||||||||||||||
| 188 | applicable. Return package manager as string. This is not exported, it is | ||||||||||||||||||||
| 189 | used for the package test. | ||||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | =cut | ||||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | sub detect_pkg_manager { | ||||||||||||||||||||
| 194 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
detect_pkg_manager: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 195 | 0 | given ("$^O") { | |||||||||||||||||||
| 196 | 0 | when (/^freebsd$/) { return 'pkg_info'; } | |||||||||||||||||||
| 197 | 0 | when (/^netbsd$/) { return 'pkg_info'; } | |||||||||||||||||||
| 198 | 0 | when (/^linux$/) { | |||||||||||||||||||
| 199 | 0 | given (Linux::Distribution::distribution_name()) { | |||||||||||||||||||
| 200 | 0 | when (/^(?:debian|ubuntu)$/) { return 'dpkg'; } | |||||||||||||||||||
| 201 | 0 | when (/^(?:fedora|redhat|centos)$/) { return 'rpm'; } | |||||||||||||||||||
| 202 | 0 | when (/^gentoo$/) { return 'emerge'; } | |||||||||||||||||||
| 203 | 0 | when (/^arch$/) { return 'pacman'; } | |||||||||||||||||||
| 204 | 0 | default { return undef; } | |||||||||||||||||||
| 205 | } | ||||||||||||||||||||
| 206 | } | ||||||||||||||||||||
| 207 | 0 | when (/^darwin$/) { return undef; } | |||||||||||||||||||
| 208 | 0 | default { return undef; } | |||||||||||||||||||
| 209 | } | ||||||||||||||||||||
| 210 | } | ||||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | =head2 pkg_name($package, $type) | ||||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | Based on input package, return package name. With OS and distribution detection, | ||||||||||||||||||||
| 215 | $package can be passed as a string or a hashref. | ||||||||||||||||||||
| 216 | |||||||||||||||||||||
| 217 | See pkg() for information on passing hashrefs as package name | ||||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | =cut | ||||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | sub pkg_name { | ||||||||||||||||||||
| 222 | 17 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
pkg_name: called CC=4 SCAR=14.4
Pod
documented
| |||||||||||||||||||||
| 223 | 17 | my $pkg = $self->{target}; | |||||||||||||||||||
| 224 | 17 | my $type = $self->{type}; | |||||||||||||||||||
| 225 | |||||||||||||||||||||
| 226 | 17 | given (ref $pkg) { | |||||||||||||||||||
| 227 | 17 | when ('') { | |||||||||||||||||||
| 228 | 13 | ▶ | return $pkg if ($pkg =~ m/^[A-Za-z0-9\-\_\.\/]+$/); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 229 | } | ||||||||||||||||||||
| 230 | 4 | when ('HASH') { | |||||||||||||||||||
| 231 | 4 | ▶ | my $pkg_name = $pkg->{$type} // $pkg->{default}; | ||||||||||||||||||
|
Condition66%2/3
$pkg->{$type} // $pkg->{"default"}
MC/DC0%0/2
$pkg->{$type} // $pkg->{"default"}
$pkg->{$type}
$pkg->{"default"}
Truth table66%2/3
A // B
A= $pkg->{$type}B= $pkg->{"default"}
| |||||||||||||||||||||
| 232 | 4 | ▶ | return $pkg_name if ($pkg_name =~ m/^[A-Za-z0-9\-\_\.\/]+$/); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 233 | } | ||||||||||||||||||||
| 234 | } | ||||||||||||||||||||
| 235 | |||||||||||||||||||||
| 236 | 0 | die('Invalid package'); | |||||||||||||||||||
| 237 | } | ||||||||||||||||||||
| 238 | |||||||||||||||||||||
| 239 | =head2 pkg_type() | ||||||||||||||||||||
| 240 | |||||||||||||||||||||
| 241 | Return fixed package type | ||||||||||||||||||||
| 242 | |||||||||||||||||||||
| 243 | =cut | ||||||||||||||||||||
| 244 | |||||||||||||||||||||
| 245 | 13 | ▶ | sub pkg_type { $_[0]->{type}; } | ||||||||||||||||||
|
Subroutine
pkg_type: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 246 | |||||||||||||||||||||
| 247 | 17 | 1; | |||||||||||||||||||
| 248 | __END__ | ||||||||||||||||||||