blib/lib/MYDan/Collector/Stat/Output.pm untested

filter: Statementstmt 0.0% 0 / 61 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 96 CC 12 SCAR 45.5
CC
12
Cov
0%
co
156.0
BEGIN
2.0
BEGIN
2.0
CRAP 94.4
1 package MYDan::Collector::Stat::Output;
2  
3 use strict;
4 use warnings;
5 use Carp;
6 use POSIX;
7 use File::Spec;
8 use FindBin qw( $RealBin );
9 use Digest::MD5;
10 use MYDan::Collector::Util;
11  
12 our $path = "$RealBin/../exec";
13  
14 sub co
15 {
16 my ( $this, @output, @stat, %output ) = @_;
17  
18 push @stat, [ 'OUTPUT', 'stdout', 'md5sum', 'exit' ];
19  
20 map{ $output{$1} = 1 if $_ =~ /^\{OUTPUT\}\{([^}]+)\}/ }@output;
21  
22 for my $output ( keys %output )
23 {
24 my $todo = $output;
25  
26 if( $output =~ /^(.+)::(\w{32})::$/ )
27 {
28 my ( $cmd, $md5, $sudo ) = ( $1, $2 );
29 $sudo = $1 if $cmd =~ s/^(\w+)://;
30  
31 if ( $cmd !~ /^\// )
32 {
33 unless( $path ) { warn "[WARN]undef path for exec.\n"; next; }
34 $cmd = File::Spec->join( $path, $cmd );
35 }
36  
37 unless ( $cmd ){ warn"[WARN]no command defined on $output.\n"; next; }
38 unless ( -x $cmd ){ warn"[WARN]$cmd is not an executable file.\n"; next; }
39  
40 my $filemd5 = MYDan::Collector::Util::qx( "md5sum '$cmd'" );
41 unless( $filemd5 =~ /^(\w{32})\s+$cmd$/ )
42 {
43 warn"[WARN]get $cmd md5 fail.\n";next;
44 }
45 $filemd5 = $1;
46  
47 if( $md5 ne $filemd5 )
48 {
49 warn"[WARN] $output MD5 comparison failed <> $filemd5\n";next;
50 }
51  
52 $todo = $sudo ? "sudo -u $sudo $cmd" : $cmd;
53 }
54  
55 my $stdout = MYDan::Collector::Util::qx( "$todo 2>&1" );
56 my $exit = $? == -1 ? -1 : $? >> 8;
57 my $md5sum = Digest::MD5->new()->add( $stdout ||'' )->hexdigest();
58 push @stat, [ $output, $stdout, $md5sum, $exit ];
59 }
60  
61 return \@stat;
62 }
63  
64 1;